Controller/WikiTagController.php
changeset 20 985f1992895d
parent 16 876df98c9208
child 21 780ef37e63b9
equal deleted inserted replaced
19:7051e55a3131 20:985f1992895d
    24 
    24 
    25 
    25 
    26 class WikiTagController extends Controller
    26 class WikiTagController extends Controller
    27 {
    27 {
    28     private static $SEARCH_STAR_CHARACTER = "*";
    28     private static $SEARCH_STAR_CHARACTER = "*";
       
    29     private static $ROUTE_FOR_DOCUMENTS_BY_TAG = "company_other";
    29     
    30     
    30     /**
    31     /**
    31      * Fake index action
    32      * Fake index action
    32      */
    33      */
    33     public function indexAction()
    34     public function indexAction()
   427             $sort = $_GET['sort'];
   428             $sort = $_GET['sort'];
   428         }
   429         }
   429         
   430         
   430         // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
   431         // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
   431         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
   432         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
       
   433         //return new Response($ar);
   432         $tags = $ar[0];
   434         $tags = $ar[0];
   433         $num_page = $ar[1];
   435         $num_page = $ar[1];
   434         $nb_by_page = $ar[2];
   436         $nb_by_page = $ar[2];
   435         $searched = $ar[3];
   437         $searched = $ar[3];
   436         $sort = $ar[4];
   438         $sort = $ar[4];
   462         }
   464         }
   463         
   465         
   464         return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
   466         return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
   465             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 
   467             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 
   466             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page, 
   468             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page, 
   467         	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort));
   469         	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => WikiTagController::$ROUTE_FOR_DOCUMENTS_BY_TAG));
   468     }
   470     }
   469 
   471 
   470 
   472 
   471     /**
   473     /**
   472      * Generic render partial template for tag list
   474      * Generic render partial template for tag list
   473      */
   475      */
   474     public function renderAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
   476     public function renderAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
   475     {
   477     {
   476         // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
   478         
       
   479         //We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
   477         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
   480         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
   478         $tags = $ar[0];
   481         $tags = $ar[0];
   479         $num_page = $ar[1];
   482         $num_page = $ar[1];
   480         $nb_by_page = $ar[2];
   483         $nb_by_page = $ar[2];
   481         $searched = $ar[3];
   484         $searched = $ar[3];
   482         $sort = $ar[4];
   485         $sort = $ar[4];
   483         $reverse_sort = $ar[5];
   486         $reverse_sort = $ar[5];
   484         
   487         
   485         return $this->render('WikiTagBundle:WikiTag:TagListTable.html.twig', 
   488         return $this->render('WikiTagBundle:WikiTag:TagListTable.html.twig', 
   486             array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page, 'reverse_sort' => $reverse_sort));
   489             array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page, 
       
   490         	'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => WikiTagController::$ROUTE_FOR_DOCUMENTS_BY_TAG));
       
   491         
       
   492         return $this->getAllTags();
   487     }
   493     }
   488 
   494 
   489 
   495 
   490     /**
   496     /**
   491      * Generic to get all tags with the context (pagination number, nb by page, searched string, sort)
   497      * Generic to get all tags with the context (pagination number, nb by page, searched string, sort)
   506             $num_page = 1;
   512             $num_page = 1;
   507         }
   513         }
   508         
   514         
   509         // We build the query.
   515         // We build the query.
   510         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
   516         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
   511         $qb->select('t')->from('WikiTagBundle:Tag','t');
   517         $qb->select('t', 'COUNT( dt.id ) AS nb_docs');
   512         // We add the search string if necessary ('* bugs)
   518         $qb->from('WikiTagBundle:Tag','t');
   513         if($searched!="" && $searched!="'*"){
   519         $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
   514             // We replace "*" by "%".
   520         $qb->addGroupBy('t.id');
   515             $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
   521         
   516         }
   522         // We add the search string if necessary
       
   523         if($searched!=""){
       
   524             // We replace "*" by "%", and doctrine wants ' to be ''.
       
   525             $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("'", "''", str_replace("*", "%", $searched))."'")));
       
   526         }
       
   527         //return $qb->getDql();
       
   528         
   517         // We add the sorting criteria
   529         // We add the sorting criteria
   518         if($sort==NULL){
   530         if($sort==NULL){
   519             $sort = "popd"; // sort by descendent popularity by default.
   531             $sort = "popd"; // sort by descendent popularity by default.
   520             $reverse_sort = "popa";
   532             $reverse_sort = "popa";
   521         }
   533         }
   547         
   559         
   548         // We paginate
   560         // We paginate
   549         $adapter = new DoctrineORMAdapter($qb);
   561         $adapter = new DoctrineORMAdapter($qb);
   550         $pagerfanta = new Pagerfanta($adapter);
   562         $pagerfanta = new Pagerfanta($adapter);
   551         $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
   563         $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
   552         //$last_page = $pagerfanta->getNbPages();
       
   553         $pagerfanta->setCurrentPage($num_page); // 1 by default
   564         $pagerfanta->setCurrentPage($num_page); // 1 by default
   554         $nb_total = $pagerfanta->getNbResults();
   565         $nb_total = $pagerfanta->getNbResults();
   555         $tags = $pagerfanta->getCurrentPageResults();
   566         $tags = $pagerfanta->getCurrentPageResults();
   556         $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
   567         $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
   557         
   568