Controller/WikiTagController.php
changeset 11 5f038a505cd7
parent 10 a1234ceba912
child 12 81cc9274c20a
equal deleted inserted replaced
10:a1234ceba912 11:5f038a505cd7
   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');
   143         $id_moved_tag = $this->getRequest()->request->get('id');
   143         $id_moved_tag = $this->getRequest()->request->get('id');
   144         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
   144         $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag));
   145         if($tagLabel!=$movedTag->getLabel()){
   145         if($tag_label!=$moved_tag->getLabel()){
   146             // We get the DocumentTags
   146             // We get the DocumentTags
   147             $em = $this->getDoctrine()->getEntityManager();
   147             $em = $this->getDoctrine()->getEntityManager();
   148             $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc));
   148             $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc));
   149             $nb_tags = count($tags);
   149             $nb_tags = count($tags);
   150             $found = false;
   150             $found = false;
   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             // We get the DocumentTag and change its tag
   168             // We get the DocumentTag and change its tag
   169             $dt = $this->getDoctrine()->getRepository('WikiTagBundle:WikiTagDocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag));
   169             $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag));
   170             $dt->setTag($tag);
   170             $dt->setTag($tag);
   171             $dt->setWikipediaRevisionId($revision_id);
   171             $dt->setWikipediaRevisionId($revision_id);
   172             //
   172             //
   173             // HERE QUERY TO GET A INDEX_NOTE/SCORE for the tag. Here is python code :
   173             // HERE QUERY TO GET A INDEX_NOTE/SCORE for the tag. Here is python code :
   174             //kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
   174             //kwargs = {DJANGO_ID + "__exact": unicode(ds_id)}
   311             $cat = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOneBy(array('label' => $cat_label));
   311             $cat = $this->getDoctrine()->getRepository('WikiTagBundle:Category')->findOneBy(array('label' => $cat_label));
   312             $tag->setCategory($cat);
   312             $tag->setCategory($cat);
   313         }
   313         }
   314         $em->flush();
   314         $em->flush();
   315 
   315 
   316         return $this->renderDocTags($id_doc);
   316         if($id_doc && $id_doc!=""){
       
   317             return $this->renderDocTags($id_doc);
       
   318         }
       
   319         else{
       
   320             $num_page = $this->getRequest()->request->get('num_page');
       
   321             $nb_by_page = $this->getRequest()->request->get('nb_by_page');
       
   322             $sort = $this->getRequest()->request->get('sort');
       
   323             $searched = $this->getRequest()->request->get('searched');
       
   324             return $this->renderAllTags($num_page, $nb_by_page, $sort, $searched);
       
   325         }
   317     }
   326     }
   318 
   327 
   319 
   328 
   320     /**
   329     /**
   321      *
   330      *
   357      * List of all tags
   366      * List of all tags
   358      * TODO: Enter description here ...
   367      * TODO: Enter description here ...
   359      */
   368      */
   360     public function allTagsAction()
   369     public function allTagsAction()
   361     {
   370     {
   362         // We get/set all the parameters for the search and pagination.
       
   363         // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
   371         // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
   364         // Searched string 
   372         // Searched string
   365         $searched = "";
   373         $searched = NULL;
   366         if(array_key_exists('searched', $_GET)){
   374         if(array_key_exists('searched', $_GET)){
   367             $searched = $_GET['searched'];
   375             $searched = $_GET['searched'];
   368         }
   376         }
   369         // Number of tags per page
   377         // Number of tags per page
   370         $nb_by_page = 50;
   378         $nb_by_page = 50;
   373         }
   381         }
   374         // Current page number
   382         // Current page number
   375         $num_page = 1;
   383         $num_page = 1;
   376         if(array_key_exists('num_page', $_GET)){
   384         if(array_key_exists('num_page', $_GET)){
   377             $num_page = intval($_GET['num_page']);
   385             $num_page = intval($_GET['num_page']);
       
   386         }
       
   387         // Sorting criteria
       
   388         $sort = NULL;
       
   389         if(array_key_exists('sort', $_GET)){
       
   390             $sort = $_GET['sort'];
       
   391         }
       
   392         
       
   393         // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
       
   394         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
       
   395         $tags = $ar[0];
       
   396         $num_page = $ar[1];
       
   397         $nb_by_page = $ar[2];
       
   398         $searched = $ar[3];
       
   399         $sort = $ar[4];
       
   400         $reverse_sort = $ar[5];
       
   401         $pagerfanta = $ar[6];
       
   402         
       
   403         // We get the needed vars : number totals of tags, previous and next page number
       
   404         $last_page = $pagerfanta->getNbPages();
       
   405         $nb_total = $pagerfanta->getNbResults();
       
   406         $prev_page = 1;
       
   407         if($pagerfanta->hasPreviousPage()){
       
   408             $prev_page = $pagerfanta->getPreviousPage();
       
   409         }
       
   410         $next_page = $last_page;
       
   411         if($pagerfanta->hasNextPage()){
       
   412             $next_page = $pagerfanta->getNextPage();
       
   413         }
       
   414         // We calculate start_index and end_index (number of tags in the whole list)
       
   415         $start_index = 1 + (($num_page - 1) * $nb_by_page);
       
   416         $end_index = min($nb_total, $start_index + $nb_by_page - 1);
       
   417         
       
   418         // We build the list of tags's first letters to make quick search.
       
   419         $conn = $this->getDoctrine()->getEntityManager()->getConnection();
       
   420         $sql = "SELECT UPPER(SUBSTRING(normalized_label,1,1)) as fl FROM wikitag_tag GROUP BY fl ORDER BY fl";
       
   421         $letters = $conn->query($sql)->fetchAll();
       
   422         $search_def = array();
       
   423         foreach ($letters as $l){
       
   424             $search_def[$l[0]] = $l[0].WikiTagController::$SEARCH_STAR_CHARACTER;
       
   425         }
       
   426         
       
   427         return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
       
   428             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 
       
   429             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page, 
       
   430         	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort));
       
   431     }
       
   432 
       
   433 
       
   434     /**
       
   435      * Generic render partial template for tag list
       
   436      */
       
   437     public function renderAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
       
   438     {
       
   439         // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
       
   440         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
       
   441         $tags = $ar[0];
       
   442         $num_page = $ar[1];
       
   443         $nb_by_page = $ar[2];
       
   444         $searched = $ar[3];
       
   445         $sort = $ar[4];
       
   446         $reverse_sort = $ar[5];
       
   447         //$pagerfanta = $ar[6];
       
   448         
       
   449         return $this->render('WikiTagBundle:WikiTag:TagListTable.html.twig', 
       
   450             array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page, 'reverse_sort' => $reverse_sort));
       
   451     }
       
   452 
       
   453 
       
   454     /**
       
   455      * Generic to get all tags with the context (pagination number, nb by page, searched string, sort)
       
   456      */
       
   457     private function getAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
       
   458     {
       
   459         // We get/set all the parameters for the search and pagination.
       
   460         // Searched string
       
   461         if($searched==NULL){
       
   462             $searched = "";
       
   463         }
       
   464         // Number of tags per page
       
   465         if($nb_by_page==NULL){
       
   466             $nb_by_page = 50;
       
   467         }
       
   468         // Current page number
       
   469         if($num_page==NULL){
       
   470             $num_page = 1;
   378         }
   471         }
   379         
   472         
   380         // We build the query.
   473         // We build the query.
   381         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
   474         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
   382         $qb->select('t')->from('WikiTagBundle:Tag','t');
   475         $qb->select('t')->from('WikiTagBundle:Tag','t');
   384         if($searched!="" && $searched!="'*"){
   477         if($searched!="" && $searched!="'*"){
   385             // We replace "*" by "%".
   478             // We replace "*" by "%".
   386             $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
   479             $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
   387         }
   480         }
   388         // We add the sorting criteria
   481         // We add the sorting criteria
   389         $sort = "popd"; // sort by descendent popularity by default.
   482         if($sort==NULL){
   390         $reverse_sort = "popa";
   483             $sort = "popd"; // sort by descendent popularity by default.
   391         if(array_key_exists('sort', $_GET)){
   484             $reverse_sort = "popa";
   392             $sort = $_GET['sort'];
       
   393         }
   485         }
   394         $sort_query = "t.popularity DESC t.normalizedLabel ASC t.label ASC";
   486         $sort_query = "t.popularity DESC t.normalizedLabel ASC t.label ASC";
   395         switch($sort){
   487         switch($sort){
   396             case "popd":
   488             case "popd":
   397                 $qb->addOrderBy('t.popularity','DESC');
   489                 $qb->addOrderBy('t.popularity','DESC');
   419         
   511         
   420         // We paginate
   512         // We paginate
   421         $adapter = new DoctrineORMAdapter($qb);
   513         $adapter = new DoctrineORMAdapter($qb);
   422         $pagerfanta = new Pagerfanta($adapter);
   514         $pagerfanta = new Pagerfanta($adapter);
   423         $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
   515         $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
   424         $last_page = $pagerfanta->getNbPages();
   516         //$last_page = $pagerfanta->getNbPages();
   425         $pagerfanta->setCurrentPage($num_page); // 1 by default
   517         $pagerfanta->setCurrentPage($num_page); // 1 by default
   426         $nb_total = $pagerfanta->getNbResults();
   518         $nb_total = $pagerfanta->getNbResults();
   427         $tags = $pagerfanta->getCurrentPageResults();
   519         $tags = $pagerfanta->getCurrentPageResults();
   428         $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
   520         $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
   429         
   521         
   430         // We get the previous and next page number
   522         return array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
   431         $prev_page = 1;
       
   432         if($pagerfanta->hasPreviousPage()){
       
   433             $prev_page = $pagerfanta->getPreviousPage();
       
   434         }
       
   435         $next_page = $last_page;
       
   436         if($pagerfanta->hasNextPage()){
       
   437             $next_page = $pagerfanta->getNextPage();
       
   438         }
       
   439         // We calculate start_index and end_index (tag number in the whole list)
       
   440         $start_index = 1 + (($num_page - 1) * $nb_by_page);
       
   441         $end_index = min($nb_total, $start_index + $nb_by_page - 1);
       
   442         
       
   443         // We build the list of tags's first letters to make quick search.
       
   444         $conn = $this->getDoctrine()->getEntityManager()->getConnection();
       
   445         $sql = "SELECT UPPER(SUBSTRING(normalized_label,1,1)) as fl FROM wikitag_tag GROUP BY fl ORDER BY fl";
       
   446         $letters = $conn->query($sql)->fetchAll();
       
   447         $search_def = array();
       
   448         foreach ($letters as $l){
       
   449             $search_def[$l[0]] = $l[0].WikiTagController::$SEARCH_STAR_CHARACTER;
       
   450         }
       
   451         
       
   452         return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
       
   453             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 
       
   454             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page, 
       
   455         	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort));
       
   456     }
   523     }
   457 
   524 
   458 
   525 
   459 }
   526 }