diff -r a1234ceba912 -r 5f038a505cd7 Controller/WikiTagController.php --- a/Controller/WikiTagController.php Fri Oct 21 19:22:06 2011 +0200 +++ b/Controller/WikiTagController.php Mon Oct 24 18:45:34 2011 +0200 @@ -142,7 +142,7 @@ $tag_label = $this->getRequest()->request->get('value'); $id_moved_tag = $this->getRequest()->request->get('id'); $moved_tag = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->findOneBy(array('id' => $id_moved_tag)); - if($tagLabel!=$movedTag->getLabel()){ + if($tag_label!=$moved_tag->getLabel()){ // We get the DocumentTags $em = $this->getDoctrine()->getEntityManager(); $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findBy(array('document' => $id_doc)); @@ -166,7 +166,7 @@ $revision_id = $ar[1]; $created = $ar[2]; // We get the DocumentTag and change its tag - $dt = $this->getDoctrine()->getRepository('WikiTagBundle:WikiTagDocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag)); + $dt = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findOneBy(array('document' => $id_doc, 'tag' => $id_moved_tag)); $dt->setTag($tag); $dt->setWikipediaRevisionId($revision_id); // @@ -313,7 +313,16 @@ } $em->flush(); - return $this->renderDocTags($id_doc); + if($id_doc && $id_doc!=""){ + return $this->renderDocTags($id_doc); + } + else{ + $num_page = $this->getRequest()->request->get('num_page'); + $nb_by_page = $this->getRequest()->request->get('nb_by_page'); + $sort = $this->getRequest()->request->get('sort'); + $searched = $this->getRequest()->request->get('searched'); + return $this->renderAllTags($num_page, $nb_by_page, $sort, $searched); + } } @@ -359,10 +368,9 @@ */ public function allTagsAction() { - // 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 - $searched = ""; + // Searched string + $searched = NULL; if(array_key_exists('searched', $_GET)){ $searched = $_GET['searched']; } @@ -376,6 +384,91 @@ if(array_key_exists('num_page', $_GET)){ $num_page = intval($_GET['num_page']); } + // Sorting criteria + $sort = NULL; + if(array_key_exists('sort', $_GET)){ + $sort = $_GET['sort']; + } + + // 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]; + $nb_by_page = $ar[2]; + $searched = $ar[3]; + $sort = $ar[4]; + $reverse_sort = $ar[5]; + $pagerfanta = $ar[6]; + + // We get the needed vars : number totals of tags, previous and next page number + $last_page = $pagerfanta->getNbPages(); + $nb_total = $pagerfanta->getNbResults(); + $prev_page = 1; + 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 (number of tags 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. + $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, + '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)); + } + + + /** + * Generic render partial template for tag list + */ + 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); + $ar = $this->getAllTags($num_page, $nb_by_page, $sort, $searched); + $tags = $ar[0]; + $num_page = $ar[1]; + $nb_by_page = $ar[2]; + $searched = $ar[3]; + $sort = $ar[4]; + $reverse_sort = $ar[5]; + //$pagerfanta = $ar[6]; + + 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)); + } + + + /** + * Generic to get all tags with the context (pagination number, nb by page, searched string, sort) + */ + private function getAllTags($num_page=NULL, $nb_by_page=NULL, $sort=NULL, $searched=NULL) + { + // We get/set all the parameters for the search and pagination. + // Searched string + if($searched==NULL){ + $searched = ""; + } + // Number of tags per page + if($nb_by_page==NULL){ + $nb_by_page = 50; + } + // Current page number + if($num_page==NULL){ + $num_page = 1; + } // We build the query. $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder(); @@ -386,10 +479,9 @@ $qb->where($qb->expr()->orx($qb->expr()->like('t.normalizedLabel', "'".str_replace("*", "%", $searched)."'"))); } // We add the sorting criteria - $sort = "popd"; // sort by descendent popularity by default. - $reverse_sort = "popa"; - if(array_key_exists('sort', $_GET)){ - $sort = $_GET['sort']; + if($sort==NULL){ + $sort = "popd"; // sort by descendent popularity by default. + $reverse_sort = "popa"; } $sort_query = "t.popularity DESC t.normalizedLabel ASC t.label ASC"; switch($sort){ @@ -421,38 +513,13 @@ $adapter = new DoctrineORMAdapter($qb); $pagerfanta = new Pagerfanta($adapter); $pagerfanta->setMaxPerPage($nb_by_page); // 10 by default - $last_page = $pagerfanta->getNbPages(); + //$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; - 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. - $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, - '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)); + return array($tags, $num_page, $nb_by_page, $searched, $sort, $reverse_sort, $pagerfanta); }