| author | ymh <ymh.work@gmail.com> |
| Fri, 26 Sep 2014 10:04:40 +0200 | |
| changeset 136 | f209fcb0df6c |
| parent 80 | cf6a88559482 |
| permissions | -rwxr-xr-x |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
<?php |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
/* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
* This file is part of the WikiTagBundle package. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
* For the full copyright and license information, please view the LICENSE |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
* file that was distributed with this source code. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
namespace IRI\Bundle\WikiTagBundle\Services; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
|
| 63 | 12 |
use IRI\Bundle\WikiTagBundle\Entity\DocumentTag; |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
use Symfony\Component\DependencyInjection\ContainerAware; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
use Symfony\Component\DependencyInjection\ContainerInterface; |
| 63 | 15 |
use IRI\Bundle\WikiTagBundle\Utils\WikiTagUtils; |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
class DocumentService extends ContainerAware |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
/** |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
* Get the container associated with this service. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
* @return ContainerInterface |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
public function getContainer() |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
return $this->container; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
/** |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
* Public constructor with container as parameter for contruct injection. |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
* @param ContainerInterface $container |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
public function __construct(ContainerInterface $container) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
$this->setContainer($container); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
private $doctrine; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
public function getDoctrine() |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
if(is_null($this->doctrine)) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
$this->doctrine = $this->getContainer()->get('doctrine'); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
return $this->doctrine; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
/** |
| 67 | 50 |
* Copy the list of tags of one document to another. |
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. |
|
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
* |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
* @param mixed $id_doc_src the source document id |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
* @param mixed $id_doc_tgt the target document id |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
public function copyTags($id_doc_src, $id_doc_tgt) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
$doctrine = $this->getDoctrine(); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
$em = $doctrine->getEntityManager(); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
$doc_rep = $em->getRepository("WikiTagBundle:Document"); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
$src_doc = $doc_rep->findOneByExternalId($id_doc_src); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
if(is_null($src_doc)) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
{ |
|
80
cf6a88559482
Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents:
78
diff
changeset
|
69 |
throw new \Exception("cloneTags: no source doc"); |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
$tgt_doc = $doc_rep->findOneByExternalId($id_doc_tgt); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
if(is_null($tgt_doc)) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
{ |
|
80
cf6a88559482
Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents:
78
diff
changeset
|
75 |
throw new \Exception("cloneTags: no target doc"); |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
$doc_rep->copyTags($src_doc, $tgt_doc); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
$em->flush(); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
|
| 63 | 85 |
/** |
| 67 | 86 |
* Add a new tag (or tags) to a "host" document. |
| 63 | 87 |
* If the label already exists, an exception is raised. |
| 67 | 88 |
* Also, the document must exists in the database, i.e. Entitymanager::flush() must have been called on this objects before. |
| 63 | 89 |
* |
| 67 | 90 |
* @param mixed $doc the document to add the tags to |
| 63 | 91 |
* @param string|array $tag_label : the label of the new tag |
92 |
*/ |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
93 |
public function addTags($doc, $tag_labels) |
| 63 | 94 |
{ |
|
80
cf6a88559482
Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents:
78
diff
changeset
|
95 |
if(is_null($tag_labels) || (!is_string($tag_labels) && !is_array($tag_labels)) ) { |
|
cf6a88559482
Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents:
78
diff
changeset
|
96 |
return; |
|
cf6a88559482
Correct p[roblem with getTags returning Null
ymh <ymh.work@gmail.com>
parents:
78
diff
changeset
|
97 |
} |
| 63 | 98 |
// We get the DocumentTags |
99 |
$em = $this->getDoctrine()->getEntityManager(); |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
100 |
|
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
101 |
$class = $this->getContainer()->getParameter("wiki_tag.document_class"); |
| 63 | 102 |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
103 |
if(! is_a($doc,"\IRI\Bundle\WikiTagBundle\Model\DocumentInterface")) { |
| 67 | 104 |
$doc_rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); |
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
105 |
if(is_a($doc, $class)) { |
| 78 | 106 |
// Get the document column id, set in the config file. |
| 67 | 107 |
$doc_id = $doc_rep->reflectionGetField($doc, Container()->getParameter("wiki_tag.document_id_column")); |
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
108 |
} |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
109 |
else { |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
110 |
$doc_id = $doc; |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
111 |
} |
| 67 | 112 |
$doc = $doc_rep->findOneByExternalId($doc_id); |
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
113 |
} |
| 63 | 114 |
|
115 |
||
116 |
if(!is_array($tag_labels)) { |
|
117 |
$tag_labels = array($tag_labels); |
|
118 |
} |
|
119 |
||
120 |
foreach ($tag_labels as $tag_label) { |
|
121 |
||
122 |
$normalized_tag_label = WikiTagUtils::normalizeTag($tag_label); |
|
| 67 | 123 |
$created = false; |
| 63 | 124 |
|
125 |
$query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :id_doc AND t.normalizedLabel = :label"); |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
126 |
$query->setParameters(array("id_doc"=>$doc, "label"=>$normalized_tag_label)); |
| 63 | 127 |
|
128 |
$nb_tags = $query->getSingleScalarResult(); |
|
129 |
||
| 67 | 130 |
if($nb_tags == 0) { |
131 |
# look in unit of work |
|
132 |
$uow = $em->getUnitOfWork(); |
|
133 |
foreach($uow->getScheduledEntityInsertions() as $entity) { |
|
134 |
if(is_a($entity, "\IRI\Bundle\WikiTagBundle\Model\DocumentTagInterface")) { |
|
135 |
$tag = $entity->getTag(); |
|
136 |
if(!is_null($tag)) { |
|
137 |
if($tag->getNormalizedLabel() === $normalized_tag_label) |
|
138 |
{ |
|
139 |
$nb_tags++; |
|
140 |
break; |
|
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
| 63 | 147 |
// If the label was found, we sent a bad request |
| 67 | 148 |
if($nb_tags > 0) { |
| 63 | 149 |
throw new WikiTagServiceException(sprintf("Le tag %s existe déjà pour cette fiche.", $tag_label), 400, null, "duplicate_tag"); |
150 |
} |
|
151 |
// returns array($tag, $revision_id, $created) |
|
152 |
try { |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
153 |
$ar = $this->getDoctrine()->getRepository('WikiTagBundle:Tag')->getOrCreateTag($tag_label, $this->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error'), $this->getContainer()->get('logger')); |
| 63 | 154 |
} |
155 |
catch (\Exception $e){ |
|
156 |
throw new WikiTagServiceException($e->getMessage(), 500 , $e, "wikipedia_request_failed"); |
|
157 |
} |
|
158 |
||
159 |
$tag = $ar[0]; |
|
160 |
$revision_id = $ar[1]; |
|
161 |
$created = $ar[2]; |
|
162 |
||
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
163 |
if(!$created) { |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
164 |
$query = $em->createQuery("SELECT COUNT(dt.id) FROM WikiTagBundle:DocumentTag dt WHERE dt.document = :id_doc AND dt.tag = :tag"); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
165 |
$query->setParameters(array("id_doc"=>$doc, "tag"=>$tag)); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
166 |
$nb_tags = $query->getSingleScalarResult(); |
|
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
167 |
} |
| 78 | 168 |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
169 |
if($created || $nb_tags==0){ |
| 78 | 170 |
$max_order = $this->getDoctrine()->getRepository('WikiTagBundle:DocumentTag')->getMaxOrder($doc_id); |
| 67 | 171 |
$new_order = $max_order + 1; |
| 63 | 172 |
$new_DT = new DocumentTag(); |
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
173 |
$new_DT->setDocument($doc); |
| 63 | 174 |
$new_DT->setTag($tag); |
175 |
$new_DT->setOriginalOrder($new_order); |
|
176 |
$new_DT->setTagOrder($new_order); |
|
177 |
$new_DT->setWikipediaRevisionId($revision_id); |
|
178 |
$em->persist($new_DT); |
|
179 |
} |
|
180 |
} |
|
|
65
ba6b8e38d90e
Correct event mamangement in hdabo_sf
ymh <ymh.work@gmail.com>
parents:
63
diff
changeset
|
181 |
|
| 63 | 182 |
} |
183 |
||
| 67 | 184 |
/** |
185 |
* Returns the list of tags labels for @author ymh |
|
186 |
* |
|
187 |
* @param mixed $id_doc the document id. |
|
188 |
* @throws WikiTagServiceException if the document is not found |
|
189 |
*/ |
|
| 63 | 190 |
public function getTagLabels($id_doc) |
191 |
{ |
|
192 |
$rep = $this->getDoctrine()->getRepository('WikiTagBundle:Document'); |
|
193 |
$doc = $rep->findOneByExternalId($id_doc); |
|
194 |
||
195 |
if(is_null($doc)) { |
|
196 |
throw new WikiTagServiceException("Unknown document id"); |
|
197 |
} |
|
198 |
||
199 |
return $rep->getTagsStr($doc); |
|
200 |
} |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
201 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
202 |
/** |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
203 |
* Service to reorder the tags using their notes in the index search. |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
204 |
* This service is configured in the configuration file by affecting the weight in each field definition. |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
205 |
* |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
206 |
* @param IRI\Bundle\WikiTagBundle\Model\DocumentInterface $document |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
207 |
*/ |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
208 |
public function reorderTags($document) |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
209 |
{ |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
210 |
$doctrine = $this->getContainer()->get('doctrine'); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
211 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
212 |
$tags_score = array(); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
213 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
214 |
foreach($document->getTags() as $tag) |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
215 |
{ |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
216 |
$label = $tag->getTag()->getLabel(); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
217 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
218 |
$score_res = $this->search($label, array("id"=>$document->getId())); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
219 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
220 |
if(count($score_res)>0) |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
221 |
{ |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
222 |
$score = floatval($score_res[0]['score']); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
223 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
224 |
else |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
225 |
{ |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
226 |
$score = 0.0; |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
227 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
228 |
$tags_score[] = array($score,$tag); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
229 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
230 |
// sort tags based on score |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
231 |
$i=1; |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
232 |
usort($tags_score, function($a, $b) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
233 |
return $a[0]<$b[0]?1:-1; |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
234 |
}); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
235 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
236 |
foreach($tags_score as $item) |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
237 |
{ |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
238 |
$tag = $item[1]; |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
239 |
$tag->setTagOrder($i++); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
240 |
$tag->setIndexNote($item[0]); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
241 |
$doctrine->getEntityManager()->persist($tag); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
242 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
243 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
244 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
245 |
|
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
246 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
247 |
} |