]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
091bafeb | 6 | use Doctrine\ORM\NoResultException; |
52b84c11 | 7 | use Doctrine\ORM\QueryBuilder; |
bcf53ab7 WD |
8 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
9 | use Pagerfanta\Pagerfanta; | |
52b84c11 | 10 | use Wallabag\CoreBundle\Entity\Entry; |
fc732227 | 11 | use Wallabag\CoreBundle\Entity\Tag; |
4a551637 | 12 | use Wallabag\CoreBundle\Helper\UrlHasher; |
9d50517c | 13 | |
be463487 | 14 | class EntryRepository extends EntityRepository |
9d50517c | 15 | { |
2b7a4889 NL |
16 | /** |
17 | * Retrieves all entries for a user. | |
18 | * | |
96295ec8 JB |
19 | * @param int $userId |
20 | * @param string $sortBy Field to sort | |
21 | * @param string $direction Direction of the order | |
2b7a4889 NL |
22 | * |
23 | * @return QueryBuilder | |
24 | */ | |
8d2527ec | 25 | public function getBuilderForAllByUser($userId, $sortBy = 'id', $direction = 'DESC') |
2b7a4889 NL |
26 | { |
27 | return $this | |
8d2527ec | 28 | ->getSortedQueryBuilderByUser($userId, $sortBy, $direction) |
2b7a4889 NL |
29 | ; |
30 | } | |
31 | ||
0ab7404f JB |
32 | /** |
33 | * Retrieves unread entries for a user. | |
34 | * | |
96295ec8 JB |
35 | * @param int $userId |
36 | * @param string $sortBy Field to sort | |
37 | * @param string $direction Direction of the order | |
0ab7404f JB |
38 | * |
39 | * @return QueryBuilder | |
40 | */ | |
8d2527ec | 41 | public function getBuilderForUnreadByUser($userId, $sortBy = 'id', $direction = 'DESC') |
0ab7404f JB |
42 | { |
43 | return $this | |
96295ec8 | 44 | ->getSortedQueryBuilderByUser($userId, $sortBy, $direction) |
0ab7404f JB |
45 | ->andWhere('e.isArchived = false') |
46 | ; | |
9d50517c | 47 | } |
bd9f0815 | 48 | |
b84a8055 | 49 | /** |
4346a860 | 50 | * Retrieves read entries for a user. |
b84a8055 | 51 | * |
96295ec8 JB |
52 | * @param int $userId |
53 | * @param string $sortBy Field to sort | |
54 | * @param string $direction Direction of the order | |
3b815d2d | 55 | * |
26864574 | 56 | * @return QueryBuilder |
b84a8055 | 57 | */ |
96295ec8 | 58 | public function getBuilderForArchiveByUser($userId, $sortBy = 'archivedAt', $direction = 'DESC') |
bd9f0815 | 59 | { |
0ab7404f | 60 | return $this |
96295ec8 | 61 | ->getSortedQueryBuilderByUser($userId, $sortBy, $direction) |
0ab7404f JB |
62 | ->andWhere('e.isArchived = true') |
63 | ; | |
bd9f0815 NL |
64 | } |
65 | ||
b84a8055 | 66 | /** |
4346a860 | 67 | * Retrieves starred entries for a user. |
b84a8055 | 68 | * |
96295ec8 JB |
69 | * @param int $userId |
70 | * @param string $sortBy Field to sort | |
71 | * @param string $direction Direction of the order | |
3b815d2d | 72 | * |
26864574 | 73 | * @return QueryBuilder |
b84a8055 | 74 | */ |
96295ec8 | 75 | public function getBuilderForStarredByUser($userId, $sortBy = 'starredAt', $direction = 'DESC') |
bd9f0815 | 76 | { |
0ab7404f | 77 | return $this |
96295ec8 | 78 | ->getSortedQueryBuilderByUser($userId, $sortBy, $direction) |
0ab7404f JB |
79 | ->andWhere('e.isStarred = true') |
80 | ; | |
bd9f0815 | 81 | } |
a8c90c5c | 82 | |
ee122a75 NL |
83 | /** |
84 | * Retrieves entries filtered with a search term for a user. | |
85 | * | |
86 | * @param int $userId | |
87 | * @param string $term | |
52b84c11 | 88 | * @param string $currentRoute |
ee122a75 NL |
89 | * |
90 | * @return QueryBuilder | |
91 | */ | |
49b042df | 92 | public function getBuilderForSearchByUser($userId, $term, $currentRoute) |
ee122a75 | 93 | { |
49b042df | 94 | $qb = $this |
b7c5fda5 | 95 | ->getSortedQueryBuilderByUser($userId); |
49b042df NL |
96 | |
97 | if ('starred' === $currentRoute) { | |
98 | $qb->andWhere('e.isStarred = true'); | |
99 | } elseif ('unread' === $currentRoute) { | |
100 | $qb->andWhere('e.isArchived = false'); | |
101 | } elseif ('archive' === $currentRoute) { | |
102 | $qb->andWhere('e.isArchived = true'); | |
103 | } | |
104 | ||
eac09b48 | 105 | // We lower() all parts here because PostgreSQL 'LIKE' verb is case-sensitive |
49b042df | 106 | $qb |
f808b016 | 107 | ->andWhere('lower(e.content) LIKE lower(:term) OR lower(e.title) LIKE lower(:term) OR lower(e.url) LIKE lower(:term)')->setParameter('term', '%' . $term . '%') |
ee122a75 | 108 | ->leftJoin('e.tags', 't') |
32f455c1 | 109 | ->groupBy('e.id'); |
49b042df NL |
110 | |
111 | return $qb; | |
ee122a75 NL |
112 | } |
113 | ||
b6520f0b | 114 | /** |
06366972 | 115 | * Retrieve a sorted list of untagged entries for a user. |
b6520f0b NL |
116 | * |
117 | * @param int $userId | |
118 | * | |
119 | * @return QueryBuilder | |
120 | */ | |
121 | public function getBuilderForUntaggedByUser($userId) | |
122 | { | |
09ef25c3 | 123 | return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); |
06366972 KD |
124 | } |
125 | ||
126 | /** | |
127 | * Retrieve untagged entries for a user. | |
b8115ff4 | 128 | * |
06366972 | 129 | * @param int $userId |
b8115ff4 | 130 | * |
06366972 KD |
131 | * @return QueryBuilder |
132 | */ | |
133 | public function getRawBuilderForUntaggedByUser($userId) | |
134 | { | |
135 | return $this->getQueryBuilderByUser($userId) | |
136 | ->leftJoin('e.tags', 't') | |
137 | ->andWhere('t.id is null'); | |
b6520f0b NL |
138 | } |
139 | ||
ad51743e KD |
140 | /** |
141 | * Retrieve the number of untagged entries for a user. | |
0f2d24fe | 142 | * |
ad51743e | 143 | * @param int $userId |
0f2d24fe | 144 | * |
ad51743e KD |
145 | * @return int |
146 | */ | |
147 | public function countUntaggedEntriesByUser($userId) | |
148 | { | |
0f2d24fe | 149 | return (int) $this->getRawBuilderForUntaggedByUser($userId) |
ad51743e | 150 | ->select('count(e.id)') |
0f2d24fe | 151 | ->getQuery() |
ad51743e KD |
152 | ->getSingleScalarResult(); |
153 | } | |
154 | ||
3b815d2d | 155 | /** |
4346a860 | 156 | * Find Entries. |
3b815d2d | 157 | * |
2a94b1d1 NL |
158 | * @param int $userId |
159 | * @param bool $isArchived | |
160 | * @param bool $isStarred | |
1112e547 | 161 | * @param bool $isPublic |
2a94b1d1 NL |
162 | * @param string $sort |
163 | * @param string $order | |
8cb869ea TC |
164 | * @param int $since |
165 | * @param string $tags | |
2c290747 KD |
166 | * @param string $detail 'metadata' or 'full'. Include content field if 'full' |
167 | * | |
168 | * @todo Breaking change: replace default detail=full by detail=metadata in a future version | |
3b815d2d | 169 | * |
52b84c11 | 170 | * @return Pagerfanta |
3b815d2d | 171 | */ |
2c290747 | 172 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '', $detail = 'full') |
a8c90c5c | 173 | { |
2c290747 KD |
174 | if (!\in_array(strtolower($detail), ['full', 'metadata'], true)) { |
175 | throw new \Exception('Detail "' . $detail . '" parameter is wrong, allowed: full or metadata'); | |
176 | } | |
177 | ||
a8c90c5c | 178 | $qb = $this->createQueryBuilder('e') |
28803f10 | 179 | ->leftJoin('e.tags', 't') |
7c04b739 | 180 | ->where('e.user = :userId')->setParameter('userId', $userId); |
6e334aba | 181 | |
2c290747 KD |
182 | if ('metadata' === $detail) { |
183 | $fieldNames = $this->getClassMetadata()->getFieldNames(); | |
184 | $fields = array_filter($fieldNames, function ($k) { | |
185 | return 'content' !== $k; | |
186 | }); | |
187 | $qb->select(sprintf('partial e.{%s}', implode(',', $fields))); | |
188 | } | |
189 | ||
3b815d2d | 190 | if (null !== $isArchived) { |
1112e547 | 191 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); |
6e334aba NL |
192 | } |
193 | ||
3b815d2d | 194 | if (null !== $isStarred) { |
1112e547 JB |
195 | $qb->andWhere('e.isStarred = :isStarred')->setParameter('isStarred', (bool) $isStarred); |
196 | } | |
197 | ||
198 | if (null !== $isPublic) { | |
f808b016 | 199 | $qb->andWhere('e.uid IS ' . (true === $isPublic ? 'NOT' : '') . ' NULL'); |
6e334aba NL |
200 | } |
201 | ||
c3f8b428 | 202 | if ($since > 0) { |
e5fb89e5 | 203 | $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since))); |
4f0558a0 TC |
204 | } |
205 | ||
2a1ceb67 | 206 | if (\is_string($tags) && '' !== $tags) { |
7c04b739 JB |
207 | foreach (explode(',', $tags) as $i => $tag) { |
208 | $entryAlias = 'e' . $i; | |
209 | $tagAlias = 't' . $i; | |
210 | ||
33264c2d | 211 | // Complexe queries to ensure multiple tags are associated to an entry |
7c04b739 JB |
212 | // https://stackoverflow.com/a/6638146/569101 |
213 | $qb->andWhere($qb->expr()->in( | |
214 | 'e.id', | |
215 | $this->createQueryBuilder($entryAlias) | |
216 | ->select($entryAlias . '.id') | |
217 | ->leftJoin($entryAlias . '.tags', $tagAlias) | |
218 | ->where($tagAlias . '.label = :label' . $i) | |
219 | ->getDQL() | |
220 | )); | |
221 | ||
222 | // bound parameter to the main query builder | |
223 | $qb->setParameter('label' . $i, $tag); | |
28803f10 | 224 | } |
e5fb89e5 TC |
225 | } |
226 | ||
78e3fafa JB |
227 | if (!\in_array(strtolower($order), ['asc', 'desc'], true)) { |
228 | throw new \Exception('Order "' . $order . '" parameter is wrong, allowed: asc or desc'); | |
229 | } | |
230 | ||
bc782eaa | 231 | if ('created' === $sort) { |
2385f891 | 232 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
233 | } elseif ('updated' === $sort) { |
234 | $qb->orderBy('e.updatedAt', $order); | |
7c0d6826 | 235 | } elseif ('archived' === $sort) { |
0e70e812 | 236 | $qb->orderBy('e.archivedAt', $order); |
bc782eaa NL |
237 | } |
238 | ||
5a5da369 | 239 | $pagerAdapter = new DoctrineORMAdapter($qb, true, false); |
bcf53ab7 WD |
240 | |
241 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 242 | } |
46bbd8d3 | 243 | |
a36737f4 NL |
244 | /** |
245 | * Fetch an entry with a tag. Only used for tests. | |
246 | * | |
5c072d2b NL |
247 | * @param int $userId |
248 | * | |
52b84c11 | 249 | * @return array |
a36737f4 | 250 | */ |
092ca707 | 251 | public function findOneWithTags($userId) |
46bbd8d3 NL |
252 | { |
253 | $qb = $this->createQueryBuilder('e') | |
254 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
255 | ->innerJoin('e.user', 'u') |
256 | ->addSelect('t', 'u') | |
7c04b739 | 257 | ->where('e.user = :userId')->setParameter('userId', $userId) |
0ca374e6 | 258 | ; |
092ca707 | 259 | |
0ca374e6 | 260 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 261 | } |
d4ebe5c5 JB |
262 | |
263 | /** | |
264 | * Find distinct language for a given user. | |
265 | * Used to build the filter language list. | |
266 | * | |
267 | * @param int $userId User id | |
268 | * | |
269 | * @return array | |
270 | */ | |
271 | public function findDistinctLanguageByUser($userId) | |
272 | { | |
273 | $results = $this->createQueryBuilder('e') | |
274 | ->select('e.language') | |
275 | ->where('e.user = :userId')->setParameter('userId', $userId) | |
276 | ->andWhere('e.language IS NOT NULL') | |
277 | ->groupBy('e.language') | |
278 | ->orderBy('e.language', ' ASC') | |
279 | ->getQuery() | |
280 | ->getResult(); | |
281 | ||
4094ea47 | 282 | $languages = []; |
d4ebe5c5 JB |
283 | foreach ($results as $result) { |
284 | $languages[$result['language']] = $result['language']; | |
285 | } | |
286 | ||
287 | return $languages; | |
288 | } | |
159986c4 | 289 | |
159986c4 | 290 | /** |
cfb28c9d | 291 | * Used only in test case to get the right entry associated to the right user. |
159986c4 | 292 | * |
cfb28c9d | 293 | * @param string $username |
159986c4 JB |
294 | * |
295 | * @return Entry | |
296 | */ | |
297 | public function findOneByUsernameAndNotArchived($username) | |
298 | { | |
299 | return $this->createQueryBuilder('e') | |
300 | ->leftJoin('e.user', 'u') | |
301 | ->where('u.username = :username')->setParameter('username', $username) | |
302 | ->andWhere('e.isArchived = false') | |
303 | ->setMaxResults(1) | |
304 | ->getQuery() | |
305 | ->getSingleResult(); | |
306 | } | |
fc732227 JB |
307 | |
308 | /** | |
309 | * Remove a tag from all user entries. | |
4059a061 JB |
310 | * |
311 | * We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation. | |
6be97501 JB |
312 | * It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix: |
313 | * | |
314 | * DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId | |
fc732227 JB |
315 | * |
316 | * @param int $userId | |
fc732227 JB |
317 | */ |
318 | public function removeTag($userId, Tag $tag) | |
319 | { | |
b7c5fda5 | 320 | $entries = $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
321 | ->innerJoin('e.tags', 't') |
322 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId()) | |
323 | ->getQuery() | |
324 | ->getResult(); | |
325 | ||
326 | foreach ($entries as $entry) { | |
327 | $entry->removeTag($tag); | |
328 | } | |
329 | ||
330 | $this->getEntityManager()->flush(); | |
4059a061 JB |
331 | } |
332 | ||
4da01f49 | 333 | /** |
9bf83f1f | 334 | * Remove tags from all user entries. |
4da01f49 | 335 | * |
9bf83f1f | 336 | * @param int $userId |
4da01f49 TC |
337 | * @param Array<Tag> $tags |
338 | */ | |
9bf83f1f TC |
339 | public function removeTags($userId, $tags) |
340 | { | |
4da01f49 TC |
341 | foreach ($tags as $tag) { |
342 | $this->removeTag($userId, $tag); | |
343 | } | |
344 | } | |
345 | ||
4059a061 JB |
346 | /** |
347 | * Find all entries that are attached to a give tag id. | |
348 | * | |
349 | * @param int $userId | |
350 | * @param int $tagId | |
351 | * | |
352 | * @return array | |
353 | */ | |
354 | public function findAllByTagId($userId, $tagId) | |
355 | { | |
b7c5fda5 | 356 | return $this->getSortedQueryBuilderByUser($userId) |
4059a061 JB |
357 | ->innerJoin('e.tags', 't') |
358 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | |
359 | ->getQuery() | |
360 | ->getResult(); | |
fc732227 | 361 | } |
303768df NL |
362 | |
363 | /** | |
364 | * Find an entry by its url and its owner. | |
5a4bbcc9 | 365 | * If it exists, return the entry otherwise return false. |
303768df | 366 | * |
50f35f0d JB |
367 | * @param string $url |
368 | * @param int $userId | |
303768df | 369 | * |
52e8d932 | 370 | * @return Entry|false |
303768df | 371 | */ |
78833672 | 372 | public function findByUrlAndUserId($url, $userId) |
303768df | 373 | { |
d5744bf0 | 374 | return $this->findByHashedUrlAndUserId( |
4a551637 | 375 | UrlHasher::hashUrl($url), |
0132ccd2 JB |
376 | $userId |
377 | ); | |
303768df | 378 | } |
5c072d2b | 379 | |
9c2b2aae JB |
380 | /** |
381 | * Find an entry by its hashed url and its owner. | |
382 | * If it exists, return the entry otherwise return false. | |
383 | * | |
c579ce23 JB |
384 | * @param string $hashedUrl Url hashed using sha1 |
385 | * @param int $userId | |
9c2b2aae | 386 | * |
52e8d932 | 387 | * @return Entry|false |
9c2b2aae JB |
388 | */ |
389 | public function findByHashedUrlAndUserId($hashedUrl, $userId) | |
390 | { | |
70df4c33 | 391 | // try first using hashed_url (to use the database index) |
9c2b2aae | 392 | $res = $this->createQueryBuilder('e') |
c579ce23 | 393 | ->where('e.hashedUrl = :hashed_url')->setParameter('hashed_url', $hashedUrl) |
9c2b2aae JB |
394 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
395 | ->getQuery() | |
396 | ->getResult(); | |
397 | ||
398 | if (\count($res)) { | |
399 | return current($res); | |
400 | } | |
401 | ||
70df4c33 JB |
402 | // then try using hashed_given_url (to use the database index) |
403 | $res = $this->createQueryBuilder('e') | |
404 | ->where('e.hashedGivenUrl = :hashed_given_url')->setParameter('hashed_given_url', $hashedUrl) | |
9c2b2aae JB |
405 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) |
406 | ->getQuery() | |
407 | ->getResult(); | |
408 | ||
409 | if (\count($res)) { | |
410 | return current($res); | |
411 | } | |
412 | ||
413 | return false; | |
414 | } | |
415 | ||
5c072d2b NL |
416 | /** |
417 | * Count all entries for a user. | |
418 | * | |
419 | * @param int $userId | |
420 | * | |
e678c475 | 421 | * @return int |
5c072d2b | 422 | */ |
273b6f06 | 423 | public function countAllEntriesByUser($userId) |
5c072d2b NL |
424 | { |
425 | $qb = $this->createQueryBuilder('e') | |
426 | ->select('count(e)') | |
7c04b739 JB |
427 | ->where('e.user = :userId')->setParameter('userId', $userId) |
428 | ; | |
429 | ||
430 | return (int) $qb->getQuery()->getSingleScalarResult(); | |
431 | } | |
432 | ||
191564b7 JB |
433 | /** |
434 | * Remove all entries for a user id. | |
8c61fd12 | 435 | * Used when a user want to reset all informations. |
191564b7 | 436 | * |
8c61fd12 | 437 | * @param int $userId |
191564b7 JB |
438 | */ |
439 | public function removeAllByUserId($userId) | |
440 | { | |
441 | $this->getEntityManager() | |
b0de88f7 JB |
442 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') |
443 | ->setParameter('userId', $userId) | |
191564b7 JB |
444 | ->execute(); |
445 | } | |
6da1aebc TC |
446 | |
447 | public function removeArchivedByUserId($userId) | |
448 | { | |
449 | $this->getEntityManager() | |
450 | ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId AND e.isArchived = TRUE') | |
451 | ->setParameter('userId', $userId) | |
452 | ->execute(); | |
453 | } | |
e2f3800c TC |
454 | |
455 | /** | |
456 | * Get id and url from all entries | |
457 | * Used for the clean-duplicates command. | |
458 | */ | |
dbf1188c | 459 | public function findAllEntriesIdAndUrlByUserId($userId) |
e2f3800c TC |
460 | { |
461 | $qb = $this->createQueryBuilder('e') | |
462 | ->select('e.id, e.url') | |
463 | ->where('e.user = :userid')->setParameter(':userid', $userId); | |
464 | ||
465 | return $qb->getQuery()->getArrayResult(); | |
466 | } | |
d09fe4d2 | 467 | |
511f1ce1 NH |
468 | /** |
469 | * @param int $userId | |
470 | * | |
471 | * @return array | |
472 | */ | |
215409a8 | 473 | public function findAllEntriesIdByUserId($userId = null) |
511f1ce1 NH |
474 | { |
475 | $qb = $this->createQueryBuilder('e') | |
476 | ->select('e.id'); | |
477 | ||
478 | if (null !== $userId) { | |
479 | $qb->where('e.user = :userid')->setParameter(':userid', $userId); | |
480 | } | |
481 | ||
482 | return $qb->getQuery()->getArrayResult(); | |
483 | } | |
484 | ||
d09fe4d2 NL |
485 | /** |
486 | * Find all entries by url and owner. | |
487 | * | |
50f35f0d JB |
488 | * @param string $url |
489 | * @param int $userId | |
d09fe4d2 NL |
490 | * |
491 | * @return array | |
492 | */ | |
493 | public function findAllByUrlAndUserId($url, $userId) | |
494 | { | |
89f108b4 | 495 | return $this->createQueryBuilder('e') |
d09fe4d2 NL |
496 | ->where('e.url = :url')->setParameter('url', urldecode($url)) |
497 | ->andWhere('e.user = :user_id')->setParameter('user_id', $userId) | |
498 | ->getQuery() | |
499 | ->getResult(); | |
d09fe4d2 | 500 | } |
f808b016 | 501 | |
09ef25c3 NL |
502 | /** |
503 | * Returns a random entry, filtering by status. | |
504 | * | |
50f35f0d JB |
505 | * @param int $userId |
506 | * @param string $type Can be unread, archive, starred, etc | |
09ef25c3 | 507 | * |
091bafeb | 508 | * @throws NoResultException |
062fad43 | 509 | * |
09ef25c3 NL |
510 | * @return Entry |
511 | */ | |
9a57653a | 512 | public function getRandomEntry($userId, $type = '') |
09ef25c3 | 513 | { |
062fad43 | 514 | $qb = $this->getQueryBuilderByUser($userId) |
50f35f0d | 515 | ->select('e.id'); |
09ef25c3 | 516 | |
9a57653a JB |
517 | switch ($type) { |
518 | case 'unread': | |
519 | $qb->andWhere('e.isArchived = false'); | |
520 | break; | |
521 | case 'archive': | |
522 | $qb->andWhere('e.isArchived = true'); | |
523 | break; | |
524 | case 'starred': | |
525 | $qb->andWhere('e.isStarred = true'); | |
526 | break; | |
527 | case 'untagged': | |
528 | $qb->leftJoin('e.tags', 't'); | |
529 | $qb->andWhere('t.id is null'); | |
530 | break; | |
f85d220c JB |
531 | } |
532 | ||
50f35f0d | 533 | $ids = $qb->getQuery()->getArrayResult(); |
062fad43 | 534 | |
091bafeb JB |
535 | if (empty($ids)) { |
536 | throw new NoResultException(); | |
537 | } | |
538 | ||
50f35f0d JB |
539 | // random select one in the list |
540 | $randomId = $ids[mt_rand(0, \count($ids) - 1)]['id']; | |
541 | ||
542 | return $this->find($randomId); | |
09ef25c3 NL |
543 | } |
544 | ||
f808b016 | 545 | /** |
b7c5fda5 KD |
546 | * Return a query builder to be used by other getBuilderFor* method. |
547 | * | |
b8115ff4 | 548 | * @param int $userId |
b7c5fda5 KD |
549 | * |
550 | * @return QueryBuilder | |
551 | */ | |
552 | private function getQueryBuilderByUser($userId) | |
553 | { | |
554 | return $this->createQueryBuilder('e') | |
555 | ->andWhere('e.user = :userId')->setParameter('userId', $userId); | |
556 | } | |
557 | ||
558 | /** | |
559 | * Return a sorted query builder to be used by other getBuilderFor* method. | |
f808b016 | 560 | * |
a991c46e F |
561 | * @param int $userId |
562 | * @param string $sortBy | |
563 | * @param string $direction | |
f808b016 JB |
564 | * |
565 | * @return QueryBuilder | |
566 | */ | |
b7c5fda5 | 567 | private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') |
f808b016 | 568 | { |
17476f4d | 569 | return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction); |
b7c5fda5 KD |
570 | } |
571 | ||
572 | /** | |
b8115ff4 KD |
573 | * Return the given QueryBuilder with an orderBy() call. |
574 | * | |
8d4ed0df JB |
575 | * @param string $sortBy |
576 | * @param string $direction | |
b7c5fda5 KD |
577 | * |
578 | * @return QueryBuilder | |
579 | */ | |
580 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | |
581 | { | |
09ef25c3 | 582 | return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); |
f808b016 | 583 | } |
9d50517c | 584 | } |