--- a/Controller/WikiTagController.php Thu Oct 20 18:35:33 2011 +0200
+++ b/Controller/WikiTagController.php Fri Oct 21 17:10:54 2011 +0200
@@ -13,6 +13,9 @@
use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
use IRI\Bundle\WikiTagBundle\Entity\Tag;
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
+use Pagerfanta\Pagerfanta;
+use Pagerfanta\Adapter\ArrayAdapter;
+use Pagerfanta\Adapter\DoctrineORMAdapter;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
@@ -351,25 +354,100 @@
*/
public function allTagsAction()
{
- $tags = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findBy(array('id' => '10'));
+ //$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.
- $toto = null;
- if(array_key_exists('toto', $_GET)){
- $toto = $_GET['toto'];
+ // Searched string
+ $searched = "";
+ if(array_key_exists('searched', $_GET)){
+ $searched = $_GET['searched'];
}
- $searched = "truc";
- $search_def = array('A', 'B');
+ // Number of tags per page
$nb_by_page = 50;
- $sort = "p+";
- $current_page = NULL;// avec start_index et end_index
- $nb_total = "truc";// nb de pages
- $num_page = 2;
- $last_page = 4;
+ if(array_key_exists('nb_by_page', $_GET)){
+ $nb_by_page = intval($_GET['nb_by_page']);
+ }
+ // Current page number
+ $num_page = 1;
+ if(array_key_exists('num_page', $_GET)){
+ $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!=""){
+ // We replace "*" by "%".
+ $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'")));
+ }
+ // W add the sorting criteria
+ $sort = "popd"; // sort by descendent popularity by default.
+ $reverse_sort = "popa";
+ if(array_key_exists('sort', $_GET)){
+ $sort = $_GET['sort'];
+ }
+ $sort_query = "t.popularity DESC t.normalizedLabel ASC t.label ASC";
+ switch($sort){
+ case "popd":
+ $qb->addOrderBy('t.popularity','DESC');
+ $qb->addOrderBy('t.normalizedLabel','ASC');
+ $qb->addOrderBy('t.label','ASC');
+ $reverse_sort = "popa";
+ break;
+ case "popa":
+ $qb->addOrderBy('t.popularity','ASC');
+ $qb->addOrderBy('t.normalizedLabel','ASC');
+ $qb->addOrderBy('t.label','ASC');
+ $reverse_sort = "popd";
+ break;
+ case "labd":
+ $qb->addOrderBy('t.normalizedLabel','DESC');
+ $qb->addOrderBy('t.label','DESC');
+ $reverse_sort = "laba";
+ break;
+ case "laba":
+ $qb->addOrderBy('t.normalizedLabel','ASC');
+ $qb->addOrderBy('t.label','ASC');
+ $reverse_sort = "labd";
+ break;
+ }
+
+ // We paginate
+ $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();
+ $pagerfanta->haveToPaginate(); // whether the number of results if higher than the max per page
+
+ // We get the previous and next page number
$prev_page = 1;
- $next_page = 3;
+ if($pagerfanta->hasPreviousPage()){
+ $prev_page = $pagerfanta->getPreviousPage();
+ }
+ $next_page = $last_page;
+ if($pagerfanta->hasNextPage()){
+ $next_page = $pagerfanta->getNextPage();
+ }
+ // We calculate start_index and end_index (tag number in the whole list)
+ $start_index = 1 + (($num_page - 1) * $nb_by_page);
+ $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*');
+
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, '$current_page' => $current_page,
- '$nb_total' => $nb_total, '$num_page' => $num_page, '$last_page' => $last_page, '$prev_page' => $prev_page, '$next_page' => $next_page));
+ 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));
}