]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
fix merge issue
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Pagerfanta\Adapter\DoctrineORMAdapter;
7 use Pagerfanta\Pagerfanta;
8 use Wallabag\CoreBundle\Entity\Tag;
9
10 class EntryRepository extends EntityRepository
11 {
12 /**
13 * Return a query builder to used by other getBuilderFor* method.
14 *
15 * @param int $userId
16 *
17 * @return QueryBuilder
18 */
19 private function getBuilderByUser($userId)
20 {
21 return $this->createQueryBuilder('e')
22 ->leftJoin('e.user', 'u')
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.id', 'desc')
25 ;
26 }
27
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
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 ;
55 }
56
57 /**
58 * Retrieves read entries for a user.
59 *
60 * @param int $userId
61 *
62 * @return QueryBuilder
63 */
64 public function getBuilderForArchiveByUser($userId)
65 {
66 return $this
67 ->getBuilderByUser($userId)
68 ->andWhere('e.isArchived = true')
69 ;
70 }
71
72 /**
73 * Retrieves starred entries for a user.
74 *
75 * @param int $userId
76 *
77 * @return QueryBuilder
78 */
79 public function getBuilderForStarredByUser($userId)
80 {
81 return $this
82 ->getBuilderByUser($userId)
83 ->andWhere('e.isStarred = true')
84 ;
85 }
86
87 /**
88 * Find Entries.
89 *
90 * @param int $userId
91 * @param bool $isArchived
92 * @param bool $isStarred
93 * @param string $sort
94 * @param string $order
95 *
96 * @return array
97 */
98 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
99 {
100 $qb = $this->createQueryBuilder('e')
101 ->leftJoin('e.tags', 't')
102 ->where('e.user =:userId')->setParameter('userId', $userId);
103
104 if (null !== $isArchived) {
105 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
106 }
107
108 if (null !== $isStarred) {
109 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
110 }
111
112 if ($since >= 0) {
113 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \DateTime(date('Y-m-d H:i:s', $since)));
114 }
115
116 if ('' !== $tags) {
117 foreach (explode(',', $tags) as $tag) {
118 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
119 }
120 }
121
122 if ('created' === $sort) {
123 $qb->orderBy('e.id', $order);
124 } elseif ('updated' === $sort) {
125 $qb->orderBy('e.updatedAt', $order);
126 }
127
128 $pagerAdapter = new DoctrineORMAdapter($qb);
129
130 return new Pagerfanta($pagerAdapter);
131 }
132
133 /**
134 * Fetch an entry with a tag. Only used for tests.
135 *
136 * @param int $userId
137 *
138 * @return Entry
139 */
140 public function findOneWithTags($userId)
141 {
142 $qb = $this->createQueryBuilder('e')
143 ->innerJoin('e.tags', 't')
144 ->innerJoin('e.user', 'u')
145 ->addSelect('t', 'u')
146 ->where('e.user=:userId')->setParameter('userId', $userId)
147 ;
148
149 return $qb->getQuery()->getResult();
150 }
151
152 /**
153 * Find distinct language for a given user.
154 * Used to build the filter language list.
155 *
156 * @param int $userId User id
157 *
158 * @return array
159 */
160 public function findDistinctLanguageByUser($userId)
161 {
162 $results = $this->createQueryBuilder('e')
163 ->select('e.language')
164 ->where('e.user = :userId')->setParameter('userId', $userId)
165 ->andWhere('e.language IS NOT NULL')
166 ->groupBy('e.language')
167 ->orderBy('e.language', ' ASC')
168 ->getQuery()
169 ->getResult();
170
171 $languages = [];
172 foreach ($results as $result) {
173 $languages[$result['language']] = $result['language'];
174 }
175
176 return $languages;
177 }
178
179 /**
180 * Used only in test case to get the right entry associated to the right user.
181 *
182 * @param string $username
183 *
184 * @return Entry
185 */
186 public function findOneByUsernameAndNotArchived($username)
187 {
188 return $this->createQueryBuilder('e')
189 ->leftJoin('e.user', 'u')
190 ->where('u.username = :username')->setParameter('username', $username)
191 ->andWhere('e.isArchived = false')
192 ->setMaxResults(1)
193 ->getQuery()
194 ->getSingleResult();
195 }
196
197 /**
198 * Remove a tag from all user entries.
199 *
200 * 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.
201 * 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:
202 *
203 * 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
204 *
205 * @param int $userId
206 * @param Tag $tag
207 */
208 public function removeTag($userId, Tag $tag)
209 {
210 $entries = $this->getBuilderByUser($userId)
211 ->innerJoin('e.tags', 't')
212 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
213 ->getQuery()
214 ->getResult();
215
216 foreach ($entries as $entry) {
217 $entry->removeTag($tag);
218 }
219
220 $this->getEntityManager()->flush();
221 }
222
223 /**
224 * Find all entries that are attached to a give tag id.
225 *
226 * @param int $userId
227 * @param int $tagId
228 *
229 * @return array
230 */
231 public function findAllByTagId($userId, $tagId)
232 {
233 return $this->getBuilderByUser($userId)
234 ->innerJoin('e.tags', 't')
235 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
236 ->getQuery()
237 ->getResult();
238 }
239
240 /**
241 * Find an entry by its url and its owner.
242 * If it exists, return the entry otherwise return false.
243 *
244 * @param $url
245 * @param $userId
246 *
247 * @return Entry|bool
248 */
249 public function findByUrlAndUserId($url, $userId)
250 {
251 $res = $this->createQueryBuilder('e')
252 ->where('e.url = :url')->setParameter('url', $url)
253 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
254 ->getQuery()
255 ->getResult();
256
257 if (count($res)) {
258 return current($res);
259 }
260
261 return false;
262 }
263
264 /**
265 * Count all entries for a user.
266 *
267 * @param int $userId
268 *
269 * @return int
270 */
271 public function countAllEntriesByUsername($userId)
272 {
273 $qb = $this->createQueryBuilder('e')
274 ->select('count(e)')
275 ->where('e.user=:userId')->setParameter('userId', $userId)
276 ;
277
278 return $qb->getQuery()->getSingleScalarResult();
279 }
280 }