Services/DocumentService.php
changeset 65 ba6b8e38d90e
parent 63 774ba82dca59
child 67 989d9e117586
equal deleted inserted replaced
64:caeb4c8b5487 65:ba6b8e38d90e
    83     /**
    83     /**
    84      *
    84      *
    85      * Add a new tag to a "host" document.
    85      * Add a new tag to a "host" document.
    86      * If the label already exists, an exception is raised.
    86      * If the label already exists, an exception is raised.
    87      *
    87      *
    88      * @param mixed $id_doc
    88      * @param mixed $doc
    89      * @param string|array $tag_label : the label of the new tag
    89      * @param string|array $tag_label : the label of the new tag
    90      */
    90      */
    91     public function addTags($id_doc, $tag_labels)
    91     public function addTags($doc, $tag_labels)
    92     {
    92     {
    93         // We get the DocumentTags
    93         // We get the DocumentTags
    94         $em = $this->getDoctrine()->getEntityManager();
    94         $em = $this->getDoctrine()->getEntityManager();
       
    95 
       
    96         $class = $this->getContainer()->getParameter("wiki_tag.document_class");
    95         
    97         
    96         $need_flush = false;
    98         if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) {
       
    99             if(is_a($doc, $class)) {
       
   100                 $doc_id = $doc->getId();
       
   101             }
       
   102             else {
       
   103                 $doc_id = $doc;
       
   104             }
       
   105             $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($doc_id);
       
   106         }
    97         
   107         
    98         
   108         
    99         if(!is_array($tag_labels)) {
   109         if(!is_array($tag_labels)) {
   100             $tag_labels = array($tag_labels);
   110             $tag_labels = array($tag_labels);
   101         }
   111         }
   103         foreach ($tag_labels as $tag_label) {
   113         foreach ($tag_labels as $tag_label) {
   104         
   114         
   105             $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label);
   115             $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label);
   106             
   116             
   107             $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label");
   117             $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label");
   108             $query->setParameters(array("id_doc"=>$id_doc, "label"=>$normalized_tag_label));
   118             $query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label));
   109             
   119             
   110             $nb_tags = $query->getSingleScalarResult();
   120             $nb_tags = $query->getSingleScalarResult();
   111             
   121             
   112             // If the label was found, we sent a bad request
   122             // If the label was found, we sent a bad request
   113             if($nb_tags > 0){
   123             if($nb_tags > 0){
   123             
   133             
   124             $tag = $ar[0];
   134             $tag = $ar[0];
   125             $revision_id = $ar[1];
   135             $revision_id = $ar[1];
   126             $created = $ar[2];
   136             $created = $ar[2];
   127             
   137             
   128             $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
   138             if(!$created) {
   129             $nb_tags = count($tags);
   139                 $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt WHERE dt.document = :id_doc AND dt.tag = :tag");
   130             
   140                 $query->setParameters(array("id_doc"=>$doc, "tag"=>$tag));
   131             if($created==true || $nb_tags==0){
   141                 $nb_tags = $query->getSingleScalarResult();
   132                 $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
   142             }
       
   143                         
       
   144             if($created || $nb_tags==0){
       
   145                 $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc);
   133                 // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
   146                 // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
   134                 $a1 = reset($new_order_ar);
   147                 $a1 = reset($new_order_ar);
   135                 $new_order = intval(reset($a1)) + 1;
   148                 $new_order = intval(reset($a1)) + 1;
   136                 $new_DT = new DocumentTag();
   149                 $new_DT = new DocumentTag();
   137                 $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
   150                 $new_DT->setDocument($doc);
   138                 $new_DT->setTag($tag);
   151                 $new_DT->setTag($tag);
   139                 $new_DT->setOriginalOrder($new_order);
   152                 $new_DT->setOriginalOrder($new_order);
   140                 $new_DT->setTagOrder($new_order);
   153                 $new_DT->setTagOrder($new_order);
   141                 $new_DT->setWikipediaRevisionId($revision_id);
   154                 $new_DT->setWikipediaRevisionId($revision_id);
   142                 $em->persist($new_DT);
   155                 $em->persist($new_DT);
   143                 $need_flush = true;
       
   144                 
       
   145             }
   156             }
   146         }
   157         }
   147         
   158                 
   148         if($need_flush) {
       
   149             $em->flush();
       
   150         }
       
   151         
       
   152         
       
   153     }
   159     }
   154     
   160     
   155     public function getTagLabels($id_doc)
   161     public function getTagLabels($id_doc)
   156     {
   162     {
   157         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
   163         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document');