diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Repository/EntryRepository.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 8b6cf443..a26de0a8 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -110,8 +110,7 @@ class EntryRepository extends EntityRepository | |||
110 | */ | 110 | */ |
111 | public function getBuilderForUntaggedByUser($userId) | 111 | public function getBuilderForUntaggedByUser($userId) |
112 | { | 112 | { |
113 | return $this | 113 | return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); |
114 | ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); | ||
115 | } | 114 | } |
116 | 115 | ||
117 | /** | 116 | /** |
@@ -433,6 +432,46 @@ class EntryRepository extends EntityRepository | |||
433 | } | 432 | } |
434 | 433 | ||
435 | /** | 434 | /** |
435 | * Returns a random entry, filtering by status. | ||
436 | * | ||
437 | * @param $userId | ||
438 | * @param string $status can be unread, archive or starred | ||
439 | * | ||
440 | * @throws \Doctrine\ORM\NoResultException | ||
441 | * @throws \Doctrine\ORM\NonUniqueResultException | ||
442 | * | ||
443 | * @return Entry | ||
444 | */ | ||
445 | public function getRandomEntry($userId, $status = '') | ||
446 | { | ||
447 | $max = $this->getEntityManager() | ||
448 | ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') | ||
449 | ->setParameter('userId', $userId) | ||
450 | ->getSingleScalarResult(); | ||
451 | |||
452 | $qb = $this->createQueryBuilder('e') | ||
453 | ->where('e.user = :user_id')->setParameter('user_id', $userId); | ||
454 | |||
455 | if ('unread' === $status) { | ||
456 | $qb->andWhere('e.isArchived = false'); | ||
457 | } | ||
458 | |||
459 | if ('archive' === $status) { | ||
460 | $qb->andWhere('e.isArchived = true'); | ||
461 | } | ||
462 | |||
463 | if ('starred' === $status) { | ||
464 | $qb->andWhere('e.isStarred = true'); | ||
465 | } | ||
466 | |||
467 | return $qb->andWhere('e.id >= :rand') | ||
468 | ->setParameter('rand', rand(0, $max)) | ||
469 | ->setMaxResults(1) | ||
470 | ->getQuery() | ||
471 | ->getSingleResult(); | ||
472 | } | ||
473 | |||
474 | /** | ||
436 | * Return a query builder to be used by other getBuilderFor* method. | 475 | * Return a query builder to be used by other getBuilderFor* method. |
437 | * | 476 | * |
438 | * @param int $userId | 477 | * @param int $userId |
@@ -470,7 +509,6 @@ class EntryRepository extends EntityRepository | |||
470 | */ | 509 | */ |
471 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') | 510 | private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') |
472 | { | 511 | { |
473 | return $qb | 512 | return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); |
474 | ->orderBy(sprintf('e.%s', $sortBy), $direction); | ||
475 | } | 513 | } |
476 | } | 514 | } |