]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/AnnotationBundle/Repository/AnnotationRepository.php
Merge pull request #3076 from wallabag/hotfix/2979
[github/wallabag/wallabag.git] / src / Wallabag / AnnotationBundle / Repository / AnnotationRepository.php
CommitLineData
4dc87223
NL
1<?php
2
3namespace Wallabag\AnnotationBundle\Repository;
4
5use Doctrine\ORM\EntityRepository;
6
7/**
8 * AnnotationRepository.
9 */
10class AnnotationRepository extends EntityRepository
11{
12 /**
13 * Return a query builder to used by other getBuilderFor* method.
14 *
15 * @param int $userId
16 *
17 * @return QueryBuilder
18 */
19 private function getBuilderByUser($userId)
20 {
21 return $this->createQueryBuilder('a')
22 ->leftJoin('a.user', 'u')
23 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
24 ->orderBy('a.id', 'desc')
25 ;
26 }
27
28 /**
29 * Retrieves all annotations 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
42 /**
43 * Get annotation for this id.
44 *
45 * @param int $annotationId
46 *
47 * @return array
48 */
49 public function findAnnotationById($annotationId)
50 {
51 return $this->createQueryBuilder('a')
52 ->andWhere('a.id = :annotationId')->setParameter('annotationId', $annotationId)
aa474109
JB
53 ->getQuery()
54 ->getSingleResult()
4dc87223
NL
55 ;
56 }
57
58 /**
59 * Find annotations for entry id.
60 *
61 * @param int $entryId
62 * @param int $userId
63 *
64 * @return array
65 */
66 public function findAnnotationsByPageId($entryId, $userId)
67 {
68 return $this->createQueryBuilder('a')
69 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
70 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
aa474109
JB
71 ->getQuery()
72 ->getResult()
4dc87223
NL
73 ;
74 }
75
76 /**
77 * Find last annotation for a given entry id. Used only for tests.
78 *
79 * @param int $entryId
80 *
81 * @return array
82 */
83 public function findLastAnnotationByPageId($entryId, $userId)
84 {
85 return $this->createQueryBuilder('a')
86 ->where('a.entry = :entryId')->setParameter('entryId', $entryId)
87 ->andwhere('a.user = :userId')->setParameter('userId', $userId)
88 ->orderBy('a.id', 'DESC')
89 ->setMaxResults(1)
90 ->getQuery()
91 ->getOneOrNullResult();
92 }
09d8bb6f
JB
93
94 /**
95 * Used only in test case to get the right annotation associated to the right user.
96 *
97 * @param string $username
98 *
99 * @return Annotation
100 */
101 public function findOneByUsername($username)
102 {
103 return $this->createQueryBuilder('a')
104 ->leftJoin('a.user', 'u')
105 ->where('u.username = :username')->setParameter('username', $username)
b95ffda2 106 ->orderBy('a.id', 'DESC')
09d8bb6f
JB
107 ->setMaxResults(1)
108 ->getQuery()
109 ->getSingleResult();
110 }
191564b7
JB
111
112 /**
113 * Remove all annotations for a user id.
8c61fd12 114 * Used when a user want to reset all informations.
191564b7 115 *
8c61fd12 116 * @param int $userId
191564b7
JB
117 */
118 public function removeAllByUserId($userId)
119 {
120 $this->getEntityManager()
b0de88f7
JB
121 ->createQuery('DELETE FROM Wallabag\AnnotationBundle\Entity\Annotation a WHERE a.user = :userId')
122 ->setParameter('userId', $userId)
191564b7
JB
123 ->execute();
124 }
4dc87223 125}