first step for tag list and add Pagerfanta for paginator
authorcavaliet
Fri, 21 Oct 2011 17:10:54 +0200
changeset 9 cc32af725176
parent 8 7d2fb5d7c9ff
child 10 a1234ceba912
first step for tag list and add Pagerfanta for paginator
Controller/WikiTagController.php
Resources/views/WikiTag/TagList.html.twig
Resources/views/WikiTag/TagListTable.html.twig
Resources/views/WikiTag/tagTable.html.twig
Utils/WikiTagUtils.php
--- 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));
     }
 
 
--- a/Resources/views/WikiTag/TagList.html.twig	Thu Oct 20 18:35:33 2011 +0200
+++ b/Resources/views/WikiTag/TagList.html.twig	Fri Oct 21 17:10:54 2011 +0200
@@ -1,52 +1,45 @@
 <form method="GET" action="">
-<p>Chercher : 
-{#{% for s in search_def %}
-<a href="{% url hdabo.views.all_tags num_page=1, nb_by_page=nb_by_page, sort=sort, searched=s.1 %}">{{s.0}}</a>
-{% endfor %}#}
-<input type="text" name="searched" value="{#{% if searched %}{{searched}}{% endif %}#}" id="searched" />
-</p>
-<p>Tags {#{{ current_page.start_index }} à {{ current_page.end_index }} sur {{ nb_total }}#}</p>
-<div id="count_nav_top"><p>
-{#
+  <p>Chercher : 
+    {% for k,v in search_def %}
+	   <a href="?searched={{v}}">{{k}}</a>
+    {% endfor %}
+	<input type="text" name="searched" value="{% if searched %}{{searched}}{% endif %}" id="searched" />
+  </p>
+  <p>Tags {{ start_index }} à {{ end_index }} sur {{ nb_total }}</p>
+  <div id="count_nav_top"><p>
     {% if searched and searched != "" %}
-        <a href="{% url hdabo.views.all_tags num_page=1, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&lt;&lt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=prev_page, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&lt;</a>&nbsp;&nbsp;
+        <a href="?searched={{searched}}&num_page=1&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;&lt;</a>&nbsp;&nbsp;
+        <a href="?searched={{searched}}&num_page={{prev_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;</a>&nbsp;&nbsp;
         {{ num_page }}/{{ last_page }}&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=next_page, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&gt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=last_page, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&gt;&gt;</a></p>
+        <a href="?searched={{searched}}&num_page={{next_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;</a>&nbsp;&nbsp;
+        <a href="?searched={{searched}}&num_page={{last_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;&gt;</a></p>
     {% else %}
-        <a href="{% url hdabo.views.all_tags num_page=1, nb_by_page=nb_by_page, sort=sort %}">&lt;&lt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=prev_page, nb_by_page=nb_by_page, sort=sort %}">&lt;</a>&nbsp;&nbsp;
+        <a href="?num_page=1&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;&lt;</a>&nbsp;&nbsp;
+        <a href="?num_page={{prev_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;</a>&nbsp;&nbsp;
         {{ num_page }}/{{ last_page }}&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=next_page, nb_by_page=nb_by_page, sort=sort %}">&gt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=last_page, nb_by_page=nb_by_page, sort=sort %}">&gt;&gt;</a>
+        <a href="?num_page={{next_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;</a>&nbsp;&nbsp;
+        <a href="?num_page={{last_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;&gt;</a></p>
     {% endif %}</p>
-#}
-</div>
-{#
-<input type="hidden" name="num_page" value="{{num_page}}" id="num_page" />
+  </div>
+{#<input type="hidden" name="num_page" value="{{num_page}}" id="num_page" />
 <input type="hidden" name="nb_by_page" value="{{nb_by_page}}" id="nb_by_page" />
-<input type="hidden" name="searched_str" value="{% if searched %}{{searched}}{% endif %}" id="searched_str" />
-<input type="hidden" name="sort" value="{% if sort %}{{sort}}{% endif %}" id="sort" />
-#}
+<input type="hidden" name="sort" value="{% if sort %}{{sort}}{% endif %}" id="sort" />#}
 </form>
 <div id="tag_table_container">
 {% include "WikiTagBundle:WikiTag:TagListTable.html.twig" %}
 </div>
 <div id="count_nav_bottom"><p>
-{#
     {% if searched and searched != "" %}
-        <a href="{% url hdabo.views.all_tags num_page=1, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&lt;&lt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=prev_page, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&lt;</a>&nbsp;&nbsp;
+        <a href="?searched={{searched}}&num_page=1&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;&lt;</a>&nbsp;&nbsp;
+        <a href="?searched={{searched}}&num_page={{prev_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;</a>&nbsp;&nbsp;
         {{ num_page }}/{{ last_page }}&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=next_page, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&gt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=last_page, nb_by_page=nb_by_page, sort=sort, searched=searched %}">&gt;&gt;</a></p>
+        <a href="?searched={{searched}}&num_page={{next_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;</a>&nbsp;&nbsp;
+        <a href="?searched={{searched}}&num_page={{last_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;&gt;</a></p>
     {% else %}
-        <a href="{% url hdabo.views.all_tags num_page=1, nb_by_page=nb_by_page, sort=sort %}">&lt;&lt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=prev_page, nb_by_page=nb_by_page, sort=sort %}">&lt;</a>&nbsp;&nbsp;
+        <a href="?num_page=1&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;&lt;</a>&nbsp;&nbsp;
+        <a href="?num_page={{prev_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&lt;</a>&nbsp;&nbsp;
         {{ num_page }}/{{ last_page }}&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=next_page, nb_by_page=nb_by_page, sort=sort %}">&gt;</a>&nbsp;&nbsp;
-        <a href="{% url hdabo.views.all_tags num_page=last_page, nb_by_page=nb_by_page, sort=sort %}">&gt;&gt;</a>
+        <a href="?num_page={{next_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;</a>&nbsp;&nbsp;
+        <a href="?num_page={{last_page}}&nb_by_page={{nb_by_page}}&sort={{sort}}">&gt;&gt;</a></p>
     {% endif %}</p>
-#}
 </div>
\ No newline at end of file
--- a/Resources/views/WikiTag/TagListTable.html.twig	Thu Oct 20 18:35:33 2011 +0200
+++ b/Resources/views/WikiTag/TagListTable.html.twig	Fri Oct 21 17:10:54 2011 +0200
@@ -1,1 +1,90 @@
-DUDE 2
\ No newline at end of file
+{% block tag_table %}
+    <table id="all_tags_table">
+    <tr><th>id</th>
+        <th>
+        {% if sort != "laba" and sort != "labd" %}
+            {% if searched and searched != "" %}
+                <a href="?searched={{searched}}&num_page=1&nb_by_page={{nb_by_page}}&sort=laba">label</a>
+            {% else %}
+                <a href="?num_page=1&nb_by_page={{nb_by_page}}&sort=laba">label</a>
+            {% endif %}
+        {% else %}
+            label &nbsp;
+            {% if searched and searched != "" %}
+                <a href="?searched={{searched}}&num_page=1&nb_by_page={{nb_by_page}}&sort={{reverse_sort}}">
+            {% else %}
+                <a href="?num_page=1&nb_by_page={{nb_by_page}}&sort={{reverse_sort}}">
+            {% endif %}
+            {% if sort == "laba" %}
+                <img src="{{ asset('bundles/wikitag/images/sort-asc.png') }}"/>
+            {% else %}
+                <img src="{{ asset('bundles/wikitag/images/sort-desc.png') }}"/>
+            {% endif %}
+            </a>
+        {% endif %}
+        </th>
+        <th>original_label</th>
+        <th class="text_centered">Lien W</th>
+        <th class="text_centered">Lien D</th>
+        <th>Catégorie</th>
+        <th class="large_25 text_centered">Supprimer<br/>le lien W</th>
+        <th>Alias</th>
+        <th class="text_centered">Nb de<br/>fiches</th>
+        <th>
+        {% if sort != "popa" and sort != "popd" %}
+            {% if searched and searched != "" %}
+                <a href="?searched={{searched}}&num_page=1&nb_by_page={{nb_by_page}}&sort=popd">Popularité</a>
+            {% else %}
+                <a href="?num_page=1&nb_by_page={{nb_by_page}}&sort=popd">Popularité</a>
+            {% endif %}
+        {% else %}
+            Popularité &nbsp;
+            {% if searched and searched != "" %}
+                <a href="?searched={{searched}}&num_page=1&nb_by_page={{nb_by_page}}&sort={{reverse_sort}}">
+            {% else %}
+                <a href="?num_page=1&nb_by_page={{nb_by_page}}&sort={{reverse_sort}}">
+            {% endif %}
+            {% if sort == "popa" %}
+                <img src="{{ asset('bundles/wikitag/images/sort-asc.png') }}"/>
+            {% else %}
+                <img src="{{ asset('bundles/wikitag/images/sort-desc.png') }}"/>
+            {% endif %}
+            </a>
+        {% endif %}
+        </th></tr>
+    {% for tag in tags %}
+    <tr class="imageline {{ cycle(['wikitag_oddline', 'wikitag_evenline'], loop.index) }}">
+        <td class="wikitag_reset_wp_info">{{tag.id}}</td>
+        <td class="wikitag_{{tag.urlstatustext}} wikipediatag" id="{{tag.id}}" >{{tag.label}}</td>
+        <td>{{tag.originallabel}}</td>
+        <td class="wikitag_text_centered">
+            {% if tag.wikipediaurl and tag.wikipediaurl != ""  %}
+            <a href="{{tag.wikipediaurl}}" target="_blank"><img src="{{ asset('bundles/wikitag/images/arrow_right.png') }}" ></a>
+            {% else %}
+            <a href="http://fr.wikipedia.org/w/index.php?search={{tag.label}}" target="_blank"><img src="{{ asset('bundles/wikitag/images/wikipedia_search.png') }}" ></a>
+            {% endif %}
+        </td>
+        <td class="wikitag_text_centered">
+            {% if tag.dbpediauri and tag.dbpediauri != ""  %}
+            <a href="{{tag.dbpediauri}}" target="_blank"><img src="{{ asset('bundles/wikitag/images/arrow_green_right.png') }}" ></a>
+            {% else %}
+            &nbsp;
+            {% endif %}
+        </td>
+        <td class="wikitag_category" id="{{tag.id}}">{% if tag.category %}{{ tag.category.label }}{% endif %}</td>
+        <td class="wikitag_text_centered"><img src="{{ asset('bundles/wikitag/images/red_cross.png') }}" class="wikitag_remove_wp_link" id="{{tag.id}}" alt="{{tag.label}}" /></td>
+        <td class="wikitag_alias" id="{{tag.id}}" >{% if tag.alias %}{{tag.alias}}{% endif %}</td>
+        <td class="wikitag_text_centered">
+        ?
+{#
+        {% if tag.num_ds > 0 %}
+            <a href="{% url hdabo.views.display_datasheet %}?tag={{tag.id}}" >{{tag.num_ds}}</a>
+        {% else %}
+            {{tag.num_ds}}
+        {% endif %}
+#}
+        </td>
+        <td class="wikitag_text_centered">{{tag.popularity}}</td></tr>
+    {% endfor %}
+    </table>
+{% endblock %}
\ No newline at end of file
--- a/Resources/views/WikiTag/tagTable.html.twig	Thu Oct 20 18:35:33 2011 +0200
+++ b/Resources/views/WikiTag/tagTable.html.twig	Fri Oct 21 17:10:54 2011 +0200
@@ -45,13 +45,3 @@
         <td class="wikitag_text_centered"><img src="{{ asset('bundles/wikitag/images/tag_remove.png') }}" class="wikitag_remove_tag_from_list" id="{{t.tag.id}}" alt="{{t.tag.label}}" /></td></tr>
     {% endfor %}
   </table>
-
-    {#<p>All the tags :</p>
-    <table id="wikitag_table">
-    <tr class="nodrop nodrag">
-    <th>ID</th><th>LABEL</th><th>CAT NAME</th></tr>
-    {% for tag in tags %}
-    <tr class="imageline {{ cycle(['wikitag_oddline', 'wikitag_evenline'], loop.index) }}" id="{{loop.index}}">
-    <td>{{ tag.id }}</td><td>{{ tag.label }}</td><td>{% if tag.category %}{{ tag.category.label }}{% endif %}</td></tr>
-    {% endfor %}
-    </table>#}
--- a/Utils/WikiTagUtils.php	Thu Oct 20 18:35:33 2011 +0200
+++ b/Utils/WikiTagUtils.php	Fri Oct 21 17:10:54 2011 +0200
@@ -98,6 +98,8 @@
         }
         $tag_label = trim($tag_label);//tag.strip()
         $tag_label = str_replace("_", " ", $tag_label);//tag.replace("_", " ")
+        $tag_label = str_replace("Œ", "oe", $tag_label);
+        $tag_label = str_replace("œ", "oe", $tag_label);
         $tag_label = preg_replace('/\s+/', ' ', $tag_label);//" ".join(tag.split())
         $tag_label = ucfirst($tag_label);//tag[0].upper() + tag[1:]
         return $tag_label;