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