]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #2879 from matteocoder/matteocoder-patch-1
[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)
9deac0c5 128 ->andWhere('size(e.tags) = 0');
b6520f0b
NL
129 }
130
3b815d2d 131 /**
4346a860 132 * Find Entries.
3b815d2d 133 *
2a94b1d1
NL
134 * @param int $userId
135 * @param bool $isArchived
136 * @param bool $isStarred
2a94b1d1
NL
137 * @param string $sort
138 * @param string $order
8cb869ea
TC
139 * @param int $since
140 * @param string $tags
3b815d2d 141 *
017e2089 142 * @return array
3b815d2d 143 */
28803f10 144 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
a8c90c5c
NL
145 {
146 $qb = $this->createQueryBuilder('e')
28803f10 147 ->leftJoin('e.tags', 't')
0f006880 148 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 149
3b815d2d
J
150 if (null !== $isArchived) {
151 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
152 }
153
3b815d2d
J
154 if (null !== $isStarred) {
155 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
6e334aba
NL
156 }
157
c3f8b428 158 if ($since > 0) {
e5fb89e5 159 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
4f0558a0
TC
160 }
161
28803f10
TC
162 if ('' !== $tags) {
163 foreach (explode(',', $tags) as $tag) {
164 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
165 }
e5fb89e5
TC
166 }
167
bc782eaa 168 if ('created' === $sort) {
2385f891 169 $qb->orderBy('e.id', $order);
bc782eaa
NL
170 } elseif ('updated' === $sort) {
171 $qb->orderBy('e.updatedAt', $order);
172 }
173
5a5da369 174 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
bcf53ab7
WD
175
176 return new Pagerfanta($pagerAdapter);
a8c90c5c 177 }
46bbd8d3 178
a36737f4
NL
179 /**
180 * Fetch an entry with a tag. Only used for tests.
181 *
5c072d2b
NL
182 * @param int $userId
183 *
a36737f4
NL
184 * @return Entry
185 */
092ca707 186 public function findOneWithTags($userId)
46bbd8d3
NL
187 {
188 $qb = $this->createQueryBuilder('e')
189 ->innerJoin('e.tags', 't')
0ca374e6
NL
190 ->innerJoin('e.user', 'u')
191 ->addSelect('t', 'u')
192 ->where('e.user=:userId')->setParameter('userId', $userId)
193 ;
092ca707 194
0ca374e6 195 return $qb->getQuery()->getResult();
46bbd8d3 196 }
d4ebe5c5
JB
197
198 /**
199 * Find distinct language for a given user.
200 * Used to build the filter language list.
201 *
202 * @param int $userId User id
203 *
204 * @return array
205 */
206 public function findDistinctLanguageByUser($userId)
207 {
208 $results = $this->createQueryBuilder('e')
209 ->select('e.language')
210 ->where('e.user = :userId')->setParameter('userId', $userId)
211 ->andWhere('e.language IS NOT NULL')
212 ->groupBy('e.language')
213 ->orderBy('e.language', ' ASC')
214 ->getQuery()
215 ->getResult();
216
4094ea47 217 $languages = [];
d4ebe5c5
JB
218 foreach ($results as $result) {
219 $languages[$result['language']] = $result['language'];
220 }
221
222 return $languages;
223 }
159986c4 224
159986c4 225 /**
cfb28c9d 226 * Used only in test case to get the right entry associated to the right user.
159986c4 227 *
cfb28c9d 228 * @param string $username
159986c4
JB
229 *
230 * @return Entry
231 */
232 public function findOneByUsernameAndNotArchived($username)
233 {
234 return $this->createQueryBuilder('e')
235 ->leftJoin('e.user', 'u')
236 ->where('u.username = :username')->setParameter('username', $username)
237 ->andWhere('e.isArchived = false')
238 ->setMaxResults(1)
239 ->getQuery()
240 ->getSingleResult();
241 }
fc732227
JB
242
243 /**
244 * Remove a tag from all user entries.
4059a061
JB
245 *
246 * 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
247 * 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:
248 *
249 * 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
250 *
251 * @param int $userId
252 * @param Tag $tag
253 */
254 public function removeTag($userId, Tag $tag)
255 {
4059a061
JB
256 $entries = $this->getBuilderByUser($userId)
257 ->innerJoin('e.tags', 't')
258 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
259 ->getQuery()
260 ->getResult();
261
262 foreach ($entries as $entry) {
263 $entry->removeTag($tag);
264 }
265
266 $this->getEntityManager()->flush();
4059a061
JB
267 }
268
4da01f49 269 /**
9bf83f1f 270 * Remove tags from all user entries.
4da01f49 271 *
9bf83f1f 272 * @param int $userId
4da01f49
TC
273 * @param Array<Tag> $tags
274 */
9bf83f1f
TC
275 public function removeTags($userId, $tags)
276 {
4da01f49
TC
277 foreach ($tags as $tag) {
278 $this->removeTag($userId, $tag);
279 }
280 }
281
4059a061
JB
282 /**
283 * Find all entries that are attached to a give tag id.
284 *
285 * @param int $userId
286 * @param int $tagId
287 *
288 * @return array
289 */
290 public function findAllByTagId($userId, $tagId)
291 {
292 return $this->getBuilderByUser($userId)
293 ->innerJoin('e.tags', 't')
294 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
295 ->getQuery()
296 ->getResult();
fc732227 297 }
303768df
NL
298
299 /**
300 * Find an entry by its url and its owner.
5a4bbcc9 301 * If it exists, return the entry otherwise return false.
303768df
NL
302 *
303 * @param $url
304 * @param $userId
305 *
4094ea47 306 * @return Entry|bool
303768df 307 */
78833672 308 public function findByUrlAndUserId($url, $userId)
303768df 309 {
5a4bbcc9 310 $res = $this->createQueryBuilder('e')
19ca0b2f 311 ->where('e.url = :url')->setParameter('url', urldecode($url))
303768df
NL
312 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
313 ->getQuery()
314 ->getResult();
5a4bbcc9 315
b4b592a0
JB
316 if (count($res)) {
317 return current($res);
5a4bbcc9
JB
318 }
319
320 return false;
303768df 321 }
5c072d2b
NL
322
323 /**
324 * Count all entries for a user.
325 *
326 * @param int $userId
327 *
e678c475 328 * @return int
5c072d2b
NL
329 */
330 public function countAllEntriesByUsername($userId)
331 {
332 $qb = $this->createQueryBuilder('e')
333 ->select('count(e)')
334 ->where('e.user=:userId')->setParameter('userId', $userId)
335 ;
336
337 return $qb->getQuery()->getSingleScalarResult();
338 }
28bb4890
JB
339
340 /**
341 * Count all entries for a tag and a user.
342 *
343 * @param int $userId
344 * @param int $tagId
345 *
346 * @return int
347 */
348 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
349 {
350 $qb = $this->createQueryBuilder('e')
351 ->select('count(e.id)')
352 ->leftJoin('e.tags', 't')
353 ->where('e.user=:userId')->setParameter('userId', $userId)
354 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
355 ;
356
357 return $qb->getQuery()->getSingleScalarResult();
358 }
191564b7
JB
359
360 /**
361 * Remove all entries for a user id.
8c61fd12 362 * Used when a user want to reset all informations.
191564b7 363 *
8c61fd12 364 * @param int $userId
191564b7
JB
365 */
366 public function removeAllByUserId($userId)
367 {
368 $this->getEntityManager()
b0de88f7
JB
369 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
370 ->setParameter('userId', $userId)
191564b7
JB
371 ->execute();
372 }
9d50517c 373}