3 namespace Wallabag\CoreBundle\Repository
;
5 use Doctrine\ORM\EntityRepository
;
6 use Pagerfanta\Adapter\DoctrineORMAdapter
;
7 use Pagerfanta\Pagerfanta
;
8 use Wallabag\CoreBundle\Entity\Tag
;
10 class EntryRepository
extends EntityRepository
13 * Return a query builder to used by other getBuilderFor* method.
17 * @return QueryBuilder
19 private function getBuilderByUser($userId)
21 return $this->createQueryBuilder('e')
22 ->leftJoin('e.user', 'u')
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.id', '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')
91 * @param bool $isArchived
92 * @param bool $isStarred
94 * @param string $order
100 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC', $since = 0, $tags = '')
102 $qb = $this->createQueryBuilder('e')
103 ->leftJoin('e.tags', 't')
104 ->where('e.user =:userId')->setParameter('userId', $userId);
106 if (null !== $isArchived) {
107 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
110 if (null !== $isStarred) {
111 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
115 $qb->andWhere('e.updatedAt > :since')->setParameter('since', new \
DateTime(date('Y-m-d H:i:s', $since)));
119 foreach (explode(',', $tags) as $tag) {
120 $qb->andWhere('t.label = :label')->setParameter('label', $tag);
124 if ('created' === $sort) {
125 $qb->orderBy('e.id', $order);
126 } elseif ('updated' === $sort) {
127 $qb->orderBy('e.updatedAt', $order);
130 $pagerAdapter = new DoctrineORMAdapter($qb);
132 return new Pagerfanta($pagerAdapter);
136 * Fetch an entry with a tag. Only used for tests.
142 public function findOneWithTags($userId)
144 $qb = $this->createQueryBuilder('e')
145 ->innerJoin('e.tags', 't')
146 ->innerJoin('e.user', 'u')
147 ->addSelect('t', 'u')
148 ->where('e.user=:userId')->setParameter('userId', $userId)
151 return $qb->getQuery()->getResult();
155 * Find distinct language for a given user.
156 * Used to build the filter language list.
158 * @param int $userId User id
162 public function findDistinctLanguageByUser($userId)
164 $results = $this->createQueryBuilder('e')
165 ->select('e.language')
166 ->where('e.user = :userId')->setParameter('userId', $userId)
167 ->andWhere('e.language IS NOT NULL')
168 ->groupBy('e.language')
169 ->orderBy('e.language', ' ASC')
174 foreach ($results as $result) {
175 $languages[$result['language']] = $result['language'];
182 * Used only in test case to get the right entry associated to the right user.
184 * @param string $username
188 public function findOneByUsernameAndNotArchived($username)
190 return $this->createQueryBuilder('e')
191 ->leftJoin('e.user', 'u')
192 ->where('u.username = :username')->setParameter('username', $username)
193 ->andWhere('e.isArchived = false')
200 * Remove a tag from all user entries.
202 * 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.
203 * 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:
205 * 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
210 public function removeTag($userId, Tag
$tag)
212 $entries = $this->getBuilderByUser($userId)
213 ->innerJoin('e.tags', 't')
214 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
218 foreach ($entries as $entry) {
219 $entry->removeTag($tag);
222 $this->getEntityManager()->flush();
226 * Remove tags from all user entries.
229 * @param Array<Tag> $tags
231 public function removeTags($userId, $tags)
233 foreach ($tags as $tag) {
234 $this->removeTag($userId, $tag);
239 * Find all entries that are attached to a give tag id.
246 public function findAllByTagId($userId, $tagId)
248 return $this->getBuilderByUser($userId)
249 ->innerJoin('e.tags', 't')
250 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
256 * Find an entry by its url and its owner.
257 * If it exists, return the entry otherwise return false.
264 public function findByUrlAndUserId($url, $userId)
266 $res = $this->createQueryBuilder('e')
267 ->where('e.url = :url')->setParameter('url', $url)
268 ->andWhere('e.user = :user_id')->setParameter('user_id', $userId)
273 return current($res);
280 * Count all entries for a user.
286 public function countAllEntriesByUsername($userId)
288 $qb = $this->createQueryBuilder('e')
290 ->where('e.user=:userId')->setParameter('userId', $userId)
293 return $qb->getQuery()->getSingleScalarResult();