]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Convert array + phpDoc
[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')
99 {
100 $qb = $this->createQueryBuilder('e')
101 ->where('e.user =:userId')->setParameter('userId', $userId);
102
103 if (null !== $isArchived) {
104 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
105 }
106
107 if (null !== $isStarred) {
108 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
109 }
110
111 if ('created' === $sort) {
112 $qb->orderBy('e.id', $order);
113 } elseif ('updated' === $sort) {
114 $qb->orderBy('e.updatedAt', $order);
115 }
116
117 $pagerAdapter = new DoctrineORMAdapter($qb);
118
119 return new Pagerfanta($pagerAdapter);
120 }
121
122 /**
123 * Fetch an entry with a tag. Only used for tests.
124 *
125 * @param int $userId
126 *
127 * @return Entry
128 */
129 public function findOneWithTags($userId)
130 {
131 $qb = $this->createQueryBuilder('e')
132 ->innerJoin('e.tags', 't')
133 ->innerJoin('e.user', 'u')
134 ->addSelect('t', 'u')
135 ->where('e.user=:userId')->setParameter('userId', $userId)
136 ;
137
138 return $qb->getQuery()->getResult();
139 }
140
141 /**
142 * Find distinct language for a given user.
143 * Used to build the filter language list.
144 *
145 * @param int $userId User id
146 *
147 * @return array
148 */
149 public function findDistinctLanguageByUser($userId)
150 {
151 $results = $this->createQueryBuilder('e')
152 ->select('e.language')
153 ->where('e.user = :userId')->setParameter('userId', $userId)
154 ->andWhere('e.language IS NOT NULL')
155 ->groupBy('e.language')
156 ->orderBy('e.language', ' ASC')
157 ->getQuery()
158 ->getResult();
159
160 $languages = [];
161 foreach ($results as $result) {
162 $languages[$result['language']] = $result['language'];
163 }
164
165 return $languages;
166 }
167
168 /**
169 * Used only in test case to get the right entry associated to the right user.
170 *
171 * @param string $username
172 *
173 * @return Entry
174 */
175 public function findOneByUsernameAndNotArchived($username)
176 {
177 return $this->createQueryBuilder('e')
178 ->leftJoin('e.user', 'u')
179 ->where('u.username = :username')->setParameter('username', $username)
180 ->andWhere('e.isArchived = false')
181 ->setMaxResults(1)
182 ->getQuery()
183 ->getSingleResult();
184 }
185
186 /**
187 * Remove a tag from all user entries.
188 *
189 * 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.
190 * 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:
191 *
192 * 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
193 *
194 * @param int $userId
195 * @param Tag $tag
196 */
197 public function removeTag($userId, Tag $tag)
198 {
199 $entries = $this->getBuilderByUser($userId)
200 ->innerJoin('e.tags', 't')
201 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
202 ->getQuery()
203 ->getResult();
204
205 foreach ($entries as $entry) {
206 $entry->removeTag($tag);
207 }
208
209 $this->getEntityManager()->flush();
210 }
211
212 /**
213 * Find all entries that are attached to a give tag id.
214 *
215 * @param int $userId
216 * @param int $tagId
217 *
218 * @return array
219 */
220 public function findAllByTagId($userId, $tagId)
221 {
222 return $this->getBuilderByUser($userId)
223 ->innerJoin('e.tags', 't')
224 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
225 ->getQuery()
226 ->getResult();
227 }
228
229 /**
230 * Find an entry by its url and its owner.
231 * If it exists, return the entry otherwise return false.
232 *
233 * @param $url
234 * @param $userId
235 *
236 * @return Entry|bool
237 */
238 public function findByUrlAndUserId($url, $userId)
239 {
240 $res = $this->createQueryBuilder('e')
241 ->where('e.url = :url')->setParameter('url', $url)
242 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
243 ->getQuery()
244 ->getResult();
245
246 if (count($res)) {
247 return current($res);
248 }
249
250 return false;
251 }
252
253 /**
254 * Count all entries for a user.
255 *
256 * @param int $userId
257 *
258 * @return int
259 */
260 public function countAllEntriesByUsername($userId)
261 {
262 $qb = $this->createQueryBuilder('e')
263 ->select('count(e)')
264 ->where('e.user=:userId')->setParameter('userId', $userId)
265 ;
266
267 return $qb->getQuery()->getSingleScalarResult();
268 }
269 }