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