]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Repository/EntryRepository.php
Merge pull request #1384 from wallabag/v2-fix-config-display
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Repository / EntryRepository.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Repository;
4
5 use Doctrine\ORM\EntityRepository;
6 use Pagerfanta\Adapter\DoctrineORMAdapter;
7 use Pagerfanta\Pagerfanta;
8
9 class EntryRepository extends EntityRepository
10 {
11 /**
12 * Return a query builder to used by other getBuilderFor* method.
13 *
14 * @param int $userId
15 *
16 * @return QueryBuilder
17 */
18 private function getBuilderByUser($userId)
19 {
20 return $this->createQueryBuilder('e')
21 ->leftJoin('e.user', 'u')
22 ->andWhere('u.id = :userId')->setParameter('userId', $userId)
23 ->orderBy('e.id', 'desc')
24 ;
25 }
26
27 /**
28 * Retrieves all entries for a user.
29 *
30 * @param int $userId
31 *
32 * @return QueryBuilder
33 */
34 public function getBuilderForAllByUser($userId)
35 {
36 return $this
37 ->getBuilderByUser($userId)
38 ;
39 }
40
41 /**
42 * Retrieves unread entries for a user.
43 *
44 * @param int $userId
45 *
46 * @return QueryBuilder
47 */
48 public function getBuilderForUnreadByUser($userId)
49 {
50 return $this
51 ->getBuilderByUser($userId)
52 ->andWhere('e.isArchived = false')
53 ;
54 }
55
56 /**
57 * Retrieves read entries for a user.
58 *
59 * @param int $userId
60 *
61 * @return QueryBuilder
62 */
63 public function getBuilderForArchiveByUser($userId)
64 {
65 return $this
66 ->getBuilderByUser($userId)
67 ->andWhere('e.isArchived = true')
68 ;
69 }
70
71 /**
72 * Retrieves starred entries for a user.
73 *
74 * @param int $userId
75 *
76 * @return QueryBuilder
77 */
78 public function getBuilderForStarredByUser($userId)
79 {
80 return $this
81 ->getBuilderByUser($userId)
82 ->andWhere('e.isStarred = true')
83 ;
84 }
85
86 /**
87 * Find Entries.
88 *
89 * @param int $userId
90 * @param bool $isArchived
91 * @param bool $isStarred
92 * @param string $sort
93 * @param string $order
94 *
95 * @return array
96 */
97 public function findEntries($userId, $isArchived = null, $isStarred = null, $sort = 'created', $order = 'ASC')
98 {
99 $qb = $this->createQueryBuilder('e')
100 ->where('e.user =:userId')->setParameter('userId', $userId);
101
102 if (null !== $isArchived) {
103 $qb->andWhere('e.isArchived =:isArchived')->setParameter('isArchived', (bool) $isArchived);
104 }
105
106 if (null !== $isStarred) {
107 $qb->andWhere('e.isStarred =:isStarred')->setParameter('isStarred', (bool) $isStarred);
108 }
109
110 if ('created' === $sort) {
111 $qb->orderBy('e.id', $order);
112 } elseif ('updated' === $sort) {
113 $qb->orderBy('e.updatedAt', $order);
114 }
115
116 $pagerAdapter = new DoctrineORMAdapter($qb);
117
118 return new Pagerfanta($pagerAdapter);
119 }
120
121 /**
122 * Fetch an entry with a tag. Only used for tests.
123 *
124 * @return Entry
125 */
126 public function findOneWithTags($userId)
127 {
128 $qb = $this->createQueryBuilder('e')
129 ->innerJoin('e.tags', 't')
130 ->innerJoin('e.user', 'u')
131 ->addSelect('t', 'u')
132 ->where('e.user=:userId')->setParameter('userId', $userId)
133 ;
134
135 return $qb->getQuery()->getResult();
136 }
137 }