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