1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Moo\FlashCardBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Moo\FlashCardBundle\Repository; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityRepository; |
15
|
|
|
use Doctrine\ORM\NoResultException; |
16
|
|
|
use Moo\FlashCardBundle\Entity; |
17
|
|
|
use Knp\Bundle\PaginatorBundle\Definition\PaginatorAwareInterface; |
18
|
|
|
use Knp\Component\Pager\Paginator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Card is a repository class for the card entity. |
22
|
|
|
* |
23
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
24
|
|
|
*/ |
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() |
52
|
|
|
{ |
53
|
4 |
|
return $this->paginator; |
54
|
|
|
} |
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) |
144
|
|
|
{ |
145
|
4 |
|
return $this->getPaginator()->paginate($this->getQueryBy($criteria), $page, $limit); |
146
|
|
|
} |
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) |
159
|
|
|
{ |
160
|
1 |
|
return $this->fetchCardsBy([], $page, $limit); |
161
|
|
|
} |
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) |
171
|
|
|
{ |
172
|
3 |
|
if (empty($search)) { |
173
|
|
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
try { |
177
|
3 |
|
return $this->getQuerySearchCards($search) |
178
|
3 |
|
->setMaxResults(1) |
179
|
3 |
|
->getQuery() |
180
|
3 |
|
->getSingleResult(); |
181
|
1 |
|
} catch (NoResultException $e) { |
182
|
1 |
|
return null; |
183
|
|
|
} |
184
|
|
|
} |
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) |
194
|
|
|
{ |
195
|
3 |
|
$query = $this->getEntityManager()->createQueryBuilder(); |
196
|
3 |
|
$query->select('p, c')->from('MooFlashCardBundle:Card', 'p'); |
197
|
3 |
|
$query->join('p.category', 'c'); |
198
|
3 |
|
$query->where('p.slug = :slug'); |
199
|
3 |
|
$query->setParameter('slug', $slug); |
200
|
3 |
|
$query->setMaxResults(1); |
201
|
|
|
|
202
|
|
|
try { |
203
|
3 |
|
return $query->getQuery()->getSingleResult(); |
204
|
1 |
|
} catch (NoResultException $e) { |
205
|
1 |
|
return null; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return random cards |
211
|
|
|
* |
212
|
|
|
* @param int $limit |
213
|
|
|
* |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
1 |
|
public function fetchRadomCards($limit = 30) |
217
|
|
|
{ |
218
|
1 |
|
$em = $this->getEntityManager(); |
219
|
|
|
|
220
|
|
|
// retrieve the highest card id |
221
|
1 |
|
$max = $em->createQuery('SELECT MAX(c.id) FROM MooFlashCardBundle:Card c') |
222
|
1 |
|
->getSingleScalarResult(); |
223
|
|
|
|
224
|
|
|
// get random cards |
225
|
1 |
|
$query = $this->getEntityManager()->createQueryBuilder(); |
226
|
1 |
|
$query->select('p, c')->from('MooFlashCardBundle:Card', 'p'); |
227
|
1 |
|
$query->join('p.category', 'c'); |
228
|
1 |
|
$query->where('p.id >= :rand'); |
229
|
1 |
|
$query->andWhere('p.active = 1'); |
230
|
1 |
|
$query->andWhere('c.active = 1'); |
231
|
1 |
|
$query->setParameter('rand', mt_rand(0, $max - $limit)); |
232
|
1 |
|
$query->setMaxResults($limit); |
233
|
1 |
|
$query->orderBy('p.id', 'ASC'); |
234
|
|
|
|
235
|
1 |
|
$cards = $query->getQuery()->getResult(); |
236
|
1 |
|
shuffle($cards); |
237
|
|
|
|
238
|
1 |
|
return $cards; |
239
|
|
|
} |
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) |
247
|
|
|
{ |
248
|
1 |
|
$card->setViews($card->getViews() + 1); |
249
|
1 |
|
$em = $this->getEntityManager(); |
250
|
1 |
|
$em->flush(); |
251
|
1 |
|
} |
252
|
|
|
} |
253
|
|
|
|