]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #1297 from wallabag/v2-estimated-time
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Pagerfanta\Adapter\DoctrineORMAdapter;
7 use Pagerfanta\Pagerfanta;
8
9 class EntryRepository extends EntityRepository
10 {
11 /**
12 * Retrieves unread entries for a user.
13 *
14 * @param int $userId
15 *
16 * @return QueryBuilder
17 */
18 public function findUnreadByUser($userId)
19 {
20 return $this->createQueryBuilder('e')
21 ->leftJoin('e.user', 'u')
22 ->where('e.isArchived = false')
23 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
24 ->orderBy('e.id', 'desc');
25 }
26
27 /**
28 * Retrieves read entries for a user.
29 *
30 * @param int $userId
31 *
32 * @return QueryBuilder
33 */
34 public function findArchiveByUser($userId)
35 {
36 return $this->createQueryBuilder('e')
37 ->leftJoin('e.user', 'u')
38 ->where('e.isArchived = true')
39 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
40 ->orderBy('e.id', 'desc');
41 }
42
43 /**
44 * Retrieves starred entries for a user.
45 *
46 * @param int $userId
47 *
48 * @return QueryBuilder
49 */
50 public function findStarredByUser($userId)
51 {
52 return $this->createQueryBuilder('e')
53 ->leftJoin('e.user', 'u')
54 ->where('e.isStarred = true')
55 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
56 ->orderBy('e.id', 'desc');
57 }
58
59 /**
60 * Find Entries.
61 *
62 * @param int $userId
63 * @param bool $isArchived
64 * @param bool $isStarred
65 * @param string $sort
66 * @param string $order
67 *
68 * @return array
69 */
70 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
71 {
72 $qb = $this->createQueryBuilder('e')
73 ->where('e.user =:userId')->setParameter('userId', $userId);
74
75 if (null !== $isArchived) {
76 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
77 }
78
79 if (null !== $isStarred) {
80 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
81 }
82
83 if ('created' === $sort) {
84 $qb->orderBy('e.id', $order);
85 } elseif ('updated' === $sort) {
86 $qb->orderBy('e.updatedAt', $order);
87 }
88
89 $pagerAdapter = new DoctrineORMAdapter($qb);
90
91 return new Pagerfanta($pagerAdapter);
92 }
93
94 /**
95 * Fetch an entry with a tag. Only used for tests.
96 *
97 * @return Entry
98 */
99 public function findOneWithTags($userId)
100 {
101 $qb = $this->createQueryBuilder('e')
102 ->innerJoin('e.tags', 't')
103 ->innerJoin('e.user', 'u')
104 ->addSelect('t', 'u')
105 ->where('e.user=:userId')->setParameter('userId', $userId)
106 ;
107
108 return $qb->getQuery()->getResult();
109 }
110 }