3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Doctrine\ORM\Query
;
7 use Pagerfanta\Adapter\DoctrineORMAdapter
;
8 use Pagerfanta\Pagerfanta
;
9 use Wallabag\CoreBundle\Entity\Tag
;
11 class EntryRepository
extends EntityRepository
14 * Return a query builder to used by other getBuilderFor* method.
18 * @return QueryBuilder
20 private function getBuilderByUser($userId)
22 return $this->createQueryBuilder('e')
23 ->leftJoin('e.user', 'u')
24 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
25 ->orderBy('e.createdAt', 'desc')
30 * Retrieves all entries for a user.
34 * @return QueryBuilder
36 public function getBuilderForAllByUser($userId)
39 ->getBuilderByUser($userId)
44 * Retrieves unread entries for a user.
48 * @return QueryBuilder
50 public function getBuilderForUnreadByUser($userId)
53 ->getBuilderByUser($userId)
54 ->andWhere('e.isArchived = false')
59 * Retrieves read entries for a user.
63 * @return QueryBuilder
65 public function getBuilderForArchiveByUser($userId)
68 ->getBuilderByUser($userId)
69 ->andWhere('e.isArchived = true')
74 * Retrieves starred entries for a user.
78 * @return QueryBuilder
80 public function getBuilderForStarredByUser($userId)
83 ->getBuilderByUser($userId)
84 ->andWhere('e.isStarred = true')
89 * Retrieves entries filtered with a search term for a user.
93 * @param strint $currentRoute
95 * @return QueryBuilder
97 public function getBuilderForSearchByUser($userId, $term, $currentRoute)
100 ->getBuilderByUser($userId);
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');
111 ->andWhere('e.content LIKE :term OR e.title LIKE :term')->setParameter('term', '%'.$term.'%')
112 ->leftJoin('e.tags', 't')
119 * Retrieves untagged entries for a user.
123 * @return QueryBuilder
125 public function getBuilderForUntaggedByUser($userId)
128 ->getBuilderByUser($userId)
129 ->leftJoin('e.tags', 't')
131 ->having('count(t.id) = 0');
138 * @param bool $isArchived
139 * @param bool $isStarred
140 * @param string $sort
141 * @param string $order
143 * @param string $tags
147 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
149 $qb = $this->createQueryBuilder('e')
150 ->leftJoin('e.tags', 't')
151 ->where('e.user =:userId')->setParameter('userId', $userId);
153 if (null !== $isArchived) {
154 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
157 if (null !== $isStarred) {
158 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
162 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
166 foreach (explode(',', $tags) as $tag) {
167 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
171 if ('created' === $sort) {
172 $qb->orderBy('e.id', $order);
173 } elseif ('updated' === $sort) {
174 $qb->orderBy('e.updatedAt', $order);
177 $pagerAdapter = new DoctrineORMAdapter($qb);
179 return new Pagerfanta($pagerAdapter);
183 * Fetch an entry with a tag. Only used for tests.
189 public function findOneWithTags($userId)
191 $qb = $this->createQueryBuilder('e')
192 ->innerJoin('e.tags', 't')
193 ->innerJoin('e.user', 'u')
194 ->addSelect('t', 'u')
195 ->where('e.user=:userId')->setParameter('userId', $userId)
198 return $qb->getQuery()->getResult();
202 * Find distinct language for a given user.
203 * Used to build the filter language list.
205 * @param int $userId User id
209 public function findDistinctLanguageByUser($userId)
211 $results = $this->createQueryBuilder('e')
212 ->select('e.language')
213 ->where('e.user = :userId')->setParameter('userId', $userId)
214 ->andWhere('e.language IS NOT NULL')
215 ->groupBy('e.language')
216 ->orderBy('e.language', ' ASC')
221 foreach ($results as $result) {
222 $languages[$result['language']] = $result['language'];
229 * Used only in test case to get the right entry associated to the right user.
231 * @param string $username
235 public function findOneByUsernameAndNotArchived($username)
237 return $this->createQueryBuilder('e')
238 ->leftJoin('e.user', 'u')
239 ->where('u.username = :username')->setParameter('username', $username)
240 ->andWhere('e.isArchived = false')
247 * Remove a tag from all user entries.
249 * 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.
250 * 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 * 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
257 public function removeTag($userId, Tag
$tag)
259 $entries = $this->getBuilderByUser($userId)
260 ->innerJoin('e.tags', 't')
261 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
265 foreach ($entries as $entry) {
266 $entry->removeTag($tag);
269 $this->getEntityManager()->flush();
273 * Remove tags from all user entries.
276 * @param Array<Tag> $tags
278 public function removeTags($userId, $tags)
280 foreach ($tags as $tag) {
281 $this->removeTag($userId, $tag);
286 * Find all entries that are attached to a give tag id.
293 public function findAllByTagId($userId, $tagId)
295 return $this->getBuilderByUser($userId)
296 ->innerJoin('e.tags', 't')
297 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
303 * Find an entry by its url and its owner.
304 * If it exists, return the entry otherwise return false.
311 public function findByUrlAndUserId($url, $userId)
313 $res = $this->createQueryBuilder('e')
314 ->where('e.url = :url')->setParameter('url', urldecode($url))
315 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
320 return current($res);
327 * Count all entries for a user.
333 public function countAllEntriesByUsername($userId)
335 $qb = $this->createQueryBuilder('e')
337 ->where('e.user=:userId')->setParameter('userId', $userId)
340 return $qb->getQuery()->getSingleScalarResult();
344 * Count all entries for a tag and a user.
351 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
353 $qb = $this->createQueryBuilder('e')
354 ->select('count(e.id)')
355 ->leftJoin('e.tags', 't')
356 ->where('e.user=:userId')->setParameter('userId', $userId)
357 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
360 return $qb->getQuery()->getSingleScalarResult();
364 * Remove all entries for a user id.
365 * Used when a user want to reset all informations.
369 public function removeAllByUserId($userId)
371 $this->getEntityManager()
372 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
373 ->setParameter('userId', $userId)