merged done
authorymh <ymh.work@gmail.com>
Thu, 27 Oct 2011 21:37:34 +0200
changeset 20 985f1992895d
parent 19 7051e55a3131 (current diff)
parent 16 876df98c9208 (diff)
child 21 780ef37e63b9
merged done
Model/Document.php
Model/DocumentInterface.php
Resources/config/doctrine/Document.orm.yml
--- 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();
--- a/Model/Document.php	Thu Oct 27 21:26:30 2011 +0200
+++ b/Model/Document.php	Thu Oct 27 21:37:34 2011 +0200
@@ -10,6 +10,8 @@
 
 namespace IRI\Bundle\WikiTagBundle\Model;
 
+use Doctrine\Common\Collections\ArrayCollection;
+
 abstract class Document implements DocumentInterface {
     
     /**
@@ -28,6 +30,11 @@
      */
     protected $externalId;
     
+    /**
+     * @var ArrayCollection $tags
+     */
+    protected $tags;
+    
     
     /**
      * Get id
@@ -78,4 +85,13 @@
         return $this->externalId;
     }
     
+    /**
+     * TODO: (non-PHPdoc)
+     * @see IRI\Bundle\WikiTagBundle\Model.BaseDocumentInterface::getTags()
+     */
+    function getTags()
+    {
+        return $this->tags;
+    }
+    
 }
--- a/Model/DocumentInterface.php	Thu Oct 27 21:26:30 2011 +0200
+++ b/Model/DocumentInterface.php	Thu Oct 27 21:37:34 2011 +0200
@@ -11,6 +11,8 @@
 
 namespace IRI\Bundle\WikiTagBundle\Model;
 
+use Doctrine\Common\Collections\ArrayCollection;
+
 interface DocumentInterface {
     
     /**
--- a/Model/Tag.php	Thu Oct 27 21:26:30 2011 +0200
+++ b/Model/Tag.php	Thu Oct 27 21:37:34 2011 +0200
@@ -9,7 +9,9 @@
  */
  namespace IRI\Bundle\WikiTagBundle\Model;
  
- use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
+ use Doctrine\Common\Collections\ArrayCollection;
+
+use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
 
 abstract class Tag implements TagInterface {
     
@@ -70,6 +72,11 @@
      */
     protected $category;
 
+    /**
+     * @var ArrayCollection $documents
+     */
+    protected $documents;
+
 
     /**
      * Get id
@@ -301,6 +308,16 @@
     {
         return $this->category;
     }
+
+    /**
+     * Get Documents
+     *
+     * @return ArrayCollection
+     */
+    public function getDocuments()
+    {
+        return $this->documents;
+    }
     
     /**
      * Set category to Null
--- a/Model/TagInterface.php	Thu Oct 27 21:26:30 2011 +0200
+++ b/Model/TagInterface.php	Thu Oct 27 21:37:34 2011 +0200
@@ -11,6 +11,8 @@
 namespace IRI\Bundle\WikiTagBundle\Model;
 
 
+use Doctrine\Common\Collections\ArrayCollection;
+
 interface TagInterface {
     
     /**
@@ -169,5 +171,18 @@
      */
     function getCategory();
     
+    /**
+     * Get Documents
+     *
+     * @return ArrayCollection
+     */
+    function getDocuments();
+    
+    /**
+     * Nullify category
+     *
+     */
+    function nullCategory();
+    
     
 }
\ No newline at end of file
--- a/Resources/config/doctrine/Document.orm.yml	Thu Oct 27 21:26:30 2011 +0200
+++ b/Resources/config/doctrine/Document.orm.yml	Thu Oct 27 21:37:34 2011 +0200
@@ -10,15 +10,11 @@
       id: true
       generator:
         strategy: AUTO
-#    title:
-#      type: string
-#      length: '1024'
-#    description:
-#      type: text
     manualOrder:
       type: boolean
       column: manual_order
-#    externalId:
-#      type: string
-#      length: '1024'
+  oneToMany:
+    tags:
+      targetEntity: DocumentTag
+      mappedBy: document
   lifecycleCallbacks: {  }
--- a/Resources/config/doctrine/DocumentTag.orm.yml	Thu Oct 27 21:26:30 2011 +0200
+++ b/Resources/config/doctrine/DocumentTag.orm.yml	Thu Oct 27 21:37:34 2011 +0200
@@ -23,6 +23,7 @@
   manyToOne:
     tag:
       targetEntity: Tag
+      inversedBy: documents
       joinColumn:
         name: tag_id
         referencedColumnName: id
@@ -30,6 +31,7 @@
         onDelete: CASCADE
     document:
       targetEntity: Document
+      inversedBy: tags
       joinColumn: 
         name: document_id
         referencedColumnName: id
--- a/Resources/config/doctrine/Tag.orm.yml	Thu Oct 27 21:26:30 2011 +0200
+++ b/Resources/config/doctrine/Tag.orm.yml	Thu Oct 27 21:37:34 2011 +0200
@@ -46,4 +46,8 @@
   manyToOne:
     category:
       targetEntity: Category
+  oneToMany:
+    documents:
+      targetEntity: DocumentTag
+      mappedBy: tag
   lifecycleCallbacks: {  }
--- a/Resources/views/WikiTag/TagListTable.html.twig	Thu Oct 27 21:26:30 2011 +0200
+++ b/Resources/views/WikiTag/TagListTable.html.twig	Thu Oct 27 21:37:34 2011 +0200
@@ -52,7 +52,8 @@
             </a>
         {% endif %}
         </th></tr>
-    {% for tag in tags %}
+    {% for ar in tags %}
+    {% set tag, nb_docs = ar.0, ar.nb_docs %}
     <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>
@@ -75,14 +76,11 @@
         <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>
+        {% if nb_docs > 0 %}
+            <a href="{{ url(route_for_documents_by_tag) }}?tag={{tag.id}}" >{{nb_docs}}</a>
         {% else %}
-            {{tag.num_ds}}
+            {{nb_docs}}
         {% endif %}
-#}
         </td>
         <td class="wikitag_text_centered">{{tag.popularity}}</td></tr>
     {% endfor %}