Services/DocumentService.php
changeset 63 774ba82dca59
parent 57 186c4121c7b3
child 65 ba6b8e38d90e
equal deleted inserted replaced
62:10be6b9e55e7 63:774ba82dca59
     7  * For the full copyright and license information, please view the LICENSE
     7  * For the full copyright and license information, please view the LICENSE
     8  * file that was distributed with this source code.
     8  * file that was distributed with this source code.
     9  */
     9  */
    10 namespace IRI\Bundle\WikiTagBundle\Services;
    10 namespace IRI\Bundle\WikiTagBundle\Services;
    11  
    11  
       
    12 use IRI\Bundle\WikiTagBundle\Entity\DocumentTag;
    12 use Symfony\Component\DependencyInjection\ContainerAware;
    13 use Symfony\Component\DependencyInjection\ContainerAware;
    13 use Symfony\Component\DependencyInjection\ContainerInterface;
    14 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    15 use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils;
    14 
    16 
    15 class DocumentService extends ContainerAware
    17 class DocumentService extends ContainerAware
    16 {
    18 {
    17     /**
    19     /**
    18      * Get the container associated with this service.
    20      * Get the container associated with this service.
    44     }
    46     }
    45     
    47     
    46 
    48 
    47     /**
    49     /**
    48      * Copy the list of tags of one document to another.
    50      * Copy the list of tags of one document to another.
       
    51      * The ids are the ids of the "host" document.
    49 	 *
    52 	 *
    50      * @param mixed $id_doc_src the source document id
    53      * @param mixed $id_doc_src the source document id
    51      * @param mixed $id_doc_tgt the target document id
    54      * @param mixed $id_doc_tgt the target document id
    52      */
    55      */
    53     public function copyTags($id_doc_src, $id_doc_tgt)
    56     public function copyTags($id_doc_src, $id_doc_tgt)
    75         
    78         
    76         $em->flush();
    79         $em->flush();
    77         
    80         
    78     }
    81     }
    79     
    82     
       
    83     /**
       
    84      *
       
    85      * Add a new tag to a "host" document.
       
    86      * If the label already exists, an exception is raised.
       
    87      *
       
    88      * @param mixed $id_doc
       
    89      * @param string|array $tag_label : the label of the new tag
       
    90      */
       
    91     public function addTags($id_doc, $tag_labels)
       
    92     {
       
    93         // We get the DocumentTags
       
    94         $em = $this->getDoctrine()->getEntityManager();
       
    95         
       
    96         $need_flush = false;
       
    97         
       
    98         
       
    99         if(!is_array($tag_labels)) {
       
   100             $tag_labels = array($tag_labels);
       
   101         }
       
   102         
       
   103         foreach ($tag_labels as $tag_label) {
       
   104         
       
   105             $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label);
       
   106             
       
   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");
       
   108             $query->setParameters(array("id_doc"=>$id_doc, "label"=>$normalized_tag_label));
       
   109             
       
   110             $nb_tags = $query->getSingleScalarResult();
       
   111             
       
   112             // If the label was found, we sent a bad request
       
   113             if($nb_tags > 0){
       
   114                 throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag");
       
   115             }
       
   116             // returns array($tag, $revision_id, $created)
       
   117             try {
       
   118                 $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label);
       
   119             }
       
   120             catch (\Exception $e){
       
   121                 throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed");
       
   122             }
       
   123             
       
   124             $tag = $ar[0];
       
   125             $revision_id = $ar[1];
       
   126             $created = $ar[2];
       
   127             
       
   128             $tags = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->findByDocumentExternalId($id_doc, array('tag'=>$tag->getId()));
       
   129             $nb_tags = count($tags);
       
   130             
       
   131             if($created==true || $nb_tags==0){
       
   132                 $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($id_doc);
       
   133                 // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned.
       
   134                 $a1 = reset($new_order_ar);
       
   135                 $new_order = intval(reset($a1)) + 1;
       
   136                 $new_DT = new DocumentTag();
       
   137                 $new_DT->setDocument($this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($id_doc));
       
   138                 $new_DT->setTag($tag);
       
   139                 $new_DT->setOriginalOrder($new_order);
       
   140                 $new_DT->setTagOrder($new_order);
       
   141                 $new_DT->setWikipediaRevisionId($revision_id);
       
   142                 $em->persist($new_DT);
       
   143                 $need_flush = true;
       
   144                 
       
   145             }
       
   146         }
       
   147         
       
   148         if($need_flush) {
       
   149             $em->flush();
       
   150         }
       
   151         
       
   152         
       
   153     }
       
   154     
       
   155     public function getTagLabels($id_doc)
       
   156     {
       
   157         $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document');
       
   158         $doc = $rep->findOneByExternalId($id_doc);
       
   159         
       
   160         if(is_null($doc)) {
       
   161             throw new WikiTagServiceException("Unknown document id");
       
   162         }
       
   163         
       
   164         return $rep->getTagsStr($doc);
       
   165 
       
   166     }
       
   167     
    80         
   168         
    81 }
   169 }