]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #2176 from wallabag/add-since-parameter
[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 use Wallabag\CoreBundle\Entity\Tag;
9
10 class EntryRepository extends EntityRepository
11 {
12 /**
13 * Return a query builder to used by other getBuilderFor* method.
14 *
15 * @param int $userId
16 *
17 * @return QueryBuilder
18 */
19 private function getBuilderByUser($userId)
20 {
21 return $this->createQueryBuilder('e')
22 ->leftJoin('e.user', 'u')
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.id', 'desc')
25 ;
26 }
27
28 /**
29 * Retrieves all entries for a user.
30 *
31 * @param int $userId
32 *
33 * @return QueryBuilder
34 */
35 public function getBuilderForAllByUser($userId)
36 {
37 return $this
38 ->getBuilderByUser($userId)
39 ;
40 }
41
42 /**
43 * Retrieves unread entries for a user.
44 *
45 * @param int $userId
46 *
47 * @return QueryBuilder
48 */
49 public function getBuilderForUnreadByUser($userId)
50 {
51 return $this
52 ->getBuilderByUser($userId)
53 ->andWhere('e.isArchived = false')
54 ;
55 }
56
57 /**
58 * Retrieves read entries for a user.
59 *
60 * @param int $userId
61 *
62 * @return QueryBuilder
63 */
64 public function getBuilderForArchiveByUser($userId)
65 {
66 return $this
67 ->getBuilderByUser($userId)
68 ->andWhere('e.isArchived = true')
69 ;
70 }
71
72 /**
73 * Retrieves starred entries for a user.
74 *
75 * @param int $userId
76 *
77 * @return QueryBuilder
78 */
79 public function getBuilderForStarredByUser($userId)
80 {
81 return $this
82 ->getBuilderByUser($userId)
83 ->andWhere('e.isStarred = true')
84 ;
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', $since = 0)
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 ($since >= 0) {
112 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
113 }
114
115 if ('created' === $sort) {
116 $qb->orderBy('e.id', $order);
117 } elseif ('updated' === $sort) {
118 $qb->orderBy('e.updatedAt', $order);
119 }
120
121 $pagerAdapter = new DoctrineORMAdapter($qb);
122
123 return new Pagerfanta($pagerAdapter);
124 }
125
126 /**
127 * Fetch an entry with a tag. Only used for tests.
128 *
129 * @param int $userId
130 *
131 * @return Entry
132 */
133 public function findOneWithTags($userId)
134 {
135 $qb = $this->createQueryBuilder('e')
136 ->innerJoin('e.tags', 't')
137 ->innerJoin('e.user', 'u')
138 ->addSelect('t', 'u')
139 ->where('e.user=:userId')->setParameter('userId', $userId)
140 ;
141
142 return $qb->getQuery()->getResult();
143 }
144
145 /**
146 * Find distinct language for a given user.
147 * Used to build the filter language list.
148 *
149 * @param int $userId User id
150 *
151 * @return array
152 */
153 public function findDistinctLanguageByUser($userId)
154 {
155 $results = $this->createQueryBuilder('e')
156 ->select('e.language')
157 ->where('e.user = :userId')->setParameter('userId', $userId)
158 ->andWhere('e.language IS NOT NULL')
159 ->groupBy('e.language')
160 ->orderBy('e.language', ' ASC')
161 ->getQuery()
162 ->getResult();
163
164 $languages = [];
165 foreach ($results as $result) {
166 $languages[$result['language']] = $result['language'];
167 }
168
169 return $languages;
170 }
171
172 /**
173 * Used only in test case to get the right entry associated to the right user.
174 *
175 * @param string $username
176 *
177 * @return Entry
178 */
179 public function findOneByUsernameAndNotArchived($username)
180 {
181 return $this->createQueryBuilder('e')
182 ->leftJoin('e.user', 'u')
183 ->where('u.username = :username')->setParameter('username', $username)
184 ->andWhere('e.isArchived = false')
185 ->setMaxResults(1)
186 ->getQuery()
187 ->getSingleResult();
188 }
189
190 /**
191 * Remove a tag from all user entries.
192 *
193 * We need to loop on each entry attached to the given tag to remove it, since Doctrine doesn't know EntryTag entity because it's a ManyToMany relation.
194 * It could be faster with one query but I don't know how to retrieve the table name `entry_tag` which can have a prefix:
195 *
196 * DELETE et FROM entry_tag et WHERE et.entry_id IN ( SELECT e.id FROM entry e WHERE e.user_id = :userId ) AND et.tag_id = :tagId
197 *
198 * @param int $userId
199 * @param Tag $tag
200 */
201 public function removeTag($userId, Tag $tag)
202 {
203 $entries = $this->getBuilderByUser($userId)
204 ->innerJoin('e.tags', 't')
205 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
206 ->getQuery()
207 ->getResult();
208
209 foreach ($entries as $entry) {
210 $entry->removeTag($tag);
211 }
212
213 $this->getEntityManager()->flush();
214 }
215
216 /**
217 * Find all entries that are attached to a give tag id.
218 *
219 * @param int $userId
220 * @param int $tagId
221 *
222 * @return array
223 */
224 public function findAllByTagId($userId, $tagId)
225 {
226 return $this->getBuilderByUser($userId)
227 ->innerJoin('e.tags', 't')
228 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
229 ->getQuery()
230 ->getResult();
231 }
232
233 /**
234 * Find an entry by its url and its owner.
235 * If it exists, return the entry otherwise return false.
236 *
237 * @param $url
238 * @param $userId
239 *
240 * @return Entry|bool
241 */
242 public function findByUrlAndUserId($url, $userId)
243 {
244 $res = $this->createQueryBuilder('e')
245 ->where('e.url = :url')->setParameter('url', $url)
246 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
247 ->getQuery()
248 ->getResult();
249
250 if (count($res)) {
251 return current($res);
252 }
253
254 return false;
255 }
256
257 /**
258 * Count all entries for a user.
259 *
260 * @param int $userId
261 *
262 * @return int
263 */
264 public function countAllEntriesByUsername($userId)
265 {
266 $qb = $this->createQueryBuilder('e')
267 ->select('count(e)')
268 ->where('e.user=:userId')->setParameter('userId', $userId)
269 ;
270
271 return $qb->getQuery()->getSingleScalarResult();
272 }
273 }