]>
Commit | Line | Data |
---|---|---|
4dc87223 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\AnnotationBundle\Repository; | |
4 | ||
5 | use Doctrine\ORM\EntityRepository; | |
52b84c11 NH |
6 | use Doctrine\ORM\QueryBuilder; |
7 | use Wallabag\AnnotationBundle\Entity\Annotation; | |
4dc87223 NL |
8 | |
9 | /** | |
10 | * AnnotationRepository. | |
11 | */ | |
12 | class AnnotationRepository extends EntityRepository | |
13 | { | |
4dc87223 NL |
14 | /** |
15 | * Retrieves all annotations for a user. | |
16 | * | |
17 | * @param int $userId | |
18 | * | |
19 | * @return QueryBuilder | |
20 | */ | |
21 | public function getBuilderForAllByUser($userId) | |
22 | { | |
23 | return $this | |
69b56394 | 24 | ->getSortedQueryBuilderByUser($userId) |
4dc87223 NL |
25 | ; |
26 | } | |
27 | ||
28 | /** | |
29 | * Get annotation for this id. | |
30 | * | |
31 | * @param int $annotationId | |
32 | * | |
33 | * @return array | |
34 | */ | |
35 | public function findAnnotationById($annotationId) | |
36 | { | |
37 | return $this->createQueryBuilder('a') | |
38 | ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId) | |
aa474109 JB |
39 | ->getQuery() |
40 | ->getSingleResult() | |
4dc87223 NL |
41 | ; |
42 | } | |
43 | ||
44 | /** | |
45 | * Find annotations for entry id. | |
46 | * | |
47 | * @param int $entryId | |
48 | * @param int $userId | |
49 | * | |
50 | * @return array | |
51 | */ | |
52 | public function findAnnotationsByPageId($entryId, $userId) | |
53 | { | |
54 | return $this->createQueryBuilder('a') | |
55 | ->where('a.entry = :entryId')->setParameter('entryId', $entryId) | |
56 | ->andwhere('a.user = :userId')->setParameter('userId', $userId) | |
aa474109 JB |
57 | ->getQuery() |
58 | ->getResult() | |
4dc87223 NL |
59 | ; |
60 | } | |
61 | ||
62 | /** | |
63 | * Find last annotation for a given entry id. Used only for tests. | |
64 | * | |
65 | * @param int $entryId | |
66 | * | |
67 | * @return array | |
68 | */ | |
69 | public function findLastAnnotationByPageId($entryId, $userId) | |
70 | { | |
71 | return $this->createQueryBuilder('a') | |
72 | ->where('a.entry = :entryId')->setParameter('entryId', $entryId) | |
73 | ->andwhere('a.user = :userId')->setParameter('userId', $userId) | |
74 | ->orderBy('a.id', 'DESC') | |
75 | ->setMaxResults(1) | |
76 | ->getQuery() | |
77 | ->getOneOrNullResult(); | |
78 | } | |
09d8bb6f JB |
79 | |
80 | /** | |
81 | * Used only in test case to get the right annotation associated to the right user. | |
82 | * | |
83 | * @param string $username | |
84 | * | |
85 | * @return Annotation | |
86 | */ | |
87 | public function findOneByUsername($username) | |
88 | { | |
89 | return $this->createQueryBuilder('a') | |
90 | ->leftJoin('a.user', 'u') | |
91 | ->where('u.username = :username')->setParameter('username', $username) | |
b95ffda2 | 92 | ->orderBy('a.id', 'DESC') |
09d8bb6f JB |
93 | ->setMaxResults(1) |
94 | ->getQuery() | |
95 | ->getSingleResult(); | |
96 | } | |
191564b7 JB |
97 | |
98 | /** | |
99 | * Remove all annotations for a user id. | |
8c61fd12 | 100 | * Used when a user want to reset all informations. |
191564b7 | 101 | * |
8c61fd12 | 102 | * @param int $userId |
191564b7 JB |
103 | */ |
104 | public function removeAllByUserId($userId) | |
105 | { | |
106 | $this->getEntityManager() | |
b0de88f7 JB |
107 | ->createQuery('DELETE FROM Wallabag\AnnotationBundle\Entity\Annotation a WHERE a.user = :userId') |
108 | ->setParameter('userId', $userId) | |
191564b7 JB |
109 | ->execute(); |
110 | } | |
6da1aebc TC |
111 | |
112 | /** | |
9102851f | 113 | * Find all annotations related to archived entries. |
6da1aebc TC |
114 | * |
115 | * @param $userId | |
9102851f | 116 | * |
6da1aebc TC |
117 | * @return mixed |
118 | */ | |
13a592a1 | 119 | public function findAllArchivedEntriesByUser($userId) |
6da1aebc TC |
120 | { |
121 | return $this->createQueryBuilder('a') | |
122 | ->leftJoin('a.entry', 'e') | |
123 | ->where('a.user = :userid')->setParameter(':userid', $userId) | |
124 | ->andWhere('e.isArchived = true') | |
125 | ->getQuery() | |
126 | ->getResult(); | |
127 | } | |
f808b016 JB |
128 | |
129 | /** | |
130 | * Return a query builder to used by other getBuilderFor* method. | |
131 | * | |
132 | * @param int $userId | |
133 | * | |
134 | * @return QueryBuilder | |
135 | */ | |
69b56394 | 136 | private function getSortedQueryBuilderByUser($userId) |
f808b016 JB |
137 | { |
138 | return $this->createQueryBuilder('a') | |
139 | ->leftJoin('a.user', 'u') | |
140 | ->andWhere('u.id = :userId')->setParameter('userId', $userId) | |
141 | ->orderBy('a.id', 'desc') | |
142 | ; | |
143 | } | |
4dc87223 | 144 | } |