diff -r 868d0fcdbb99 -r 989d9e117586 Services/DocumentService.php --- a/Services/DocumentService.php Mon Dec 19 18:15:42 2011 +0100 +++ b/Services/DocumentService.php Mon Dec 26 22:53:50 2011 +0100 @@ -47,8 +47,10 @@ /** - * Copy the list of tags of one document to another. - * The ids are the ids of the "host" document. + * Copy the list of tags of one document to another. + * The ids are the ids of the "host" document. + * Beware, both Documents must exists in the database. therefore this method can be called only after a EntityManager::push() + * If one or the other doc is not found, an execption is raised. * * @param mixed $id_doc_src the source document id * @param mixed $id_doc_tgt the target document id @@ -81,11 +83,11 @@ } /** - * - * Add a new tag to a "host" document. + * Add a new tag (or tags) to a "host" document. * If the label already exists, an exception is raised. + * Also, the document must exists in the database, i.e. Entitymanager::flush() must have been called on this objects before. * - * @param mixed $doc + * @param mixed $doc the document to add the tags to * @param string|array $tag_label : the label of the new tag */ public function addTags($doc, $tag_labels) @@ -96,13 +98,15 @@ $class = $this->getContainer()->getParameter("wiki_tag.document_class"); if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) { + $doc_rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); if(is_a($doc, $class)) { - $doc_id = $doc->getId(); + // todo: use reflection to get the id + $doc_id = $doc_rep->reflectionGetField($doc, Container()->getParameter("wiki_tag.document_id_column")); } else { $doc_id = $doc; } - $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($doc_id); + $doc = $doc_rep->findOneByExternalId($doc_id); } @@ -113,19 +117,37 @@ foreach ($tag_labels as $tag_label) { $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label); + $created = false; $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"=>$doc, "label"=>$normalized_tag_label)); $nb_tags = $query->getSingleScalarResult(); + if($nb_tags == 0) { + # look in unit of work + $uow = $em->getUnitOfWork(); + foreach($uow->getScheduledEntityInsertions() as $entity) { + if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface")) { + $tag = $entity->getTag(); + if(!is_null($tag)) { + if($tag->getNormalizedLabel() === $normalized_tag_label) + { + $nb_tags++; + break; + } + } + } + } + } + // If the label was found, we sent a bad request - if($nb_tags > 0){ + if($nb_tags > 0) { throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag"); } // returns array($tag, $revision_id, $created) try { - $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label); + $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error')); } catch (\Exception $e){ throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed"); @@ -142,10 +164,9 @@ } 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; + + $max_order = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc); + $new_order = $max_order + 1; $new_DT = new DocumentTag(); $new_DT->setDocument($doc); $new_DT->setTag($tag); @@ -158,6 +179,12 @@ } + /** + * Returns the list of tags labels for @author ymh + * + * @param mixed $id_doc the document id. + * @throws WikiTagServiceException if the document is not found + */ public function getTagLabels($id_doc) { $rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); @@ -168,8 +195,6 @@ } return $rep->getTagsStr($doc); - } - } \ No newline at end of file