Services/DocumentService.php
changeset 65 ba6b8e38d90e
parent 63 774ba82dca59
child 67 989d9e117586
--- a/Services/DocumentService.php	Wed Dec 14 23:53:37 2011 +0100
+++ b/Services/DocumentService.php	Mon Dec 19 17:50:04 2011 +0100
@@ -85,15 +85,25 @@
      * Add a new tag to a "host" document.
      * If the label already exists, an exception is raised.
      *
-     * @param mixed $id_doc
+     * @param mixed $doc
      * @param string|array $tag_label : the label of the new tag
      */
-    public function addTags($id_doc, $tag_labels)
+    public function addTags($doc, $tag_labels)
     {
         // We get the DocumentTags
         $em = $this->getDoctrine()->getEntityManager();
+
+        $class = $this->getContainer()->getParameter("wiki_tag.document_class");
         
-        $need_flush = false;
+        if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) {
+            if(is_a($doc, $class)) {
+                $doc_id = $doc->getId();
+            }
+            else {
+                $doc_id = $doc;
+            }
+            $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($doc_id);
+        }
         
         
         if(!is_array($tag_labels)) {
@@ -105,7 +115,7 @@
             $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label);
             
             $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label");
-            $query->setParameters(array("id_doc"=>$id_doc, "label"=>$normalized_tag_label));
+            $query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label));
             
             $nb_tags = $query->getSingleScalarResult();
             
@@ -125,31 +135,27 @@
             $revision_id = $ar[1];
             $created = $ar[2];
             
-            $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
-            $nb_tags = count($tags);
-            
-            if($created==true || $nb_tags==0){
-                $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
+            if(!$created) {
+                $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt WHERE dt.document = :id_doc AND dt.tag = :tag");
+                $query->setParameters(array("id_doc"=>$doc, "tag"=>$tag));
+                $nb_tags = $query->getSingleScalarResult();
+            }
+                        
+            if($created || $nb_tags==0){
+                $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc);
                 // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
                 $a1 = reset($new_order_ar);
                 $new_order = intval(reset($a1)) + 1;
                 $new_DT = new DocumentTag();
-                $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
+                $new_DT->setDocument($doc);
                 $new_DT->setTag($tag);
                 $new_DT->setOriginalOrder($new_order);
                 $new_DT->setTagOrder($new_order);
                 $new_DT->setWikipediaRevisionId($revision_id);
                 $em->persist($new_DT);
-                $need_flush = true;
-                
             }
         }
-        
-        if($need_flush) {
-            $em->flush();
-        }
-        
-        
+                
     }
     
     public function getTagLabels($id_doc)