]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
add some missing phpdoc parameters
[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;
bcf53ab7
WD
6use Pagerfanta\Adapter\DoctrineORMAdapter;
7use Pagerfanta\Pagerfanta;
fc732227 8use Wallabag\CoreBundle\Entity\Tag;
9d50517c 9
be463487 10class EntryRepository extends EntityRepository
9d50517c 11{
b84a8055 12 /**
0ab7404f 13 * Return a query builder to used by other getBuilderFor* method.
b84a8055 14 *
3b815d2d 15 * @param int $userId
3b815d2d 16 *
26864574 17 * @return QueryBuilder
b84a8055 18 */
0ab7404f 19 private function getBuilderByUser($userId)
9d50517c 20 {
26864574 21 return $this->createQueryBuilder('e')
3b815d2d 22 ->leftJoin('e.user', 'u')
0ab7404f
JB
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.id', 'desc')
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
3b815d2d 87 /**
4346a860 88 * Find Entries.
3b815d2d 89 *
2a94b1d1
NL
90 * @param int $userId
91 * @param bool $isArchived
92 * @param bool $isStarred
2a94b1d1
NL
93 * @param string $sort
94 * @param string $order
8cb869ea
TC
95 * @param int $since
96 * @param string $tags
3b815d2d 97 *
017e2089 98 * @return array
3b815d2d 99 */
28803f10 100 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
a8c90c5c
NL
101 {
102 $qb = $this->createQueryBuilder('e')
28803f10 103 ->leftJoin('e.tags', 't')
0f006880 104 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 105
3b815d2d
J
106 if (null !== $isArchived) {
107 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
108 }
109
3b815d2d
J
110 if (null !== $isStarred) {
111 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
6e334aba
NL
112 }
113
e5fb89e5
TC
114 if ($since >= 0) {
115 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
4f0558a0
TC
116 }
117
28803f10
TC
118 if ('' !== $tags) {
119 foreach (explode(',', $tags) as $tag) {
120 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
121 }
e5fb89e5
TC
122 }
123
bc782eaa 124 if ('created' === $sort) {
2385f891 125 $qb->orderBy('e.id', $order);
bc782eaa
NL
126 } elseif ('updated' === $sort) {
127 $qb->orderBy('e.updatedAt', $order);
128 }
129
bcf53ab7
WD
130 $pagerAdapter = new DoctrineORMAdapter($qb);
131
132 return new Pagerfanta($pagerAdapter);
a8c90c5c 133 }
46bbd8d3 134
a36737f4
NL
135 /**
136 * Fetch an entry with a tag. Only used for tests.
137 *
5c072d2b
NL
138 * @param int $userId
139 *
a36737f4
NL
140 * @return Entry
141 */
092ca707 142 public function findOneWithTags($userId)
46bbd8d3
NL
143 {
144 $qb = $this->createQueryBuilder('e')
145 ->innerJoin('e.tags', 't')
0ca374e6
NL
146 ->innerJoin('e.user', 'u')
147 ->addSelect('t', 'u')
148 ->where('e.user=:userId')->setParameter('userId', $userId)
149 ;
092ca707 150
0ca374e6 151 return $qb->getQuery()->getResult();
46bbd8d3 152 }
d4ebe5c5
JB
153
154 /**
155 * Find distinct language for a given user.
156 * Used to build the filter language list.
157 *
158 * @param int $userId User id
159 *
160 * @return array
161 */
162 public function findDistinctLanguageByUser($userId)
163 {
164 $results = $this->createQueryBuilder('e')
165 ->select('e.language')
166 ->where('e.user = :userId')->setParameter('userId', $userId)
167 ->andWhere('e.language IS NOT NULL')
168 ->groupBy('e.language')
169 ->orderBy('e.language', ' ASC')
170 ->getQuery()
171 ->getResult();
172
4094ea47 173 $languages = [];
d4ebe5c5
JB
174 foreach ($results as $result) {
175 $languages[$result['language']] = $result['language'];
176 }
177
178 return $languages;
179 }
159986c4 180
159986c4 181 /**
cfb28c9d 182 * Used only in test case to get the right entry associated to the right user.
159986c4 183 *
cfb28c9d 184 * @param string $username
159986c4
JB
185 *
186 * @return Entry
187 */
188 public function findOneByUsernameAndNotArchived($username)
189 {
190 return $this->createQueryBuilder('e')
191 ->leftJoin('e.user', 'u')
192 ->where('u.username = :username')->setParameter('username', $username)
193 ->andWhere('e.isArchived = false')
194 ->setMaxResults(1)
195 ->getQuery()
196 ->getSingleResult();
197 }
fc732227
JB
198
199 /**
200 * Remove a tag from all user entries.
4059a061
JB
201 *
202 * 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
203 * 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:
204 *
205 * 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
206 *
207 * @param int $userId
208 * @param Tag $tag
209 */
210 public function removeTag($userId, Tag $tag)
211 {
4059a061
JB
212 $entries = $this->getBuilderByUser($userId)
213 ->innerJoin('e.tags', 't')
214 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
215 ->getQuery()
216 ->getResult();
217
218 foreach ($entries as $entry) {
219 $entry->removeTag($tag);
220 }
221
222 $this->getEntityManager()->flush();
4059a061
JB
223 }
224
225 /**
226 * Find all entries that are attached to a give tag id.
227 *
228 * @param int $userId
229 * @param int $tagId
230 *
231 * @return array
232 */
233 public function findAllByTagId($userId, $tagId)
234 {
235 return $this->getBuilderByUser($userId)
236 ->innerJoin('e.tags', 't')
237 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
238 ->getQuery()
239 ->getResult();
fc732227 240 }
303768df
NL
241
242 /**
243 * Find an entry by its url and its owner.
5a4bbcc9 244 * If it exists, return the entry otherwise return false.
303768df
NL
245 *
246 * @param $url
247 * @param $userId
248 *
4094ea47 249 * @return Entry|bool
303768df 250 */
78833672 251 public function findByUrlAndUserId($url, $userId)
303768df 252 {
5a4bbcc9 253 $res = $this->createQueryBuilder('e')
303768df
NL
254 ->where('e.url = :url')->setParameter('url', $url)
255 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
256 ->getQuery()
257 ->getResult();
5a4bbcc9 258
b4b592a0
JB
259 if (count($res)) {
260 return current($res);
5a4bbcc9
JB
261 }
262
263 return false;
303768df 264 }
5c072d2b
NL
265
266 /**
267 * Count all entries for a user.
268 *
269 * @param int $userId
270 *
e678c475 271 * @return int
5c072d2b
NL
272 */
273 public function countAllEntriesByUsername($userId)
274 {
275 $qb = $this->createQueryBuilder('e')
276 ->select('count(e)')
277 ->where('e.user=:userId')->setParameter('userId', $userId)
278 ;
279
280 return $qb->getQuery()->getSingleScalarResult();
281 }
9d50517c 282}