]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Repository; |
9d50517c | 4 | |
9d50517c | 5 | use Doctrine\ORM\EntityRepository; |
163eae0b | 6 | use Doctrine\ORM\Tools\Pagination\Paginator; |
9d50517c | 7 | |
be463487 | 8 | class EntryRepository extends EntityRepository |
9d50517c | 9 | { |
b84a8055 NL |
10 | /** |
11 | * Retrieves unread entries for a user | |
12 | * | |
3b815d2d J |
13 | * @param int $userId |
14 | * @param int $firstResult | |
15 | * @param int $maxResults | |
16 | * | |
b84a8055 NL |
17 | * @return Paginator |
18 | */ | |
163eae0b | 19 | public function findUnreadByUser($userId, $firstResult, $maxResults = 12) |
9d50517c NL |
20 | { |
21 | $qb = $this->createQueryBuilder('e') | |
163eae0b NL |
22 | ->setFirstResult($firstResult) |
23 | ->setMaxResults($maxResults) | |
3b815d2d | 24 | ->leftJoin('e.user', 'u') |
905ae369 | 25 | ->where('e.isArchived = false') |
3b815d2d | 26 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) |
bc782eaa | 27 | ->orderBy('e.createdAt', 'desc') |
163eae0b | 28 | ->getQuery(); |
9d50517c | 29 | |
b84a8055 | 30 | $paginator = new Paginator($qb); |
163eae0b | 31 | |
b84a8055 | 32 | return $paginator; |
9d50517c | 33 | } |
bd9f0815 | 34 | |
b84a8055 NL |
35 | /** |
36 | * Retrieves read entries for a user | |
37 | * | |
3b815d2d J |
38 | * @param int $userId |
39 | * @param int $firstResult | |
40 | * @param int $maxResults | |
41 | * | |
b84a8055 NL |
42 | * @return Paginator |
43 | */ | |
163eae0b | 44 | public function findArchiveByUser($userId, $firstResult, $maxResults = 12) |
bd9f0815 NL |
45 | { |
46 | $qb = $this->createQueryBuilder('e') | |
47 | ->select('e') | |
163eae0b NL |
48 | ->setFirstResult($firstResult) |
49 | ->setMaxResults($maxResults) | |
3b815d2d | 50 | ->leftJoin('e.user', 'u') |
905ae369 | 51 | ->where('e.isArchived = true') |
3b815d2d | 52 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) |
bc782eaa | 53 | ->orderBy('e.createdAt', 'desc') |
b84a8055 NL |
54 | ->getQuery(); |
55 | ||
56 | $paginator = new Paginator($qb); | |
bd9f0815 | 57 | |
b84a8055 | 58 | return $paginator; |
bd9f0815 NL |
59 | } |
60 | ||
b84a8055 NL |
61 | /** |
62 | * Retrieves starred entries for a user | |
63 | * | |
3b815d2d J |
64 | * @param int $userId |
65 | * @param int $firstResult | |
66 | * @param int $maxResults | |
67 | * | |
b84a8055 NL |
68 | * @return Paginator |
69 | */ | |
163eae0b | 70 | public function findStarredByUser($userId, $firstResult, $maxResults = 12) |
bd9f0815 NL |
71 | { |
72 | $qb = $this->createQueryBuilder('e') | |
73 | ->select('e') | |
163eae0b NL |
74 | ->setFirstResult($firstResult) |
75 | ->setMaxResults($maxResults) | |
3b815d2d | 76 | ->leftJoin('e.user', 'u') |
905ae369 | 77 | ->where('e.isStarred = true') |
3b815d2d | 78 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) |
bc782eaa | 79 | ->orderBy('e.createdAt', 'desc') |
b84a8055 NL |
80 | ->getQuery(); |
81 | ||
82 | $paginator = new Paginator($qb); | |
bd9f0815 | 83 | |
b84a8055 | 84 | return $paginator; |
bd9f0815 | 85 | } |
a8c90c5c | 86 | |
3b815d2d J |
87 | /** |
88 | * Find Entries | |
89 | * | |
2a94b1d1 NL |
90 | * @param int $userId |
91 | * @param bool $isArchived | |
92 | * @param bool $isStarred | |
2a94b1d1 NL |
93 | * @param string $sort |
94 | * @param string $order | |
3b815d2d | 95 | * |
017e2089 | 96 | * @return array |
3b815d2d | 97 | */ |
1d147791 | 98 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC') |
a8c90c5c NL |
99 | { |
100 | $qb = $this->createQueryBuilder('e') | |
0f006880 | 101 | ->where('e.user =:userId')->setParameter('userId', $userId); |
6e334aba | 102 | |
3b815d2d J |
103 | if (null !== $isArchived) { |
104 | $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); | |
6e334aba NL |
105 | } |
106 | ||
3b815d2d J |
107 | if (null !== $isStarred) { |
108 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | |
6e334aba NL |
109 | } |
110 | ||
bc782eaa NL |
111 | if ('created' === $sort) { |
112 | $qb->orderBy('e.createdAt', $order); | |
113 | } elseif ('updated' === $sort) { | |
114 | $qb->orderBy('e.updatedAt', $order); | |
115 | } | |
116 | ||
6e334aba | 117 | return $qb |
a8c90c5c | 118 | ->getQuery() |
eaf95758 | 119 | ->getResult(); |
a8c90c5c | 120 | } |
46bbd8d3 NL |
121 | |
122 | public function findOneWithTags() | |
123 | { | |
124 | $qb = $this->createQueryBuilder('e') | |
125 | ->innerJoin('e.tags', 't') | |
126 | ->addSelect('t'); | |
127 | ||
128 | return $qb | |
129 | ->getQuery() | |
130 | ->getOneOrNullResult(); | |
131 | } | |
9d50517c | 132 | } |