Controller/WikiTagController.php
changeset 44 c114504de4a8
parent 43 54f204bceb28
child 47 34718ebfb3c0
equal deleted inserted replaced
41:1c4e3fdba170 44:c114504de4a8
    43         return $this->render('WikiTagBundle:WikiTag:css.html.twig');
    43         return $this->render('WikiTagBundle:WikiTag:css.html.twig');
    44     }
    44     }
    45 
    45 
    46     /**
    46     /**
    47      * Renders the little html to add the javascript
    47      * Renders the little html to add the javascript
    48      * TODO: review why this injection in javascript, t10n?
    48      *
    49      */
    49      * @param unknown_type $tags_list
    50     public function addJavascriptAction($tags_list=FALSE)
    50      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
       
    51      */
       
    52     public function addJavascriptAction($tags_list=false)
    51     {
    53     {
    52         $cats = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOrderedCategories();
    54         $cats = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOrderedCategories();
    53         // $cats is {"Label":"Créateur"},{"Label":"Datation"},...
    55         // $cats is {"Label":"Créateur"},{"Label":"Datation"},...
    54         $nbCats = count($cats);
    56         $nbCats = count($cats);
    55         $ar = array('' => '');
    57         $ar = array('' => '');
    56         for($i=0;$i<$nbCats;$i++){
    58         for($i=0;$i<$nbCats;$i++) {
    57             $temp = array($cats[$i]["label"] => $cats[$i]["label"]);
    59             $temp = array($cats[$i]["label"] => $cats[$i]["label"]);
    58             $ar = array_merge($ar, $temp);
    60             $ar = array_merge($ar, $temp);
    59         }
    61         }
    60         // ... so we create is json like {"":""},{"Créateur":"Créateur"},{"Datation":"Datation"},...
    62         // ... so we create is json like {"":""},{"Créateur":"Créateur"},{"Datation":"Datation"},...
    61         $categories = json_encode($ar);
    63         $categories = json_encode($ar);
    94         return $this->render('WikiTagBundle:WikiTag:documentTags.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
    96         return $this->render('WikiTagBundle:WikiTag:documentTags.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
    95     }
    97     }
    96 
    98 
    97     /**
    99     /**
    98      *
   100      *
    99      * TODO : Enter description here ...
   101      * The action called when a tag is moved in a document tag list.
       
   102      *
   100      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   103      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   101      */
   104      */
   102     public function tagUpDownAction()
   105     public function tagUpDownAction()
   103     {
   106     {
   104 
   107 
   135 
   138 
   136         return $this->renderDocTags($id_doc);
   139         return $this->renderDocTags($id_doc);
   137     }
   140     }
   138 
   141 
   139     /**
   142     /**
   140      *
   143      * Action to remove a tag from a document tag list
   141      * TODO: Enter description here ...
       
   142      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   144      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   143      */
   145      */
   144     public function removeTagFromListAction()
   146     public function removeTagFromListAction()
   145     {
   147     {
   146         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   148         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   147         $id_tag = $this->getRequest()->request->get('tag_id');
   149         $id_tag = $this->getRequest()->request->get('tag_id');
   148         // We get the DocumentTag meant to be deleted, and remove it.
   150         // We get the DocumentTag meant to be deleted, and remove it.
   149         $em = $this->getDoctrine()->getEntityManager();
   151         $em = $this->getDoctrine()->getEntityManager();
   150         //TODO: use external id
       
   151         
   152         
   152         $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $id_tag));
   153         $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $id_tag));
   153         $em->remove($dt);
   154         $em->remove($dt);
   154         $em->flush();
   155         $em->flush();
   155 
   156 
   183             // If the label was found, we sent a bad request
   184             // If the label was found, we sent a bad request
   184             if($found==true){
   185             if($found==true){
   185                 return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   186                 return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   186             }
   187             }
   187             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
   188             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
   188             $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());
   189             $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
   189             $tag = $ar[0];
   190             $tag = $ar[0];
   190             $revision_id = $ar[1];
   191             $revision_id = $ar[1];
   191             $created = $ar[2];
   192             $created = $ar[2];
   192             
   193             
   193             // We get the DocumentTag and change its tag
   194             // We get the DocumentTag and change its tag
   218         return $this->renderDocTags($id_doc);
   219         return $this->renderDocTags($id_doc);
   219 
   220 
   220     }
   221     }
   221 
   222 
   222     /**
   223     /**
   223      *
   224      * The action called to reorder the the tags of a document. The tags are reordered according to the indexation score of the tag label on the document.
   224      * TODO : Enter description here ...
   225      * The fields taken into account for calculating the score are defined in the wikitag configuration.
   225      * TODO : implement
       
   226      */
   226      */
   227     public function reorderTagDocumentAction()
   227     public function reorderTagDocumentAction()
   228     {
   228     {
   229         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   229         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   230         $res = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
   230         $res = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
   241 
   241 
   242         return $this->renderDocTags($id_doc);
   242         return $this->renderDocTags($id_doc);
   243     }
   243     }
   244 
   244 
   245     /**
   245     /**
   246      *
   246      * The action called to add a new tag (especially from the completion box)
   247      * TODO: Enter description here ...
       
   248      */
   247      */
   249     public function addTagAction()
   248     public function addTagAction()
   250     {
   249     {
   251         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   250         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   252         $tag_label = $this->getRequest()->request->get('value');
   251         $tag_label = $this->getRequest()->request->get('value');
   267         if($found==true){
   266         if($found==true){
   268             //TODO : translation
   267             //TODO : translation
   269             return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   268             return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
   270         }
   269         }
   271         // returns array($tag, $revision_id, $created)
   270         // returns array($tag, $revision_id, $created)
   272         $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label)
   271         $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);// tag, revision_id, created = get_or_create_tag(tag_label)
   273         $tag = $ar[0];
   272         $tag = $ar[0];
   274         $revision_id = $ar[1];
   273         $revision_id = $ar[1];
   275         $created = $ar[2];
   274         $created = $ar[2];
   276         
   275         
   277         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
   276         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
   280         if($created==true || $nb_tags==0){
   279         if($created==true || $nb_tags==0){
   281             $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
   280             $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
   282             // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
   281             // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
   283             $a1 = reset($new_order_ar);
   282             $a1 = reset($new_order_ar);
   284             $new_order = intval(reset($a1)) + 1;
   283             $new_order = intval(reset($a1)) + 1;
   285             // TODO: use a factory that returns an DocumentTagInterface
       
   286             $new_DT = new DocumentTag();
   284             $new_DT = new DocumentTag();
   287             $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
   285             $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
   288             $new_DT->setTag($tag);
   286             $new_DT->setTag($tag);
   289             $new_DT->setOriginalOrder($new_order);
   287             $new_DT->setOriginalOrder($new_order);
   290             $new_DT->setTagOrder($new_order);
   288             $new_DT->setTagOrder($new_order);
   296         return $this->renderDocTags($id_doc);
   294         return $this->renderDocTags($id_doc);
   297     }
   295     }
   298 
   296 
   299 
   297 
   300     /**
   298     /**
   301      *
   299      * Action to remove the wikipedia link form a tag. This action create a copy of the original tag with all the link to wikipedia set to null.
   302      * TODO: Enter description here ...
   300      *
   303      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   301      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   304      */
   302      */
   305     public function removeWpLinkAction()
   303     public function removeWpLinkAction()
   306     {
   304     {
   307         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   305         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   312         $un_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('label'=>$tag->getLabel(), 'urlStatus'=>Tag::$TAG_URL_STATUS_DICT['null_result']));
   310         $un_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('label'=>$tag->getLabel(), 'urlStatus'=>Tag::$TAG_URL_STATUS_DICT['null_result']));
   313         $em = $this->getDoctrine()->getEntityManager();
   311         $em = $this->getDoctrine()->getEntityManager();
   314         $un_tag_created = FALSE;
   312         $un_tag_created = FALSE;
   315         if(!$un_tag){
   313         if(!$un_tag){
   316             // Create another tag almost identical, without the W info
   314             // Create another tag almost identical, without the W info
   317             // TODO: use a factory that return a TagInterface
       
   318             $un_tag = new Tag();
   315             $un_tag = new Tag();
   319             $un_tag->setLabel($tag->getLabel());
   316             $un_tag->setLabel($tag->getLabel());
   320             $un_tag->setOriginalLabel($tag->getOriginalLabel());
   317             $un_tag->setOriginalLabel($tag->getOriginalLabel());
   321             $un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['null_result']);
   318             $un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['null_result']);
   322             $un_tag->setWikipediaUrl(null);
   319             $un_tag->setWikipediaUrl(null);
   323             $un_tag->setWikipediaPageId(null);
   320             $un_tag->setWikipediaPageId(null);
       
   321             $un_tag->setAlternativeWikipediaUrl(null);
       
   322             $un_tag->setAlternativeWikipediaPageId(null);
       
   323             $un_tag->setAlternativeLabel(null);
   324             $un_tag->setDbpediaUri(null);
   324             $un_tag->setDbpediaUri(null);
   325             $un_tag->setCategory($tag->getCategory());
   325             $un_tag->setCategory($tag->getCategory());
   326             $un_tag->setAlias($tag->getAlias());
   326             $un_tag->setAlias($tag->getAlias());
   327             $un_tag->setPopularity($tag->getPopularity());
   327             $un_tag->setPopularity($tag->getPopularity());
   328             $em->persist($un_tag);
   328             $em->persist($un_tag);
   329             $un_tag_created = TRUE;
   329             $un_tag_created = true;
   330         }
   330         }
   331         
   331         
   332         if($id_doc && $id_doc!=""){
   332         if($id_doc && $id_doc!=""){
   333             // We associate the unsemantized tag to the DocumentTag and save datas
   333             // We associate the unsemantized tag to the DocumentTag and save datas
   334             $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $id_tag));
   334             $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneByDocumentExternalId($id_doc, array('tag' => $id_tag));
   353         }
   353         }
   354     }
   354     }
   355 
   355 
   356 
   356 
   357     /**
   357     /**
   358      *
   358      * Action to update a tag category.
   359      * TODO: Enter description here ...
   359      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   360      */
   360      */
   361     public function updateTagCategoryAction()
   361     public function updateTagCategoryAction()
   362     {
   362     {
   363         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   363         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   364         $id_tag = $this->getRequest()->request->get('id');
   364         $id_tag = $this->getRequest()->request->get('id');
   400         return $this->render('WikiTagBundle:WikiTag:tagTable.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
   400         return $this->render('WikiTagBundle:WikiTag:tagTable.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
   401     }
   401     }
   402 
   402 
   403 
   403 
   404     /**
   404     /**
   405      *
   405      * Action to update the tag alias.
   406      * TODO : Enter description here ...
   406 	 *
   407      * TODO : implement
       
   408      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   407      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   409      */
   408      */
   410     public function updateTagAliasAction()
   409     public function updateTagAliasAction()
   411     {
   410     {
   412         $id_tag = $this->getRequest()->request->get('id');
   411         $id_tag = $this->getRequest()->request->get('id');
   431             return $this->renderAllTags($num_page, $nb_by_page, $sort, $searched);
   430             return $this->renderAllTags($num_page, $nb_by_page, $sort, $searched);
   432         }
   431         }
   433     }
   432     }
   434     
   433     
   435     /**
   434     /**
   436      * List of all tags
   435      * List all tags, with pagination and search.
   437      * TODO: Enter description here ...
   436      *
       
   437      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
   438      */
   438      */
   439     public function allTagsAction()
   439     public function allTagsAction()
   440     {
   440     {
   441         // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
   441         // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
   442         // Searched string
   442         // Searched string
   549         if($tag!=null && $label!=null){
   549         if($tag!=null && $label!=null){
   550             if($label!=$tag->getLabel()){
   550             if($label!=$tag->getLabel()){
   551                 // We get the Wikipedia informations for the sent label
   551                 // We get the Wikipedia informations for the sent label
   552                 $tag_label_normalized = WikiTagUtils::normalizeTag($label);
   552                 $tag_label_normalized = WikiTagUtils::normalizeTag($label);
   553                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
   553                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
   554                 $new_label = $wp_response['new_label'];
   554                 
   555                 $status = $wp_response['status'];
   555                 $tag->setWikipediaInfo($wp_response);
   556                 $url = $wp_response['wikipedia_url'];
   556 
   557                 $pageid = $wp_response['pageid'];
       
   558                 $dbpedia_uri = $wp_response["dbpedia_uri"];
       
   559                 $wikipedia_revision_id = $wp_response['revision_id'];
       
   560                 // We save the datas : we DO NOT create a new tag, we change the current tag's informations
       
   561                 if($new_label!=null){
       
   562                     $tag->setLabel($new_label);
       
   563                 }
       
   564                 if($status!=null){
       
   565                     $tag->setUrlStatus($status);
       
   566                 }
       
   567                 $tag->setWikipediaUrl($url);
       
   568                 $tag->setWikipediaPageId($pageid);
       
   569                 $tag->setDbpediaUri($dbpedia_uri);
       
   570                 // Save datas.
   557                 // Save datas.
   571                 $em = $this->getDoctrine()->getEntityManager();
   558                 $em = $this->getDoctrine()->getEntityManager();
   572                 $em->persist($tag);
   559                 $em->persist($tag);
   573                 $em->flush();
   560                 $em->flush();
   574             }
   561             }