]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
french translation
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Pagerfanta\Adapter\DoctrineORMAdapter;
7 use Pagerfanta\Pagerfanta;
8
9 class EntryRepository extends EntityRepository
10 {
11 /**
12 * Return a query builder to used by other getBuilderFor* method.
13 *
14 * @param int $userId
15 *
16 * @return QueryBuilder
17 */
18 private function getBuilderByUser($userId)
19 {
20 return $this->createQueryBuilder('e')
21 ->leftJoin('e.user', 'u')
22 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
23 ->orderBy('e.id', 'desc')
24 ;
25 }
26
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
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 ;
54 }
55
56 /**
57 * Retrieves read entries for a user.
58 *
59 * @param int $userId
60 *
61 * @return QueryBuilder
62 */
63 public function getBuilderForArchiveByUser($userId)
64 {
65 return $this
66 ->getBuilderByUser($userId)
67 ->andWhere('e.isArchived = true')
68 ;
69 }
70
71 /**
72 * Retrieves starred entries for a user.
73 *
74 * @param int $userId
75 *
76 * @return QueryBuilder
77 */
78 public function getBuilderForStarredByUser($userId)
79 {
80 return $this
81 ->getBuilderByUser($userId)
82 ->andWhere('e.isStarred = true')
83 ;
84 }
85
86 /**
87 * Find Entries.
88 *
89 * @param int $userId
90 * @param bool $isArchived
91 * @param bool $isStarred
92 * @param string $sort
93 * @param string $order
94 *
95 * @return array
96 */
97 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
98 {
99 $qb = $this->createQueryBuilder('e')
100 ->where('e.user =:userId')->setParameter('userId', $userId);
101
102 if (null !== $isArchived) {
103 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
104 }
105
106 if (null !== $isStarred) {
107 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
108 }
109
110 if ('created' === $sort) {
111 $qb->orderBy('e.id', $order);
112 } elseif ('updated' === $sort) {
113 $qb->orderBy('e.updatedAt', $order);
114 }
115
116 $pagerAdapter = new DoctrineORMAdapter($qb);
117
118 return new Pagerfanta($pagerAdapter);
119 }
120
121 /**
122 * Fetch an entry with a tag. Only used for tests.
123 *
124 * @return Entry
125 */
126 public function findOneWithTags($userId)
127 {
128 $qb = $this->createQueryBuilder('e')
129 ->innerJoin('e.tags', 't')
130 ->innerJoin('e.user', 'u')
131 ->addSelect('t', 'u')
132 ->where('e.user=:userId')->setParameter('userId', $userId)
133 ;
134
135 return $qb->getQuery()->getResult();
136 }
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 }
164
165 /**
166 * Used only in test case to get the right entry associated to the right user.
167 *
168 * @param string $username
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 }
182 }