Controller/WikiTagController.php
changeset 10 a1234ceba912
parent 9 cc32af725176
child 11 5f038a505cd7
--- a/Controller/WikiTagController.php	Fri Oct 21 17:10:54 2011 +0200
+++ b/Controller/WikiTagController.php	Fri Oct 21 19:22:06 2011 +0200
@@ -10,6 +10,9 @@
 
 namespace IRI\Bundle\WikiTagBundle\Controller;
 
+use Doctrine\ORM\Query\ResultSetMapping;
+use Doctrine\DBAL\DriverManager;
+
 use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
 use IRI\Bundle\WikiTagBundle\Entity\Tag;
 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
@@ -22,6 +25,8 @@
 
 class WikiTagController extends Controller
 {
+    private static $SEARCH_STAR_CHARACTER = "*";
+    
     /**
      * Fake index action
      */
@@ -354,8 +359,6 @@
      */
     public function allTagsAction()
     {
-        //$tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('urlStatus' => 2));
-        //$tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('id' => 10));
         // We get/set all the parameters for the search and pagination.
         // $this->getRequest()->query->get('foo') does not work "because" we are a second controller. So we have to use $_GET.
         // Searched string 
@@ -374,18 +377,15 @@
             $num_page = intval($_GET['num_page']);
         }
         
-        
-        
         // We build the query.
         $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
-        $qb->select('t')
-           ->from('WikiTagBundle:Tag','t');
-        // We add the search string if necessary
-        if($searched!=""){
+        $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)."'")));
         }
-        // W add the sorting criteria
+        // We add the sorting criteria
         $sort = "popd"; // sort by descendent popularity by default.
         $reverse_sort = "popa";
         if(array_key_exists('sort', $_GET)){
@@ -441,8 +441,13 @@
         $end_index = min($nb_total, $start_index + $nb_by_page - 1);
         
         // We build the list of tags's first letters to make quick search.
-        $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*',
-        	'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*');
+        $conn = $this->getDoctrine()->getEntityManager()->getConnection();
+        $sql = "SELECT UPPER(SUBSTRING(normalized_label,1,1)) as fl FROM wikitag_tag GROUP BY fl ORDER BY fl";
+        $letters = $conn->query($sql)->fetchAll();
+        $search_def = array();
+        foreach ($letters as $l){
+            $search_def[$l[0]] = $l[0].WikiTagController::$SEARCH_STAR_CHARACTER;
+        }
         
         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,