|
@@ -0,0 +1,421 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace Muzich\CoreBundle\Searcher;
|
|
4
|
+
|
|
5
|
+use Doctrine\ORM\EntityManager;
|
|
6
|
+use Doctrine\ORM\QueryBuilder;
|
|
7
|
+use Doctrine\ORM\Query\Expr\Join;
|
|
8
|
+
|
|
9
|
+class ElementSearcherQueryBuilder
|
|
10
|
+{
|
|
11
|
+
|
|
12
|
+ /**
|
|
13
|
+ *
|
|
14
|
+ * @var \Doctrine\ORM\EntityManager
|
|
15
|
+ */
|
|
16
|
+ protected $em;
|
|
17
|
+
|
|
18
|
+ protected $user_id;
|
|
19
|
+
|
|
20
|
+ /**
|
|
21
|
+ *
|
|
22
|
+ * @var array
|
|
23
|
+ */
|
|
24
|
+ protected $parameters_ids = array();
|
|
25
|
+
|
|
26
|
+ /**
|
|
27
|
+ *
|
|
28
|
+ * @var array
|
|
29
|
+ */
|
|
30
|
+ protected $parameters_elements = array();
|
|
31
|
+
|
|
32
|
+ /**
|
|
33
|
+ *
|
|
34
|
+ * @var ElementSearcher
|
|
35
|
+ */
|
|
36
|
+ protected $es;
|
|
37
|
+
|
|
38
|
+ /**
|
|
39
|
+ *
|
|
40
|
+ * @var QueryBuilder
|
|
41
|
+ */
|
|
42
|
+ protected $query_ids;
|
|
43
|
+
|
|
44
|
+ /**
|
|
45
|
+ *
|
|
46
|
+ * @var QueryBuilder
|
|
47
|
+ */
|
|
48
|
+ protected $query_elements;
|
|
49
|
+
|
|
50
|
+ /**
|
|
51
|
+ *
|
|
52
|
+ * @var array
|
|
53
|
+ */
|
|
54
|
+ protected $builder_params = array();
|
|
55
|
+
|
|
56
|
+ /**
|
|
57
|
+ *
|
|
58
|
+ * @param EntityManager $em
|
|
59
|
+ * @param ElementSearcher $es
|
|
60
|
+ */
|
|
61
|
+ public function __construct(EntityManager $em, ElementSearcher $es, $user_id, $builder_params = array())
|
|
62
|
+ {
|
|
63
|
+ $this->em = $em;
|
|
64
|
+ $this->es = $es;
|
|
65
|
+ $this->user_id = $user_id;
|
|
66
|
+ $this->builder_params = $builder_params;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ private function buildTags()
|
|
70
|
+ {
|
|
71
|
+ if (count($tags = $this->es->getTags()))
|
|
72
|
+ {
|
|
73
|
+ // Un truc pas propre fait que des fois (quand ajax on dirais)
|
|
74
|
+ // on a du string a la place du tableau
|
|
75
|
+ if (!is_array($tags))
|
|
76
|
+ {
|
|
77
|
+
|
|
78
|
+ $tags_decoded = json_decode($tags);
|
|
79
|
+ $tags = array();
|
|
80
|
+ foreach ($tags_decoded as $tag_id)
|
|
81
|
+ {
|
|
82
|
+ $tags[$tag_id] = $tag_id;
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+ if (count($tags))
|
|
88
|
+ {
|
|
89
|
+ $str_or = $this->query_ids->expr()->orx();
|
|
90
|
+ $this->query_ids->leftJoin('e.tags', 't');
|
|
91
|
+ foreach ($tags as $tag_id => $tag_name)
|
|
92
|
+ {
|
|
93
|
+ $str_or->add($this->query_ids->expr()->eq('t.id', ":itag".$tag_id));
|
|
94
|
+ $this->parameters_ids["itag".$tag_id] = $tag_id;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ $this->query_ids->andWhere($str_or);
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ private function buildString()
|
|
104
|
+ {
|
|
105
|
+ if (($string = $this->es->getString()))
|
|
106
|
+ {
|
|
107
|
+ // On prépare notre liste de mots
|
|
108
|
+ $words = array_unique(array_merge(
|
|
109
|
+ explode(' ', $string),
|
|
110
|
+ explode('-', $string),
|
|
111
|
+ explode('- ', $string),
|
|
112
|
+ explode(' -', $string),
|
|
113
|
+ explode(' - ', $string),
|
|
114
|
+ explode(',', $string),
|
|
115
|
+ explode(', ', $string),
|
|
116
|
+ explode(' ,', $string),
|
|
117
|
+ explode(' , ', $string)
|
|
118
|
+ ));
|
|
119
|
+
|
|
120
|
+ // On récupère les ids des elements correspondants
|
|
121
|
+ $word_min_length = 0;
|
|
122
|
+ if (isset($this->builder_params['word_min_length']))
|
|
123
|
+ {
|
|
124
|
+ $word_min_length = $this->builder_params['word_min_length'];
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ // On prépare un sous-where avec des or
|
|
128
|
+ $str_or = $this->query_ids->expr()->orx();
|
|
129
|
+
|
|
130
|
+ foreach ($words as $i => $word)
|
|
131
|
+ {
|
|
132
|
+ if (strlen($word) >= $word_min_length)
|
|
133
|
+ {
|
|
134
|
+ // On ajoute un or pour chaque mots
|
|
135
|
+ $str_or->add($this->query_ids->expr()->like('UPPER(e.name)', ":str".$i));
|
|
136
|
+ $this->parameters_ids['str'.$i] = '%'.strtoupper($word).'%';
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ $this->query_ids->andWhere($str_or);
|
|
141
|
+ }
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ private function buildNetwork()
|
|
145
|
+ {
|
|
146
|
+ if ($this->es->getNetwork() == ElementSearcher::NETWORK_PERSONAL)
|
|
147
|
+ {
|
|
148
|
+ $this->query_ids
|
|
149
|
+ ->join('e.owner', 'o')
|
|
150
|
+ ->leftJoin('e.group', 'g')
|
|
151
|
+ ->leftJoin('o.followers_users', 'f')
|
|
152
|
+ ->leftJoin('g.followers', 'gf')
|
|
153
|
+ ;
|
|
154
|
+
|
|
155
|
+ $this->query_ids->andWhere('f.follower = :userid OR gf.follower = :useridg');
|
|
156
|
+ $this->parameters_ids['userid'] = $this->user_id;
|
|
157
|
+ $this->parameters_ids['useridg'] = $this->user_id;
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ private function buildFavorite()
|
|
162
|
+ {
|
|
163
|
+ if ($this->es->isFavorite())
|
|
164
|
+ {
|
|
165
|
+ if (($favorite_user_id = $this->es->getUserId()) && !$this->es->getGroupId())
|
|
166
|
+ {
|
|
167
|
+ $this->query_ids->leftJoin('e.elements_favorites', 'fav2');
|
|
168
|
+ $this->query_ids->andWhere('fav2.user = :fuid');
|
|
169
|
+ $this->parameters_ids['fuid'] = $favorite_user_id;
|
|
170
|
+ }
|
|
171
|
+ else if (($favorite_group_id = $this->es->getGroupId()) && !$this->es->getUserId())
|
|
172
|
+ {
|
|
173
|
+ // TODO: Faire en sorte que ça affiche les favoris des gens suivant
|
|
174
|
+ // le groupe
|
|
175
|
+ }
|
|
176
|
+ else
|
|
177
|
+ {
|
|
178
|
+ throw new Exception('For use favorite search element, you must specify an user_id or group_id');
|
|
179
|
+ }
|
|
180
|
+ }
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ private function buildUserAndGroup()
|
|
184
|
+ {
|
|
185
|
+ // (flou) Si on recherche les élements d'un user
|
|
186
|
+ if (($search_user_id = $this->es->getUserId()) && !$this->es->isFavorite())
|
|
187
|
+ {
|
|
188
|
+ $this->query_ids->andWhere('e.owner = :suid');
|
|
189
|
+ $this->parameters_ids['suid'] = $search_user_id;
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ // (flou) Si on recherche les éléments d'un groupe
|
|
193
|
+ if (($search_group_id = $this->es->getGroupId()) && !$this->es->isFavorite())
|
|
194
|
+ {
|
|
195
|
+ $this->query_ids->andWhere('e.group = :sgid');
|
|
196
|
+ $this->parameters_ids['sgid'] = $search_group_id;
|
|
197
|
+ }
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ private function buildLimits()
|
|
201
|
+ {
|
|
202
|
+ // Si id_limit est précisé c'est que l'on demande "la suite" ou "les nouveaux"
|
|
203
|
+ if (($id_limit = $this->es->getIdLimit()) && !$this->es->isSearchingNew())
|
|
204
|
+ {
|
|
205
|
+ $this->query_ids->andWhere("e.id < :id_limit");
|
|
206
|
+ $this->parameters_ids['id_limit'] = $id_limit;
|
|
207
|
+ }
|
|
208
|
+ elseif ($id_limit && $this->es->isSearchingNew())
|
|
209
|
+ {
|
|
210
|
+ $this->query_ids->andWhere("e.id > :id_limit");
|
|
211
|
+ $this->parameters_ids['id_limit'] = $id_limit;
|
|
212
|
+ $this->query_ids->orderBy("e.created", 'ASC')
|
|
213
|
+ ->addOrderBy("e.id", 'ASC');
|
|
214
|
+ }
|
|
215
|
+ }
|
|
216
|
+
|
|
217
|
+ private function buildStrict()
|
|
218
|
+ {
|
|
219
|
+ // Recherche strict ou non ?
|
|
220
|
+ if ($this->es->getTagStrict() && count(($tags = $this->es->getTags())))
|
|
221
|
+ {
|
|
222
|
+ // On a besoin de récupérer la liste des element_id qui ont les tags
|
|
223
|
+ // demandés.
|
|
224
|
+ $tag_ids = '';
|
|
225
|
+ foreach ($tags as $tag_id => $tag_name)
|
|
226
|
+ {
|
|
227
|
+ if ($tag_ids === '')
|
|
228
|
+ {
|
|
229
|
+ $tag_ids .= (int)$tag_id;
|
|
230
|
+ }
|
|
231
|
+ else
|
|
232
|
+ {
|
|
233
|
+ $tag_ids .= ','.(int)$tag_id;
|
|
234
|
+ }
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ $sql = "SELECT et.element_id FROM elements_tag et "
|
|
238
|
+ ."WHERE et.tag_id IN ($tag_ids) group by et.element_id "
|
|
239
|
+ ."having count(distinct et.tag_id) = ".count($tags);
|
|
240
|
+ $rsm = new \Doctrine\ORM\Query\ResultSetMapping;
|
|
241
|
+ $rsm->addScalarResult('element_id', 'element_id');
|
|
242
|
+
|
|
243
|
+ $strict_element_ids_result = $this->em
|
|
244
|
+ ->createNativeQuery($sql, $rsm)
|
|
245
|
+ //->setParameter('ids', $tag_ids)
|
|
246
|
+ ->getScalarResult()
|
|
247
|
+ ;
|
|
248
|
+
|
|
249
|
+ $strict_element_ids = array();
|
|
250
|
+ foreach ($strict_element_ids_result as $strict_id)
|
|
251
|
+ {
|
|
252
|
+ $strict_element_ids[] = $strict_id['element_id'];
|
|
253
|
+ }
|
|
254
|
+
|
|
255
|
+ if (count($strict_element_ids))
|
|
256
|
+ {
|
|
257
|
+ $this->query_ids->andWhere('e.id IN (:tag_strict_ids)');
|
|
258
|
+ $this->parameters_ids['tag_strict_ids'] = $strict_element_ids;
|
|
259
|
+ }
|
|
260
|
+ // Ce else palie au bug du au cas ou $strict_element_ids est egal a array();
|
|
261
|
+ else
|
|
262
|
+ {
|
|
263
|
+ $this->query_ids->andWhere('1 == 2');
|
|
264
|
+ }
|
|
265
|
+ }
|
|
266
|
+ }
|
|
267
|
+
|
|
268
|
+ /**
|
|
269
|
+ *
|
|
270
|
+ * @param boolean $disable_limit
|
|
271
|
+ */
|
|
272
|
+ protected function proceedIdsQuery($disable_limit = false)
|
|
273
|
+ {
|
|
274
|
+
|
|
275
|
+ $this->query_ids = $this->em->createQueryBuilder()
|
|
276
|
+ ->select('e.id')
|
|
277
|
+ ->from('MuzichCoreBundle:Element', 'e')
|
|
278
|
+ ->groupBy('e.id')
|
|
279
|
+ ->orderBy('e.created', 'DESC')
|
|
280
|
+ ->addOrderBy('e.id', 'DESC')
|
|
281
|
+ ;
|
|
282
|
+
|
|
283
|
+ if (!$disable_limit)
|
|
284
|
+ {
|
|
285
|
+ $this->query_ids->setMaxResults($this->es->getCount());
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ // Prise en compte des tags
|
|
289
|
+ $this->buildTags();
|
|
290
|
+ // Si on effectue une recherche avec un string
|
|
291
|
+ $this->buildString();
|
|
292
|
+ // Paramètrage en fonction du réseau
|
|
293
|
+ $this->buildNetwork();
|
|
294
|
+ // Si on recherche des elements d'user ou de groupe
|
|
295
|
+ $this->buildUserAndGroup();
|
|
296
|
+ // Si on recherche des elements mis en favoris
|
|
297
|
+ $this->buildFavorite();
|
|
298
|
+ // Pour les demandes de "more" ou "nouveaux" elements.
|
|
299
|
+ $this->buildLimits();
|
|
300
|
+ // Si on recherche les tags de manière stricte
|
|
301
|
+ $this->buildStrict();
|
|
302
|
+
|
|
303
|
+ $this->query_ids->setParameters($this->parameters_ids);
|
|
304
|
+ }
|
|
305
|
+
|
|
306
|
+ /**
|
|
307
|
+ *
|
|
308
|
+ * @param boolean $disable_limit
|
|
309
|
+ * @return \Doctrine\ORM\Query
|
|
310
|
+ * @throws \Exception
|
|
311
|
+ */
|
|
312
|
+ public function getIdsQuery($disable_limit = false)
|
|
313
|
+ {
|
|
314
|
+ // Contrôle de la demande
|
|
315
|
+ if ($this->es->hasIds())
|
|
316
|
+ {
|
|
317
|
+ throw new \Exception("Vous demandez un Query_ids avec un ElementSearcher "
|
|
318
|
+ ."possédant déjà une liste d'ids");
|
|
319
|
+ }
|
|
320
|
+
|
|
321
|
+ $this->proceedIdsQuery($disable_limit);
|
|
322
|
+ return $this->query_ids->getQuery();
|
|
323
|
+ }
|
|
324
|
+
|
|
325
|
+ protected function proceedElementsQuery()
|
|
326
|
+ {
|
|
327
|
+ // On récupère les ids d'éléments
|
|
328
|
+ if (!$this->es->hasIds())
|
|
329
|
+ {
|
|
330
|
+
|
|
331
|
+// $query = $this->getIdsQuery();
|
|
332
|
+//
|
|
333
|
+//
|
|
334
|
+// var_dump($this->em->getRepository('MuzichCoreBundle:Element')->findBySearchOLD(
|
|
335
|
+// $this->es, $this->user_id, 'count', $this->builder_params
|
|
336
|
+// )->getDql()
|
|
337
|
+//
|
|
338
|
+// );
|
|
339
|
+// die(var_dump($this->getIdsQuery()->getArrayResult()));
|
|
340
|
+
|
|
341
|
+ // On va récupérer les ids en base en fonction des paramètres
|
|
342
|
+ $q_ids = $this->getIdsQuery()->getArrayResult();
|
|
343
|
+
|
|
344
|
+ if (count($q_ids))
|
|
345
|
+ {
|
|
346
|
+ // On prépare les ids pour la requete des éléments
|
|
347
|
+ foreach ($q_ids as $r_id)
|
|
348
|
+ {
|
|
349
|
+ $element_ids[] = $r_id['id'];
|
|
350
|
+ }
|
|
351
|
+ }
|
|
352
|
+ }
|
|
353
|
+ else
|
|
354
|
+ {
|
|
355
|
+ // Les ids sont déjà transmis par la demande
|
|
356
|
+ $element_ids = $this->es->getIds();
|
|
357
|
+ }
|
|
358
|
+
|
|
359
|
+ if (!count($element_ids))
|
|
360
|
+ {
|
|
361
|
+ // Si on a pas d'ids on retourne une requete qui ne donnera rien
|
|
362
|
+ return $this->getEntityManager()
|
|
363
|
+ ->createQuery("SELECT e FROM MuzichCoreBundle:Element e WHERE 1 = 2")
|
|
364
|
+ ;
|
|
365
|
+ }
|
|
366
|
+
|
|
367
|
+ // On prépare des paramètres de la requete d'éléments
|
|
368
|
+ $this->parameters_elements['uidt'] = '%"'. $this->user_id.'"%';
|
|
369
|
+ $this->parameters_elements['uid'] = $this->user_id;
|
|
370
|
+ $this->parameters_elements['ids'] = $element_ids;
|
|
371
|
+
|
|
372
|
+ // On prépare la requete des elements
|
|
373
|
+ $this->query_elements = $this->em->createQueryBuilder()
|
|
374
|
+ ->select('e', 't', 'o', 'g', 'fav')
|
|
375
|
+ ->from('MuzichCoreBundle:Element', 'e')
|
|
376
|
+ ->leftJoin('e.group', 'g')
|
|
377
|
+ ->leftJoin('e.tags', 't', Join::WITH,
|
|
378
|
+ "(t.tomoderate = 'FALSE' OR t.tomoderate IS NULL OR t.privateids LIKE :uidt)")
|
|
379
|
+ ->leftJoin('e.elements_favorites', 'fav', Join::WITH,
|
|
380
|
+ 'fav.user = :uid')
|
|
381
|
+ ->join('e.owner', 'o')
|
|
382
|
+ ->where('e.id IN (:ids)')
|
|
383
|
+ ->orderBy("e.created", 'DESC')
|
|
384
|
+ ->addOrderBy("e.id", 'DESC')
|
|
385
|
+ ;
|
|
386
|
+
|
|
387
|
+ // Ce cas de figure se présente lorsque l'on fait un ajax de "plus d'éléments"
|
|
388
|
+ if (($id_limit = $this->es->getIdLimit()))
|
|
389
|
+ {
|
|
390
|
+ $this->query_elements->andWhere("e.id < :id_limit");
|
|
391
|
+ $this->query_elements->setMaxResults($this->es->getCount());
|
|
392
|
+ $this->parameters_elements['id_limit'] = $id_limit;
|
|
393
|
+ }
|
|
394
|
+
|
|
395
|
+ // Lorsque l'on impose les ids (typiquement affichage des éléments avec un commentaire etc)
|
|
396
|
+ // On charge les tags proposés dés la requete pour économiser les échanges avec la bdd
|
|
397
|
+ if (($ids_display = $this->es->getIdsDisplay()))
|
|
398
|
+ {
|
|
399
|
+ $this->query_elements
|
|
400
|
+ ->addSelect('tp', 'tpu', 'tpt')
|
|
401
|
+ ->leftJoin('e.tags_propositions', 'tp')
|
|
402
|
+ ->leftJoin('tp.user', 'tpu')
|
|
403
|
+ ->leftJoin('tp.tags', 'tpt')
|
|
404
|
+ ;
|
|
405
|
+ }
|
|
406
|
+
|
|
407
|
+ $this->query_elements->setParameters($this->parameters_elements);
|
|
408
|
+
|
|
409
|
+// die(var_dump(count($this->query_elements->getQuery()->getResult())));
|
|
410
|
+//
|
|
411
|
+// var_dump($element_ids);
|
|
412
|
+// die(var_dump($this->query_elements->getQuery()->getParameters()));
|
|
413
|
+ }
|
|
414
|
+
|
|
415
|
+ public function getElementsQuery()
|
|
416
|
+ {
|
|
417
|
+ $this->proceedElementsQuery();
|
|
418
|
+ return $this->query_elements->getQuery();
|
|
419
|
+ }
|
|
420
|
+
|
|
421
|
+}
|