]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Use created_at as default sort
[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)
ce119524 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
b6520f0b
NL
88 /**
89 * Retrieves untagged entries for a user.
90 *
91 * @param int $userId
92 *
93 * @return QueryBuilder
94 */
95 public function getBuilderForUntaggedByUser($userId)
96 {
97 return $this
98 ->getBuilderByUser($userId)
99 ->leftJoin('e.tags', 't')
100 ->groupBy('e.id')
101 ->having('count(t.id) = 0');
102 }
103
3b815d2d 104 /**
4346a860 105 * Find Entries.
3b815d2d 106 *
2a94b1d1
NL
107 * @param int $userId
108 * @param bool $isArchived
109 * @param bool $isStarred
2a94b1d1
NL
110 * @param string $sort
111 * @param string $order
8cb869ea
TC
112 * @param int $since
113 * @param string $tags
3b815d2d 114 *
017e2089 115 * @return array
3b815d2d 116 */
28803f10 117 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
a8c90c5c
NL
118 {
119 $qb = $this->createQueryBuilder('e')
28803f10 120 ->leftJoin('e.tags', 't')
0f006880 121 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 122
3b815d2d
J
123 if (null !== $isArchived) {
124 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
125 }
126
3b815d2d
J
127 if (null !== $isStarred) {
128 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
6e334aba
NL
129 }
130
c3f8b428 131 if ($since > 0) {
e5fb89e5 132 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
4f0558a0
TC
133 }
134
28803f10
TC
135 if ('' !== $tags) {
136 foreach (explode(',', $tags) as $tag) {
137 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
138 }
e5fb89e5
TC
139 }
140
bc782eaa 141 if ('created' === $sort) {
2385f891 142 $qb->orderBy('e.id', $order);
bc782eaa
NL
143 } elseif ('updated' === $sort) {
144 $qb->orderBy('e.updatedAt', $order);
145 }
146
bcf53ab7
WD
147 $pagerAdapter = new DoctrineORMAdapter($qb);
148
149 return new Pagerfanta($pagerAdapter);
a8c90c5c 150 }
46bbd8d3 151
a36737f4
NL
152 /**
153 * Fetch an entry with a tag. Only used for tests.
154 *
5c072d2b
NL
155 * @param int $userId
156 *
a36737f4
NL
157 * @return Entry
158 */
092ca707 159 public function findOneWithTags($userId)
46bbd8d3
NL
160 {
161 $qb = $this->createQueryBuilder('e')
162 ->innerJoin('e.tags', 't')
0ca374e6
NL
163 ->innerJoin('e.user', 'u')
164 ->addSelect('t', 'u')
165 ->where('e.user=:userId')->setParameter('userId', $userId)
166 ;
092ca707 167
0ca374e6 168 return $qb->getQuery()->getResult();
46bbd8d3 169 }
d4ebe5c5
JB
170
171 /**
172 * Find distinct language for a given user.
173 * Used to build the filter language list.
174 *
175 * @param int $userId User id
176 *
177 * @return array
178 */
179 public function findDistinctLanguageByUser($userId)
180 {
181 $results = $this->createQueryBuilder('e')
182 ->select('e.language')
183 ->where('e.user = :userId')->setParameter('userId', $userId)
184 ->andWhere('e.language IS NOT NULL')
185 ->groupBy('e.language')
186 ->orderBy('e.language', ' ASC')
187 ->getQuery()
188 ->getResult();
189
4094ea47 190 $languages = [];
d4ebe5c5
JB
191 foreach ($results as $result) {
192 $languages[$result['language']] = $result['language'];
193 }
194
195 return $languages;
196 }
159986c4 197
159986c4 198 /**
cfb28c9d 199 * Used only in test case to get the right entry associated to the right user.
159986c4 200 *
cfb28c9d 201 * @param string $username
159986c4
JB
202 *
203 * @return Entry
204 */
205 public function findOneByUsernameAndNotArchived($username)
206 {
207 return $this->createQueryBuilder('e')
208 ->leftJoin('e.user', 'u')
209 ->where('u.username = :username')->setParameter('username', $username)
210 ->andWhere('e.isArchived = false')
211 ->setMaxResults(1)
212 ->getQuery()
213 ->getSingleResult();
214 }
fc732227
JB
215
216 /**
217 * Remove a tag from all user entries.
4059a061
JB
218 *
219 * 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
220 * 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:
221 *
222 * 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
223 *
224 * @param int $userId
225 * @param Tag $tag
226 */
227 public function removeTag($userId, Tag $tag)
228 {
4059a061
JB
229 $entries = $this->getBuilderByUser($userId)
230 ->innerJoin('e.tags', 't')
231 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
232 ->getQuery()
233 ->getResult();
234
235 foreach ($entries as $entry) {
236 $entry->removeTag($tag);
237 }
238
239 $this->getEntityManager()->flush();
4059a061
JB
240 }
241
4da01f49 242 /**
9bf83f1f 243 * Remove tags from all user entries.
4da01f49 244 *
9bf83f1f 245 * @param int $userId
4da01f49
TC
246 * @param Array<Tag> $tags
247 */
9bf83f1f
TC
248 public function removeTags($userId, $tags)
249 {
4da01f49
TC
250 foreach ($tags as $tag) {
251 $this->removeTag($userId, $tag);
252 }
253 }
254
4059a061
JB
255 /**
256 * Find all entries that are attached to a give tag id.
257 *
258 * @param int $userId
259 * @param int $tagId
260 *
261 * @return array
262 */
263 public function findAllByTagId($userId, $tagId)
264 {
265 return $this->getBuilderByUser($userId)
266 ->innerJoin('e.tags', 't')
267 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
268 ->getQuery()
269 ->getResult();
fc732227 270 }
303768df
NL
271
272 /**
273 * Find an entry by its url and its owner.
5a4bbcc9 274 * If it exists, return the entry otherwise return false.
303768df
NL
275 *
276 * @param $url
277 * @param $userId
278 *
4094ea47 279 * @return Entry|bool
303768df 280 */
78833672 281 public function findByUrlAndUserId($url, $userId)
303768df 282 {
5a4bbcc9 283 $res = $this->createQueryBuilder('e')
19ca0b2f 284 ->where('e.url = :url')->setParameter('url', urldecode($url))
303768df
NL
285 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
286 ->getQuery()
287 ->getResult();
5a4bbcc9 288
b4b592a0
JB
289 if (count($res)) {
290 return current($res);
5a4bbcc9
JB
291 }
292
293 return false;
303768df 294 }
5c072d2b
NL
295
296 /**
297 * Count all entries for a user.
298 *
299 * @param int $userId
300 *
e678c475 301 * @return int
5c072d2b
NL
302 */
303 public function countAllEntriesByUsername($userId)
304 {
305 $qb = $this->createQueryBuilder('e')
306 ->select('count(e)')
307 ->where('e.user=:userId')->setParameter('userId', $userId)
308 ;
309
310 return $qb->getQuery()->getSingleScalarResult();
311 }
28bb4890
JB
312
313 /**
314 * Count all entries for a tag and a user.
315 *
316 * @param int $userId
317 * @param int $tagId
318 *
319 * @return int
320 */
321 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
322 {
323 $qb = $this->createQueryBuilder('e')
324 ->select('count(e.id)')
325 ->leftJoin('e.tags', 't')
326 ->where('e.user=:userId')->setParameter('userId', $userId)
327 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
328 ;
329
330 return $qb->getQuery()->getSingleScalarResult();
331 }
9d50517c 332}