]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Fix the way to remove a tag from all user entries
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Repository;
9d50517c 4
9d50517c 5use Doctrine\ORM\EntityRepository;
bcf53ab7
WD
6use Pagerfanta\Adapter\DoctrineORMAdapter;
7use Pagerfanta\Pagerfanta;
fc732227 8use Wallabag\CoreBundle\Entity\Tag;
9d50517c 9
be463487 10class EntryRepository extends EntityRepository
9d50517c 11{
b84a8055 12 /**
0ab7404f 13 * Return a query builder to used by other getBuilderFor* method.
b84a8055 14 *
3b815d2d 15 * @param int $userId
3b815d2d 16 *
26864574 17 * @return QueryBuilder
b84a8055 18 */
0ab7404f 19 private function getBuilderByUser($userId)
9d50517c 20 {
26864574 21 return $this->createQueryBuilder('e')
3b815d2d 22 ->leftJoin('e.user', 'u')
0ab7404f
JB
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('e.id', 'desc')
25 ;
26 }
27
2b7a4889
NL
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
0ab7404f
JB
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 ;
9d50517c 55 }
bd9f0815 56
b84a8055 57 /**
4346a860 58 * Retrieves read entries for a user.
b84a8055 59 *
3b815d2d 60 * @param int $userId
3b815d2d 61 *
26864574 62 * @return QueryBuilder
b84a8055 63 */
0ab7404f 64 public function getBuilderForArchiveByUser($userId)
bd9f0815 65 {
0ab7404f
JB
66 return $this
67 ->getBuilderByUser($userId)
68 ->andWhere('e.isArchived = true')
69 ;
bd9f0815
NL
70 }
71
b84a8055 72 /**
4346a860 73 * Retrieves starred entries for a user.
b84a8055 74 *
3b815d2d 75 * @param int $userId
3b815d2d 76 *
26864574 77 * @return QueryBuilder
b84a8055 78 */
0ab7404f 79 public function getBuilderForStarredByUser($userId)
bd9f0815 80 {
0ab7404f
JB
81 return $this
82 ->getBuilderByUser($userId)
83 ->andWhere('e.isStarred = true')
84 ;
bd9f0815 85 }
a8c90c5c 86
3b815d2d 87 /**
4346a860 88 * Find Entries.
3b815d2d 89 *
2a94b1d1
NL
90 * @param int $userId
91 * @param bool $isArchived
92 * @param bool $isStarred
2a94b1d1
NL
93 * @param string $sort
94 * @param string $order
3b815d2d 95 *
017e2089 96 * @return array
3b815d2d 97 */
1d147791 98 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
a8c90c5c
NL
99 {
100 $qb = $this->createQueryBuilder('e')
0f006880 101 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 102
3b815d2d
J
103 if (null !== $isArchived) {
104 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
105 }
106
3b815d2d
J
107 if (null !== $isStarred) {
108 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
6e334aba
NL
109 }
110
bc782eaa 111 if ('created' === $sort) {
2385f891 112 $qb->orderBy('e.id', $order);
bc782eaa
NL
113 } elseif ('updated' === $sort) {
114 $qb->orderBy('e.updatedAt', $order);
115 }
116
bcf53ab7
WD
117 $pagerAdapter = new DoctrineORMAdapter($qb);
118
119 return new Pagerfanta($pagerAdapter);
a8c90c5c 120 }
46bbd8d3 121
a36737f4
NL
122 /**
123 * Fetch an entry with a tag. Only used for tests.
124 *
125 * @return Entry
126 */
092ca707 127 public function findOneWithTags($userId)
46bbd8d3
NL
128 {
129 $qb = $this->createQueryBuilder('e')
130 ->innerJoin('e.tags', 't')
0ca374e6
NL
131 ->innerJoin('e.user', 'u')
132 ->addSelect('t', 'u')
133 ->where('e.user=:userId')->setParameter('userId', $userId)
134 ;
092ca707 135
0ca374e6 136 return $qb->getQuery()->getResult();
46bbd8d3 137 }
d4ebe5c5
JB
138
139 /**
140 * Find distinct language for a given user.
141 * Used to build the filter language list.
142 *
143 * @param int $userId User id
144 *
145 * @return array
146 */
147 public function findDistinctLanguageByUser($userId)
148 {
149 $results = $this->createQueryBuilder('e')
150 ->select('e.language')
151 ->where('e.user = :userId')->setParameter('userId', $userId)
152 ->andWhere('e.language IS NOT NULL')
153 ->groupBy('e.language')
154 ->orderBy('e.language', ' ASC')
155 ->getQuery()
156 ->getResult();
157
158 $languages = array();
159 foreach ($results as $result) {
160 $languages[$result['language']] = $result['language'];
161 }
162
163 return $languages;
164 }
159986c4 165
159986c4 166 /**
cfb28c9d 167 * Used only in test case to get the right entry associated to the right user.
159986c4 168 *
cfb28c9d 169 * @param string $username
159986c4
JB
170 *
171 * @return Entry
172 */
173 public function findOneByUsernameAndNotArchived($username)
174 {
175 return $this->createQueryBuilder('e')
176 ->leftJoin('e.user', 'u')
177 ->where('u.username = :username')->setParameter('username', $username)
178 ->andWhere('e.isArchived = false')
179 ->setMaxResults(1)
180 ->getQuery()
181 ->getSingleResult();
182 }
fc732227
JB
183
184 /**
185 * Remove a tag from all user entries.
4059a061
JB
186 *
187 * 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.
188 * 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.
fc732227
JB
189 *
190 * @param int $userId
191 * @param Tag $tag
192 */
193 public function removeTag($userId, Tag $tag)
194 {
4059a061
JB
195 $entries = $this->getBuilderByUser($userId)
196 ->innerJoin('e.tags', 't')
197 ->andWhere('t.id = :tagId')->setParameter('tagId', $tag->getId())
198 ->getQuery()
199 ->getResult();
200
201 foreach ($entries as $entry) {
202 $entry->removeTag($tag);
203 }
204
205 $this->getEntityManager()->flush();
206
207 // An other solution can be to use raw query but I can't find a way to retrieve the `entry_tag` table name since it can be prefixed....
208 // $sql = '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';
209 // $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
210 // $stmt->execute([
211 // 'userId' => $userId,
212 // 'tagId' => $tag->getId(),
213 // ]);
214 }
215
216 /**
217 * Find all entries that are attached to a give tag id.
218 *
219 * @param int $userId
220 * @param int $tagId
221 *
222 * @return array
223 */
224 public function findAllByTagId($userId, $tagId)
225 {
226 return $this->getBuilderByUser($userId)
227 ->innerJoin('e.tags', 't')
228 ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId)
229 ->getQuery()
230 ->getResult();
fc732227 231 }
9d50517c 232}