1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\CategoryBundle\Controller; |
||
13 | |||
14 | use Mautic\CategoryBundle\CategoryEvents; |
||
15 | use Mautic\CategoryBundle\Event\CategoryTypesEvent; |
||
16 | use Mautic\CategoryBundle\Model\CategoryModel; |
||
17 | use Mautic\CoreBundle\Controller\AbstractFormController; |
||
18 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
19 | |||
20 | class CategoryController extends AbstractFormController |
||
21 | { |
||
22 | /** |
||
23 | * @param $bundle |
||
24 | * @param $objectAction |
||
25 | * @param int $objectId |
||
26 | * @param string $objectModel |
||
27 | * |
||
28 | * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
29 | */ |
||
30 | public function executeCategoryAction($bundle, $objectAction, $objectId = 0, $objectModel = '') |
||
31 | { |
||
32 | if (method_exists($this, "{$objectAction}Action")) { |
||
33 | return $this->{"{$objectAction}Action"}($bundle, $objectId, $objectModel); |
||
34 | } else { |
||
35 | return $this->accessDenied(); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param $bundle |
||
41 | * @param int $page |
||
42 | * |
||
43 | * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
44 | */ |
||
45 | public function indexAction($bundle, $page = 1) |
||
46 | { |
||
47 | $session = $this->get('session'); |
||
48 | |||
49 | $search = $this->request->query->get('search', $session->get('mautic.category.filter', '')); |
||
50 | $bundle = $this->request->query->get('bundle', $session->get('mautic.category.type', $bundle)); |
||
51 | |||
52 | if ($bundle) { |
||
53 | $session->set('mautic.category.type', $bundle); |
||
54 | } |
||
55 | |||
56 | // hack to make pagination work for default list view |
||
57 | if ('all' == $bundle) { |
||
58 | $bundle = 'category'; |
||
59 | } |
||
60 | |||
61 | $session->set('mautic.category.filter', $search); |
||
62 | |||
63 | //set some permissions |
||
64 | $permissionBase = $this->getModel('category')->getPermissionBase($bundle); |
||
65 | $permissions = $this->get('mautic.security')->isGranted( |
||
66 | [ |
||
67 | $permissionBase.':view', |
||
68 | $permissionBase.':create', |
||
69 | $permissionBase.':edit', |
||
70 | $permissionBase.':delete', |
||
71 | ], |
||
72 | 'RETURN_ARRAY' |
||
73 | ); |
||
74 | |||
75 | if (!$permissions[$permissionBase.':view']) { |
||
76 | return $this->accessDenied(); |
||
77 | } |
||
78 | |||
79 | $this->setListFilters(); |
||
80 | |||
81 | $viewParams = [ |
||
82 | 'page' => $page, |
||
83 | 'bundle' => $bundle, |
||
84 | ]; |
||
85 | |||
86 | //set limits |
||
87 | $limit = $session->get('mautic.category.limit', $this->coreParametersHelper->get('default_pagelimit')); |
||
88 | $start = (1 === $page) ? 0 : (($page - 1) * $limit); |
||
89 | if ($start < 0) { |
||
90 | $start = 0; |
||
91 | } |
||
92 | |||
93 | $filter = ['string' => $search]; |
||
94 | |||
95 | if ('category' != $bundle) { |
||
96 | $filter['force'] = [ |
||
97 | [ |
||
98 | 'column' => 'c.bundle', |
||
99 | 'expr' => 'eq', |
||
100 | 'value' => $bundle, |
||
101 | ], |
||
102 | ]; |
||
103 | } |
||
104 | |||
105 | $orderBy = $this->get('session')->get('mautic.category.orderby', 'c.title'); |
||
106 | $orderByDir = $this->get('session')->get('mautic.category.orderbydir', 'DESC'); |
||
107 | |||
108 | $entities = $this->getModel('category')->getEntities( |
||
109 | [ |
||
110 | 'start' => $start, |
||
111 | 'limit' => $limit, |
||
112 | 'filter' => $filter, |
||
113 | 'orderBy' => $orderBy, |
||
114 | 'orderByDir' => $orderByDir, |
||
115 | ] |
||
116 | ); |
||
117 | |||
118 | $count = count($entities); |
||
119 | if ($count && $count < ($start + 1)) { |
||
120 | //the number of entities are now less then the current page so redirect to the last page |
||
121 | if (1 === $count) { |
||
122 | $lastPage = 1; |
||
123 | } else { |
||
124 | $lastPage = (ceil($count / $limit)) ?: 1; |
||
125 | } |
||
126 | $viewParams['page'] = $lastPage; |
||
127 | $session->set('mautic.category.page', $lastPage); |
||
128 | $returnUrl = $this->generateUrl('mautic_category_index', $viewParams); |
||
129 | |||
130 | return $this->postActionRedirect( |
||
131 | [ |
||
132 | 'returnUrl' => $returnUrl, |
||
133 | 'viewParameters' => ['page' => $lastPage], |
||
134 | 'contentTemplate' => 'MauticCategoryBundle:Category:index', |
||
135 | 'passthroughVars' => [ |
||
136 | 'activeLink' => '#mautic_'.$bundle.'category_index', |
||
137 | 'mauticContent' => 'category', |
||
138 | ], |
||
139 | ] |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | $categoryTypes = ['category' => $this->get('translator')->trans('mautic.core.select')]; |
||
144 | |||
145 | $dispatcher = $this->dispatcher; |
||
146 | if ($dispatcher->hasListeners(CategoryEvents::CATEGORY_ON_BUNDLE_LIST_BUILD)) { |
||
147 | $event = new CategoryTypesEvent(); |
||
148 | $dispatcher->dispatch(CategoryEvents::CATEGORY_ON_BUNDLE_LIST_BUILD, $event); |
||
149 | $categoryTypes = array_merge($categoryTypes, $event->getCategoryTypes()); |
||
150 | } |
||
151 | |||
152 | //set what page currently on so that we can return here after form submission/cancellation |
||
153 | $session->set('mautic.category.page', $page); |
||
154 | |||
155 | $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index'; |
||
156 | |||
157 | return $this->delegateView( |
||
158 | [ |
||
159 | 'returnUrl' => $this->generateUrl('mautic_category_index', $viewParams), |
||
160 | 'viewParameters' => [ |
||
161 | 'bundle' => $bundle, |
||
162 | 'permissionBase' => $permissionBase, |
||
163 | 'searchValue' => $search, |
||
164 | 'items' => $entities, |
||
165 | 'page' => $page, |
||
166 | 'limit' => $limit, |
||
167 | 'permissions' => $permissions, |
||
168 | 'tmpl' => $tmpl, |
||
169 | 'categoryTypes' => $categoryTypes, |
||
170 | ], |
||
171 | 'contentTemplate' => 'MauticCategoryBundle:Category:list.html.php', |
||
172 | 'passthroughVars' => [ |
||
173 | 'activeLink' => '#mautic_'.$bundle.'category_index', |
||
174 | 'mauticContent' => 'category', |
||
175 | 'route' => $this->generateUrl('mautic_category_index', $viewParams), |
||
176 | ], |
||
177 | ] |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Generates new form and processes post data. |
||
183 | * |
||
184 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
185 | */ |
||
186 | public function newAction($bundle) |
||
187 | { |
||
188 | $session = $this->get('session'); |
||
189 | $model = $this->getModel('category'); |
||
190 | $entity = $model->getEntity(); |
||
191 | $success = $closeModal = 0; |
||
192 | $cancelled = $valid = false; |
||
193 | $method = $this->request->getMethod(); |
||
194 | $inForm = ('POST' == $method) ? $this->request->request->get('category_form')['inForm'] : $this->request->get('inForm', 0); |
||
195 | $showSelect = $this->request->get('show_bundle_select', false); |
||
196 | |||
197 | //not found |
||
198 | if (!$this->get('mautic.security')->isGranted($model->getPermissionBase($bundle).':create')) { |
||
199 | return $this->modalAccessDenied(); |
||
200 | } |
||
201 | //Create the form |
||
202 | $action = $this->generateUrl('mautic_category_action', [ |
||
203 | 'objectAction' => 'new', |
||
204 | 'bundle' => $bundle, |
||
205 | ]); |
||
206 | $form = $model->createForm($entity, $this->get('form.factory'), $action, ['bundle' => $bundle, 'show_bundle_select' => $showSelect]); |
||
207 | $form['inForm']->setData($inForm); |
||
208 | ///Check for a submitted form and process it |
||
209 | if ('POST' == $method) { |
||
210 | $valid = false; |
||
211 | if (!$cancelled = $this->isFormCancelled($form)) { |
||
212 | if ($valid = $this->isFormValid($form)) { |
||
213 | $success = 1; |
||
214 | |||
215 | //form is valid so process the data |
||
216 | $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked()); |
||
217 | |||
218 | $this->addFlash('mautic.category.notice.created', [ |
||
219 | '%name%' => $entity->getName(), |
||
220 | ]); |
||
221 | } |
||
222 | } else { |
||
223 | $success = 1; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | $closeModal = ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())); |
||
228 | |||
229 | if ($closeModal) { |
||
230 | if ($inForm) { |
||
231 | return new JsonResponse([ |
||
232 | 'mauticContent' => 'category', |
||
233 | 'closeModal' => 1, |
||
234 | 'inForm' => 1, |
||
235 | 'categoryName' => $entity->getName(), |
||
236 | 'categoryId' => $entity->getId(), |
||
237 | ]); |
||
238 | } |
||
239 | |||
240 | $viewParameters = [ |
||
241 | 'page' => $session->get('mautic.category.page'), |
||
242 | 'bundle' => $bundle, |
||
243 | ]; |
||
244 | |||
245 | return $this->postActionRedirect([ |
||
246 | 'returnUrl' => $this->generateUrl('mautic_category_index', $viewParameters), |
||
247 | 'viewParameters' => $viewParameters, |
||
248 | 'contentTemplate' => 'MauticCategoryBundle:Category:index', |
||
249 | 'passthroughVars' => [ |
||
250 | 'activeLink' => '#mautic_'.$bundle.'category_index', |
||
251 | 'mauticContent' => 'category', |
||
252 | 'closeModal' => 1, |
||
253 | ], |
||
254 | ]); |
||
255 | } elseif (!empty($valid)) { |
||
256 | //return edit view to prevent duplicates |
||
257 | return $this->editAction($bundle, $entity->getId(), true); |
||
258 | } else { |
||
259 | return $this->ajaxAction([ |
||
260 | 'contentTemplate' => 'MauticCategoryBundle:Category:form.html.php', |
||
261 | 'viewParameters' => [ |
||
262 | 'form' => $form->createView(), |
||
263 | 'activeCategory' => $entity, |
||
264 | 'bundle' => $bundle, |
||
265 | ], |
||
266 | 'passthroughVars' => [ |
||
267 | 'mauticContent' => 'category', |
||
268 | 'success' => $success, |
||
269 | 'route' => false, |
||
270 | ], |
||
271 | ]); |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Generates edit form and processes post data. |
||
277 | * |
||
278 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
279 | */ |
||
280 | public function editAction($bundle, $objectId, $ignorePost = false) |
||
281 | { |
||
282 | $session = $this->get('session'); |
||
283 | /** @var CategoryModel $model */ |
||
284 | $model = $this->getModel('category'); |
||
285 | $entity = $model->getEntity($objectId); |
||
286 | $success = $closeModal = 0; |
||
287 | $cancelled = $valid = false; |
||
288 | $method = $this->request->getMethod(); |
||
289 | $inForm = ('POST' == $method) ? $this->request->request->get('category_form')['inForm'] : $this->request->get('inForm', 0); |
||
290 | |||
291 | //not found |
||
292 | if (null === $entity) { |
||
293 | $closeModal = true; |
||
294 | } elseif (!$this->get('mautic.security')->isGranted($model->getPermissionBase($bundle).':view')) { |
||
295 | return $this->modalAccessDenied(); |
||
296 | } elseif ($model->isLocked($entity)) { |
||
297 | return $this->modalAccessDenied(); |
||
298 | } |
||
299 | |||
300 | //Create the form |
||
301 | $action = $this->generateUrl( |
||
302 | 'mautic_category_action', |
||
303 | [ |
||
304 | 'objectAction' => 'edit', |
||
305 | 'objectId' => $objectId, |
||
306 | 'bundle' => $bundle, |
||
307 | ] |
||
308 | ); |
||
309 | $form = $model->createForm($entity, $this->get('form.factory'), $action, ['bundle' => $bundle]); |
||
310 | $form['inForm']->setData($inForm); |
||
311 | |||
312 | ///Check for a submitted form and process it |
||
313 | if (!$ignorePost && 'POST' == $method) { |
||
314 | $valid = false; |
||
315 | if (!$cancelled = $this->isFormCancelled($form)) { |
||
316 | if ($valid = $this->isFormValid($form)) { |
||
317 | $success = 1; |
||
318 | |||
319 | //form is valid so process the data |
||
320 | $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked()); |
||
321 | |||
322 | $this->addFlash( |
||
323 | 'mautic.category.notice.updated', |
||
324 | [ |
||
325 | '%name%' => $entity->getTitle(), |
||
326 | ] |
||
327 | ); |
||
328 | |||
329 | if ($form->get('buttons')->get('apply')->isClicked()) { |
||
330 | // Rebuild the form with new action so that apply doesn't keep creating a clone |
||
331 | $action = $this->generateUrl( |
||
332 | 'mautic_category_action', |
||
333 | [ |
||
334 | 'objectAction' => 'edit', |
||
335 | 'objectId' => $entity->getId(), |
||
336 | 'bundle' => $bundle, |
||
337 | ] |
||
338 | ); |
||
339 | $form = $model->createForm($entity, $this->get('form.factory'), $action, ['bundle' => $bundle]); |
||
340 | } |
||
341 | } |
||
342 | } else { |
||
343 | $success = 1; |
||
344 | |||
345 | //unlock the entity |
||
346 | $model->unlockEntity($entity); |
||
347 | } |
||
348 | } else { |
||
349 | //lock the entity |
||
350 | $model->lockEntity($entity); |
||
351 | } |
||
352 | |||
353 | $closeModal = ($closeModal || $cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())); |
||
354 | |||
355 | if ($closeModal) { |
||
356 | if ($inForm) { |
||
357 | return new JsonResponse( |
||
358 | [ |
||
359 | 'mauticContent' => 'category', |
||
360 | 'closeModal' => 1, |
||
361 | 'inForm' => 1, |
||
362 | 'categoryName' => $entity->getTitle(), |
||
363 | 'categoryId' => $entity->getId(), |
||
364 | ] |
||
365 | ); |
||
366 | } |
||
367 | |||
368 | $viewParameters = [ |
||
369 | 'page' => $session->get('mautic.category.page'), |
||
370 | 'bundle' => $bundle, |
||
371 | ]; |
||
372 | |||
373 | return $this->postActionRedirect( |
||
374 | [ |
||
375 | 'returnUrl' => $this->generateUrl('mautic_category_index', $viewParameters), |
||
376 | 'viewParameters' => $viewParameters, |
||
377 | 'contentTemplate' => 'MauticCategoryBundle:Category:index', |
||
378 | 'passthroughVars' => [ |
||
379 | 'activeLink' => '#mautic_'.$bundle.'category_index', |
||
380 | 'mauticContent' => 'category', |
||
381 | 'closeModal' => 1, |
||
382 | ], |
||
383 | ] |
||
384 | ); |
||
385 | } else { |
||
386 | return $this->ajaxAction( |
||
387 | [ |
||
388 | 'contentTemplate' => 'MauticCategoryBundle:Category:form.html.php', |
||
389 | 'viewParameters' => [ |
||
390 | 'form' => $form->createView(), |
||
391 | 'activeCategory' => $entity, |
||
392 | 'bundle' => $bundle, |
||
393 | ], |
||
394 | 'passthroughVars' => [ |
||
395 | 'mauticContent' => 'category', |
||
396 | 'success' => $success, |
||
397 | 'route' => false, |
||
398 | ], |
||
399 | ] |
||
400 | ); |
||
401 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Deletes the entity. |
||
406 | * |
||
407 | * @param $objectId |
||
408 | * |
||
409 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
410 | */ |
||
411 | public function deleteAction($bundle, $objectId) |
||
412 | { |
||
413 | $session = $this->get('session'); |
||
414 | $page = $session->get('mautic.category.page', 1); |
||
415 | $viewParams = [ |
||
416 | 'page' => $page, |
||
417 | 'bundle' => $bundle, |
||
418 | ]; |
||
419 | $returnUrl = $this->generateUrl('mautic_category_index', $viewParams); |
||
420 | $flashes = []; |
||
421 | |||
422 | $postActionVars = [ |
||
423 | 'returnUrl' => $returnUrl, |
||
424 | 'viewParameters' => $viewParams, |
||
425 | 'contentTemplate' => 'MauticCategoryBundle:Category:index', |
||
426 | 'passthroughVars' => [ |
||
427 | 'activeLink' => 'mautic_'.$bundle.'category_index', |
||
428 | 'mauticContent' => 'category', |
||
429 | ], |
||
430 | ]; |
||
431 | |||
432 | if ('POST' == $this->request->getMethod()) { |
||
433 | $model = $this->getModel('category'); |
||
434 | $entity = $model->getEntity($objectId); |
||
435 | |||
436 | if (null === $entity) { |
||
437 | $flashes[] = [ |
||
438 | 'type' => 'error', |
||
439 | 'msg' => 'mautic.category.error.notfound', |
||
440 | 'msgVars' => ['%id%' => $objectId], |
||
441 | ]; |
||
442 | } elseif (!$this->get('mautic.security')->isGranted($model->getPermissionBase($bundle).':delete')) { |
||
443 | return $this->accessDenied(); |
||
444 | } elseif ($model->isLocked($entity)) { |
||
445 | return $this->isLocked($postActionVars, $entity, 'category.category'); |
||
446 | } |
||
447 | |||
448 | $model->deleteEntity($entity); |
||
449 | |||
450 | $flashes[] = [ |
||
451 | 'type' => 'notice', |
||
452 | 'msg' => 'mautic.core.notice.deleted', |
||
453 | 'msgVars' => [ |
||
454 | '%name%' => $entity->getTitle(), |
||
455 | '%id%' => $objectId, |
||
456 | ], |
||
457 | ]; |
||
458 | } //else don't do anything |
||
459 | |||
460 | return $this->postActionRedirect( |
||
461 | array_merge($postActionVars, [ |
||
462 | 'flashes' => $flashes, |
||
463 | ]) |
||
464 | ); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Deletes a group of entities. |
||
469 | * |
||
470 | * @param string $bundle |
||
471 | * |
||
472 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
473 | */ |
||
474 | public function batchDeleteAction($bundle) |
||
475 | { |
||
476 | $session = $this->get('session'); |
||
477 | $page = $session->get('mautic.category.page', 1); |
||
478 | $viewParams = [ |
||
479 | 'page' => $page, |
||
480 | 'bundle' => $bundle, |
||
481 | ]; |
||
482 | $returnUrl = $this->generateUrl('mautic_category_index', $viewParams); |
||
483 | $flashes = []; |
||
484 | |||
485 | $postActionVars = [ |
||
486 | 'returnUrl' => $returnUrl, |
||
487 | 'viewParameters' => $viewParams, |
||
488 | 'contentTemplate' => 'MauticCategoryBundle:Category:index', |
||
489 | 'passthroughVars' => [ |
||
490 | 'activeLink' => 'mautic_'.$bundle.'category_index', |
||
491 | 'mauticContent' => 'category', |
||
492 | ], |
||
493 | ]; |
||
494 | |||
495 | if ('POST' == $this->request->getMethod()) { |
||
496 | $model = $this->getModel('category'); |
||
497 | $ids = json_decode($this->request->query->get('ids', '{}')); |
||
498 | $deleteIds = []; |
||
499 | |||
500 | // Loop over the IDs to perform access checks pre-delete |
||
501 | foreach ($ids as $objectId) { |
||
502 | $entity = $model->getEntity($objectId); |
||
503 | |||
504 | if (null === $entity) { |
||
505 | $flashes[] = [ |
||
506 | 'type' => 'error', |
||
507 | 'msg' => 'mautic.category.error.notfound', |
||
508 | 'msgVars' => ['%id%' => $objectId], |
||
509 | ]; |
||
510 | } elseif (!$this->get('mautic.security')->isGranted($model->getPermissionBase($bundle).':delete')) { |
||
511 | $flashes[] = $this->accessDenied(true); |
||
512 | } elseif ($model->isLocked($entity)) { |
||
513 | $flashes[] = $this->isLocked($postActionVars, $entity, 'category', true); |
||
514 | } else { |
||
515 | $deleteIds[] = $objectId; |
||
516 | } |
||
517 | } |
||
518 | |||
519 | // Delete everything we are able to |
||
520 | if (!empty($deleteIds)) { |
||
521 | $entities = $model->deleteEntities($deleteIds); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
522 | |||
523 | $flashes[] = [ |
||
524 | 'type' => 'notice', |
||
525 | 'msg' => 'mautic.category.notice.batch_deleted', |
||
526 | 'msgVars' => [ |
||
527 | '%count%' => count($entities), |
||
528 | ], |
||
529 | ]; |
||
530 | } |
||
531 | } //else don't do anything |
||
532 | |||
533 | return $this->postActionRedirect( |
||
534 | array_merge($postActionVars, [ |
||
535 | 'flashes' => $flashes, |
||
536 | ]) |
||
537 | ); |
||
538 | } |
||
539 | } |
||
540 |