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