X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FRepository%2FEntryRepository.php;h=2286317c177097f53ff8cf389e1dd05a72dfc0a5;hb=02d17813a11d27e0232c38d1adf037cefdb176c1;hp=a8085ac9cbeb01bdc4c7bb6331ef1b72f6380765;hpb=092ca70725b0263390e45c46f93828c613eca3f0;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index a8085ac9..2286317c 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -3,89 +3,88 @@ namespace Wallabag\CoreBundle\Repository; use Doctrine\ORM\EntityRepository; -use Doctrine\ORM\Tools\Pagination\Paginator; +use Pagerfanta\Adapter\DoctrineORMAdapter; +use Pagerfanta\Pagerfanta; class EntryRepository extends EntityRepository { /** - * Retrieves unread entries for a user + * Return a query builder to used by other getBuilderFor* method. * * @param int $userId - * @param int $firstResult - * @param int $maxResults * - * @return Paginator + * @return QueryBuilder */ - public function findUnreadByUser($userId, $firstResult, $maxResults = 12) + private function getBuilderByUser($userId) { - $qb = $this->createQueryBuilder('e') - ->setFirstResult($firstResult) - ->setMaxResults($maxResults) + return $this->createQueryBuilder('e') ->leftJoin('e.user', 'u') - ->where('e.isArchived = false') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.createdAt', 'desc') - ->getQuery(); - - $paginator = new Paginator($qb); - - return $paginator; + ->andWhere('u.id = :userId')->setParameter('userId', $userId) + ->orderBy('e.id', 'desc') + ; } /** - * Retrieves read entries for a user + * Retrieves all entries for a user. * * @param int $userId - * @param int $firstResult - * @param int $maxResults * - * @return Paginator + * @return QueryBuilder */ - public function findArchiveByUser($userId, $firstResult, $maxResults = 12) + public function getBuilderForAllByUser($userId) { - $qb = $this->createQueryBuilder('e') - ->select('e') - ->setFirstResult($firstResult) - ->setMaxResults($maxResults) - ->leftJoin('e.user', 'u') - ->where('e.isArchived = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.createdAt', 'desc') - ->getQuery(); - - $paginator = new Paginator($qb); - - return $paginator; + return $this + ->getBuilderByUser($userId) + ; } /** - * Retrieves starred entries for a user + * Retrieves unread entries for a user. * * @param int $userId - * @param int $firstResult - * @param int $maxResults * - * @return Paginator + * @return QueryBuilder */ - public function findStarredByUser($userId, $firstResult, $maxResults = 12) + public function getBuilderForUnreadByUser($userId) { - $qb = $this->createQueryBuilder('e') - ->select('e') - ->setFirstResult($firstResult) - ->setMaxResults($maxResults) - ->leftJoin('e.user', 'u') - ->where('e.isStarred = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.createdAt', 'desc') - ->getQuery(); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = false') + ; + } - $paginator = new Paginator($qb); + /** + * Retrieves read entries for a user. + * + * @param int $userId + * + * @return QueryBuilder + */ + public function getBuilderForArchiveByUser($userId) + { + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = true') + ; + } - return $paginator; + /** + * Retrieves starred entries for a user. + * + * @param int $userId + * + * @return QueryBuilder + */ + public function getBuilderForStarredByUser($userId) + { + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isStarred = true') + ; } /** - * Find Entries + * Find Entries. * * @param int $userId * @param bool $isArchived @@ -109,14 +108,14 @@ class EntryRepository extends EntityRepository } if ('created' === $sort) { - $qb->orderBy('e.createdAt', $order); + $qb->orderBy('e.id', $order); } elseif ('updated' === $sort) { $qb->orderBy('e.updatedAt', $order); } - return $qb - ->getQuery() - ->getResult(); + $pagerAdapter = new DoctrineORMAdapter($qb); + + return new Pagerfanta($pagerAdapter); } /** @@ -128,13 +127,56 @@ class EntryRepository extends EntityRepository { $qb = $this->createQueryBuilder('e') ->innerJoin('e.tags', 't') - ->addSelect('t') - ->where('t.user=:userId')->setParameter('userId', 1); + ->innerJoin('e.user', 'u') + ->addSelect('t', 'u') + ->where('e.user=:userId')->setParameter('userId', $userId) + ; + + return $qb->getQuery()->getResult(); + } - return $qb->getQuery()->getOneOrNullResult(); + /** + * Find distinct language for a given user. + * Used to build the filter language list. + * + * @param int $userId User id + * + * @return array + */ + public function findDistinctLanguageByUser($userId) + { + $results = $this->createQueryBuilder('e') + ->select('e.language') + ->where('e.user = :userId')->setParameter('userId', $userId) + ->andWhere('e.language IS NOT NULL') + ->groupBy('e.language') + ->orderBy('e.language', ' ASC') + ->getQuery() + ->getResult(); - return $qb + $languages = array(); + foreach ($results as $result) { + $languages[$result['language']] = $result['language']; + } + + return $languages; + } + + /** + * Used only in test case to get the right entry associated to the right user + * + * @param string $username + * + * @return Entry + */ + public function findOneByUsernameAndNotArchived($username) + { + return $this->createQueryBuilder('e') + ->leftJoin('e.user', 'u') + ->where('u.username = :username')->setParameter('username', $username) + ->andWhere('e.isArchived = false') + ->setMaxResults(1) ->getQuery() - ->getOneOrNullResult(); + ->getSingleResult(); } }