Controller/WikiTagController.php
changeset 9 cc32af725176
parent 8 7d2fb5d7c9ff
child 10 a1234ceba912
equal deleted inserted replaced
8:7d2fb5d7c9ff 9:cc32af725176
    11 namespace IRI\Bundle\WikiTagBundle\Controller;
    11 namespace IRI\Bundle\WikiTagBundle\Controller;
    12 
    12 
    13 use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
    13 use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
    14 use IRI\Bundle\WikiTagBundle\Entity\Tag;
    14 use IRI\Bundle\WikiTagBundle\Entity\Tag;
    15 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
    15 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
       
    16 use Pagerfanta\Pagerfanta;
       
    17 use Pagerfanta\Adapter\ArrayAdapter;
       
    18 use Pagerfanta\Adapter\DoctrineORMAdapter;
    16 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    19 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    17 use Symfony\Component\HttpFoundation\Response;
    20 use Symfony\Component\HttpFoundation\Response;
    18 
    21 
    19 
    22 
    20 class WikiTagController extends Controller
    23 class WikiTagController extends Controller
   349      * List of all tags
   352      * List of all tags
   350      * TODO: Enter description here ...
   353      * TODO: Enter description here ...
   351      */
   354      */
   352     public function allTagsAction()
   355     public function allTagsAction()
   353     {
   356     {
   354         $tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('id' => '10'));
   357         //$tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('urlStatus' => 2));
       
   358         //$tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('id' => 10));
       
   359         // We get/set all the parameters for the search and pagination.
   355         // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
   360         // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
   356         $toto = null;
   361         // Searched string 
   357         if(array_key_exists('toto', $_GET)){
   362         $searched = "";
   358             $toto = $_GET['toto'];
   363         if(array_key_exists('searched', $_GET)){
   359         }
   364             $searched = $_GET['searched'];
   360         $searched = "truc";
   365         }
   361         $search_def = array('A', 'B');
   366         // Number of tags per page
   362         $nb_by_page = 50;
   367         $nb_by_page = 50;
   363         $sort = "p+";
   368         if(array_key_exists('nb_by_page', $_GET)){
   364         $current_page = NULL;// avec start_index et end_index
   369             $nb_by_page = intval($_GET['nb_by_page']);
   365         $nb_total = "truc";// nb de pages
   370         }
   366         $num_page = 2;
   371         // Current page number
   367         $last_page = 4;
   372         $num_page = 1;
       
   373         if(array_key_exists('num_page', $_GET)){
       
   374             $num_page = intval($_GET['num_page']);
       
   375         }
       
   376         
       
   377         
       
   378         
       
   379         // We build the query.
       
   380         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
       
   381         $qb->select('t')
       
   382            ->from('WikiTagBundle:Tag','t');
       
   383         // We add the search string if necessary
       
   384         if($searched!=""){
       
   385             // We replace "*" by "%".
       
   386             $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
       
   387         }
       
   388         // W add the sorting criteria
       
   389         $sort = "popd"; // sort by descendent popularity by default.
       
   390         $reverse_sort = "popa";
       
   391         if(array_key_exists('sort', $_GET)){
       
   392             $sort = $_GET['sort'];
       
   393         }
       
   394         $sort_query = "t.popularity DESC t.normalizedLabel ASC t.label ASC";
       
   395         switch($sort){
       
   396             case "popd":
       
   397                 $qb->addOrderBy('t.popularity','DESC');
       
   398                 $qb->addOrderBy('t.normalizedLabel','ASC');
       
   399                 $qb->addOrderBy('t.label','ASC');
       
   400                 $reverse_sort = "popa";
       
   401                 break;
       
   402             case "popa":
       
   403                 $qb->addOrderBy('t.popularity','ASC');
       
   404                 $qb->addOrderBy('t.normalizedLabel','ASC');
       
   405                 $qb->addOrderBy('t.label','ASC');
       
   406                 $reverse_sort = "popd";
       
   407                 break;
       
   408             case "labd":
       
   409                 $qb->addOrderBy('t.normalizedLabel','DESC');
       
   410                 $qb->addOrderBy('t.label','DESC');
       
   411                 $reverse_sort = "laba";
       
   412                 break;
       
   413             case "laba":
       
   414                 $qb->addOrderBy('t.normalizedLabel','ASC');
       
   415                 $qb->addOrderBy('t.label','ASC');
       
   416                 $reverse_sort = "labd";
       
   417                 break;
       
   418         }
       
   419         
       
   420         // We paginate
       
   421         $adapter = new DoctrineORMAdapter($qb);
       
   422         $pagerfanta = new Pagerfanta($adapter);
       
   423         $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
       
   424         $last_page = $pagerfanta->getNbPages();
       
   425         $pagerfanta->setCurrentPage($num_page); // 1 by default
       
   426         $nb_total = $pagerfanta->getNbResults();
       
   427         $tags = $pagerfanta->getCurrentPageResults();
       
   428         $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
       
   429         
       
   430         // We get the previous and next page number
   368         $prev_page = 1;
   431         $prev_page = 1;
   369         $next_page = 3;
   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         $search_def = array('A'=>'a*', 'B'=>'b*', 'C'=>'c*', 'D'=>'d*', 'E'=>'e*', 'F'=>'f*', 'G'=>'g*', 'H'=>'h*', 'I'=>'i*', 'J'=>'j*', 'K'=>'k*', 'L'=>'l*',
       
   445         	'M'=>'m*', 'N'=>'n*', 'O'=>'o*', 'P'=>'p*', 'Q'=>'q*', 'R'=>'r*', 'S'=>'s*', 'T'=>'t*', 'U'=>'u*', 'V'=>'v*', 'W'=>'w*', 'X'=>'x*', 'Y'=>'y*', 'Z'=>'z*');
       
   446         
   370         return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
   447         return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
   371             array('tags' => $tags, 'searched' => $searched, '$search_def' => $search_def, '$nb_by_page' => $nb_by_page, '$sort' => $sort, '$current_page' => $current_page,
   448             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 
   372             '$nb_total' => $nb_total, '$num_page' => $num_page, '$last_page' => $last_page, '$prev_page' => $prev_page, '$next_page' => $next_page));
   449             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page, 
       
   450         	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort));
   373     }
   451     }
   374 
   452 
   375 
   453 
   376 }
   454 }