]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Added translations and currentRoute parameter
[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')
3b815d2d 23 ->leftJoin('e.user', 'u')
0ab7404f 24 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
1e7b04d4 25 ->orderBy('e.createdAt', 'desc')
0ab7404f
JB
26 ;
27 }
28
2b7a4889
NL
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
0ab7404f
JB
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 ;
9d50517c 56 }
bd9f0815 57
b84a8055 58 /**
4346a860 59 * Retrieves read entries for a user.
b84a8055 60 *
3b815d2d 61 * @param int $userId
3b815d2d 62 *
26864574 63 * @return QueryBuilder
b84a8055 64 */
0ab7404f 65 public function getBuilderForArchiveByUser($userId)
bd9f0815 66 {
0ab7404f
JB
67 return $this
68 ->getBuilderByUser($userId)
69 ->andWhere('e.isArchived = true')
70 ;
bd9f0815
NL
71 }
72
b84a8055 73 /**
4346a860 74 * Retrieves starred entries for a user.
b84a8055 75 *
3b815d2d 76 * @param int $userId
3b815d2d 77 *
26864574 78 * @return QueryBuilder
b84a8055 79 */
0ab7404f 80 public function getBuilderForStarredByUser($userId)
bd9f0815 81 {
0ab7404f
JB
82 return $this
83 ->getBuilderByUser($userId)
84 ->andWhere('e.isStarred = true')
85 ;
bd9f0815 86 }
a8c90c5c 87
ee122a75
NL
88 /**
89 * Retrieves entries filtered with a search term for a user.
90 *
91 * @param int $userId
92 * @param string $term
49b042df 93 * @param strint $currentRoute
ee122a75
NL
94 *
95 * @return QueryBuilder
96 */
49b042df 97 public function getBuilderForSearchByUser($userId, $term, $currentRoute)
ee122a75 98 {
49b042df
NL
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.'%')
ee122a75
NL
112 ->leftJoin('e.tags', 't')
113 ->groupBy('e.id')
114 ->having('count(t.id) = 0');
49b042df
NL
115
116 return $qb;
ee122a75
NL
117 }
118
b6520f0b
NL
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
3b815d2d 135 /**
4346a860 136 * Find Entries.
3b815d2d 137 *
2a94b1d1
NL
138 * @param int $userId
139 * @param bool $isArchived
140 * @param bool $isStarred
2a94b1d1
NL
141 * @param string $sort
142 * @param string $order
8cb869ea
TC
143 * @param int $since
144 * @param string $tags
3b815d2d 145 *
017e2089 146 * @return array
3b815d2d 147 */
28803f10 148 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
a8c90c5c
NL
149 {
150 $qb = $this->createQueryBuilder('e')
28803f10 151 ->leftJoin('e.tags', 't')
0f006880 152 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 153
3b815d2d
J
154 if (null !== $isArchived) {
155 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
156 }
157
3b815d2d
J
158 if (null !== $isStarred) {
159 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
6e334aba
NL
160 }
161
c3f8b428 162 if ($since > 0) {
e5fb89e5 163 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
4f0558a0
TC
164 }
165
28803f10
TC
166 if ('' !== $tags) {
167 foreach (explode(',', $tags) as $tag) {
168 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
169 }
e5fb89e5
TC
170 }
171
bc782eaa 172 if ('created' === $sort) {
2385f891 173 $qb->orderBy('e.id', $order);
bc782eaa
NL
174 } elseif ('updated' === $sort) {
175 $qb->orderBy('e.updatedAt', $order);
176 }
177
bcf53ab7
WD
178 $pagerAdapter = new DoctrineORMAdapter($qb);
179
180 return new Pagerfanta($pagerAdapter);
a8c90c5c 181 }
46bbd8d3 182
a36737f4
NL
183 /**
184 * Fetch an entry with a tag. Only used for tests.
185 *
5c072d2b
NL
186 * @param int $userId
187 *
a36737f4
NL
188 * @return Entry
189 */
092ca707 190 public function findOneWithTags($userId)
46bbd8d3
NL
191 {
192 $qb = $this->createQueryBuilder('e')
193 ->innerJoin('e.tags', 't')
0ca374e6
NL
194 ->innerJoin('e.user', 'u')
195 ->addSelect('t', 'u')
196 ->where('e.user=:userId')->setParameter('userId', $userId)
197 ;
092ca707 198
0ca374e6 199 return $qb->getQuery()->getResult();
46bbd8d3 200 }
d4ebe5c5
JB
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
4094ea47 221 $languages = [];
d4ebe5c5
JB
222 foreach ($results as $result) {
223 $languages[$result['language']] = $result['language'];
224 }
225
226 return $languages;
227 }
159986c4 228
159986c4 229 /**
cfb28c9d 230 * Used only in test case to get the right entry associated to the right user.
159986c4 231 *
cfb28c9d 232 * @param string $username
159986c4
JB
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 }
fc732227
JB
246
247 /**
248 * Remove a tag from all user entries.
4059a061
JB
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.
6be97501
JB
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
fc732227
JB
254 *
255 * @param int $userId
256 * @param Tag $tag
257 */
258 public function removeTag($userId, Tag $tag)
259 {
4059a061
JB
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();
4059a061
JB
271 }
272
4da01f49 273 /**
9bf83f1f 274 * Remove tags from all user entries.
4da01f49 275 *
9bf83f1f 276 * @param int $userId
4da01f49
TC
277 * @param Array<Tag> $tags
278 */
9bf83f1f
TC
279 public function removeTags($userId, $tags)
280 {
4da01f49
TC
281 foreach ($tags as $tag) {
282 $this->removeTag($userId, $tag);
283 }
284 }
285
4059a061
JB
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();
fc732227 301 }
303768df
NL
302
303 /**
304 * Find an entry by its url and its owner.
5a4bbcc9 305 * If it exists, return the entry otherwise return false.
303768df
NL
306 *
307 * @param $url
308 * @param $userId
309 *
4094ea47 310 * @return Entry|bool
303768df 311 */
78833672 312 public function findByUrlAndUserId($url, $userId)
303768df 313 {
5a4bbcc9 314 $res = $this->createQueryBuilder('e')
19ca0b2f 315 ->where('e.url = :url')->setParameter('url', urldecode($url))
303768df
NL
316 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
317 ->getQuery()
318 ->getResult();
5a4bbcc9 319
b4b592a0
JB
320 if (count($res)) {
321 return current($res);
5a4bbcc9
JB
322 }
323
324 return false;
303768df 325 }
5c072d2b
NL
326
327 /**
328 * Count all entries for a user.
329 *
330 * @param int $userId
331 *
e678c475 332 * @return int
5c072d2b
NL
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 }
28bb4890
JB
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 }
191564b7
JB
363
364 /**
365 * Remove all entries for a user id.
8c61fd12 366 * Used when a user want to reset all informations.
191564b7 367 *
8c61fd12 368 * @param int $userId
191564b7
JB
369 */
370 public function removeAllByUserId($userId)
371 {
372 $this->getEntityManager()
b0de88f7
JB
373 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
374 ->setParameter('userId', $userId)
191564b7
JB
375 ->execute();
376 }
9d50517c 377}