]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #1250 from frankoa/v2_pagination_and_api
[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 Pagerfanta
17 */
18 public function findUnreadByUser($userId)
19 {
20 $qb = $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 ->getQuery();
26
27 $pagerAdapter = new DoctrineORMAdapter($qb);
28
29 return new Pagerfanta($pagerAdapter);
30 }
31
32 /**
33 * Retrieves read entries for a user.
34 *
35 * @param int $userId
36 *
37 * @return Pagerfanta
38 */
39 public function findArchiveByUser($userId)
40 {
41 $qb = $this->createQueryBuilder('e')
42 ->select('e')
43 ->leftJoin('e.user', 'u')
44 ->where('e.isArchived = true')
45 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
46 ->orderBy('e.id', 'desc')
47 ->getQuery();
48
49 $pagerAdapter = new DoctrineORMAdapter($qb);
50
51 return new Pagerfanta($pagerAdapter);
52 }
53
54 /**
55 * Retrieves starred entries for a user.
56 *
57 * @param int $userId
58 *
59 * @return Pagerfanta
60 */
61 public function findStarredByUser($userId)
62 {
63
64 $qb = $this->createQueryBuilder('e')
65 ->select('e')
66 ->leftJoin('e.user', 'u')
67 ->where('e.isStarred = true')
68 ->andWhere('u.id =:userId')->setParameter('userId', $userId)
69 ->orderBy('e.id', 'desc')
70 ->getQuery();
71
72 $pagerAdapter = new DoctrineORMAdapter($qb);
73
74 return new Pagerfanta($pagerAdapter);
75 }
76
77 /**
78 * Find Entries.
79 *
80 * @param int $userId
81 * @param bool $isArchived
82 * @param bool $isStarred
83 * @param string $sort
84 * @param string $order
85 *
86 * @return array
87 */
88 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
89 {
90 $qb = $this->createQueryBuilder('e')
91 ->where('e.user =:userId')->setParameter('userId', $userId);
92
93 if (null !== $isArchived) {
94 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
95 }
96
97 if (null !== $isStarred) {
98 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
99 }
100
101 if ('created' === $sort) {
102 $qb->orderBy('e.id', $order);
103 } elseif ('updated' === $sort) {
104 $qb->orderBy('e.updatedAt', $order);
105 }
106
107 $pagerAdapter = new DoctrineORMAdapter($qb);
108
109 return new Pagerfanta($pagerAdapter);
110 }
111
112 /**
113 * Fetch an entry with a tag. Only used for tests.
114 *
115 * @return Entry
116 */
117 public function findOneWithTags($userId)
118 {
119 $qb = $this->createQueryBuilder('e')
120 ->innerJoin('e.tags', 't')
121 ->innerJoin('e.user', 'u')
122 ->addSelect('t', 'u')
123 ->where('e.user=:userId')->setParameter('userId', $userId)
124 ;
125
126 return $qb->getQuery()->getResult();
127 }
128 }