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