Controller/WikiTagController.php
changeset 2 13f43f53d0ba
child 5 45378793512a
equal deleted inserted replaced
1:06a22ff5d58d 2:13f43f53d0ba
       
     1 <?php
       
     2 /*
       
     3  * This file is part of the WikiTagBundle package.
       
     4 *
       
     5 * (c) IRI <http://www.iri.centrepompidou.fr/>
       
     6 *
       
     7 * For the full copyright and license information, please view the LICENSE
       
     8 * file that was distributed with this source code.
       
     9 */
       
    10 
       
    11 namespace IRI\Bundle\WikiTagBundle\Controller;
       
    12 
       
    13 use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
       
    14 use IRI\Bundle\WikiTagBundle\Entity\Tag;
       
    15 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
       
    16 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
       
    17 use Symfony\Component\HttpFoundation\Response;
       
    18 
       
    19 
       
    20 class WikiTagController extends Controller
       
    21 {
       
    22     /**
       
    23      * Fake index action
       
    24      */
       
    25     public function indexAction()
       
    26     {
       
    27         return new Response('<html><body>Nothing to see here.</body></html>');
       
    28     }
       
    29 
       
    30     /**
       
    31      * Renders the little html to add the css
       
    32      */
       
    33     public function addCssAction()
       
    34     {
       
    35         return $this->render('WikiTagBundle:WikiTag:css.html.twig');
       
    36     }
       
    37 
       
    38     /**
       
    39      * Renders the little html to add the javascript
       
    40      * TODO: review why this injection in javascript, t10n?
       
    41      */
       
    42     public function addJavascriptAction()
       
    43     {
       
    44         $cats = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOrderedCategories();
       
    45         // $cats is {"Label":"Créateur"},{"Label":"Datation"},...
       
    46         $nbCats = count($cats);
       
    47         $ar = array('' => '');
       
    48         for($i=0;$i<$nbCats;$i++){
       
    49             $temp = array($cats[$i]["Label"] => $cats[$i]["Label"]);
       
    50             $ar = array_merge($ar, $temp);
       
    51         }
       
    52         // ... so we create is json like {"":""},{"Créateur":"Créateur"},{"Datation":"Datation"},...
       
    53         $categories = json_encode($ar);
       
    54         return $this->render('WikiTagBundle:WikiTag:javascript.html.twig', array('categories' => $categories));
       
    55     }
       
    56 
       
    57     /**
       
    58      * List all tags for doc
       
    59      * TODO: doc_id as parameter
       
    60      * TODO: Is this controller usefull ?
       
    61      */
       
    62     public function allTagsAction()
       
    63     {
       
    64         $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findAll();
       
    65         return $this->render('WikiTagBundle:WikiTag:documentTags.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => 1));
       
    66     }
       
    67 
       
    68     /**
       
    69      * Display a list of ordered tag for a document
       
    70      * @param integer $id_doc
       
    71      */
       
    72     public function documentTagsAction($id_doc)
       
    73     {
       
    74         $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
       
    75         return $this->render('WikiTagBundle:WikiTag:documentTags.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
       
    76     }
       
    77 
       
    78     /**
       
    79      *
       
    80      * TODO : Enter description here ...
       
    81      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
       
    82      */
       
    83     public function tagUpDownAction()
       
    84     {
       
    85 
       
    86         $req = $this->getRequest()->request;
       
    87         $id_doc = $req->get('wikitag_document_id');
       
    88         // post vars new_order and old_order indicate the position (from 1) of the tag in the list.
       
    89         // NB : it is different from the DocumentTag.order in the database.
       
    90         $new_order = intval($req->get('new_order')) - 1;
       
    91         $old_order = intval($req->get('old_order')) - 1;
       
    92         // First we get the DocumentTags
       
    93         $em = $this->getDoctrine()->getEntityManager();
       
    94         $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
       
    95         // We change the moved DocumentTag's order
       
    96         $new_dt_order = $ordered_tags[$new_order]->getTagOrder();
       
    97         $moved_dt = $ordered_tags[$old_order];
       
    98         $moved_dt->setTagOrder($new_dt_order);
       
    99         // We move the TaggedSheets's order
       
   100         if($new_order > $old_order){
       
   101             // And we decrease the other ones
       
   102             for ($i=($old_order+1); $i <= ($new_order); $i++){
       
   103                 $dt = $ordered_tags[$i];
       
   104                 $dt->setTagOrder($dt->getTagOrder() - 1);
       
   105             }
       
   106         }
       
   107         else{
       
   108             // And we increase the other ones
       
   109             for ($i=$new_order; $i <= ($old_order-1); $i++){
       
   110                 $dt = $ordered_tags[$i];
       
   111                 $dt->setTagOrder($dt->getTagOrder() + 1);
       
   112             }
       
   113         }
       
   114         // Save datas.
       
   115         $em->flush();
       
   116 
       
   117         return $this->renderDocTags($id_doc);
       
   118     }
       
   119 
       
   120     /**
       
   121      *
       
   122      * TODO: Enter description here ...
       
   123      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
       
   124      */
       
   125     public function removeTagFromListAction()
       
   126     {
       
   127         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   128         $id_tag = $this->getRequest()->request->get('tag_id');
       
   129         // We get the DocumentTag meant to be deleted, and remove it.
       
   130         $em = $this->getDoctrine()->getEntityManager();
       
   131         $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('tag' => $id_tag, 'document' => $id_doc));
       
   132         $em->remove($dt);
       
   133         $em->flush();
       
   134 
       
   135         return $this->renderDocTags($id_doc);
       
   136     }
       
   137 
       
   138     /**
       
   139      *
       
   140      * TODO: Enter description here ...
       
   141      */
       
   142     public function modifyDocumentTagAction()
       
   143     {
       
   144         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   145         $tag_label = $this->getRequest()->request->get('value');
       
   146         $id_moved_tag = $this->getRequest()->request->get('id');
       
   147         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
       
   148         if($tagLabel!=$movedTag->getLabel()){
       
   149             // We get the DocumentTags
       
   150             $em = $this->getDoctrine()->getEntityManager();
       
   151             $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc));
       
   152             $nb_tags = count($tags);
       
   153             $found = false;
       
   154             $i = 0;
       
   155             while($i<$nb_tags && $found==false){
       
   156                 $dt = $tags[$i];
       
   157                 if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){
       
   158                     $found = true;
       
   159                 }
       
   160                 $i++;
       
   161             }
       
   162             // If the label was found, we sent a bad request
       
   163             if($found==true){
       
   164                 return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
       
   165             }
       
   166             // We create the new tag or get the already existing tag. $tag, $revision_id, $created
       
   167             $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label)
       
   168             $tag = $ar[0];
       
   169             $revision_id = $ar[1];
       
   170             $created = $ar[2];
       
   171             // We get the DocumentTag and change its tag
       
   172             $dt = $this->getDoctrine()->getRepository('WikiTagBundle:WikiTagDocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag));
       
   173             $dt->setTag($tag);
       
   174             $dt->setWikipediaRevisionId($revision_id);
       
   175             //
       
   176             // HERE QUERY TO GET A INDEX_NOTE/SCORE for the tag. Here is python code :
       
   177             //kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
       
   178             //results = SearchQuerySet().filter(title=tag_label).filter_or(description=tag_label).filter(**kwargs)
       
   179             //if len(results) > 0:
       
   180             //    ts.index_note = results[0].score
       
   181             //
       
   182             // We save the datas
       
   183             $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneBy(array('id' => $id_doc));
       
   184             $doc->setManualOrder(true);
       
   185             $em->flush();
       
   186         }
       
   187 
       
   188         return $this->renderDocTags($id_doc);
       
   189     }
       
   190 
       
   191     /**
       
   192      *
       
   193      * TODO: Enter description here ...
       
   194      */
       
   195     public function resetWpInfoAction()
       
   196     {
       
   197         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   198         return $this->renderDocTags($id_doc);
       
   199     }
       
   200 
       
   201     /**
       
   202      *
       
   203      * TODO : Enter description here ...
       
   204      */
       
   205     public function reorderTagDocumentAction()
       
   206     {
       
   207         $id_Doc = $this->getRequest()->request->get('wikitag_document_id');
       
   208         return $this->renderDocTags($id_doc);
       
   209     }
       
   210 
       
   211     /**
       
   212      *
       
   213      * TODO: Enter description here ...
       
   214      */
       
   215     public function addTagAction()
       
   216     {
       
   217         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   218         $tag_label = $this->getRequest()->request->get('value');
       
   219         // We get the DocumentTags
       
   220         $em = $this->getDoctrine()->getEntityManager();
       
   221         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc));
       
   222         $nb_tags = count($tags);
       
   223         $found = false;
       
   224         $i = 0;
       
   225         while($i<$nb_tags && $found==false){
       
   226             $dt = $tags[$i];
       
   227             if(strtolower($dt->getTag()->getLabel())==strtolower($tag_label)){
       
   228                 $found = true;
       
   229             }
       
   230             $i++;
       
   231         }
       
   232         // If the label was found, we sent a bad request
       
   233         if($found==true){
       
   234             //TODO : translation
       
   235             return new Response(json_encode(array('error' => 'duplicate_tag', 'message' => sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label))),400);
       
   236         }
       
   237         // $tag, $revision_id, $created
       
   238         $ar = WikiTagUtils::getOrCreateTag($tag_label, $this->getDoctrine());// tag, revision_id, created = get_or_create_tag(tag_label)
       
   239 
       
   240         $tag = $ar[0];
       
   241         $revision_id = $ar[1];
       
   242         $created = $ar[2];
       
   243 
       
   244         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc, 'tag'=>$tag->getId()));
       
   245         $nb_tags = count($tags);
       
   246 
       
   247         if($created==true || $nb_tags==0){
       
   248             $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
       
   249             // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
       
   250             $a1 = reset($new_order_ar);
       
   251             $new_order = intval(reset($a1)) + 1;
       
   252             // TODO: use a factory that returns an DocumentTagInterface
       
   253             $new_DT = new DocumentTag();
       
   254             //return new Response(var_dump($this->getDoctrine()->getRepository('WikiTagBundle:WikiTagDocument')->findOneBy(array('id' => $idDoc))));
       
   255             $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneBy(array('id' => $id_doc)));
       
   256             $new_DT->setTag($tag);
       
   257             $new_DT->setOriginalOrder($new_order);
       
   258             $new_DT->setTagOrder($new_order);
       
   259             $new_DT->setWikipediaRevisionId($revision_id);
       
   260             $em->persist($new_DT);
       
   261             $em->flush();
       
   262         }
       
   263 
       
   264         return $this->renderDocTags($id_doc);
       
   265     }
       
   266 
       
   267 
       
   268     /**
       
   269      *
       
   270      * TODO: Enter description here ...
       
   271      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
       
   272      */
       
   273     public function removeWpLinkAction()
       
   274     {
       
   275         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   276         $id_tag = $this->getRequest()->request->get('tag_id');
       
   277         $tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->find($id_tag);
       
   278         //return new Response(var_dump(array($tag)));
       
   279         // We search if the unsemantized version of the tag already exist.
       
   280         $un_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('label'=>$tag->getLabel(), 'urlStatus'=>Tag::$TAG_URL_STATUS_DICT['null_result']));
       
   281         $em = $this->getDoctrine()->getEntityManager();
       
   282         if(!$un_tag){
       
   283             // Create another tag almost identical, without the W info
       
   284             // TODO: use a factory that return a TagInterface
       
   285             $un_tag = new Tag();
       
   286             $un_tag->setLabel($tag->getLabel());
       
   287             $un_tag->setOriginalLabel($tag->getOriginalLabel());
       
   288             $un_tag->setUrlStatus(Tag::$TAG_URL_STATUS_DICT['null_result']);
       
   289             $un_tag->setWikipediaUrl(null);
       
   290             $un_tag->setWikipediaPageId(null);
       
   291             $un_tag->setDbpediaUri(null);
       
   292             $un_tag->setCategory($tag->getCategory());
       
   293             $un_tag->setAlias($tag->getAlias());
       
   294             $un_tag->setPopularity($tag->getPopularity());
       
   295             $em->persist($un_tag);
       
   296         }
       
   297         // We associate the unsemantized tag to the DocumentTag and save datas
       
   298         $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $idDoc, 'tag' => $idTag));
       
   299         $dt->setTag($un_tag);
       
   300         $em->flush();
       
   301         
       
   302         return $this->renderDocTags($id_doc);
       
   303     }
       
   304 
       
   305 
       
   306     /**
       
   307      *
       
   308      * TODO : Enter description here ...
       
   309      * @return \Symfony\Bundle\FrameworkBundle\Controller\Response
       
   310      */
       
   311     public function updateTagAliasAction()
       
   312     {
       
   313         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   314         return $this->renderDocTags($id_doc);
       
   315     }
       
   316 
       
   317 
       
   318     /**
       
   319      *
       
   320      * TODO: Enter description here ...
       
   321      */
       
   322     public function updateTagCategoryAction()
       
   323     {
       
   324         $id_doc = $this->getRequest()->request->get('wikitag_document_id');
       
   325         $id_tag = $this->getRequest()->request->get('id');
       
   326         $cat_label = $this->getRequest()->request->get('value');
       
   327         // We get the Tag and update its category.
       
   328         $em = $this->getDoctrine()->getEntityManager();
       
   329         $tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->find($id_tag);
       
   330         if($cat_label==''){
       
   331             $cat = null;
       
   332             $tag->nullCategory();
       
   333         }
       
   334         else{
       
   335             $cat = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOneBy(array('label' => $cat_label));
       
   336             $tag->setCategory($cat);
       
   337         }
       
   338         $em->flush();
       
   339 
       
   340         return $this->renderDocTags($id_doc);
       
   341     }
       
   342 
       
   343 
       
   344     /**
       
   345      *
       
   346      * Generic render partial template
       
   347      * @param unknown_type $id_doc
       
   348      */
       
   349     public function renderDocTags($id_doc)
       
   350     {
       
   351         $ordered_tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOrderedTagsForDoc($id_doc);
       
   352         return $this->render('WikiTagBundle:WikiTag:tagTable.html.twig', array('ordered_tags' => $ordered_tags, 'doc_id' => $id_doc));
       
   353     }
       
   354 
       
   355 
       
   356 }