1 | <?php |
||
25 | class Card extends EntityRepository implements PaginatorAwareInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var Paginator |
||
29 | */ |
||
30 | private $paginator; |
||
31 | |||
32 | /** |
||
33 | * Set the KnpPaginator instance. |
||
34 | * |
||
35 | * @param Paginator $paginator |
||
36 | * |
||
37 | * @return $this |
||
38 | */ |
||
39 | 12 | public function setPaginator(Paginator $paginator) |
|
40 | { |
||
41 | 12 | $this->paginator = $paginator; |
|
42 | |||
43 | 12 | return $this; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Return the KnpPaginator instance. |
||
48 | * |
||
49 | * @return Paginator |
||
50 | */ |
||
51 | 4 | public function getPaginator() |
|
55 | |||
56 | /** |
||
57 | * Return query to select all active cards |
||
58 | * |
||
59 | * @return \Doctrine\ORM\QueryBuilder |
||
60 | */ |
||
61 | 2 | public function getQueryAllCards() |
|
62 | { |
||
63 | 2 | $query = $this->getEntityManager()->createQueryBuilder(); |
|
64 | 2 | $query->select('p, c')->from('MooFlashCardBundle:Card', 'p'); |
|
65 | 2 | $query->join('p.category', 'c'); |
|
66 | 2 | $query->where('p.active = 1'); |
|
67 | 2 | $query->where('c.active = 1'); |
|
68 | 2 | $query->orderBy('p.id', 'ASC'); |
|
69 | |||
70 | 2 | return $query; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Return query to search for active cards |
||
75 | * |
||
76 | * @param string $search |
||
77 | * |
||
78 | * @return \Doctrine\ORM\QueryBuilder |
||
79 | */ |
||
80 | 5 | public function getQuerySearchCards($search) |
|
81 | { |
||
82 | 5 | $query = $this->getEntityManager()->createQueryBuilder(); |
|
83 | 5 | $query->select('p, c')->from('MooFlashCardBundle:Card', 'p'); |
|
84 | 5 | $query->join('p.category', 'c'); |
|
85 | 5 | $query->where('p.content LIKE :name'); |
|
86 | 5 | $query->orWhere('p.title LIKE :name'); |
|
87 | 5 | $query->orWhere('p.slug LIKE :name'); |
|
88 | 5 | $query->andWhere('p.active = 1'); |
|
89 | 5 | $query->andWhere('c.active = 1'); |
|
90 | 5 | $query->setParameter('name', '%' . $search . '%'); |
|
91 | 5 | $query->orderBy('p.id', 'ASC'); |
|
92 | |||
93 | 5 | return $query; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Return query to search cards by a specific criteria. |
||
98 | * - Cards by category |
||
99 | * - Cards by keyword |
||
100 | * - All cards |
||
101 | * |
||
102 | * @param array $criteria |
||
103 | * |
||
104 | * @return \Knp\Component\Pager\Pagination\PaginationInterface |
||
105 | */ |
||
106 | 4 | public function getQueryBy(array $criteria) |
|
107 | { |
||
108 | 4 | $query = ''; |
|
109 | 4 | $category = 0; |
|
110 | 4 | extract($criteria); |
|
111 | |||
112 | // Search query or all cards |
||
113 | 4 | if ($query !== '') { |
|
114 | 2 | $cards = $this->getQuerySearchCards($query); |
|
115 | } else { |
||
116 | 2 | $cards = $this->getQueryAllCards(); |
|
117 | } |
||
118 | |||
119 | // Filter query by category ID |
||
120 | 4 | if ($category > 0) { |
|
121 | $cards = $cards |
||
122 | ->where('c.id = :category') |
||
123 | ->setParameter('category', $category); |
||
124 | } |
||
125 | |||
126 | 4 | return $cards; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Return paginated cards by a specific criteria. |
||
131 | * - Cards by category |
||
132 | * - Cards by keyword |
||
133 | * - All cards |
||
134 | * |
||
135 | * @param array $criteria |
||
136 | * @param int $page |
||
137 | * @param int $limit |
||
138 | * |
||
139 | * @throws \LogicException |
||
140 | * |
||
141 | * @return \Knp\Component\Pager\Pagination\PaginationInterface |
||
142 | */ |
||
143 | 4 | public function fetchCardsBy(array $criteria, $page = 1, $limit = 20) |
|
147 | |||
148 | /** |
||
149 | * Return all cards paginated |
||
150 | * |
||
151 | * @param int $page |
||
152 | * @param int $limit |
||
153 | * |
||
154 | * @throws \LogicException |
||
155 | * |
||
156 | * @return \Knp\Component\Pager\Pagination\PaginationInterface |
||
157 | */ |
||
158 | 1 | public function fetchCards($page = 1, $limit = 20) |
|
162 | |||
163 | /** |
||
164 | * Search for a card by keyword |
||
165 | * |
||
166 | * @param string $search |
||
167 | * |
||
168 | * @return null|\Moo\FlashCardBundle\Entity\Card |
||
169 | */ |
||
170 | 3 | public function searchForOne($search) |
|
185 | |||
186 | /** |
||
187 | * Search for a card by a slug |
||
188 | * |
||
189 | * @param string $slug |
||
190 | * |
||
191 | * @return null|\Moo\FlashCardBundle\Entity\Card |
||
192 | */ |
||
193 | 3 | public function findOneBySlugJoinedToCategory($slug) |
|
208 | |||
209 | /** |
||
210 | * Return random cards |
||
211 | * |
||
212 | * @param int $limit |
||
213 | * |
||
214 | * @return array |
||
215 | */ |
||
216 | 1 | public function fetchRadomCards($limit = 30) |
|
240 | |||
241 | /** |
||
242 | * Update card view counter by 1 |
||
243 | * |
||
244 | * @param \Moo\FlashCardBundle\Entity\Card $card |
||
245 | */ |
||
246 | 1 | public function incrementViews(Entity\Card $card) |
|
252 | } |
||
253 |