]>
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 | ->orderBy('e.createdAt', 'desc') | |
28 | ->getQuery(); | |
29 | ||
30 | $paginator = new Paginator($qb); | |
31 | ||
32 | return $paginator; | |
33 | } | |
34 | ||
35 | /** | |
36 | * Retrieves read entries for a user | |
37 | * | |
38 | * @param int $userId | |
39 | * @param int $firstResult | |
40 | * @param int $maxResults | |
41 | * | |
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 | ->leftJoin('e.user', 'u') | |
51 | ->where('e.isArchived = true') | |
52 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) | |
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 int $userId | |
65 | * @param int $firstResult | |
66 | * @param int $maxResults | |
67 | * | |
68 | * @return Paginator | |
69 | */ | |
70 | public function findStarredByUser($userId, $firstResult, $maxResults = 12) | |
71 | { | |
72 | $qb = $this->createQueryBuilder('e') | |
73 | ->select('e') | |
74 | ->setFirstResult($firstResult) | |
75 | ->setMaxResults($maxResults) | |
76 | ->leftJoin('e.user', 'u') | |
77 | ->where('e.isStarred = true') | |
78 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) | |
79 | ->orderBy('e.createdAt', 'desc') | |
80 | ->getQuery(); | |
81 | ||
82 | $paginator = new Paginator($qb); | |
83 | ||
84 | return $paginator; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Find Entries | |
89 | * | |
90 | * @param int $userId | |
91 | * @param bool $isArchived | |
92 | * @param bool $isStarred | |
93 | * @param string $sort | |
94 | * @param string $order | |
95 | * | |
96 | * @return array | |
97 | */ | |
98 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC') | |
99 | { | |
100 | $qb = $this->createQueryBuilder('e') | |
101 | ->where('e.user =:userId')->setParameter('userId', $userId); | |
102 | ||
103 | if (null !== $isArchived) { | |
104 | $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); | |
105 | } | |
106 | ||
107 | if (null !== $isStarred) { | |
108 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | |
109 | } | |
110 | ||
111 | if ('created' === $sort) { | |
112 | $qb->orderBy('e.createdAt', $order); | |
113 | } elseif ('updated' === $sort) { | |
114 | $qb->orderBy('e.updatedAt', $order); | |
115 | } | |
116 | ||
117 | return $qb | |
118 | ->getQuery() | |
119 | ->getResult(); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Fetch an entry with a tag. Only used for tests. | |
124 | * | |
125 | * @return Entry | |
126 | */ | |
127 | public function findOneWithTags($userId) | |
128 | { | |
129 | $qb = $this->createQueryBuilder('e') | |
130 | ->innerJoin('e.tags', 't') | |
131 | ->addSelect('t') | |
132 | ->where('t.user=:userId')->setParameter('userId', 1); | |
133 | ||
134 | return $qb->getQuery()->getOneOrNullResult(); | |
135 | } | |
136 | } |