Issues (3627)

bundles/StageBundle/Controller/StageController.php (1 issue)

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\StageBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\AbstractFormController;
15
use Mautic\CoreBundle\Factory\PageHelperFactoryInterface;
16
use Mautic\StageBundle\Entity\Stage;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\Response;
19
20
class StageController 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
            [
32
                'stage:stages:view',
33
                'stage:stages:create',
34
                'stage:stages:edit',
35
                'stage:stages:delete',
36
                'stage:stages:publish',
37
            ],
38
            'RETURN_ARRAY'
39
        );
40
41
        if (!$permissions['stage:stages:view']) {
42
            return $this->accessDenied();
43
        }
44
45
        $this->setListFilters();
46
47
        /** @var PageHelperFactoryInterface $pageHelperFacotry */
48
        $pageHelperFacotry = $this->get('mautic.page.helper.factory');
49
        $pageHelper        = $pageHelperFacotry->make('mautic.stage', $page);
50
51
        $limit      = $pageHelper->getLimit();
52
        $start      = $pageHelper->getStart();
53
        $search     = $this->request->get('search', $this->get('session')->get('mautic.stage.filter', ''));
54
        $filter     = ['string' => $search, 'force' => []];
55
        $orderBy    = $this->get('session')->get('mautic.stage.orderby', 's.name');
56
        $orderByDir = $this->get('session')->get('mautic.stage.orderbydir', 'ASC');
57
        $stages     = $this->getModel('stage')->getEntities(
58
            [
59
                'start'      => $start,
60
                'limit'      => $limit,
61
                'filter'     => $filter,
62
                'orderBy'    => $orderBy,
63
                'orderByDir' => $orderByDir,
64
            ]
65
        );
66
67
        $this->get('session')->set('mautic.stage.filter', $search);
68
69
        $count = count($stages);
70
        if ($count && $count < ($start + 1)) {
71
            $lastPage  = $pageHelper->countPage($count);
72
            $returnUrl = $this->generateUrl('mautic_stage_index', ['page' => $lastPage]);
73
            $pageHelper->rememberPage($lastPage);
74
75
            return $this->postActionRedirect(
76
                [
77
                    'returnUrl'       => $returnUrl,
78
                    'viewParameters'  => ['page' => $lastPage],
79
                    'contentTemplate' => 'MauticStageBundle:Stage:index',
80
                    'passthroughVars' => [
81
                        'activeLink'    => '#mautic_stage_index',
82
                        'mauticContent' => 'stage',
83
                    ],
84
                ]
85
            );
86
        }
87
88
        $pageHelper->rememberPage($page);
89
90
        //get the list of actions
91
        $actions = $this->getModel('stage')->getStageActions();
92
93
        return $this->delegateView(
94
            [
95
                'viewParameters' => [
96
                    'searchValue' => $search,
97
                    'items'       => $stages,
98
                    'actions'     => $actions['actions'],
99
                    'page'        => $page,
100
                    'limit'       => $limit,
101
                    'permissions' => $permissions,
102
                    'tmpl'        => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index',
103
                ],
104
                'contentTemplate' => 'MauticStageBundle:Stage:list.html.php',
105
                'passthroughVars' => [
106
                    'activeLink'    => '#mautic_stage_index',
107
                    'mauticContent' => 'stage',
108
                    'route'         => $this->generateUrl('mautic_stage_index', ['page' => $page]),
109
                ],
110
            ]
111
        );
112
    }
113
114
    /**
115
     * Generates new form and processes post data.
116
     *
117
     * @param \Mautic\StageBundle\Entity\Stage $entity
118
     *
119
     * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
120
     */
121
    public function newAction($entity = null)
122
    {
123
        $model = $this->getModel('stage');
124
125
        if (!($entity instanceof Stage)) {
126
            /** @var \Mautic\StageBundle\Entity\Stage $entity */
127
            $entity = $model->getEntity();
128
        }
129
130
        if (!$this->get('mautic.security')->isGranted('stage:stages:create')) {
131
            return $this->accessDenied();
132
        }
133
134
        //set the page we came from
135
        $page       = $this->get('session')->get('mautic.stage.page', 1);
136
        $method     = $this->request->getMethod();
137
        $stage      = $this->request->request->get('stage', []);
138
        $actionType = 'POST' === $method ? ($stage['type'] ?? '') : '';
139
        $action     = $this->generateUrl('mautic_stage_action', ['objectAction' => 'new']);
140
        $actions    = $model->getStageActions();
141
        $form       = $model->createForm(
0 ignored issues
show
The method createForm() does not exist on Mautic\CoreBundle\Model\AbstractCommonModel. It seems like you code against a sub-type of Mautic\CoreBundle\Model\AbstractCommonModel such as Mautic\CoreBundle\Model\FormModel. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        /** @scrutinizer ignore-call */ 
142
        $form       = $model->createForm(
Loading history...
142
            $entity,
143
            $this->get('form.factory'),
144
            $action,
145
            [
146
                'stageActions' => $actions,
147
                'actionType'   => $actionType,
148
            ]
149
        );
150
        $viewParameters = ['page' => $page];
151
152
        ///Check for a submitted form and process it
153
        if ('POST' === $method) {
154
            $valid = false;
155
156
            if (!$cancelled = $this->isFormCancelled($form)) {
157
                if ($valid = $this->isFormValid($form)) {
158
                    //form is valid so process the data
159
                    $model->saveEntity($entity);
160
161
                    $this->addFlash(
162
                        'mautic.core.notice.created',
163
                        [
164
                            '%name%'      => $entity->getName(),
165
                            '%menu_link%' => 'mautic_stage_index',
166
                            '%url%'       => $this->generateUrl(
167
                                'mautic_stage_action',
168
                                [
169
                                    'objectAction' => 'edit',
170
                                    'objectId'     => $entity->getId(),
171
                                ]
172
                            ),
173
                        ]
174
                    );
175
176
                    if ($form->get('buttons')->get('save')->isClicked()) {
177
                        $returnUrl = $this->generateUrl('mautic_stage_index', $viewParameters);
178
                        $template  = 'MauticStageBundle:Stage:index';
179
                    } else {
180
                        //return edit view so that all the session stuff is loaded
181
                        return $this->editAction($entity->getId(), true);
182
                    }
183
                }
184
            } else {
185
                $returnUrl = $this->generateUrl('mautic_stage_index', $viewParameters);
186
                $template  = 'MauticStageBundle:Stage:index';
187
            }
188
189
            if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) {
190
                return $this->postActionRedirect(
191
                    [
192
                        'returnUrl'       => $returnUrl,
193
                        'viewParameters'  => $viewParameters,
194
                        'contentTemplate' => $template,
195
                        'passthroughVars' => [
196
                            'activeLink'    => '#mautic_stage_index',
197
                            'mauticContent' => 'stage',
198
                        ],
199
                    ]
200
                );
201
            }
202
        }
203
204
        $themes = ['MauticStageBundle:FormTheme\Action'];
205
        if ($actionType && !empty($actions['actions'][$actionType]['formTheme'])) {
206
            $themes[] = $actions['actions'][$actionType]['formTheme'];
207
        }
208
209
        return $this->delegateView(
210
            [
211
                'viewParameters' => [
212
                    'tmpl'    => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index',
213
                    'entity'  => $entity,
214
                    'form'    => $this->setFormTheme($form, 'MauticStageBundle:Stage:form.html.php', $themes),
215
                    'actions' => $actions['actions'],
216
                ],
217
                'contentTemplate' => 'MauticStageBundle:Stage:form.html.php',
218
                'passthroughVars' => [
219
                    'activeLink'    => '#mautic_stage_index',
220
                    'mauticContent' => 'stage',
221
                    'route'         => $this->generateUrl(
222
                        'mautic_stage_action',
223
                        [
224
                            'objectAction' => (!empty($valid) ? 'edit' : 'new'), //valid means a new form was applied
225
                            'objectId'     => $entity->getId(),
226
                        ]
227
                    ),
228
                ],
229
            ]
230
        );
231
    }
232
233
    /**
234
     * Generates edit form and processes post data.
235
     *
236
     * @param int  $objectId
237
     * @param bool $ignorePost
238
     *
239
     * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
240
     */
241
    public function editAction($objectId, $ignorePost = false)
242
    {
243
        $model  = $this->getModel('stage');
244
        $entity = $model->getEntity($objectId);
245
246
        //set the page we came from
247
        $page = $this->get('session')->get('mautic.stage.page', 1);
248
249
        $viewParameters = ['page' => $page];
250
251
        //set the return URL
252
        $returnUrl = $this->generateUrl('mautic_stage_index', ['page' => $page]);
253
254
        $postActionVars = [
255
            'returnUrl'       => $returnUrl,
256
            'viewParameters'  => $viewParameters,
257
            'contentTemplate' => 'MauticStageBundle:Stage:index',
258
            'passthroughVars' => [
259
                'activeLink'    => '#mautic_stage_index',
260
                'mauticContent' => 'stage',
261
            ],
262
        ];
263
264
        //form not found
265
        if (null === $entity) {
266
            return $this->postActionRedirect(
267
                array_merge(
268
                    $postActionVars,
269
                    [
270
                        'flashes' => [
271
                            [
272
                                'type'    => 'error',
273
                                'msg'     => 'mautic.stage.error.notfound',
274
                                'msgVars' => ['%id%' => $objectId],
275
                            ],
276
                        ],
277
                    ]
278
                )
279
            );
280
        } elseif (!$this->get('mautic.security')->isGranted('stage:stages:edit')) {
281
            return $this->accessDenied();
282
        } elseif ($model->isLocked($entity)) {
283
            //deny access if the entity is locked
284
            return $this->isLocked($postActionVars, $entity, 'stage');
285
        }
286
287
        $actionType = 'moved to stage';
288
289
        $action  = $this->generateUrl('mautic_stage_action', ['objectAction' => 'edit', 'objectId' => $objectId]);
290
        $actions = $model->getStageActions();
291
        $form    = $model->createForm(
292
            $entity,
293
            $this->get('form.factory'),
294
            $action,
295
            [
296
                'stageActions' => $actions,
297
                'actionType'   => $actionType,
298
            ]
299
        );
300
301
        ///Check for a submitted form and process it
302
        if (!$ignorePost && 'POST' == $this->request->getMethod()) {
303
            $valid = false;
304
            if (!$cancelled = $this->isFormCancelled($form)) {
305
                if ($valid = $this->isFormValid($form)) {
306
                    //form is valid so process the data
307
                    $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked());
308
309
                    $this->addFlash(
310
                        'mautic.core.notice.updated',
311
                        [
312
                            '%name%'      => $entity->getName(),
313
                            '%menu_link%' => 'mautic_stage_index',
314
                            '%url%'       => $this->generateUrl(
315
                                'mautic_stage_action',
316
                                [
317
                                    'objectAction' => 'edit',
318
                                    'objectId'     => $entity->getId(),
319
                                ]
320
                            ),
321
                        ]
322
                    );
323
324
                    if ($form->get('buttons')->get('save')->isClicked()) {
325
                        $returnUrl = $this->generateUrl('mautic_stage_index', $viewParameters);
326
                        $template  = 'MauticStageBundle:Stage:index';
327
                    }
328
                }
329
            } else {
330
                //unlock the entity
331
                $model->unlockEntity($entity);
332
333
                $returnUrl = $this->generateUrl('mautic_stage_index', $viewParameters);
334
                $template  = 'MauticStageBundle:Stage:index';
335
            }
336
337
            if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) {
338
                return $this->postActionRedirect(
339
                    array_merge(
340
                        $postActionVars,
341
                        [
342
                            'returnUrl'       => $returnUrl,
343
                            'viewParameters'  => $viewParameters,
344
                            'contentTemplate' => $template,
345
                        ]
346
                    )
347
                );
348
            }
349
        } else {
350
            //lock the entity
351
            $model->lockEntity($entity);
352
        }
353
354
        $themes = ['MauticStageBundle:FormTheme\Action'];
355
        if (!empty($actions['actions'][$actionType]['formTheme'])) {
356
            $themes[] = $actions['actions'][$actionType]['formTheme'];
357
        }
358
359
        return $this->delegateView(
360
            [
361
                'viewParameters' => [
362
                    'tmpl'    => $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index',
363
                    'entity'  => $entity,
364
                    'form'    => $this->setFormTheme($form, 'MauticStageBundle:Stage:form.html.php', $themes),
365
                    'actions' => $actions['actions'],
366
                ],
367
                'contentTemplate' => 'MauticStageBundle:Stage:form.html.php',
368
                'passthroughVars' => [
369
                    'activeLink'    => '#mautic_stage_index',
370
                    'mauticContent' => 'stage',
371
                    'route'         => $this->generateUrl(
372
                        'mautic_stage_action',
373
                        [
374
                            'objectAction' => 'edit',
375
                            'objectId'     => $entity->getId(),
376
                        ]
377
                    ),
378
                ],
379
            ]
380
        );
381
    }
382
383
    /**
384
     * Clone an entity.
385
     *
386
     * @param int $objectId
387
     *
388
     * @return array|JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
389
     */
390
    public function cloneAction($objectId)
391
    {
392
        $model  = $this->getModel('stage');
393
        $entity = $model->getEntity($objectId);
394
395
        if (null != $entity) {
396
            if (!$this->get('mautic.security')->isGranted('stage:stages:create')) {
397
                return $this->accessDenied();
398
            }
399
400
            $entity = clone $entity;
401
            $entity->setIsPublished(false);
402
        }
403
404
        return $this->newAction($entity);
405
    }
406
407
    /**
408
     * Deletes the entity.
409
     *
410
     * @param int $objectId
411
     *
412
     * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
413
     */
414
    public function deleteAction($objectId)
415
    {
416
        $page      = $this->get('session')->get('mautic.stage.page', 1);
417
        $returnUrl = $this->generateUrl('mautic_stage_index', ['page' => $page]);
418
        $flashes   = [];
419
420
        $postActionVars = [
421
            'returnUrl'       => $returnUrl,
422
            'viewParameters'  => ['page' => $page],
423
            'contentTemplate' => 'MauticStageBundle:Stage:index',
424
            'passthroughVars' => [
425
                'activeLink'    => '#mautic_stage_index',
426
                'mauticContent' => 'stage',
427
            ],
428
        ];
429
430
        if ('POST' == $this->request->getMethod()) {
431
            $model  = $this->getModel('stage');
432
            $entity = $model->getEntity($objectId);
433
434
            if (null === $entity) {
435
                $flashes[] = [
436
                    'type'    => 'error',
437
                    'msg'     => 'mautic.stage.error.notfound',
438
                    'msgVars' => ['%id%' => $objectId],
439
                ];
440
            } elseif (!$this->get('mautic.security')->isGranted('stage:stages:delete')) {
441
                return $this->accessDenied();
442
            } elseif ($model->isLocked($entity)) {
443
                return $this->isLocked($postActionVars, $entity, 'stage');
444
            }
445
446
            $model->deleteEntity($entity);
447
448
            $identifier = $this->get('translator')->trans($entity->getName());
449
            $flashes[]  = [
450
                'type'    => 'notice',
451
                'msg'     => 'mautic.core.notice.deleted',
452
                'msgVars' => [
453
                    '%name%' => $identifier,
454
                    '%id%'   => $objectId,
455
                ],
456
            ];
457
        } //else don't do anything
458
459
        return $this->postActionRedirect(
460
            array_merge(
461
                $postActionVars,
462
                [
463
                    'flashes' => $flashes,
464
                ]
465
            )
466
        );
467
    }
468
469
    /**
470
     * Deletes a group of entities.
471
     *
472
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
473
     */
474
    public function batchDeleteAction()
475
    {
476
        $page      = $this->get('session')->get('mautic.stage.page', 1);
477
        $returnUrl = $this->generateUrl('mautic_stage_index', ['page' => $page]);
478
        $flashes   = [];
479
480
        $postActionVars = [
481
            'returnUrl'       => $returnUrl,
482
            'viewParameters'  => ['page' => $page],
483
            'contentTemplate' => 'MauticStageBundle:Stage:index',
484
            'passthroughVars' => [
485
                'activeLink'    => '#mautic_stage_index',
486
                'mauticContent' => 'stage',
487
            ],
488
        ];
489
490
        if ('POST' == $this->request->getMethod()) {
491
            $model     = $this->getModel('stage');
492
            $ids       = json_decode($this->request->query->get('ids', '{}'));
493
            $deleteIds = [];
494
495
            // Loop over the IDs to perform access checks pre-delete
496
            foreach ($ids as $objectId) {
497
                $entity = $model->getEntity($objectId);
498
499
                if (null === $entity) {
500
                    $flashes[] = [
501
                        'type'    => 'error',
502
                        'msg'     => 'mautic.stage.error.notfound',
503
                        'msgVars' => ['%id%' => $objectId],
504
                    ];
505
                } elseif (!$this->get('mautic.security')->isGranted('stage:stages:delete')) {
506
                    $flashes[] = $this->accessDenied(true);
507
                } elseif ($model->isLocked($entity)) {
508
                    $flashes[] = $this->isLocked($postActionVars, $entity, 'stage', true);
509
                } else {
510
                    $deleteIds[] = $objectId;
511
                }
512
            }
513
514
            // Delete everything we are able to
515
            if (!empty($deleteIds)) {
516
                $entities = $model->deleteEntities($deleteIds);
517
518
                $flashes[] = [
519
                    'type'    => 'notice',
520
                    'msg'     => 'mautic.stage.notice.batch_deleted',
521
                    'msgVars' => [
522
                        '%count%' => count($entities),
523
                    ],
524
                ];
525
            }
526
        } //else don't do anything
527
528
        return $this->postActionRedirect(
529
            array_merge(
530
                $postActionVars,
531
                [
532
                    'flashes' => $flashes,
533
                ]
534
            )
535
        );
536
    }
537
}
538