]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Repository/EntryRepository.php
french translation
[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;
9d50517c 8
be463487 9class EntryRepository extends EntityRepository
9d50517c 10{
b84a8055 11 /**
0ab7404f 12 * Return a query builder to used by other getBuilderFor* method.
b84a8055 13 *
3b815d2d 14 * @param int $userId
3b815d2d 15 *
26864574 16 * @return QueryBuilder
b84a8055 17 */
0ab7404f 18 private function getBuilderByUser($userId)
9d50517c 19 {
26864574 20 return $this->createQueryBuilder('e')
3b815d2d 21 ->leftJoin('e.user', 'u')
0ab7404f
JB
22 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
23 ->orderBy('e.id', 'desc')
24 ;
25 }
26
2b7a4889
NL
27 /**
28 * Retrieves all entries for a user.
29 *
30 * @param int $userId
31 *
32 * @return QueryBuilder
33 */
34 public function getBuilderForAllByUser($userId)
35 {
36 return $this
37 ->getBuilderByUser($userId)
38 ;
39 }
40
0ab7404f
JB
41 /**
42 * Retrieves unread entries for a user.
43 *
44 * @param int $userId
45 *
46 * @return QueryBuilder
47 */
48 public function getBuilderForUnreadByUser($userId)
49 {
50 return $this
51 ->getBuilderByUser($userId)
52 ->andWhere('e.isArchived = false')
53 ;
9d50517c 54 }
bd9f0815 55
b84a8055 56 /**
4346a860 57 * Retrieves read entries for a user.
b84a8055 58 *
3b815d2d 59 * @param int $userId
3b815d2d 60 *
26864574 61 * @return QueryBuilder
b84a8055 62 */
0ab7404f 63 public function getBuilderForArchiveByUser($userId)
bd9f0815 64 {
0ab7404f
JB
65 return $this
66 ->getBuilderByUser($userId)
67 ->andWhere('e.isArchived = true')
68 ;
bd9f0815
NL
69 }
70
b84a8055 71 /**
4346a860 72 * Retrieves starred entries for a user.
b84a8055 73 *
3b815d2d 74 * @param int $userId
3b815d2d 75 *
26864574 76 * @return QueryBuilder
b84a8055 77 */
0ab7404f 78 public function getBuilderForStarredByUser($userId)
bd9f0815 79 {
0ab7404f
JB
80 return $this
81 ->getBuilderByUser($userId)
82 ->andWhere('e.isStarred = true')
83 ;
bd9f0815 84 }
a8c90c5c 85
3b815d2d 86 /**
4346a860 87 * Find Entries.
3b815d2d 88 *
2a94b1d1
NL
89 * @param int $userId
90 * @param bool $isArchived
91 * @param bool $isStarred
2a94b1d1
NL
92 * @param string $sort
93 * @param string $order
3b815d2d 94 *
017e2089 95 * @return array
3b815d2d 96 */
1d147791 97 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
a8c90c5c
NL
98 {
99 $qb = $this->createQueryBuilder('e')
0f006880 100 ->where('e.user =:userId')->setParameter('userId', $userId);
6e334aba 101
3b815d2d
J
102 if (null !== $isArchived) {
103 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
6e334aba
NL
104 }
105
3b815d2d
J
106 if (null !== $isStarred) {
107 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
6e334aba
NL
108 }
109
bc782eaa 110 if ('created' === $sort) {
2385f891 111 $qb->orderBy('e.id', $order);
bc782eaa
NL
112 } elseif ('updated' === $sort) {
113 $qb->orderBy('e.updatedAt', $order);
114 }
115
bcf53ab7
WD
116 $pagerAdapter = new DoctrineORMAdapter($qb);
117
118 return new Pagerfanta($pagerAdapter);
a8c90c5c 119 }
46bbd8d3 120
a36737f4
NL
121 /**
122 * Fetch an entry with a tag. Only used for tests.
123 *
124 * @return Entry
125 */
092ca707 126 public function findOneWithTags($userId)
46bbd8d3
NL
127 {
128 $qb = $this->createQueryBuilder('e')
129 ->innerJoin('e.tags', 't')
0ca374e6
NL
130 ->innerJoin('e.user', 'u')
131 ->addSelect('t', 'u')
132 ->where('e.user=:userId')->setParameter('userId', $userId)
133 ;
092ca707 134
0ca374e6 135 return $qb->getQuery()->getResult();
46bbd8d3 136 }
d4ebe5c5
JB
137
138 /**
139 * Find distinct language for a given user.
140 * Used to build the filter language list.
141 *
142 * @param int $userId User id
143 *
144 * @return array
145 */
146 public function findDistinctLanguageByUser($userId)
147 {
148 $results = $this->createQueryBuilder('e')
149 ->select('e.language')
150 ->where('e.user = :userId')->setParameter('userId', $userId)
151 ->andWhere('e.language IS NOT NULL')
152 ->groupBy('e.language')
153 ->orderBy('e.language', ' ASC')
154 ->getQuery()
155 ->getResult();
156
157 $languages = array();
158 foreach ($results as $result) {
159 $languages[$result['language']] = $result['language'];
160 }
161
162 return $languages;
163 }
159986c4 164
159986c4 165 /**
cfb28c9d 166 * Used only in test case to get the right entry associated to the right user.
159986c4 167 *
cfb28c9d 168 * @param string $username
159986c4
JB
169 *
170 * @return Entry
171 */
172 public function findOneByUsernameAndNotArchived($username)
173 {
174 return $this->createQueryBuilder('e')
175 ->leftJoin('e.user', 'u')
176 ->where('u.username = :username')->setParameter('username', $username)
177 ->andWhere('e.isArchived = false')
178 ->setMaxResults(1)
179 ->getQuery()
180 ->getSingleResult();
181 }
9d50517c 182}