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 ->andWhere('e.user = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.createdAt', 'desc')
29 * Retrieves all entries for a user.
33 * @return QueryBuilder
35 public function getBuilderForAllByUser($userId)
38 ->getBuilderByUser($userId)
43 * Retrieves unread entries for a user.
47 * @return QueryBuilder
49 public function getBuilderForUnreadByUser($userId)
52 ->getBuilderByUser($userId)
53 ->andWhere('e.isArchived = false')
58 * Retrieves read entries for a user.
62 * @return QueryBuilder
64 public function getBuilderForArchiveByUser($userId)
67 ->getBuilderByUser($userId)
68 ->andWhere('e.isArchived = true')
73 * Retrieves starred entries for a user.
77 * @return QueryBuilder
79 public function getBuilderForStarredByUser($userId)
82 ->getBuilderByUser($userId)
83 ->andWhere('e.isStarred = true')
88 * Retrieves entries filtered with a search term for a user.
92 * @param strint $currentRoute
94 * @return QueryBuilder
96 public function getBuilderForSearchByUser($userId, $term, $currentRoute)
99 ->getBuilderByUser($userId);
101 if ('starred' === $currentRoute) {
102 $qb->andWhere('e.isStarred = true');
103 } elseif ('unread' === $currentRoute) {
104 $qb->andWhere('e.isArchived = false');
105 } elseif ('archive' === $currentRoute) {
106 $qb->andWhere('e.isArchived = true');
110 ->andWhere('e.content LIKE :term OR e.title LIKE :term')->setParameter('term', '%'.$term.'%')
111 ->leftJoin('e.tags', 't')
118 * Retrieves untagged entries for a user.
122 * @return QueryBuilder
124 public function getBuilderForUntaggedByUser($userId)
127 ->getBuilderByUser($userId)
128 ->andWhere('size(e.tags) = 0');
135 * @param bool $isArchived
136 * @param bool $isStarred
137 * @param string $sort
138 * @param string $order
140 * @param string $tags
144 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
146 $qb = $this->createQueryBuilder('e')
147 ->leftJoin('e.tags', 't')
148 ->where('e.user =:userId')->setParameter('userId', $userId);
150 if (null !== $isArchived) {
151 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
154 if (null !== $isStarred) {
155 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
159 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
163 foreach (explode(',', $tags) as $tag) {
164 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
168 if ('created' === $sort) {
169 $qb->orderBy('e.id', $order);
170 } elseif ('updated' === $sort) {
171 $qb->orderBy('e.updatedAt', $order);
174 $pagerAdapter = new DoctrineORMAdapter($qb, true, false);
176 return new Pagerfanta($pagerAdapter);
180 * Fetch an entry with a tag. Only used for tests.
186 public function findOneWithTags($userId)
188 $qb = $this->createQueryBuilder('e')
189 ->innerJoin('e.tags', 't')
190 ->innerJoin('e.user', 'u')
191 ->addSelect('t', 'u')
192 ->where('e.user=:userId')->setParameter('userId', $userId)
195 return $qb->getQuery()->getResult();
199 * Find distinct language for a given user.
200 * Used to build the filter language list.
202 * @param int $userId User id
206 public function findDistinctLanguageByUser($userId)
208 $results = $this->createQueryBuilder('e')
209 ->select('e.language')
210 ->where('e.user = :userId')->setParameter('userId', $userId)
211 ->andWhere('e.language IS NOT NULL')
212 ->groupBy('e.language')
213 ->orderBy('e.language', ' ASC')
218 foreach ($results as $result) {
219 $languages[$result['language']] = $result['language'];
226 * Used only in test case to get the right entry associated to the right user.
228 * @param string $username
232 public function findOneByUsernameAndNotArchived($username)
234 return $this->createQueryBuilder('e')
235 ->leftJoin('e.user', 'u')
236 ->where('u.username = :username')->setParameter('username', $username)
237 ->andWhere('e.isArchived = false')
244 * Remove a tag from all user entries.
246 * 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.
247 * 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:
249 * 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
254 public function removeTag($userId, Tag
$tag)
256 $entries = $this->getBuilderByUser($userId)
257 ->innerJoin('e.tags', 't')
258 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
262 foreach ($entries as $entry) {
263 $entry->removeTag($tag);
266 $this->getEntityManager()->flush();
270 * Remove tags from all user entries.
273 * @param Array<Tag> $tags
275 public function removeTags($userId, $tags)
277 foreach ($tags as $tag) {
278 $this->removeTag($userId, $tag);
283 * Find all entries that are attached to a give tag id.
290 public function findAllByTagId($userId, $tagId)
292 return $this->getBuilderByUser($userId)
293 ->innerJoin('e.tags', 't')
294 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
300 * Find an entry by its url and its owner.
301 * If it exists, return the entry otherwise return false.
308 public function findByUrlAndUserId($url, $userId)
310 $res = $this->createQueryBuilder('e')
311 ->where('e.url = :url')->setParameter('url', urldecode($url))
312 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
317 return current($res);
324 * Count all entries for a user.
330 public function countAllEntriesByUsername($userId)
332 $qb = $this->createQueryBuilder('e')
334 ->where('e.user=:userId')->setParameter('userId', $userId)
337 return $qb->getQuery()->getSingleScalarResult();
341 * Count all entries for a tag and a user.
348 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
350 $qb = $this->createQueryBuilder('e')
351 ->select('count(e.id)')
352 ->leftJoin('e.tags', 't')
353 ->where('e.user=:userId')->setParameter('userId', $userId)
354 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
357 return $qb->getQuery()->getSingleScalarResult();
361 * Remove all entries for a user id.
362 * Used when a user want to reset all informations.
366 public function removeAllByUserId($userId)
368 $this->getEntityManager()
369 ->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = :userId')
370 ->setParameter('userId', $userId)