]>
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 | /** |
4346a860 | 12 | * Retrieves unread entries for a user. |
b84a8055 | 13 | * |
3b815d2d | 14 | * @param int $userId |
3b815d2d | 15 | * |
26864574 | 16 | * @return QueryBuilder |
b84a8055 | 17 | */ |
9fb6ac83 | 18 | public function findUnreadByUser($userId) |
9d50517c | 19 | { |
26864574 | 20 | return $this->createQueryBuilder('e') |
3b815d2d | 21 | ->leftJoin('e.user', 'u') |
905ae369 | 22 | ->where('e.isArchived = false') |
3b815d2d | 23 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) |
26864574 | 24 | ->orderBy('e.id', 'desc'); |
9d50517c | 25 | } |
bd9f0815 | 26 | |
b84a8055 | 27 | /** |
4346a860 | 28 | * Retrieves read entries for a user. |
b84a8055 | 29 | * |
3b815d2d | 30 | * @param int $userId |
3b815d2d | 31 | * |
26864574 | 32 | * @return QueryBuilder |
b84a8055 | 33 | */ |
9fb6ac83 | 34 | public function findArchiveByUser($userId) |
bd9f0815 | 35 | { |
26864574 | 36 | return $this->createQueryBuilder('e') |
3b815d2d | 37 | ->leftJoin('e.user', 'u') |
905ae369 | 38 | ->where('e.isArchived = true') |
3b815d2d | 39 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) |
26864574 | 40 | ->orderBy('e.id', 'desc'); |
bd9f0815 NL |
41 | } |
42 | ||
b84a8055 | 43 | /** |
4346a860 | 44 | * Retrieves starred entries for a user. |
b84a8055 | 45 | * |
3b815d2d | 46 | * @param int $userId |
3b815d2d | 47 | * |
26864574 | 48 | * @return QueryBuilder |
b84a8055 | 49 | */ |
9fb6ac83 | 50 | public function findStarredByUser($userId) |
bd9f0815 | 51 | { |
26864574 | 52 | return $this->createQueryBuilder('e') |
3b815d2d | 53 | ->leftJoin('e.user', 'u') |
905ae369 | 54 | ->where('e.isStarred = true') |
3b815d2d | 55 | ->andWhere('u.id =:userId')->setParameter('userId', $userId) |
26864574 | 56 | ->orderBy('e.id', 'desc'); |
bd9f0815 | 57 | } |
a8c90c5c | 58 | |
3b815d2d | 59 | /** |
4346a860 | 60 | * Find Entries. |
3b815d2d | 61 | * |
2a94b1d1 NL |
62 | * @param int $userId |
63 | * @param bool $isArchived | |
64 | * @param bool $isStarred | |
2a94b1d1 NL |
65 | * @param string $sort |
66 | * @param string $order | |
3b815d2d | 67 | * |
017e2089 | 68 | * @return array |
3b815d2d | 69 | */ |
1d147791 | 70 | public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC') |
a8c90c5c NL |
71 | { |
72 | $qb = $this->createQueryBuilder('e') | |
0f006880 | 73 | ->where('e.user =:userId')->setParameter('userId', $userId); |
6e334aba | 74 | |
3b815d2d J |
75 | if (null !== $isArchived) { |
76 | $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived); | |
6e334aba NL |
77 | } |
78 | ||
3b815d2d J |
79 | if (null !== $isStarred) { |
80 | $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred); | |
6e334aba NL |
81 | } |
82 | ||
bc782eaa | 83 | if ('created' === $sort) { |
2385f891 | 84 | $qb->orderBy('e.id', $order); |
bc782eaa NL |
85 | } elseif ('updated' === $sort) { |
86 | $qb->orderBy('e.updatedAt', $order); | |
87 | } | |
88 | ||
bcf53ab7 WD |
89 | $pagerAdapter = new DoctrineORMAdapter($qb); |
90 | ||
91 | return new Pagerfanta($pagerAdapter); | |
a8c90c5c | 92 | } |
46bbd8d3 | 93 | |
a36737f4 NL |
94 | /** |
95 | * Fetch an entry with a tag. Only used for tests. | |
96 | * | |
97 | * @return Entry | |
98 | */ | |
092ca707 | 99 | public function findOneWithTags($userId) |
46bbd8d3 NL |
100 | { |
101 | $qb = $this->createQueryBuilder('e') | |
102 | ->innerJoin('e.tags', 't') | |
0ca374e6 NL |
103 | ->innerJoin('e.user', 'u') |
104 | ->addSelect('t', 'u') | |
105 | ->where('e.user=:userId')->setParameter('userId', $userId) | |
106 | ; | |
092ca707 | 107 | |
0ca374e6 | 108 | return $qb->getQuery()->getResult(); |
46bbd8d3 | 109 | } |
9d50517c | 110 | } |