Controller/WikiTagController.php
changeset 22 99c15cfe420b
parent 21 780ef37e63b9
child 26 9eab3e86db7d
equal deleted inserted replaced
21:780ef37e63b9 22:99c15cfe420b
    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      * TODO: review why this injection in javascript, t10n?
    49      */
    49      */
    50     public function addJavascriptAction()
    50     public function addJavascriptAction($tags_list=FALSE)
    51     {
    51     {
    52         $cats = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOrderedCategories();
    52         $cats = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOrderedCategories();
    53         // $cats is {"Label":"Créateur"},{"Label":"Datation"},...
    53         // $cats is {"Label":"Créateur"},{"Label":"Datation"},...
    54         $nbCats = count($cats);
    54         $nbCats = count($cats);
    55         $ar = array('' => '');
    55         $ar = array('' => '');
    57             $temp = array($cats[$i]["label"] => $cats[$i]["label"]);
    57             $temp = array($cats[$i]["label"] => $cats[$i]["label"]);
    58             $ar = array_merge($ar, $temp);
    58             $ar = array_merge($ar, $temp);
    59         }
    59         }
    60         // ... so we create is json like {"":""},{"Créateur":"Créateur"},{"Datation":"Datation"},...
    60         // ... so we create is json like {"":""},{"Créateur":"Créateur"},{"Datation":"Datation"},...
    61         $categories = json_encode($ar);
    61         $categories = json_encode($ar);
    62         return $this->render('WikiTagBundle:WikiTag:javascript.html.twig', array('categories' => $categories));
    62         return $this->render('WikiTagBundle:WikiTag:javascript.html.twig', array('categories' => $categories,'tags_list' => $tags_list));
    63     }
    63     }
    64 
    64 
    65     /**
    65     /**
    66      * Display a list of ordered tag for a document
    66      * Display a list of ordered tag for a document
    67      * @param integer $id_doc
    67      * @param integer $id_doc
   131 
   131 
   132         return $this->renderDocTags($id_doc);
   132         return $this->renderDocTags($id_doc);
   133     }
   133     }
   134 
   134 
   135     /**
   135     /**
   136      *
   136      * Modify the tag in the context of a tag list for one document
   137      * TODO: Enter description here ...
   137      * 
   138      */
   138      */
   139     public function modifyDocumentTagAction()
   139     public function modifyDocumentTagAction()
   140     {
   140     {
   141         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   141         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
   142         $tag_label = $this->getRequest()->request->get('value');
   142         $tag_label = $this->getRequest()->request->get('value');
   163             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
   163             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
   164             $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label)
   164             $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label)
   165             $tag = $ar[0];
   165             $tag = $ar[0];
   166             $revision_id = $ar[1];
   166             $revision_id = $ar[1];
   167             $created = $ar[2];
   167             $created = $ar[2];
       
   168             
   168             // We get the DocumentTag and change its tag
   169             // We get the DocumentTag and change its tag
   169             $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag));
   170             $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag));
   170             $dt->setTag($tag);
   171             $dt->setTag($tag);
   171             $dt->setWikipediaRevisionId($revision_id);
   172             $dt->setWikipediaRevisionId($revision_id);
   172             //
   173             //
   174             //kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
   175             //kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
   175             //results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs)
   176             //results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs)
   176             //if len(results) > 0:
   177             //if len(results) > 0:
   177             //    ts.index_note = results[0].score
   178             //    ts.index_note = results[0].score
   178             //
   179             //
       
   180             // We set ManualOrder = true for the current document
       
   181             $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneBy(array('externalId' => $id_doc));
       
   182             $doc->setManualOrder(true);
   179             // We save the datas
   183             // We save the datas
   180             $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneBy(array('id' => $id_doc));
       
   181             $doc->setManualOrder(true);
       
   182             $em->flush();
   184             $em->flush();
   183         }
   185             // We render the document's tags
   184 
   186             return $this->renderDocTags($id_doc);
   185         return $this->renderDocTags($id_doc);
   187         }
       
   188 
   186     }
   189     }
   187 
   190 
   188     /**
   191     /**
   189      *
   192      *
   190      * @Route("/wtrtd")
   193      * @Route("/wtrtd")
   353     public function renderDocTags($id_doc)
   356     public function renderDocTags($id_doc)
   354     {
   357     {
   355         $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
   358         $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
   356         return $this->render('WikiTagBundle:WikiTag:tagTable.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
   359         return $this->render('WikiTagBundle:WikiTag:tagTable.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
   357     }
   360     }
   358     
       
   359     /**
       
   360      *
       
   361      * TODO: Enter description here ...
       
   362      * TODO : implement
       
   363      */
       
   364     public function resetWpInfoAction()
       
   365     {
       
   366         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   367         return $this->renderDocTags($id_doc);
       
   368     }
       
   369 
   361 
   370 
   362 
   371     /**
   363     /**
   372      *
   364      *
   373      * TODO : Enter description here ...
   365      * TODO : Enter description here ...
   466             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort,
   458             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort,
   467             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page,
   459             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page,
   468         	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => $this->container->getParameter("wiki_tag.route_for_documents_by_tag")));
   460         	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => $this->container->getParameter("wiki_tag.route_for_documents_by_tag")));
   469     }
   461     }
   470 
   462 
       
   463     /**
       
   464      * Modify the tag in the context of all tags list.
       
   465      */
       
   466     public function modifyTagAction()
       
   467     {
       
   468         $tag_label = $this->getRequest()->request->get('value');
       
   469         $id_moved_tag = $this->getRequest()->request->get('id');
       
   470         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
       
   471         // We update the tag label and its wikipedia info with the new label.
       
   472         $this->updateTagWithNewLabel($moved_tag, $tag_label);
       
   473         
       
   474         // We render the tag list.
       
   475         $num_page = $this->getRequest()->request->get('num_page');
       
   476         $nb_by_page = $this->getRequest()->request->get('nb_by_page');
       
   477         $sort = $this->getRequest()->request->get('sort');
       
   478         $searched = $this->getRequest()->request->get('searched');
       
   479         return $this->renderAllTags($num_page, $nb_by_page, $sort, $searched);
       
   480     }
       
   481     
       
   482     /**
       
   483      *
       
   484      * Resemantize the tag with its original label. Kind of undo if we changed the tag's label.
       
   485      * 
       
   486      */
       
   487     public function resetWpInfoAction()
       
   488     {
       
   489         $id_moved_tag = $this->getRequest()->request->get('tag_id');
       
   490         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
       
   491         // We update the tag label and its wikipedia info with the original label.
       
   492         $this->updateTagWithNewLabel($moved_tag, $moved_tag->getOriginalLabel());
       
   493         
       
   494         // We render the tag list.
       
   495         $num_page = $this->getRequest()->request->get('num_page');
       
   496         $nb_by_page = $this->getRequest()->request->get('nb_by_page');
       
   497         $sort = $this->getRequest()->request->get('sort');
       
   498         $searched = $this->getRequest()->request->get('searched');
       
   499         return $this->renderAllTags($num_page, $nb_by_page, $sort, $searched);
       
   500     }
       
   501 
       
   502 
       
   503     /**
       
   504      * Generic render partial template for tag list
       
   505      */
       
   506     private function updateTagWithNewLabel($tag, $label)
       
   507     {
       
   508         if($tag!=null && $label!=null){
       
   509             if($label!=$tag->getLabel()){
       
   510                 // We get the Wikipedia informations for the sent label
       
   511                 $tag_label_normalized = WikiTagUtils::normalizeTag($label);
       
   512                 $wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized);
       
   513                 $new_label = $wp_response['new_label'];
       
   514                 $status = $wp_response['status'];
       
   515                 $url = $wp_response['wikipedia_url'];
       
   516                 $pageid = $wp_response['pageid'];
       
   517                 $dbpedia_uri = $wp_response["dbpedia_uri"];
       
   518                 $wikipedia_revision_id = $wp_response['revision_id'];
       
   519                 // We save the datas : we DO NOT create a new tag, we change the current tag's informations
       
   520                 if($new_label!=null){
       
   521                     $tag->setLabel($new_label);
       
   522                 }
       
   523                 if($status!=null){
       
   524                     $tag->setUrlStatus($status);
       
   525                 }
       
   526                 $tag->setWikipediaUrl($url);
       
   527                 $tag->setWikipediaPageId($pageid);
       
   528                 $tag->setDbpediaUri($dbpedia_uri);
       
   529                 // Save datas.
       
   530                 $em = $this->getDoctrine()->getEntityManager();
       
   531                 $em->persist($tag);
       
   532                 $em->flush();
       
   533             }
       
   534         }
       
   535     }
       
   536 
   471 
   537 
   472     /**
   538     /**
   473      * Generic render partial template for tag list
   539      * Generic render partial template for tag list
   474      */
   540      */
   475     public function renderAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
   541     public function renderAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
   484         $sort = $ar[4];
   550         $sort = $ar[4];
   485         $reverse_sort = $ar[5];
   551         $reverse_sort = $ar[5];
   486         
   552         
   487         return $this->render('WikiTagBundle:WikiTag:TagListTable.html.twig',
   553         return $this->render('WikiTagBundle:WikiTag:TagListTable.html.twig',
   488             array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page,
   554             array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page,
   489         	'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => WikiTagController::$ROUTE_FOR_DOCUMENTS_BY_TAG));
   555         	'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => $this->container->getParameter("wiki_tag.route_for_documents_by_tag")));
   490         
   556         
   491         return $this->getAllTags();
   557         return $this->getAllTags();
   492     }
   558     }
   493 
   559 
   494 
   560