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.id', '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 untagged entries for a user.
93 * @return QueryBuilder
95 public function getBuilderForUntaggedByUser($userId)
98 ->getBuilderByUser($userId)
99 ->leftJoin('e.tags', 't')
101 ->having('count(t.id) = 0');
108 * @param bool $isArchived
109 * @param bool $isStarred
110 * @param string $sort
111 * @param string $order
113 * @param string $tags
117 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
119 $qb = $this->createQueryBuilder('e')
120 ->leftJoin('e.tags', 't')
121 ->where('e.user =:userId')->setParameter('userId', $userId);
123 if (null !== $isArchived) {
124 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
127 if (null !== $isStarred) {
128 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
132 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
136 foreach (explode(',', $tags) as $tag) {
137 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
141 if ('created' === $sort) {
142 $qb->orderBy('e.id', $order);
143 } elseif ('updated' === $sort) {
144 $qb->orderBy('e.updatedAt', $order);
147 $pagerAdapter = new DoctrineORMAdapter($qb);
149 return new Pagerfanta($pagerAdapter);
153 * Fetch an entry with a tag. Only used for tests.
159 public function findOneWithTags($userId)
161 $qb = $this->createQueryBuilder('e')
162 ->innerJoin('e.tags', 't')
163 ->innerJoin('e.user', 'u')
164 ->addSelect('t', 'u')
165 ->where('e.user=:userId')->setParameter('userId', $userId)
168 return $qb->getQuery()->getResult();
172 * Find distinct language for a given user.
173 * Used to build the filter language list.
175 * @param int $userId User id
179 public function findDistinctLanguageByUser($userId)
181 $results = $this->createQueryBuilder('e')
182 ->select('e.language')
183 ->where('e.user = :userId')->setParameter('userId', $userId)
184 ->andWhere('e.language IS NOT NULL')
185 ->groupBy('e.language')
186 ->orderBy('e.language', ' ASC')
191 foreach ($results as $result) {
192 $languages[$result['language']] = $result['language'];
199 * Used only in test case to get the right entry associated to the right user.
201 * @param string $username
205 public function findOneByUsernameAndNotArchived($username)
207 return $this->createQueryBuilder('e')
208 ->leftJoin('e.user', 'u')
209 ->where('u.username = :username')->setParameter('username', $username)
210 ->andWhere('e.isArchived = false')
217 * Remove a tag from all user entries.
219 * 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.
220 * 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:
222 * 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
227 public function removeTag($userId, Tag
$tag)
229 $entries = $this->getBuilderByUser($userId)
230 ->innerJoin('e.tags', 't')
231 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
235 foreach ($entries as $entry) {
236 $entry->removeTag($tag);
239 $this->getEntityManager()->flush();
243 * Remove tags from all user entries.
246 * @param Array<Tag> $tags
248 public function removeTags($userId, $tags)
250 foreach ($tags as $tag) {
251 $this->removeTag($userId, $tag);
256 * Find all entries that are attached to a give tag id.
263 public function findAllByTagId($userId, $tagId)
265 return $this->getBuilderByUser($userId)
266 ->innerJoin('e.tags', 't')
267 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
273 * Find an entry by its url and its owner.
274 * If it exists, return the entry otherwise return false.
281 public function findByUrlAndUserId($url, $userId)
283 $res = $this->createQueryBuilder('e')
284 ->where('e.url = :url')->setParameter('url', urldecode($url))
285 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
290 return current($res);
297 * Count all entries for a user.
303 public function countAllEntriesByUsername($userId)
305 $qb = $this->createQueryBuilder('e')
307 ->where('e.user=:userId')->setParameter('userId', $userId)
310 return $qb->getQuery()->getSingleScalarResult();
314 * Count all entries for a tag and a user.
321 public function countAllEntriesByUserIdAndTagId($userId, $tagId)
323 $qb = $this->createQueryBuilder('e')
324 ->select('count(e.id)')
325 ->leftJoin('e.tags', 't')
326 ->where('e.user=:userId')->setParameter('userId', $userId)
327 ->andWhere('t.id=:tagId')->setParameter('tagId', $tagId)
330 return $qb->getQuery()->getSingleScalarResult();