]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Repository; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
6 | use Doctrine\ORM\Tools\Pagination\Paginator; | |
7 | ||
8 | class EntryRepository extends EntityRepository | |
9 | { | |
10 | /** | |
11 | * Retrieves unread entries for a user | |
12 | * | |
13 | * @param int $userId | |
14 | * @param int $firstResult | |
15 | * @param int $maxResults | |
16 | * | |
17 | * @return Paginator | |
18 | */ | |
19 | public function findUnreadByUser($userId, $firstResult, $maxResults = 12) | |
20 | { | |
21 | $qb = $this->createQueryBuilder('e') | |
22 | ->setFirstResult($firstResult) | |
23 | ->setMaxResults($maxResults) | |
24 | ->leftJoin('e.user', 'u') | |
25 | ->where('e.isArchived = false') | |
26 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) | |
27 | ->andWhere('e.isDeleted=false') | |
28 | ->orderBy('e.createdAt', 'desc') | |
29 | ->getQuery(); | |
30 | ||
31 | $paginator = new Paginator($qb); | |
32 | ||
33 | return $paginator; | |
34 | } | |
35 | ||
36 | /** | |
37 | * Retrieves read entries for a user | |
38 | * | |
39 | * @param int $userId | |
40 | * @param int $firstResult | |
41 | * @param int $maxResults | |
42 | * | |
43 | * @return Paginator | |
44 | */ | |
45 | public function findArchiveByUser($userId, $firstResult, $maxResults = 12) | |
46 | { | |
47 | $qb = $this->createQueryBuilder('e') | |
48 | ->select('e') | |
49 | ->setFirstResult($firstResult) | |
50 | ->setMaxResults($maxResults) | |
51 | ->leftJoin('e.user', 'u') | |
52 | ->where('e.isArchived = true') | |
53 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) | |
54 | ->andWhere('e.isDeleted=false') | |
55 | ->orderBy('e.createdAt', 'desc') | |
56 | ->getQuery(); | |
57 | ||
58 | $paginator = new Paginator($qb); | |
59 | ||
60 | return $paginator; | |
61 | } | |
62 | ||
63 | /** | |
64 | * Retrieves starred entries for a user | |
65 | * | |
66 | * @param int $userId | |
67 | * @param int $firstResult | |
68 | * @param int $maxResults | |
69 | * | |
70 | * @return Paginator | |
71 | */ | |
72 | public function findStarredByUser($userId, $firstResult, $maxResults = 12) | |
73 | { | |
74 | $qb = $this->createQueryBuilder('e') | |
75 | ->select('e') | |
76 | ->setFirstResult($firstResult) | |
77 | ->setMaxResults($maxResults) | |
78 | ->leftJoin('e.user', 'u') | |
79 | ->where('e.isStarred = true') | |
80 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) | |
81 | ->andWhere('e.isDeleted = false') | |
82 | ->orderBy('e.createdAt', 'desc') | |
83 | ->getQuery(); | |
84 | ||
85 | $paginator = new Paginator($qb); | |
86 | ||
87 | return $paginator; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Find Entries | |
92 | * | |
93 | * @param int $userId | |
94 | * @param bool $isArchived | |
95 | * @param bool $isStarred | |
96 | * @param bool $isDeleted | |
97 | * @param string $sort | |
98 | * @param string $order | |
99 | * | |
100 | * @return array | |
101 | */ | |
102 | public function findEntries($userId, $isArchived = null, $isStarred = null, $isDeleted = null, $sort = 'created', $order = 'ASC') | |
103 | { | |
104 | $qb = $this->createQueryBuilder('e') | |
105 | ->leftJoin('e.user', 'u') | |
106 | ->where('u.id =:userId')->setParameter('userId', $userId); | |
107 | ||
108 | if (null !== $isArchived) { | |
109 | $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); | |
110 | } | |
111 | ||
112 | if (null !== $isStarred) { | |
113 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | |
114 | } | |
115 | ||
116 | if (null !== $isDeleted) { | |
117 | $qb->andWhere('e.isDeleted =:isDeleted')->setParameter('isDeleted', (bool) $isDeleted); | |
118 | } | |
119 | ||
120 | if ('created' === $sort) { | |
121 | $qb->orderBy('e.createdAt', $order); | |
122 | } elseif ('updated' === $sort) { | |
123 | $qb->orderBy('e.updatedAt', $order); | |
124 | } | |
125 | ||
126 | return $qb | |
127 | ->getQuery() | |
128 | ->getResult(); | |
129 | } | |
130 | } |