Controller/WikiTagController.php
changeset 20 985f1992895d
parent 16 876df98c9208
child 21 780ef37e63b9
--- a/Controller/WikiTagController.php	Thu Oct 27 21:26:30 2011 +0200
+++ b/Controller/WikiTagController.php	Thu Oct 27 21:37:34 2011 +0200
@@ -26,6 +26,7 @@
 class WikiTagController extends Controller
 {
     private static $SEARCH_STAR_CHARACTER = "*";
+    private static $ROUTE_FOR_DOCUMENTS_BY_TAG = "company_other";
     
     /**
      * Fake index action
@@ -429,6 +430,7 @@
         
         // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
+        //return new Response($ar);
         $tags = $ar[0];
         $num_page = $ar[1];
         $nb_by_page = $ar[2];
@@ -464,7 +466,7 @@
         return $this->render('WikiTagBundle:WikiTag:TagList.html.twig', 
             array('tags' => $tags, 'searched' => $searched, 'search_def' => $search_def, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 
             'start_index' => $start_index, 'end_index' => $end_index, 'nb_total' => $nb_total, 'num_page' => $num_page, 'last_page' => $last_page, 
-        	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort));
+        	'prev_page' => $prev_page, 'next_page' => $next_page, 'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => WikiTagController::$ROUTE_FOR_DOCUMENTS_BY_TAG));
     }
 
 
@@ -473,7 +475,8 @@
      */
     public function renderAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL)
     {
-        // We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
+        
+        //We get the needed datas in an array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta);
         $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched);
         $tags = $ar[0];
         $num_page = $ar[1];
@@ -483,7 +486,10 @@
         $reverse_sort = $ar[5];
         
         return $this->render('WikiTagBundle:WikiTag:TagListTable.html.twig', 
-            array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page, 'reverse_sort' => $reverse_sort));
+            array('tags' => $tags, 'searched' => $searched, 'nb_by_page' => $nb_by_page, 'sort' => $sort, 'num_page' => $num_page, 
+        	'reverse_sort' => $reverse_sort, 'route_for_documents_by_tag' => WikiTagController::$ROUTE_FOR_DOCUMENTS_BY_TAG));
+        
+        return $this->getAllTags();
     }
 
 
@@ -508,12 +514,18 @@
         
         // We build the query.
         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
-        $qb->select('t')->from('WikiTagBundle:Tag','t');
-        // We add the search string if necessary ('* bugs)
-        if($searched!="" && $searched!="'*"){
-            // We replace "*" by "%".
-            $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
+        $qb->select('t', 'COUNT( dt.id ) AS nb_docs');
+        $qb->from('WikiTagBundle:Tag','t');
+        $qb->leftJoin('t.documents', 'dt', 'WITH', 't = dt.tag');
+        $qb->addGroupBy('t.id');
+        
+        // We add the search string if necessary
+        if($searched!=""){
+            // We replace "*" by "%", and doctrine wants ' to be ''.
+            $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("'", "''", str_replace("*", "%", $searched))."'")));
         }
+        //return $qb->getDql();
+        
         // We add the sorting criteria
         if($sort==NULL){
             $sort = "popd"; // sort by descendent popularity by default.
@@ -549,7 +561,6 @@
         $adapter = new DoctrineORMAdapter($qb);
         $pagerfanta = new Pagerfanta($adapter);
         $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default
-        //$last_page = $pagerfanta->getNbPages();
         $pagerfanta->setCurrentPage($num_page); // 1 by default
         $nb_total = $pagerfanta->getNbResults();
         $tags = $pagerfanta->getCurrentPageResults();