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