X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FEntryRepository.php;h=6107e19ec65244f32efe322cfc51e2f533ae3795;hb=f85d220c19872dd7e9199fd150060555584a2886;hp=34123eea699bff74a0ea1e7b126fd390a8b0dbf0;hpb=b8115ff46b15a28021612b695de8785456f8b7a6;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index 34123eea..6107e19e 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -50,7 +50,7 @@ class EntryRepository extends EntityRepository public function getBuilderForArchiveByUser($userId) { return $this - ->getSortedQueryBuilderByUser($userId) + ->getSortedQueryBuilderByUser($userId, 'archivedAt', 'desc') ->andWhere('e.isArchived = true') ; } @@ -110,8 +110,7 @@ class EntryRepository extends EntityRepository */ public function getBuilderForUntaggedByUser($userId) { - return $this - ->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); + return $this->sortQueryBuilder($this->getRawBuilderForUntaggedByUser($userId)); } /** @@ -142,7 +141,7 @@ class EntryRepository extends EntityRepository * * @return Pagerfanta */ - public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '') + public function findEntries($userId, $isArchived = null, $isStarred = null, $isPublic = null, $sort = 'created', $order = 'asc', $since = 0, $tags = '') { $qb = $this->createQueryBuilder('e') ->leftJoin('e.tags', 't') @@ -185,10 +184,16 @@ class EntryRepository extends EntityRepository } } + if (!\in_array(strtolower($order), ['asc', 'desc'], true)) { + throw new \Exception('Order "' . $order . '" parameter is wrong, allowed: asc or desc'); + } + if ('created' === $sort) { $qb->orderBy('e.id', $order); } elseif ('updated' === $sort) { $qb->orderBy('e.updatedAt', $order); + } elseif ('archived' === $sort) { + $qb->orderBy('e.archivedAt', $order); } $pagerAdapter = new DoctrineORMAdapter($qb, true, false); @@ -426,6 +431,51 @@ 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'); + } + + if ('untagged' === $status) { + $qb->leftJoin('e.tags', 't'); + $qb->andWhere('t.id is null'); + } + + 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. * @@ -450,7 +500,7 @@ class EntryRepository extends EntityRepository */ private function getSortedQueryBuilderByUser($userId, $sortBy = 'createdAt', $direction = 'desc') { - return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId)); + return $this->sortQueryBuilder($this->getQueryBuilderByUser($userId), $sortBy, $direction); } /** @@ -464,7 +514,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); } }