|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\Controller; |
|
10
|
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Item; |
|
12
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Name; |
|
13
|
|
|
use AnimeDb\Bundle\CatalogBundle\Repository\Item as ItemRepository; |
|
14
|
|
|
use AnimeDb\Bundle\CatalogBundle\Service\Item\ListControls; |
|
15
|
|
|
use AnimeDb\Bundle\CatalogBundle\Service\Item\Search\Manager; |
|
16
|
|
|
use Symfony\Component\Form\Form; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
19
|
|
|
use AnimeDb\Bundle\CatalogBundle\Form\Type\SearchSimple; |
|
20
|
|
|
use AnimeDb\Bundle\CatalogBundle\Form\Type\Settings\General as GeneralForm; |
|
21
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Settings\General as GeneralEntity; |
|
22
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Search as SearchEntity; |
|
23
|
|
|
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Chain as ChainSearch; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Main page of the catalog. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Peter Gribanov <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class HomeController extends BaseController |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Widget place top. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
const WIDGET_PALCE_TOP = 'home.top'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Widget place bottom. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
const WIDGET_PALCE_BOTTOM = 'home.bottom'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Autocomplete list limit. |
|
49
|
|
|
* |
|
50
|
|
|
* @var int |
|
51
|
|
|
*/ |
|
52
|
|
|
const AUTOCOMPLETE_LIMIT = 10; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Home. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Request $request |
|
58
|
|
|
* |
|
59
|
|
|
* @return Response |
|
60
|
|
|
*/ |
|
61
|
|
|
public function indexAction(Request $request) |
|
62
|
|
|
{ |
|
63
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse('AnimeDbCatalogBundle:Item'); |
|
64
|
|
|
// response was not modified for this request |
|
65
|
|
|
if ($response->isNotModified($request)) { |
|
66
|
|
|
return $response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// current page for paging |
|
70
|
|
|
$page = $request->get('page', 1); |
|
71
|
|
|
$current_page = $page > 1 ? $page : 1; |
|
72
|
|
|
|
|
73
|
|
|
/* @var $rep ItemRepository */ |
|
74
|
|
|
$rep = $this->getDoctrine()->getRepository('AnimeDbCatalogBundle:Item'); |
|
75
|
|
|
/* @var $controls ListControls */ |
|
76
|
|
|
$controls = $this->get('anime_db.item.list_controls'); |
|
77
|
|
|
|
|
78
|
|
|
$pagination = null; |
|
79
|
|
|
// show not all items |
|
80
|
|
|
if ($limit = $controls->getLimit($request->query->all())) { |
|
81
|
|
|
$that = $this; |
|
82
|
|
|
$query = $request->query->all(); |
|
83
|
|
|
unset($query['page']); |
|
84
|
|
|
$pagination = $this->get('anime_db.pagination') |
|
85
|
|
|
->create(ceil($rep->count() / $limit), $current_page) |
|
86
|
|
|
->setPageLink(function ($page) use ($that, $query) { |
|
87
|
|
|
return $that->generateUrl('home', array_merge($query, ['page' => $page])); |
|
88
|
|
|
}) |
|
89
|
|
|
->setFirstPageLink($this->generateUrl('home', $query)) |
|
90
|
|
|
->getView(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// get items |
|
94
|
|
|
$items = $rep->getList($limit, ($current_page - 1) * $limit); |
|
95
|
|
|
|
|
96
|
|
|
return $this->render('AnimeDbCatalogBundle:Home:index.html.twig', [ |
|
97
|
|
|
'items' => $items, |
|
98
|
|
|
'pagination' => $pagination, |
|
99
|
|
|
'widget_top' => self::WIDGET_PALCE_TOP, |
|
100
|
|
|
'widget_bottom' => self::WIDGET_PALCE_BOTTOM, |
|
101
|
|
|
], $response); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Search simple form. |
|
106
|
|
|
* |
|
107
|
|
|
* @return Response |
|
108
|
|
|
*/ |
|
109
|
|
|
public function searchSimpleFormAction() |
|
110
|
|
|
{ |
|
111
|
|
|
$form = new SearchSimple($this->generateUrl('home_autocomplete_name')); |
|
112
|
|
|
|
|
113
|
|
|
return $this->render('AnimeDbCatalogBundle:Home:searchSimpleForm.html.twig', [ |
|
114
|
|
|
'form' => $this->createForm($form)->createView(), |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Autocomplete name. |
|
120
|
|
|
* |
|
121
|
|
|
* @param Request $request |
|
122
|
|
|
* |
|
123
|
|
|
* @return Response |
|
124
|
|
|
*/ |
|
125
|
|
|
public function autocompleteNameAction(Request $request) |
|
126
|
|
|
{ |
|
127
|
|
|
/* @var $response JsonResponse */ |
|
128
|
|
|
$response = $this->getCacheTimeKeeper() |
|
129
|
|
|
->getResponse('AnimeDbCatalogBundle:Item', -1, new JsonResponse()); |
|
130
|
|
|
// response was not modified for this request |
|
131
|
|
|
if ($response->isNotModified($request)) { |
|
132
|
|
|
return $response; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$term = mb_strtolower($request->get('term'), 'UTF8'); |
|
136
|
|
|
/* @var $service Manager */ |
|
137
|
|
|
$service = $this->get('anime_db.item.search'); |
|
138
|
|
|
$result = $service->searchByName($term, self::AUTOCOMPLETE_LIMIT); |
|
139
|
|
|
|
|
140
|
|
|
$list = []; |
|
141
|
|
|
/* @var $item Item */ |
|
142
|
|
|
foreach ($result as $item) { |
|
143
|
|
|
if (strpos(mb_strtolower($item->getName(), 'UTF8'), $term) === 0) { |
|
144
|
|
|
$list[] = $item->getName(); |
|
145
|
|
|
} else { |
|
146
|
|
|
/* @var $name Name */ |
|
147
|
|
|
foreach ($item->getNames() as $name) { |
|
148
|
|
|
if (strpos(mb_strtolower($name->getName(), 'UTF8'), $term) === 0) { |
|
149
|
|
|
$list[] = $name->getName(); |
|
150
|
|
|
break; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $response->setData($list); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Search item. |
|
161
|
|
|
* |
|
162
|
|
|
* @param Request $request |
|
163
|
|
|
* |
|
164
|
|
|
* @return Response |
|
165
|
|
|
*/ |
|
166
|
|
|
public function searchAction(Request $request) |
|
167
|
|
|
{ |
|
168
|
|
|
$response = $this->getCacheTimeKeeper() |
|
169
|
|
|
->getResponse(['AnimeDbCatalogBundle:Item', 'AnimeDbCatalogBundle:Storage']); |
|
170
|
|
|
// response was not modified for this request |
|
171
|
|
|
if ($response->isNotModified($request)) { |
|
172
|
|
|
return $response; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/* @var $form Form */ |
|
176
|
|
|
$form = $this->createForm('search', new SearchEntity())->handleRequest($request); |
|
177
|
|
|
$pagination = null; |
|
178
|
|
|
$result = ['list' => [], 'total' => 0]; |
|
179
|
|
|
|
|
180
|
|
|
if ($form->isValid()) { |
|
181
|
|
|
/* @var $controls ListControls */ |
|
182
|
|
|
$controls = $this->get('anime_db.item.list_controls'); |
|
183
|
|
|
|
|
184
|
|
|
// current page for paging |
|
185
|
|
|
$current_page = $request->get('page', 1); |
|
186
|
|
|
$current_page = $current_page > 1 ? $current_page : 1; |
|
187
|
|
|
|
|
188
|
|
|
// get items limit |
|
189
|
|
|
$limit = $controls->getLimit($request->query->all()); |
|
190
|
|
|
|
|
191
|
|
|
// do search |
|
192
|
|
|
$result = $this->get('anime_db.item.search')->search( |
|
193
|
|
|
$form->getData(), |
|
194
|
|
|
$limit, |
|
195
|
|
|
($current_page - 1) * $limit, |
|
196
|
|
|
$controls->getSortColumn($request->query->all()), |
|
197
|
|
|
$controls->getSortDirection($request->query->all()) |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
if ($limit) { |
|
201
|
|
|
// build pagination |
|
202
|
|
|
$that = $this; |
|
203
|
|
|
$query = $request->query->all(); |
|
204
|
|
|
unset($query['page']); |
|
205
|
|
|
$pagination = $this->get('anime_db.pagination') |
|
206
|
|
|
->create(ceil($result['total'] / $limit), $current_page) |
|
207
|
|
|
->setPageLink(function ($page) use ($that, $query) { |
|
208
|
|
|
return $that->generateUrl('home_search', array_merge($query, ['page' => $page])); |
|
209
|
|
|
}) |
|
210
|
|
|
->setFirstPageLink($this->generateUrl('home_search', $query)) |
|
211
|
|
|
->getView(); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return $this->render('AnimeDbCatalogBundle:Home:search.html.twig', [ |
|
216
|
|
|
'form' => $form->createView(), |
|
217
|
|
|
'items' => $result['list'], |
|
218
|
|
|
'total' => $result['total'], |
|
219
|
|
|
'pagination' => $pagination, |
|
220
|
|
|
'searched' => (bool) $request->query->count(), |
|
221
|
|
|
], $response); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* General settings. |
|
226
|
|
|
* |
|
227
|
|
|
* @param Request $request |
|
228
|
|
|
* |
|
229
|
|
|
* @return Response |
|
230
|
|
|
*/ |
|
231
|
|
|
public function settingsAction(Request $request) |
|
232
|
|
|
{ |
|
233
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse(); |
|
234
|
|
|
// response was not modified for this request |
|
235
|
|
|
if ($response->isNotModified($request)) { |
|
236
|
|
|
return $response; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$entity = (new GeneralEntity()) |
|
240
|
|
|
->setTaskScheduler($this->container->getParameter('task_scheduler.enabled')) |
|
241
|
|
|
->setDefaultSearch($this->container->getParameter('anime_db.catalog.default_search')) |
|
242
|
|
|
->setLocale($request->getLocale()); |
|
243
|
|
|
|
|
244
|
|
|
/* @var $chain ChainSearch */ |
|
245
|
|
|
$chain = $this->get('anime_db.plugin.search_fill'); |
|
246
|
|
|
/* @var $form Form */ |
|
247
|
|
|
$form = $this->createForm(new GeneralForm($chain), $entity) |
|
248
|
|
|
->handleRequest($request); |
|
249
|
|
|
|
|
250
|
|
|
if ($form->isValid()) { |
|
251
|
|
|
// update params |
|
252
|
|
|
$this->get('anime_db.manipulator.parameters') |
|
253
|
|
|
->set('task_scheduler.enabled', $entity->getTaskScheduler()); |
|
254
|
|
|
$this->get('anime_db.manipulator.parameters') |
|
255
|
|
|
->set('anime_db.catalog.default_search', $entity->getDefaultSearch()); |
|
256
|
|
|
$this->get('anime_db.manipulator.parameters') |
|
257
|
|
|
->set('locale', $entity->getLocale()); |
|
258
|
|
|
$this->get('anime_db.app.listener.request')->setLocale($request, $entity->getLocale()); |
|
259
|
|
|
$this->get('anime_db.cache_clearer')->clear(); |
|
260
|
|
|
|
|
261
|
|
|
return $this->redirect($this->generateUrl('home_settings')); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
return $this->render('AnimeDbCatalogBundle:Home:settings.html.twig', [ |
|
265
|
|
|
'form' => $form->createView(), |
|
266
|
|
|
], $response); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|