X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FEntryRepository.php;h=a26de0a83a5d7e8ea3282d55f4cb999242f4e025;hb=09ef25c3c3882db94b3941f3ba33ebc78b5dbe4d;hp=8b6cf443efdb2441fa602950739b959aa482fa83;hpb=c73025ad8b684de1ac21ba7583f1af6f1d185159;p=github%2Fwallabag%2Fwallabag.git 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 */ public function getBuilderForUntaggedByUser($userId) { - return $this - ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); + return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); } /** @@ -432,6 +431,46 @@ class EntryRepository extends EntityRepository ->getResult(); } + /** + * Returns a random entry, filtering by status. + * + * @param $userId + * @param string $status can be unread, archive or starred + * + * @throws \Doctrine\ORM\NoResultException + * @throws \Doctrine\ORM\NonUniqueResultException + * + * @return Entry + */ + public function getRandomEntry($userId, $status = '') + { + $max = $this->getEntityManager() + ->createQuery('SELECT MAX(e.id) FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId') + ->setParameter('userId', $userId) + ->getSingleScalarResult(); + + $qb = $this->createQueryBuilder('e') + ->where('e.user = :user_id')->setParameter('user_id', $userId); + + if ('unread' === $status) { + $qb->andWhere('e.isArchived = false'); + } + + if ('archive' === $status) { + $qb->andWhere('e.isArchived = true'); + } + + if ('starred' === $status) { + $qb->andWhere('e.isStarred = true'); + } + + return $qb->andWhere('e.id >= :rand') + ->setParameter('rand', rand(0, $max)) + ->setMaxResults(1) + ->getQuery() + ->getSingleResult(); + } + /** * Return a query builder to be used by other getBuilderFor* method. * @@ -470,7 +509,6 @@ class EntryRepository extends EntityRepository */ private function sortQueryBuilder(QueryBuilder $qb, $sortBy = 'createdAt', $direction = 'desc') { - return $qb - ->orderBy(sprintf('e.%s', $sortBy), $direction); + return $qb->orderBy(sprintf('e.%s', $sortBy), $direction); } }