45 return $this->doctrine; |
45 return $this->doctrine; |
46 } |
46 } |
47 |
47 |
48 |
48 |
49 /** |
49 /** |
50 * 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. |
51 * The ids are the ids of the "host" document. |
|
52 * Beware, both Documents must exists in the database. therefore this method can be called only after a EntityManager::push() |
|
53 * If one or the other doc is not found, an execption is raised. |
52 * |
54 * |
53 * @param mixed $id_doc_src the source document id |
55 * @param mixed $id_doc_src the source document id |
54 * @param mixed $id_doc_tgt the target document id |
56 * @param mixed $id_doc_tgt the target document id |
55 */ |
57 */ |
56 public function copyTags($id_doc_src, $id_doc_tgt) |
58 public function copyTags($id_doc_src, $id_doc_tgt) |
79 $em->flush(); |
81 $em->flush(); |
80 |
82 |
81 } |
83 } |
82 |
84 |
83 /** |
85 /** |
|
86 * Add a new tag (or tags) to a "host" document. |
|
87 * If the label already exists, an exception is raised. |
|
88 * Also, the document must exists in the database, i.e. Entitymanager::flush() must have been called on this objects before. |
84 * |
89 * |
85 * Add a new tag to a "host" document. |
90 * @param mixed $doc the document to add the tags to |
86 * If the label already exists, an exception is raised. |
|
87 * |
|
88 * @param mixed $doc |
|
89 * @param string|array $tag_label : the label of the new tag |
91 * @param string|array $tag_label : the label of the new tag |
90 */ |
92 */ |
91 public function addTags($doc, $tag_labels) |
93 public function addTags($doc, $tag_labels) |
92 { |
94 { |
93 // We get the DocumentTags |
95 // We get the DocumentTags |
94 $em = $this->getDoctrine()->getEntityManager(); |
96 $em = $this->getDoctrine()->getEntityManager(); |
95 |
97 |
96 $class = $this->getContainer()->getParameter("wiki_tag.document_class"); |
98 $class = $this->getContainer()->getParameter("wiki_tag.document_class"); |
97 |
99 |
98 if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) { |
100 if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) { |
|
101 $doc_rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); |
99 if(is_a($doc, $class)) { |
102 if(is_a($doc, $class)) { |
100 $doc_id = $doc->getId(); |
103 // todo: use reflection to get the id |
|
104 $doc_id = $doc_rep->reflectionGetField($doc, Container()->getParameter("wiki_tag.document_id_column")); |
101 } |
105 } |
102 else { |
106 else { |
103 $doc_id = $doc; |
107 $doc_id = $doc; |
104 } |
108 } |
105 $doc = $this->getDoctrine()->getRepository('WikiTagBundle:Document')->findOneByExternalId($doc_id); |
109 $doc = $doc_rep->findOneByExternalId($doc_id); |
106 } |
110 } |
107 |
111 |
108 |
112 |
109 if(!is_array($tag_labels)) { |
113 if(!is_array($tag_labels)) { |
110 $tag_labels = array($tag_labels); |
114 $tag_labels = array($tag_labels); |
111 } |
115 } |
112 |
116 |
113 foreach ($tag_labels as $tag_label) { |
117 foreach ($tag_labels as $tag_label) { |
114 |
118 |
115 $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label); |
119 $normalized_tag_label = WikiTagUtils::normalizeTag($tag_label); |
|
120 $created = false; |
116 |
121 |
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"); |
122 $query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label"); |
118 $query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label)); |
123 $query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label)); |
119 |
124 |
120 $nb_tags = $query->getSingleScalarResult(); |
125 $nb_tags = $query->getSingleScalarResult(); |
121 |
126 |
|
127 if($nb_tags == 0) { |
|
128 # look in unit of work |
|
129 $uow = $em->getUnitOfWork(); |
|
130 foreach($uow->getScheduledEntityInsertions() as $entity) { |
|
131 if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface")) { |
|
132 $tag = $entity->getTag(); |
|
133 if(!is_null($tag)) { |
|
134 if($tag->getNormalizedLabel() === $normalized_tag_label) |
|
135 { |
|
136 $nb_tags++; |
|
137 break; |
|
138 } |
|
139 } |
|
140 } |
|
141 } |
|
142 } |
|
143 |
122 // If the label was found, we sent a bad request |
144 // If the label was found, we sent a bad request |
123 if($nb_tags > 0){ |
145 if($nb_tags > 0) { |
124 throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag"); |
146 throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag"); |
125 } |
147 } |
126 // returns array($tag, $revision_id, $created) |
148 // returns array($tag, $revision_id, $created) |
127 try { |
149 try { |
128 $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label); |
150 $ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error')); |
129 } |
151 } |
130 catch (\Exception $e){ |
152 catch (\Exception $e){ |
131 throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed"); |
153 throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed"); |
132 } |
154 } |
133 |
155 |
140 $query->setParameters(array("id_doc"=>$doc, "tag"=>$tag)); |
162 $query->setParameters(array("id_doc"=>$doc, "tag"=>$tag)); |
141 $nb_tags = $query->getSingleScalarResult(); |
163 $nb_tags = $query->getSingleScalarResult(); |
142 } |
164 } |
143 |
165 |
144 if($created || $nb_tags==0){ |
166 if($created || $nb_tags==0){ |
145 $new_order_ar = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc); |
167 |
146 // The result is a double array. And reset(reset($newOrderAr)) is not allowed. And a string is returned. |
168 $max_order = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc); |
147 $a1 = reset($new_order_ar); |
169 $new_order = $max_order + 1; |
148 $new_order = intval(reset($a1)) + 1; |
|
149 $new_DT = new DocumentTag(); |
170 $new_DT = new DocumentTag(); |
150 $new_DT->setDocument($doc); |
171 $new_DT->setDocument($doc); |
151 $new_DT->setTag($tag); |
172 $new_DT->setTag($tag); |
152 $new_DT->setOriginalOrder($new_order); |
173 $new_DT->setOriginalOrder($new_order); |
153 $new_DT->setTagOrder($new_order); |
174 $new_DT->setTagOrder($new_order); |