]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Added translations and currentRoute parameter
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
... / ...
CommitLineData
1<?php
2
3namespace Wallabag\CoreBundle\Repository;
4
5use Doctrine\ORM\EntityRepository;
6use Doctrine\ORM\Query;
7use Pagerfanta\Adapter\DoctrineORMAdapter;
8use Pagerfanta\Pagerfanta;
9use Wallabag\CoreBundle\Entity\Tag;
10
11class EntryRepository extends EntityRepository
12{
13 /**
14 * Return a query builder to used by other getBuilderFor* method.
15 *
16 * @param int $userId
17 *
18 * @return QueryBuilder
19 */
20 private function getBuilderByUser($userId)
21 {
22 return $this->createQueryBuilder('e')
23 ->leftJoin('e.user', 'u')
24 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
25 ->orderBy('e.createdAt', 'desc')
26 ;
27 }
28
29 /**
30 * Retrieves all entries for a user.
31 *
32 * @param int $userId
33 *
34 * @return QueryBuilder
35 */
36 public function getBuilderForAllByUser($userId)
37 {
38 return $this
39 ->getBuilderByUser($userId)
40 ;
41 }
42
43 /**
44 * Retrieves unread entries for a user.
45 *
46 * @param int $userId
47 *
48 * @return QueryBuilder
49 */
50 public function getBuilderForUnreadByUser($userId)
51 {
52 return $this
53 ->getBuilderByUser($userId)
54 ->andWhere('e.isArchived = false')
55 ;
56 }
57
58 /**
59 * Retrieves read entries for a user.
60 *
61 * @param int $userId
62 *
63 * @return QueryBuilder
64 */
65 public function getBuilderForArchiveByUser($userId)
66 {
67 return $this
68 ->getBuilderByUser($userId)
69 ->andWhere('e.isArchived = true')
70 ;
71 }
72
73 /**
74 * Retrieves starred entries for a user.
75 *
76 * @param int $userId
77 *
78 * @return QueryBuilder
79 */
80 public function getBuilderForStarredByUser($userId)
81 {
82 return $this
83 ->getBuilderByUser($userId)
84 ->andWhere('e.isStarred = true')
85 ;
86 }
87
88 /**
89 * Retrieves entries filtered with a search term for a user.
90 *
91 * @param int $userId
92 * @param string $term
93 * @param strint $currentRoute
94 *
95 * @return QueryBuilder
96 */
97 public function getBuilderForSearchByUser($userId, $term, $currentRoute)
98 {
99 $qb = $this
100 ->getBuilderByUser($userId);
101
102 if ('starred' === $currentRoute) {
103 $qb->andWhere('e.isStarred = true');
104 } elseif ('unread' === $currentRoute) {
105 $qb->andWhere('e.isArchived = false');
106 } elseif ('archive' === $currentRoute) {
107 $qb->andWhere('e.isArchived = true');
108 }
109
110 $qb
111 ->andWhere('e.content LIKE :term OR e.title LIKE :term')->setParameter('term', '%'.$term.'%')
112 ->leftJoin('e.tags', 't')
113 ->groupBy('e.id')
114 ->having('count(t.id) = 0');
115
116 return $qb;
117 }
118
119 /**
120 * Retrieves untagged entries for a user.
121 *
122 * @param int $userId
123 *
124 * @return QueryBuilder
125 */
126 public function getBuilderForUntaggedByUser($userId)
127 {
128 return $this
129 ->getBuilderByUser($userId)
130 ->leftJoin('e.tags', 't')
131 ->groupBy('e.id')
132 ->having('count(t.id) = 0');
133 }
134
135 /**
136 * Find Entries.
137 *
138 * @param int $userId
139 * @param bool $isArchived
140 * @param bool $isStarred
141 * @param string $sort
142 * @param string $order
143 * @param int $since
144 * @param string $tags
145 *
146 * @return array
147 */
148 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
149 {
150 $qb = $this->createQueryBuilder('e')
151 ->leftJoin('e.tags', 't')
152 ->where('e.user =:userId')->setParameter('userId', $userId);
153
154 if (null !== $isArchived) {
155 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
156 }
157
158 if (null !== $isStarred) {
159 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
160 }
161
162 if ($since > 0) {
163 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
164 }
165
166 if ('' !== $tags) {
167 foreach (explode(',', $tags) as $tag) {
168 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
169 }
170 }
171
172 if ('created' === $sort) {
173 $qb->orderBy('e.id', $order);
174 } elseif ('updated' === $sort) {
175 $qb->orderBy('e.updatedAt', $order);
176 }
177
178 $pagerAdapter = new DoctrineORMAdapter($qb);
179
180 return new Pagerfanta($pagerAdapter);
181 }
182
183 /**
184 * Fetch an entry with a tag. Only used for tests.
185 *
186 * @param int $userId
187 *
188 * @return Entry
189 */
190 public function findOneWithTags($userId)
191 {
192 $qb = $this->createQueryBuilder('e')
193 ->innerJoin('e.tags', 't')
194 ->innerJoin('e.user', 'u')
195 ->addSelect('t', 'u')
196 ->where('e.user=:userId')->setParameter('userId', $userId)
197 ;
198
199 return $qb->getQuery()->getResult();
200 }
201
202 /**
203 * Find distinct language for a given user.
204 * Used to build the filter language list.
205 *
206 * @param int $userId User id
207 *
208 * @return array
209 */
210 public function findDistinctLanguageByUser($userId)
211 {
212 $results = $this->createQueryBuilder('e')
213 ->select('e.language')
214 ->where('e.user = :userId')->setParameter('userId', $userId)
215 ->andWhere('e.language IS NOT NULL')
216 ->groupBy('e.language')
217 ->orderBy('e.language', ' ASC')
218 ->getQuery()
219 ->getResult();
220
221 $languages = [];
222 foreach ($results as $result) {
223 $languages[$result['language']] = $result['language'];
224 }
225
226 return $languages;
227 }
228
229 /**
230 * Used only in test case to get the right entry associated to the right user.
231 *
232 * @param string $username
233 *
234 * @return Entry
235 */
236 public function findOneByUsernameAndNotArchived($username)
237 {
238 return $this->createQueryBuilder('e')
239 ->leftJoin('e.user', 'u')
240 ->where('u.username = :username')->setParameter('username', $username)
241 ->andWhere('e.isArchived = false')
242 ->setMaxResults(1)
243 ->getQuery()
244 ->getSingleResult();
245 }
246
247 /**
248 * Remove a tag from all user entries.
249 *
250 * 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.
251 * 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:
252 *
253 * 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
254 *
255 * @param int $userId
256 * @param Tag $tag
257 */
258 public function removeTag($userId, Tag $tag)
259 {
260 $entries = $this->getBuilderByUser($userId)
261 ->innerJoin('e.tags', 't')
262 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
263 ->getQuery()
264 ->getResult();
265
266 foreach ($entries as $entry) {
267 $entry->removeTag($tag);
268 }
269
270 $this->getEntityManager()->flush();
271 }
272
273 /**
274 * Remove tags from all user entries.
275 *
276 * @param int $userId
277 * @param Array<Tag> $tags
278 */
279 public function removeTags($userId, $tags)
280 {
281 foreach ($tags as $tag) {
282 $this->removeTag($userId, $tag);
283 }
284 }
285
286 /**
287 * Find all entries that are attached to a give tag id.
288 *
289 * @param int $userId
290 * @param int $tagId
291 *
292 * @return array
293 */
294 public function findAllByTagId($userId, $tagId)
295 {
296 return $this->getBuilderByUser($userId)
297 ->innerJoin('e.tags', 't')
298 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
299 ->getQuery()
300 ->getResult();
301 }
302
303 /**
304 * Find an entry by its url and its owner.
305 * If it exists, return the entry otherwise return false.
306 *
307 * @param $url
308 * @param $userId
309 *
310 * @return Entry|bool
311 */
312 public function findByUrlAndUserId($url, $userId)
313 {
314 $res = $this->createQueryBuilder('e')
315 ->where('e.url = :url')->setParameter('url', urldecode($url))
316 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
317 ->getQuery()
318 ->getResult();
319
320 if (count($res)) {
321 return current($res);
322 }
323
324 return false;
325 }
326
327 /**
328 * Count all entries for a user.
329 *
330 * @param int $userId
331 *
332 * @return int
333 */
334 public function countAllEntriesByUsername($userId)
335 {
336 $qb = $this->createQueryBuilder('e')
337 ->select('count(e)')
338 ->where('e.user=:userId')->setParameter('userId', $userId)
339 ;
340
341 return $qb->getQuery()->getSingleScalarResult();
342 }
343
344 /**
345 * Count all entries for a tag and a user.
346 *
347 * @param int $userId
348 * @param int $tagId
349 *
350 * @return int
351 */
352 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
353 {
354 $qb = $this->createQueryBuilder('e')
355 ->select('count(e.id)')
356 ->leftJoin('e.tags', 't')
357 ->where('e.user=:userId')->setParameter('userId', $userId)
358 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
359 ;
360
361 return $qb->getQuery()->getSingleScalarResult();
362 }
363
364 /**
365 * Remove all entries for a user id.
366 * Used when a user want to reset all informations.
367 *
368 * @param int $userId
369 */
370 public function removeAllByUserId($userId)
371 {
372 $this->getEntityManager()
373 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
374 ->setParameter('userId', $userId)
375 ->execute();
376 }
377}