]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
bcf53ab7 WD |
6 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
7 | use Pagerfanta\Pagerfanta; | |
9d50517c | 8 | |
be463487 | 9 | class 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 | ||
27 | /** | |
28 | * Retrieves unread entries for a user. | |
29 | * | |
30 | * @param int $userId | |
31 | * | |
32 | * @return QueryBuilder | |
33 | */ | |
34 | public function getBuilderForUnreadByUser($userId) | |
35 | { | |
36 | return $this | |
37 | ->getBuilderByUser($userId) | |
38 | ->andWhere('e.isArchived = false') | |
39 | ; | |
9d50517c | 40 | } |
bd9f0815 | 41 | |
b84a8055 | 42 | /** |
4346a860 | 43 | * Retrieves read entries for a user. |
b84a8055 | 44 | * |
3b815d2d | 45 | * @param int $userId |
3b815d2d | 46 | * |
26864574 | 47 | * @return QueryBuilder |
b84a8055 | 48 | */ |
0ab7404f | 49 | public function getBuilderForArchiveByUser($userId) |
bd9f0815 | 50 | { |
0ab7404f JB |
51 | return $this |
52 | ->getBuilderByUser($userId) | |
53 | ->andWhere('e.isArchived = true') | |
54 | ; | |
bd9f0815 NL |
55 | } |
56 | ||
b84a8055 | 57 | /** |
4346a860 | 58 | * Retrieves starred entries for a user. |
b84a8055 | 59 | * |
3b815d2d | 60 | * @param int $userId |
3b815d2d | 61 | * |
26864574 | 62 | * @return QueryBuilder |
b84a8055 | 63 | */ |
0ab7404f | 64 | public function getBuilderForStarredByUser($userId) |
bd9f0815 | 65 | { |
0ab7404f JB |
66 | return $this |
67 | ->getBuilderByUser($userId) | |
68 | ->andWhere('e.isStarred = true') | |
69 | ; | |
bd9f0815 | 70 | } |
a8c90c5c | 71 | |
3b815d2d | 72 | /** |
4346a860 | 73 | * Find Entries. |
3b815d2d | 74 | * |
2a94b1d1 NL |
75 | * @param int $userId |
76 | * @param bool $isArchived | |
77 | * @param bool $isStarred | |
2a94b1d1 NL |
78 | * @param string $sort |
79 | * @param string $order | |
3b815d2d | 80 | * |
017e2089 | 81 | * @return array |
3b815d2d | 82 | */ |
1d147791 | 83 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC') |
a8c90c5c NL |
84 | { |
85 | $qb = $this->createQueryBuilder('e') | |
0f006880 | 86 | ->where('e.user =:userId')->setParameter('userId', $userId); |
6e334aba | 87 | |
3b815d2d J |
88 | if (null !== $isArchived) { |
89 | $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); | |
6e334aba NL |
90 | } |
91 | ||
3b815d2d J |
92 | if (null !== $isStarred) { |
93 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | |
6e334aba NL |
94 | } |
95 | ||
bc782eaa | 96 | if ('created' === $sort) { |
2385f891 | 97 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
98 | } elseif ('updated' === $sort) { |
99 | $qb->orderBy('e.updatedAt', $order); | |
100 | } | |
101 | ||
bcf53ab7 WD |
102 | $pagerAdapter = new DoctrineORMAdapter($qb); |
103 | ||
104 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 105 | } |
46bbd8d3 | 106 | |
a36737f4 NL |
107 | /** |
108 | * Fetch an entry with a tag. Only used for tests. | |
109 | * | |
110 | * @return Entry | |
111 | */ | |
092ca707 | 112 | public function findOneWithTags($userId) |
46bbd8d3 NL |
113 | { |
114 | $qb = $this->createQueryBuilder('e') | |
115 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
116 | ->innerJoin('e.user', 'u') |
117 | ->addSelect('t', 'u') | |
118 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
119 | ; | |
092ca707 | 120 | |
0ca374e6 | 121 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 122 | } |
9d50517c | 123 | } |