Controller/WikiTagController.php
changeset 59 ea562f37d7a5
parent 54 ec51b9f4695f
parent 58 87bf6ec8af90
child 60 984ba20c150b
--- a/Controller/WikiTagController.php	Thu Dec 08 18:43:14 2011 +0100
+++ b/Controller/WikiTagController.php	Fri Dec 09 02:21:17 2011 +0100
@@ -329,17 +329,21 @@
         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
         $id_tag = $this->getRequest()->request->get('tag_id');
         $tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->find($id_tag);
-        //return new Response(var_dump(array($tag)));
-        // We search if the unsemantized version of the tag already exist.
-        $un_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('label'=>$tag->getLabel(), 'urlStatus'=>Tag::$TAG_URL_STATUS_DICT['null_result']));
+ 
         $em = $this->getDoctrine()->getEntityManager();
-        $un_tag_created = FALSE;
+        $query = $em->createQuery("SELECT t FROM WikiTagBundle:Tag t WHERE t.label = :label AND (t.urlStatus = :status_null OR t.urlStatus = :status_unsemantized)");
+        $query->setParameters(array("label"=>$tag->getLabel(),"status_null"=>Tag::$TAG_URL_STATUS_DICT['null_result'],"status_unsemantized"=>Tag::$TAG_URL_STATUS_DICT['unsemantized']));
+        $un_tag = null;
+        $un_tags = $query->getResult();
+        if(count($un_tags)>0) {
+            $un_tag = $un_tags[0];
+        }
+        $un_tag_created = false;
         if(!$un_tag){
             // Create another tag almost identical, without the W info
             $un_tag = new Tag();
             $un_tag->setLabel($tag->getLabel());
             $un_tag->setOriginalLabel($tag->getOriginalLabel());
-            $un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['null_result']);
             $un_tag->setWikipediaUrl(null);
             $un_tag->setWikipediaPageId(null);
             $un_tag->setAlternativeWikipediaUrl(null);
@@ -349,9 +353,18 @@
             $un_tag->setCategory($tag->getCategory());
             $un_tag->setAlias($tag->getAlias());
             $un_tag->setPopularity($tag->getPopularity());
-            $em->persist($un_tag);
+            $un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['unsemantized']);
             $un_tag_created = true;
+            $em->persist($un_tag);
         }
+        elseif($un_tag->getUrlStatus()==Tag::$TAG_URL_STATUS_DICT['null_result'])
+        {
+            $un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['unsemantized']);
+            $un_tag_created = true;
+            $em->persist($un_tag);
+        }
+        
+        
         
         if($id_doc && $id_doc!=""){
             // We associate the unsemantized tag to the DocumentTag and save datas
@@ -362,7 +375,7 @@
         }
         else{
             // Here we are in the context of tag list.
-            if($un_tag_created==TRUE){
+            if($un_tag_created==true){
                 $em->flush();
                 $num_page = $this->getRequest()->request->get('num_page');
                 $nb_by_page = $this->getRequest()->request->get('nb_by_page');
@@ -548,7 +561,8 @@
         $searched = $this->getRequest()->request->get('searched');
         return $this->renderAllTags($num_page, $nb_by_page, $sort, $searched);
     }
-    
+        
+        
     /**
      *
      * Resemantize the tag with its original label. Kind of undo if we changed the tag's label.