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\PointBundle\Controller; |
||
13 | |||
14 | use Mautic\CoreBundle\Controller\AbstractFormController; |
||
15 | use Mautic\CoreBundle\Factory\PageHelperFactoryInterface; |
||
16 | use Mautic\PointBundle\Entity\Point; |
||
17 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
18 | use Symfony\Component\HttpFoundation\Response; |
||
19 | |||
20 | class PointController extends AbstractFormController |
||
21 | { |
||
22 | /** |
||
23 | * @param int $page |
||
24 | * |
||
25 | * @return JsonResponse|Response |
||
26 | */ |
||
27 | public function indexAction($page = 1) |
||
28 | { |
||
29 | //set some permissions |
||
30 | $permissions = $this->get('mautic.security')->isGranted([ |
||
31 | 'point:points:view', |
||
32 | 'point:points:create', |
||
33 | 'point:points:edit', |
||
34 | 'point:points:delete', |
||
35 | 'point:points:publish', |
||
36 | ], 'RETURN_ARRAY'); |
||
37 | |||
38 | if (!$permissions['point:points:view']) { |
||
39 | return $this->accessDenied(); |
||
40 | } |
||
41 | |||
42 | $this->setListFilters(); |
||
43 | |||
44 | /** @var PageHelperFactoryInterface $pageHelperFacotry */ |
||
45 | $pageHelperFacotry = $this->get('mautic.page.helper.factory'); |
||
46 | $pageHelper = $pageHelperFacotry->make('mautic.point', $page); |
||
47 | |||
48 | $limit = $pageHelper->getLimit(); |
||
49 | $start = $pageHelper->getStart(); |
||
50 | $search = $this->request->get('search', $this->get('session')->get('mautic.point.filter', '')); |
||
51 | $filter = ['string' => $search, 'force' => []]; |
||
52 | $orderBy = $this->get('session')->get('mautic.point.orderby', 'p.name'); |
||
53 | $orderByDir = $this->get('session')->get('mautic.point.orderbydir', 'ASC'); |
||
54 | $points = $this->getModel('point')->getEntities([ |
||
55 | 'start' => $start, |
||
56 | 'limit' => $limit, |
||
57 | 'filter' => $filter, |
||
58 | 'orderBy' => $orderBy, |
||
59 | 'orderByDir' => $orderByDir, |
||
60 | ]); |
||
61 | |||
62 | $this->get('session')->set('mautic.point.filter', $search); |
||
63 | |||
64 | $count = count($points); |
||
65 | if ($count && $count < ($start + 1)) { |
||
66 | $lastPage = $pageHelper->countPage($count); |
||
67 | $returnUrl = $this->generateUrl('mautic_point_index', ['page' => $lastPage]); |
||
68 | $pageHelper->rememberPage($lastPage); |
||
69 | |||
70 | return $this->postActionRedirect([ |
||
71 | 'returnUrl' => $returnUrl, |
||
72 | 'viewParameters' => ['page' => $lastPage], |
||
73 | 'contentTemplate' => 'MauticPointBundle:Point:index', |
||
74 | 'passthroughVars' => [ |
||
75 | 'activeLink' => '#mautic_point_index', |
||
76 | 'mauticContent' => 'point', |
||
77 | ], |
||
78 | ]); |
||
79 | } |
||
80 | |||
81 | $pageHelper->rememberPage($page); |
||
82 | |||
83 | //get the list of actions |
||
84 | $actions = $this->getModel('point')->getPointActions(); |
||
85 | |||
86 | return $this->delegateView([ |
||
87 | 'viewParameters' => [ |
||
88 | 'searchValue' => $search, |
||
89 | 'items' => $points, |
||
90 | 'actions' => $actions['actions'], |
||
91 | 'page' => $page, |
||
92 | 'limit' => $limit, |
||
93 | 'permissions' => $permissions, |
||
94 | 'tmpl' => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index', |
||
95 | ], |
||
96 | 'contentTemplate' => 'MauticPointBundle:Point:list.html.php', |
||
97 | 'passthroughVars' => [ |
||
98 | 'activeLink' => '#mautic_point_index', |
||
99 | 'mauticContent' => 'point', |
||
100 | 'route' => $this->generateUrl('mautic_point_index', ['page' => $page]), |
||
101 | ], |
||
102 | ]); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Generates new form and processes post data. |
||
107 | * |
||
108 | * @param \Mautic\PointBundle\Entity\Point $entity |
||
109 | * |
||
110 | * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response |
||
111 | */ |
||
112 | public function newAction($entity = null) |
||
113 | { |
||
114 | $model = $this->getModel('point'); |
||
115 | |||
116 | if (!($entity instanceof Point)) { |
||
117 | /** @var \Mautic\PointBundle\Entity\Point $entity */ |
||
118 | $entity = $model->getEntity(); |
||
119 | } |
||
120 | |||
121 | if (!$this->get('mautic.security')->isGranted('point:points:create')) { |
||
122 | return $this->accessDenied(); |
||
123 | } |
||
124 | |||
125 | //set the page we came from |
||
126 | $page = $this->get('session')->get('mautic.point.page', 1); |
||
127 | $method = $this->request->getMethod(); |
||
128 | $point = $this->request->request->get('point', []); |
||
129 | $actionType = 'POST' === $method ? ($point['type'] ?? '') : ''; |
||
130 | $action = $this->generateUrl('mautic_point_action', ['objectAction' => 'new']); |
||
131 | $actions = $model->getPointActions(); |
||
132 | $form = $model->createForm($entity, $this->get('form.factory'), $action, [ |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
133 | 'pointActions' => $actions, |
||
134 | 'actionType' => $actionType, |
||
135 | ]); |
||
136 | $viewParameters = ['page' => $page]; |
||
137 | |||
138 | ///Check for a submitted form and process it |
||
139 | if ('POST' === $method) { |
||
140 | $valid = false; |
||
141 | |||
142 | if (!$cancelled = $this->isFormCancelled($form)) { |
||
143 | if ($valid = $this->isFormValid($form)) { |
||
144 | //form is valid so process the data |
||
145 | $model->saveEntity($entity); |
||
146 | |||
147 | $this->addFlash('mautic.core.notice.created', [ |
||
148 | '%name%' => $entity->getName(), |
||
149 | '%menu_link%' => 'mautic_point_index', |
||
150 | '%url%' => $this->generateUrl('mautic_point_action', [ |
||
151 | 'objectAction' => 'edit', |
||
152 | 'objectId' => $entity->getId(), |
||
153 | ]), |
||
154 | ]); |
||
155 | |||
156 | if ($form->get('buttons')->get('save')->isClicked()) { |
||
157 | $returnUrl = $this->generateUrl('mautic_point_index', $viewParameters); |
||
158 | $template = 'MauticPointBundle:Point:index'; |
||
159 | } else { |
||
160 | //return edit view so that all the session stuff is loaded |
||
161 | return $this->editAction($entity->getId(), true); |
||
162 | } |
||
163 | } |
||
164 | } else { |
||
165 | $returnUrl = $this->generateUrl('mautic_point_index', $viewParameters); |
||
166 | $template = 'MauticPointBundle:Point:index'; |
||
167 | } |
||
168 | |||
169 | if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) { |
||
170 | return $this->postActionRedirect([ |
||
171 | 'returnUrl' => $returnUrl, |
||
172 | 'viewParameters' => $viewParameters, |
||
173 | 'contentTemplate' => $template, |
||
174 | 'passthroughVars' => [ |
||
175 | 'activeLink' => '#mautic_point_index', |
||
176 | 'mauticContent' => 'point', |
||
177 | ], |
||
178 | ]); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | $themes = ['MauticPointBundle:FormTheme\Action']; |
||
183 | if ($actionType && !empty($actions['actions'][$actionType]['formTheme'])) { |
||
184 | $themes[] = $actions['actions'][$actionType]['formTheme']; |
||
185 | } |
||
186 | |||
187 | return $this->delegateView([ |
||
188 | 'viewParameters' => [ |
||
189 | 'tmpl' => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index', |
||
190 | 'entity' => $entity, |
||
191 | 'form' => $this->setFormTheme($form, 'MauticPointBundle:Point:form.html.php', $themes), |
||
192 | 'actions' => $actions['actions'], |
||
193 | ], |
||
194 | 'contentTemplate' => 'MauticPointBundle:Point:form.html.php', |
||
195 | 'passthroughVars' => [ |
||
196 | 'activeLink' => '#mautic_point_index', |
||
197 | 'mauticContent' => 'point', |
||
198 | 'route' => $this->generateUrl('mautic_point_action', [ |
||
199 | 'objectAction' => (!empty($valid) ? 'edit' : 'new'), //valid means a new form was applied |
||
200 | 'objectId' => $entity->getId(), |
||
201 | ] |
||
202 | ), |
||
203 | ], |
||
204 | ]); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Generates edit form and processes post data. |
||
209 | * |
||
210 | * @param int $objectId |
||
211 | * @param bool $ignorePost |
||
212 | * |
||
213 | * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response |
||
214 | */ |
||
215 | public function editAction($objectId, $ignorePost = false) |
||
216 | { |
||
217 | $model = $this->getModel('point'); |
||
218 | $entity = $model->getEntity($objectId); |
||
219 | |||
220 | //set the page we came from |
||
221 | $page = $this->get('session')->get('mautic.point.page', 1); |
||
222 | |||
223 | $viewParameters = ['page' => $page]; |
||
224 | |||
225 | //set the return URL |
||
226 | $returnUrl = $this->generateUrl('mautic_point_index', ['page' => $page]); |
||
227 | |||
228 | $postActionVars = [ |
||
229 | 'returnUrl' => $returnUrl, |
||
230 | 'viewParameters' => $viewParameters, |
||
231 | 'contentTemplate' => 'MauticPointBundle:Point:index', |
||
232 | 'passthroughVars' => [ |
||
233 | 'activeLink' => '#mautic_point_index', |
||
234 | 'mauticContent' => 'point', |
||
235 | ], |
||
236 | ]; |
||
237 | |||
238 | //form not found |
||
239 | if (null === $entity) { |
||
240 | return $this->postActionRedirect( |
||
241 | array_merge($postActionVars, [ |
||
242 | 'flashes' => [ |
||
243 | [ |
||
244 | 'type' => 'error', |
||
245 | 'msg' => 'mautic.point.error.notfound', |
||
246 | 'msgVars' => ['%id%' => $objectId], |
||
247 | ], |
||
248 | ], |
||
249 | ]) |
||
250 | ); |
||
251 | } elseif (!$this->get('mautic.security')->isGranted('point:points:edit')) { |
||
252 | return $this->accessDenied(); |
||
253 | } elseif ($model->isLocked($entity)) { |
||
254 | //deny access if the entity is locked |
||
255 | return $this->isLocked($postActionVars, $entity, 'point'); |
||
256 | } |
||
257 | |||
258 | $method = $this->request->getMethod(); |
||
259 | $point = $this->request->request->get('point', []); |
||
260 | $actionType = 'POST' === $method ? ($point['type'] ?? '') : $entity->getType(); |
||
261 | |||
262 | $action = $this->generateUrl('mautic_point_action', ['objectAction' => 'edit', 'objectId' => $objectId]); |
||
263 | $actions = $model->getPointActions(); |
||
264 | $form = $model->createForm($entity, $this->get('form.factory'), $action, [ |
||
265 | 'pointActions' => $actions, |
||
266 | 'actionType' => $actionType, |
||
267 | ]); |
||
268 | |||
269 | ///Check for a submitted form and process it |
||
270 | if (!$ignorePost && 'POST' === $method) { |
||
271 | $valid = false; |
||
272 | |||
273 | if (!$cancelled = $this->isFormCancelled($form)) { |
||
274 | if ($valid = $this->isFormValid($form)) { |
||
275 | //form is valid so process the data |
||
276 | $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked()); |
||
277 | |||
278 | $this->addFlash('mautic.core.notice.updated', [ |
||
279 | '%name%' => $entity->getName(), |
||
280 | '%menu_link%' => 'mautic_point_index', |
||
281 | '%url%' => $this->generateUrl('mautic_point_action', [ |
||
282 | 'objectAction' => 'edit', |
||
283 | 'objectId' => $entity->getId(), |
||
284 | ]), |
||
285 | ]); |
||
286 | |||
287 | if ($form->get('buttons')->get('save')->isClicked()) { |
||
288 | $returnUrl = $this->generateUrl('mautic_point_index', $viewParameters); |
||
289 | $template = 'MauticPointBundle:Point:index'; |
||
290 | } |
||
291 | } |
||
292 | } else { |
||
293 | //unlock the entity |
||
294 | $model->unlockEntity($entity); |
||
295 | |||
296 | $returnUrl = $this->generateUrl('mautic_point_index', $viewParameters); |
||
297 | $template = 'MauticPointBundle:Point:index'; |
||
298 | } |
||
299 | |||
300 | if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) { |
||
301 | return $this->postActionRedirect( |
||
302 | array_merge($postActionVars, [ |
||
303 | 'returnUrl' => $returnUrl, |
||
304 | 'viewParameters' => $viewParameters, |
||
305 | 'contentTemplate' => $template, |
||
306 | ]) |
||
307 | ); |
||
308 | } |
||
309 | } else { |
||
310 | //lock the entity |
||
311 | $model->lockEntity($entity); |
||
312 | } |
||
313 | |||
314 | $themes = ['MauticPointBundle:FormTheme\Action']; |
||
315 | if (!empty($actions['actions'][$actionType]['formTheme'])) { |
||
316 | $themes[] = $actions['actions'][$actionType]['formTheme']; |
||
317 | } |
||
318 | |||
319 | return $this->delegateView([ |
||
320 | 'viewParameters' => [ |
||
321 | 'tmpl' => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index', |
||
322 | 'entity' => $entity, |
||
323 | 'form' => $this->setFormTheme($form, 'MauticPointBundle:Point:form.html.php', $themes), |
||
324 | 'actions' => $actions['actions'], |
||
325 | ], |
||
326 | 'contentTemplate' => 'MauticPointBundle:Point:form.html.php', |
||
327 | 'passthroughVars' => [ |
||
328 | 'activeLink' => '#mautic_point_index', |
||
329 | 'mauticContent' => 'point', |
||
330 | 'route' => $this->generateUrl('mautic_point_action', [ |
||
331 | 'objectAction' => 'edit', |
||
332 | 'objectId' => $entity->getId(), |
||
333 | ] |
||
334 | ), |
||
335 | ], |
||
336 | ]); |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Clone an entity. |
||
341 | * |
||
342 | * @param int $objectId |
||
343 | * |
||
344 | * @return array|JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response |
||
345 | */ |
||
346 | public function cloneAction($objectId) |
||
347 | { |
||
348 | $model = $this->getModel('point'); |
||
349 | $entity = $model->getEntity($objectId); |
||
350 | |||
351 | if (null != $entity) { |
||
352 | if (!$this->get('mautic.security')->isGranted('point:points:create')) { |
||
353 | return $this->accessDenied(); |
||
354 | } |
||
355 | |||
356 | $entity = clone $entity; |
||
357 | $entity->setIsPublished(false); |
||
358 | } |
||
359 | |||
360 | return $this->newAction($entity); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Deletes the entity. |
||
365 | * |
||
366 | * @param int $objectId |
||
367 | * |
||
368 | * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
369 | */ |
||
370 | public function deleteAction($objectId) |
||
371 | { |
||
372 | $page = $this->get('session')->get('mautic.point.page', 1); |
||
373 | $returnUrl = $this->generateUrl('mautic_point_index', ['page' => $page]); |
||
374 | $flashes = []; |
||
375 | |||
376 | $postActionVars = [ |
||
377 | 'returnUrl' => $returnUrl, |
||
378 | 'viewParameters' => ['page' => $page], |
||
379 | 'contentTemplate' => 'MauticPointBundle:Point:index', |
||
380 | 'passthroughVars' => [ |
||
381 | 'activeLink' => '#mautic_point_index', |
||
382 | 'mauticContent' => 'point', |
||
383 | ], |
||
384 | ]; |
||
385 | |||
386 | if ('POST' == $this->request->getMethod()) { |
||
387 | $model = $this->getModel('point'); |
||
388 | $entity = $model->getEntity($objectId); |
||
389 | |||
390 | if (null === $entity) { |
||
391 | $flashes[] = [ |
||
392 | 'type' => 'error', |
||
393 | 'msg' => 'mautic.point.error.notfound', |
||
394 | 'msgVars' => ['%id%' => $objectId], |
||
395 | ]; |
||
396 | } elseif (!$this->get('mautic.security')->isGranted('point:points:delete')) { |
||
397 | return $this->accessDenied(); |
||
398 | } elseif ($model->isLocked($entity)) { |
||
399 | return $this->isLocked($postActionVars, $entity, 'point'); |
||
400 | } |
||
401 | |||
402 | $model->deleteEntity($entity); |
||
403 | |||
404 | $identifier = $this->get('translator')->trans($entity->getName()); |
||
405 | $flashes[] = [ |
||
406 | 'type' => 'notice', |
||
407 | 'msg' => 'mautic.core.notice.deleted', |
||
408 | 'msgVars' => [ |
||
409 | '%name%' => $identifier, |
||
410 | '%id%' => $objectId, |
||
411 | ], |
||
412 | ]; |
||
413 | } //else don't do anything |
||
414 | |||
415 | return $this->postActionRedirect( |
||
416 | array_merge($postActionVars, [ |
||
417 | 'flashes' => $flashes, |
||
418 | ]) |
||
419 | ); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Deletes a group of entities. |
||
424 | * |
||
425 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse |
||
426 | */ |
||
427 | public function batchDeleteAction() |
||
428 | { |
||
429 | $page = $this->get('session')->get('mautic.point.page', 1); |
||
430 | $returnUrl = $this->generateUrl('mautic_point_index', ['page' => $page]); |
||
431 | $flashes = []; |
||
432 | |||
433 | $postActionVars = [ |
||
434 | 'returnUrl' => $returnUrl, |
||
435 | 'viewParameters' => ['page' => $page], |
||
436 | 'contentTemplate' => 'MauticPointBundle:Point:index', |
||
437 | 'passthroughVars' => [ |
||
438 | 'activeLink' => '#mautic_point_index', |
||
439 | 'mauticContent' => 'point', |
||
440 | ], |
||
441 | ]; |
||
442 | |||
443 | if ('POST' == $this->request->getMethod()) { |
||
444 | $model = $this->getModel('point'); |
||
445 | $ids = json_decode($this->request->query->get('ids', '{}')); |
||
446 | $deleteIds = []; |
||
447 | |||
448 | // Loop over the IDs to perform access checks pre-delete |
||
449 | foreach ($ids as $objectId) { |
||
450 | $entity = $model->getEntity($objectId); |
||
451 | |||
452 | if (null === $entity) { |
||
453 | $flashes[] = [ |
||
454 | 'type' => 'error', |
||
455 | 'msg' => 'mautic.point.error.notfound', |
||
456 | 'msgVars' => ['%id%' => $objectId], |
||
457 | ]; |
||
458 | } elseif (!$this->get('mautic.security')->isGranted('point:points:delete')) { |
||
459 | $flashes[] = $this->accessDenied(true); |
||
460 | } elseif ($model->isLocked($entity)) { |
||
461 | $flashes[] = $this->isLocked($postActionVars, $entity, 'point', true); |
||
462 | } else { |
||
463 | $deleteIds[] = $objectId; |
||
464 | } |
||
465 | } |
||
466 | |||
467 | // Delete everything we are able to |
||
468 | if (!empty($deleteIds)) { |
||
469 | $entities = $model->deleteEntities($deleteIds); |
||
470 | |||
471 | $flashes[] = [ |
||
472 | 'type' => 'notice', |
||
473 | 'msg' => 'mautic.point.notice.batch_deleted', |
||
474 | 'msgVars' => [ |
||
475 | '%count%' => count($entities), |
||
476 | ], |
||
477 | ]; |
||
478 | } |
||
479 | } //else don't do anything |
||
480 | |||
481 | return $this->postActionRedirect( |
||
482 | array_merge($postActionVars, [ |
||
483 | 'flashes' => $flashes, |
||
484 | ]) |
||
485 | ); |
||
486 | } |
||
487 | } |
||
488 |