| author | ymh <ymh.work@gmail.com> |
| Mon, 23 Jan 2012 00:48:55 +0100 | |
| changeset 68 | e7384fb35f7a |
| parent 67 | 989d9e117586 |
| child 74 | 901463f9b11c |
| permissions | -rwxr-xr-x |
| 2 | 1 |
<?php |
2 |
||
3 |
namespace IRI\Bundle\WikiTagBundle\Entity; |
|
4 |
||
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
5 |
use IRI\Bundle\WikiTagBundle\Model\DocumentInterface; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
6 |
|
| 2 | 7 |
use Doctrine\ORM\EntityRepository; |
| 18 | 8 |
use IRI\Bundle\WikiTagBundle\Entity\Document; |
| 29 | 9 |
use Doctrine\ORM\Query\ResultSetMapping; |
| 18 | 10 |
use \ReflectionClass; |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
11 |
use Doctrine\ORM\AbstractQuery; |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
12 |
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
13 |
use IRI\Bundle\WikiTagBundle\Model\ModelException; |
| 2 | 14 |
|
15 |
/** |
|
16 |
* DocumentRepository |
|
17 |
* |
|
18 |
* This class was generated by the Doctrine ORM. Add your own custom |
|
19 |
* repository methods below. |
|
20 |
*/ |
|
21 |
class DocumentRepository extends EntityRepository |
|
22 |
{ |
|
| 18 | 23 |
/** |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
24 |
* The cache for the host document class |
| 18 | 25 |
* @var ReflectionClass |
26 |
*/ |
|
27 |
private $reflection_class; |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
28 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
29 |
/** |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
30 |
* The cache for the wikitag document class |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
31 |
* @var ReflectionClass |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
32 |
*/ |
| 18 | 33 |
private $reflection_doc_class; |
34 |
private $set_methods = array(); |
|
35 |
private $get_methods = array(); |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
36 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
37 |
/** |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
38 |
* Find one wikitagRepository by its externalId i.e. the host document id. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
39 |
* @param $external_id |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
40 |
*/ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
41 |
public function findOneByExternalId($external_id) |
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
42 |
{ |
| 18 | 43 |
return $this->findOneBy(array("externalId" => $external_id)); |
44 |
} |
|
45 |
||
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
46 |
|
| 67 | 47 |
public function reflectionSetField($object, $method_name, $value) |
| 18 | 48 |
{ |
49 |
if(isset($this->set_methods[$method_name])) |
|
50 |
{ |
|
51 |
$set_method = $this->set_methods[$method_name]; |
|
52 |
} |
|
53 |
||
54 |
if(!isset($set_method) || is_null($set_method)) |
|
55 |
{ |
|
56 |
if(is_null($this->reflection_doc_class)) |
|
57 |
{ |
|
58 |
$this->reflection_doc_class = new ReflectionClass(get_class($object)); |
|
59 |
} |
|
60 |
||
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
61 |
$set_method = null; |
| 18 | 62 |
if($this->reflection_doc_class->hasMethod($method_name)) |
63 |
{ |
|
64 |
$set_method = $this->reflection_doc_class->getMethod($method_name); |
|
65 |
} |
|
66 |
if(!is_null($set_method)) |
|
67 |
{ |
|
68 |
$this->set_methods[$method_name]=$set_method; |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
if(!isset($set_method) || is_null($set_method) || !$set_method->isPublic()) |
|
73 |
{ |
|
74 |
throw new \Exception("setter method unknown $method_name"); |
|
75 |
} |
|
76 |
||
77 |
//set value |
|
78 |
$set_method->invoke($object, $value); |
|
79 |
||
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
80 |
} |
| 3 | 81 |
|
| 67 | 82 |
public function reflectionGetField($document, $accessor) |
| 18 | 83 |
{ |
84 |
||
85 |
if(!isset($this->get_methods[$accessor]) || is_null($this->get_methods[$accessor])) |
|
86 |
{ |
|
87 |
if(is_null($this->reflection_class)) |
|
88 |
{ |
|
89 |
$this->reflection_class = new \ReflectionClass(get_class($document)); |
|
90 |
} |
|
91 |
||
92 |
//look at properties |
|
93 |
if($this->reflection_class->hasProperty($accessor)) |
|
94 |
{ |
|
95 |
$get_object = $this->reflection_class->getProperty($accessor); |
|
96 |
if(!$get_object->isPublic()) |
|
97 |
{ |
|
98 |
$get_object = NULL; |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
if((!isset($get_object) || is_null($get_object)) && $this->reflection_class->hasMethod($accessor)) |
|
103 |
{ |
|
104 |
$get_object = $this->reflection_class->getMethod($accessor); |
|
105 |
if(!$get_object->isPublic()) |
|
106 |
{ |
|
107 |
$get_object = NULL; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
if((!isset($get_object) || is_null($get_object)) && $this->reflection_class->hasMethod("get".ucfirst($accessor))) |
|
112 |
{ |
|
113 |
$get_object = $this->reflection_class->getMethod("get".ucfirst($accessor)); |
|
114 |
if(!$get_object->isPublic()) |
|
115 |
{ |
|
116 |
$get_object = NULL; |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
if(isset($get_object) && !is_null($get_object)) |
|
121 |
{ |
|
122 |
$this->get_methods[$accessor] = $get_object; |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
if(isset($this->get_methods[$accessor])) |
|
127 |
{ |
|
128 |
$get_object = $this->get_methods[$accessor]; |
|
129 |
if(!is_null($get_object)) |
|
130 |
{ |
|
131 |
if(is_a($get_object,"\ReflectionMethod")) |
|
132 |
{ |
|
133 |
return $get_object->invoke($document); |
|
134 |
} |
|
135 |
elseif(is_a($get_object,"\ReflectionProperty")) |
|
136 |
{ |
|
137 |
return $get_object->getValue($document); |
|
138 |
} |
|
139 |
else |
|
140 |
{ |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
141 |
throw new ModelException("Bad reflection object type"); |
| 18 | 142 |
} |
143 |
} |
|
144 |
} |
|
145 |
||
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
146 |
throw new ModelException("Unknown accessor $accessor"); |
| 18 | 147 |
} |
148 |
||
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
149 |
|
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
150 |
private function getColumnName($field_name) |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
151 |
{ |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
152 |
if(isset($this->getClassMetadata()->columnNames[$field_name])) |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
153 |
{ |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
154 |
return $this->getClassMetadata()->columnNames[$field_name]; |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
155 |
} |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
156 |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
157 |
$res = null; |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
158 |
if(isset($this->getClassMetadata()->associationMappings[$field_name])) |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
159 |
{ |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
160 |
$association_mapping = $this->getClassMetadata()->associationMappings[$field_name]; |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
161 |
if( |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
162 |
isset($association_mapping['type']) |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
163 |
&& ( $association_mapping['type'] === ClassMetadataInfo::ONE_TO_ONE || $association_mapping['type'] === ClassMetadataInfo::MANY_TO_ONE) |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
164 |
&& count($association_mapping['joinColumns']) > 0 |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
165 |
) |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
166 |
{ |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
167 |
$res = $association_mapping['joinColumns'][0]['name']; |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
168 |
} |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
169 |
} |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
170 |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
171 |
if(is_null($res)) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
172 |
throw new \Exception("WikiTag.DocumentRepository: Unknown field $field_name"); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
173 |
} |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
174 |
return $res; |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
175 |
} |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
176 |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
177 |
/** |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
178 |
* Write a wikitag document given the host document and the field list. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
179 |
* @param $document The source document |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
180 |
* @param $document_id_column the name of the source document id column |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
181 |
* @param $fields The list of field definition. This is an associative array [<field name>=><field definition>]. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
182 |
* See the @IRI\Bundle\WikiTagBundle\DependencyInjection\Configuration documentation |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
183 |
*/ |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
184 |
public function writeDocument($document, $document_id_column, $fields) |
| 3 | 185 |
{ |
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
186 |
// get document from id |
|
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
187 |
|
| 18 | 188 |
$docid = $this->reflectionGetField($document, $document_id_column); |
189 |
$baseDocument = $this->findOneByExternalId($docid); |
|
| 3 | 190 |
|
191 |
if(is_null($baseDocument)) |
|
192 |
{ |
|
193 |
$baseDocument = new Document(); |
|
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
194 |
$baseDocument->setExternalId($document); |
| 3 | 195 |
} |
| 18 | 196 |
|
197 |
foreach ($fields as $name => $field_def) { |
|
198 |
if(isset($field_def['accessor'])) |
|
199 |
{ |
|
200 |
$accessor = $field_def['accessor']; |
|
201 |
} |
|
202 |
else |
|
203 |
{ |
|
204 |
$accessor = NULL; |
|
205 |
} |
|
206 |
if(is_null($accessor)) |
|
207 |
{ |
|
208 |
$accessor = $name; |
|
209 |
} |
|
210 |
||
211 |
$value = strval($this->reflectionGetField($document,$accessor)); |
|
212 |
||
213 |
$method_name = "set".ucfirst($name); |
|
214 |
||
215 |
$this->reflectionSetField($baseDocument, $method_name, $value); |
|
216 |
||
217 |
} |
|
| 3 | 218 |
|
219 |
$this->getEntityManager()->persist($baseDocument); |
|
220 |
return $baseDocument; |
|
221 |
||
222 |
} |
|
223 |
||
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
224 |
/** |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
225 |
* Remove a Wikitag doument given the host docuument. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
226 |
* @param $document The host document |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
227 |
* @param string $document_id_column The host document id column name |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
228 |
*/ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
229 |
public function removeDocument($document, $document_id_column) |
| 3 | 230 |
{ |
| 18 | 231 |
$docid = $this->reflectionGetField($document, $document_id_column); |
232 |
$baseDocument = $this->findOneByExternalId($docid); |
|
|
5
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
233 |
if(!is_null($baseDocument)) |
|
45378793512a
Correct tag insert + external id on doc
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
234 |
{ |
| 3 | 235 |
$this->getEntityManager()->remove($baseDocument); |
236 |
} |
|
237 |
} |
|
238 |
||
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
239 |
|
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
240 |
/** |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
241 |
* return the list of a wikitag documents the tags label. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
242 |
* @param DocumentInterface $document the wikitag document |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
243 |
* @return array |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
244 |
*/ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
245 |
public function getTagsStr($document) |
| 27 | 246 |
{ |
247 |
$em = $this->getEntityManager(); |
|
| 63 | 248 |
$query = $em->createQuery("SELECT t.label FROM WikiTagBundle:DocumentTag dt JOIN dt.tag t WHERE dt.document = :docid ORDER BY dt.tagOrder"); |
| 27 | 249 |
$query = $query->setParameter("docid", $document); |
250 |
$result = $query->getScalarResult(); |
|
251 |
$tagstr = array(); |
|
252 |
foreach ($result as $res) { |
|
253 |
$tagstr[] = $res['label']; |
|
254 |
} |
|
255 |
return $tagstr; |
|
256 |
} |
|
257 |
||
| 63 | 258 |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
259 |
/** |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
260 |
* Update a wikitag document tags string. |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
261 |
* @param DocumentInterface $document the wikitag document |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
262 |
*/ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
263 |
function updateTagsStr(DocumentInterface $document) |
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
264 |
{ |
| 27 | 265 |
|
266 |
$tagstr = $this->getTagsStr($document); |
|
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
267 |
|
| 27 | 268 |
$document->setTagsStr(implode(",",$tagstr)); |
269 |
$this->getEntityManager()->persist($document); |
|
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
270 |
} |
| 29 | 271 |
|
| 34 | 272 |
|
| 29 | 273 |
/** |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
274 |
* Search wikitag documents using the index. |
| 29 | 275 |
* |
| 34 | 276 |
* @param array $values : key: the fields to search into, value : array('value'=>value, 'weight'=>weight) |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
277 |
* @param array $conditions : array : key : field name, value : simple value (operator is "=") or array(valuea, value2,...) (operatr is IN) or array("operator"=>"=","!=","<".">","<=".">=","like","ilike","in">, "value"=>value) |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
278 |
* @param array $fields : array : a list of field name to include in the result |
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
37
diff
changeset
|
279 |
* @return array [["id" => <the wikitag document id>, "externalId" => <the host document ids>, "score" => <the score for this document>]] |
| 29 | 280 |
*/ |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
281 |
function search(array $values, array $conditions=null, array $fields=null) |
| 29 | 282 |
{ |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
283 |
$em = $this->getEntityManager(); |
| 29 | 284 |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
285 |
if(is_null($fields)) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
286 |
$fieldnamelist = array(); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
287 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
288 |
else { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
289 |
$fieldnamelist = $fields; |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
290 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
291 |
|
| 29 | 292 |
$rsm = new ResultSetMapping(); |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
293 |
$rsm->addEntityResult("IRI\Bundle\WikiTagBundle\Entity\Document", "d"); |
| 29 | 294 |
$rsm->addFieldResult("d", "id", "id"); |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
295 |
foreach($fieldnamelist as $fieldname) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
296 |
$rsm->addFieldResult("d", $fieldname, $fieldname); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
297 |
} |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
298 |
$rsm->addScalarResult("score", "score"); |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
299 |
$rsm->addMetaResult("d", "external_id", "external_id"); |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
300 |
|
| 29 | 301 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
302 |
$score = array(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
303 |
$i = 0; |
| 29 | 304 |
foreach ($values as $fielddef) { |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
305 |
$i++; |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
306 |
$field_list = explode(",", $fielddef["columns"]); |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
307 |
$column_list = array(); |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
308 |
foreach($field_list as $field_name) |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
309 |
{ |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
310 |
$column_list[] = $this->getColumnName(trim($field_name)); |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
311 |
} |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
312 |
$columns = join(",", $column_list); |
|
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
313 |
|
| 29 | 314 |
$value = $fielddef["value"]; |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
315 |
$weight = isset($fielddef["weight"])?$fielddef["weight"]:1.0; |
| 29 | 316 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
317 |
$score[] = "(MATCH($columns) AGAINST (:value_$i))*:weight_$i"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
318 |
$parameters["value_$i"] = $value; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
319 |
$parameters["weight_$i"] = $weight; |
| 29 | 320 |
} |
321 |
||
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
322 |
$score_def = "(".implode("+", $score).")"; |
| 29 | 323 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
324 |
$conditions_str = ""; |
| 29 | 325 |
|
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
326 |
if(!is_null($conditions)) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
327 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
328 |
$conditions_array = array(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
329 |
$i = 0; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
330 |
foreach ($conditions as $field => $conddef) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
331 |
{ |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
332 |
if(!$field) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
333 |
continue; |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
334 |
} |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
335 |
$i++; |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
336 |
$col = $this->getColumnName($field); |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
337 |
if(is_array($conddef) && isset($conddef['operator'])) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
338 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
339 |
$operator = $conddef["operator"]; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
340 |
$values = $conddef["value"]; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
341 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
342 |
elseif(is_array($conddef)) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
343 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
344 |
$operator = "IN"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
345 |
$values = $conddef; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
346 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
347 |
else |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
348 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
349 |
$operator = "="; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
350 |
$values = $conddef; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
351 |
} |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
352 |
|
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
353 |
if(!in_array( strtolower($operator), array("=","!=","<".">","<=".">=","like","ilike"))) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
354 |
throw new Exception('DocumentRepository.search : operator must be in "=","!=","<".">","<=".">=","like","ilike"'); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
355 |
} |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
356 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
357 |
if($operator === "IN") |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
358 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
359 |
$in_parameters = array(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
360 |
for ($j = 0; $j < count($values); $j++) { |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
361 |
$parameters["cond_val_$i_$j"] = $values[$j]; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
362 |
$in_parameters[] = ":cond_val_$i_$j"; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
363 |
} |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
364 |
$cond = "($col IN (".implode(",",$in_parameters)."))"; |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
365 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
366 |
else |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
367 |
{ |
|
37
9ba15af20acc
make sure that we use external ids in the controller
ymh <ymh.work@gmail.com>
parents:
34
diff
changeset
|
368 |
$cond = "($col $operator :cond_val_$i)"; |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
369 |
$parameters["cond_val_$i"] = $values; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
370 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
371 |
$conditions_array[] = $cond; |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
372 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
373 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
374 |
if(count($conditions_array) > 0) |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
375 |
{ |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
376 |
$conditions_str = " AND ".implode(" AND ", $conditions_array); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
377 |
} |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
378 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
379 |
} |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
380 |
$fieldnamequery = "d.id, d.external_id"; |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
381 |
if(count($fieldnamelist) > 0) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
382 |
$fieldnamequery .= ", d.".join(", d.", $fieldnamelist); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
383 |
} |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
384 |
$query = $em->createNativeQuery("SELECT $fieldnamequery, $score_def AS score FROM wikitag_document d WHERE $score_def > 0 $conditions_str ORDER BY score DESC", $rsm); |
|
30
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
385 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
386 |
$query->setParameters($parameters); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
387 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
388 |
$res = $query->getResult(); |
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
389 |
|
|
d2fba1e3b94b
correction of reorder tag (including the javascript)
ymh <ymh.work@gmail.com>
parents:
29
diff
changeset
|
390 |
return $res; |
| 29 | 391 |
} |
|
57
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
392 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
393 |
/** |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
394 |
* Copy the tahg lst from one document instance to another |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
395 |
* @param IRI\Bundle\WikitagBundle\Model\DocumentInterface $src_doc |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
396 |
* @param IRI\Bundle\WikitagBundle\Model\DocumentInterface $tgt_doc |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
397 |
*/ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
398 |
public function copyTags($src_doc, $tgt_doc) |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
399 |
{ |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
400 |
//remove the previous tags |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
401 |
foreach ($tgt_doc->getTags() as $doctag) { |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
402 |
$this->getEntityManager()->remove($doctag); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
403 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
404 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
405 |
// add the new ones |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
406 |
foreach ($src_doc->getTags() as $doctag) { |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
407 |
$new_doctag = clone $doctag; |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
408 |
$new_doctag->setDocument($tgt_doc); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
409 |
$this->getEntityManager()->persist($new_doctag); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
410 |
} |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
411 |
|
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
412 |
$tgt_doc->setManualOrder(false); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
413 |
$this->getEntityManager()->persist($tgt_doc); |
|
186c4121c7b3
add service tp copy tag from one document to another
ymh <ymh.work@gmail.com>
parents:
42
diff
changeset
|
414 |
} |
|
23
b435f8055cb4
improve dynamic docs. create and lad class dynamically
ymh <ymh.work@gmail.com>
parents:
18
diff
changeset
|
415 |
|
| 2 | 416 |
} |