]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
AnnotationRepository: rename getBuilderByUser
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Repository / AnnotationRepository.php
1 <?php
2
3 namespace Wallabag\AnnotationBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Doctrine\ORM\QueryBuilder;
7 use Wallabag\AnnotationBundle\Entity\Annotation;
8
9 /**
10 * AnnotationRepository.
11 */
12 class AnnotationRepository extends EntityRepository
13 {
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
24 ->getSortedQueryBuilderByUser($userId)
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)
39 ->getQuery()
40 ->getSingleResult()
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)
57 ->getQuery()
58 ->getResult()
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 }
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)
92 ->orderBy('a.id', 'DESC')
93 ->setMaxResults(1)
94 ->getQuery()
95 ->getSingleResult();
96 }
97
98 /**
99 * Remove all annotations for a user id.
100 * Used when a user want to reset all informations.
101 *
102 * @param int $userId
103 */
104 public function removeAllByUserId($userId)
105 {
106 $this->getEntityManager()
107 ->createQuery('DELETE FROM Wallabag\AnnotationBundle\Entity\Annotation a WHERE a.user = :userId')
108 ->setParameter('userId', $userId)
109 ->execute();
110 }
111
112 /**
113 * Find all annotations related to archived entries.
114 *
115 * @param $userId
116 *
117 * @return mixed
118 */
119 public function findAllArchivedEntriesByUser($userId)
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 }
128
129 /**
130 * Return a query builder to used by other getBuilderFor* method.
131 *
132 * @param int $userId
133 *
134 * @return QueryBuilder
135 */
136 private function getSortedQueryBuilderByUser($userId)
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 }
144 }