Issues (3627)

Controller/DynamicContentController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 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\DynamicContentBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController;
15
use Mautic\CoreBundle\Form\Type\DateRangeType;
16
use Mautic\DynamicContentBundle\Entity\DynamicContent;
17
use Mautic\DynamicContentBundle\Model\DynamicContentModel;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
20
class DynamicContentController extends FormController
0 ignored issues
show
Deprecated Code introduced by
The class Mautic\CoreBundle\Controller\FormController has been deprecated: 2.3 - to be removed in 3.0; use AbstractFormController instead ( Ignorable by Annotation )

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

20
class DynamicContentController extends /** @scrutinizer ignore-deprecated */ FormController
Loading history...
21
{
22
    /**
23
     * @return array
24
     */
25
    protected function getPermissions()
26
    {
27
        return (array) $this->get('mautic.security')->isGranted(
28
            [
29
                'dynamiccontent:dynamiccontents:viewown',
30
                'dynamiccontent:dynamiccontents:viewother',
31
                'dynamiccontent:dynamiccontents:create',
32
                'dynamiccontent:dynamiccontents:editown',
33
                'dynamiccontent:dynamiccontents:editother',
34
                'dynamiccontent:dynamiccontents:deleteown',
35
                'dynamiccontent:dynamiccontents:deleteother',
36
                'dynamiccontent:dynamiccontents:publishown',
37
                'dynamiccontent:dynamiccontents:publishother',
38
            ],
39
            'RETURN_ARRAY'
40
        );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function indexAction($page = 1)
47
    {
48
        $model = $this->getModel('dynamicContent');
49
50
        $permissions = $this->getPermissions();
51
52
        if (!$permissions['dynamiccontent:dynamiccontents:viewown'] && !$permissions['dynamiccontent:dynamiccontents:viewother']) {
53
            return $this->accessDenied();
54
        }
55
56
        $this->setListFilters();
57
58
        //set limits
59
        $limit = $this->get('session')->get('mautic.dynamicContent.limit', $this->coreParametersHelper->get('default_pagelimit'));
60
        $start = (1 === $page) ? 0 : (($page - 1) * $limit);
61
        if ($start < 0) {
62
            $start = 0;
63
        }
64
65
        // fetch
66
        $search = $this->request->get('search', $this->get('session')->get('mautic.dynamicContent.filter', ''));
67
        $this->get('session')->set('mautic.dynamicContent.filter', $search);
68
69
        $filter = [
70
            'string' => $search,
71
            'force'  => [
72
                ['column' => 'e.variantParent', 'expr' => 'isNull'],
73
                ['column' => 'e.translationParent', 'expr' => 'isNull'],
74
            ],
75
        ];
76
77
        $orderBy    = $this->get('session')->get('mautic.dynamicContent.orderby', 'e.name');
78
        $orderByDir = $this->get('session')->get('mautic.dynamicContent.orderbydir', 'DESC');
79
80
        $entities = $model->getEntities(
81
            [
82
                'start'      => $start,
83
                'limit'      => $limit,
84
                'filter'     => $filter,
85
                'orderBy'    => $orderBy,
86
                'orderByDir' => $orderByDir,
87
            ]
88
        );
89
90
        //set what page currently on so that we can return here after form submission/cancellation
91
        $this->get('session')->set('mautic.dynamicContent.page', $page);
92
93
        $tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';
94
95
        //retrieve a list of categories
96
        $categories = $this->getModel('page')->getLookupResults('category', '', 0);
97
98
        return $this->delegateView(
99
            [
100
                'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:list.html.php',
101
                'passthroughVars' => [
102
                    'activeLink'    => '#mautic_dynamicContent_index',
103
                    'mauticContent' => 'dynamicContent',
104
                    'route'         => $this->generateUrl('mautic_dynamicContent_index', ['page' => $page]),
105
                ],
106
                'viewParameters' => [
107
                    'searchValue' => $search,
108
                    'items'       => $entities,
109
                    'categories'  => $categories,
110
                    'page'        => $page,
111
                    'limit'       => $limit,
112
                    'permissions' => $permissions,
113
                    'model'       => $model,
114
                    'tmpl'        => $tmpl,
115
                ],
116
            ]
117
        );
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function newAction($entity = null)
124
    {
125
        if (!$this->accessGranted('dynamiccontent:dynamiccontents:viewown')) {
126
            return $this->accessDenied();
127
        }
128
129
        if (!$entity instanceof DynamicContent) {
130
            $entity = new DynamicContent();
131
        }
132
133
        /** @var \Mautic\DynamicContentBundle\Model\DynamicContentModel $model */
134
        $method       = $this->request->getMethod();
135
        $model        = $this->getModel('dynamicContent');
136
        $page         = $this->get('session')->get('mautic.dynamicContent.page', 1);
137
        $retUrl       = $this->generateUrl('mautic_dynamicContent_index', ['page' => $page]);
138
        $action       = $this->generateUrl('mautic_dynamicContent_action', ['objectAction' => 'new']);
139
        $dwc          = $this->request->request->get('dwc', []);
140
        $updateSelect = 'POST' === $method
141
            ? ($dwc['updateSelect'] ?? false)
142
            : $this->request->get('updateSelect', false);
143
        $form         = $model->createForm($entity, $this->get('form.factory'), $action, ['update_select' => $updateSelect]);
144
145
        if ('POST' === $method) {
146
            $valid = false;
147
148
            if (!$cancelled = $this->isFormCancelled($form)) {
149
                if ($valid = $this->isFormValid($form)) {
150
                    $model->saveEntity($entity);
151
152
                    $this->addFlash(
153
                        'mautic.core.notice.created',
154
                        [
155
                            '%name%'      => $entity->getName(),
156
                            '%menu_link%' => 'mautic_dynamicContent_index',
157
                            '%url%'       => $this->generateUrl(
158
                                'mautic_dynamicContent_action',
159
                                [
160
                                    'objectAction' => 'edit',
161
                                    'objectId'     => $entity->getId(),
162
                                ]
163
                            ),
164
                        ]
165
                    );
166
167
                    if ($form->get('buttons')->get('save')->isClicked()) {
168
                        $viewParameters = [
169
                            'objectAction' => 'view',
170
                            'objectId'     => $entity->getId(),
171
                        ];
172
                        $retUrl   = $this->generateUrl('mautic_dynamicContent_action', $viewParameters);
173
                        $template = 'MauticDynamicContentBundle:DynamicContent:view';
174
                    } else {
175
                        //return edit view so that all the session stuff is loaded
176
                        return $this->editAction($entity->getId(), true);
177
                    }
178
                }
179
            } else {
180
                $viewParameters = ['page' => $page];
181
                $retUrl         = $this->generateUrl('mautic_dynamicContent_index', $viewParameters);
182
                $template       = 'MauticDynamicContentBundle:DynamicContent:index';
183
            }
184
185
            $passthrough = [
186
                'activeLink'    => '#mautic_dynamicContent_index',
187
                'mauticContent' => 'dynamicContent',
188
            ];
189
190
            // Check to see if this is a popup
191
            if (isset($form['updateSelect'])) {
192
                $template    = false;
193
                $passthrough = array_merge(
194
                    $passthrough,
195
                    [
196
                        'updateSelect' => $form['updateSelect']->getData(),
197
                        'id'           => $entity->getId(),
198
                        'name'         => $entity->getName(),
199
                        'group'        => $entity->getLanguage(),
200
                    ]
201
                );
202
            }
203
204
            if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) {
205
                return $this->postActionRedirect(
206
                    [
207
                        'returnUrl'       => $retUrl,
208
                        'viewParameters'  => $viewParameters,
209
                        'contentTemplate' => $template,
210
                        'passthroughVars' => $passthrough,
211
                    ]
212
                );
213
            } elseif ($valid && !$cancelled) {
214
                return $this->editAction($entity->getId(), true);
215
            }
216
        }
217
218
        $passthrough['route'] = $action;
219
220
        return $this->delegateView(
221
            [
222
                'viewParameters' => [
223
                    'form' => $this->setFormTheme($form, 'MauticDynamicContentBundle:DynamicContent:form.html.php', 'MauticDynamicContentBundle:FormTheme\Filter'),
224
                ],
225
                'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:form.html.php',
226
                'passthroughVars' => $passthrough,
227
            ]
228
        );
229
    }
230
231
    /**
232
     * Generate's edit form and processes post data.
233
     *
234
     * @param            $objectId
235
     * @param bool|false $ignorePost
236
     *
237
     * @return array | JsonResponse | RedirectResponse | Response
238
     */
239
    public function editAction($objectId, $ignorePost = false)
240
    {
241
        /** @var DynamicContentModel $model */
242
        $model  = $this->getModel('dynamicContent');
243
        $entity = $model->getEntity($objectId);
244
        $page   = $this->get('session')->get('mautic.dynamicContent.page', 1);
245
        $retUrl = $this->generateUrl('mautic_dynamicContent_index', ['page' => $page]);
246
247
        $postActionVars = [
248
            'returnUrl'       => $retUrl,
249
            'viewParameters'  => ['page' => $page],
250
            'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:index',
251
            'passthroughVars' => [
252
                'activeLink'    => '#mautic_dynamicContent_index',
253
                'mauticContent' => 'dynamicContent',
254
            ],
255
        ];
256
257
        if (null === $entity) {
258
            return $this->postActionRedirect(
259
                array_merge(
260
                    $postActionVars,
261
                    [
262
                        'flashes' => [
263
                            [
264
                                'type'    => 'error',
265
                                'msg'     => 'mautic.dynamicContent.error.notfound',
266
                                'msgVars' => ['%id%' => $objectId],
267
                            ],
268
                        ],
269
                    ]
270
                )
271
            );
272
        } elseif (!$this->get('mautic.security')->hasEntityAccess(true, 'dynamiccontent:dynamiccontents:editother', $entity->getCreatedBy())) {
273
            return $this->accessDenied();
274
        } elseif ($model->isLocked($entity)) {
275
            //deny access if the entity is locked
276
            return $this->isLocked($postActionVars, $entity, 'dynamicContent');
277
        }
278
279
        $action       = $this->generateUrl('mautic_dynamicContent_action', ['objectAction' => 'edit', 'objectId' => $objectId]);
280
        $method       = $this->request->getMethod();
281
        $dwc          = $this->request->request->get('dwc', []);
282
        $updateSelect = 'POST' === $method
283
            ? ($dwc['updateSelect'] ?? false)
284
            : $this->request->get('updateSelect', false);
285
286
        $form = $model->createForm($entity, $this->get('form.factory'), $action, ['update_select' => $updateSelect]);
287
288
        ///Check for a submitted form and process it
289
        if (!$ignorePost && 'POST' === $method) {
290
            $valid = false;
291
292
            if (!$cancelled = $this->isFormCancelled($form)) {
293
                if ($valid = $this->isFormValid($form)) {
294
                    //form is valid so process the data
295
                    $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked());
296
297
                    $this->addFlash(
298
                        'mautic.core.notice.updated',
299
                        [
300
                            '%name%'      => $entity->getName(),
301
                            '%menu_link%' => 'mautic_dynamicContent_index',
302
                            '%url%'       => $this->generateUrl(
303
                                'mautic_dynamicContent_action',
304
                                [
305
                                    'objectAction' => 'edit',
306
                                    'objectId'     => $entity->getId(),
307
                                ]
308
                            ),
309
                        ]
310
                    );
311
                }
312
            } else {
313
                //unlock the entity
314
                $model->unlockEntity($entity);
315
            }
316
317
            if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) {
318
                return $this->viewAction($entity->getId());
319
            }
320
        } else {
321
            //lock the entity
322
            $model->lockEntity($entity);
323
        }
324
325
        return $this->delegateView(
326
            [
327
                'viewParameters' => [
328
                    'form'          => $this->setFormTheme($form, 'MauticDynamicContentBundle:DynamicContent:form.html.php', 'MauticDynamicContentBundle:FormTheme\Filter'),
329
                    'currentListId' => $objectId,
330
                ],
331
                'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:form.html.php',
332
                'passthroughVars' => [
333
                    'activeLink'    => '#mautic_dynamicContent_index',
334
                    'route'         => $action,
335
                    'mauticContent' => 'dynamicContent',
336
                ],
337
            ]
338
        );
339
    }
340
341
    /**
342
     * Loads a specific form into the detailed panel.
343
     *
344
     * @param int $objectId
345
     *
346
     * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
347
     */
348
    public function viewAction($objectId)
349
    {
350
        /** @var \Mautic\DynamicContentBundle\Model\DynamicContentModel $model */
351
        $model    = $this->getModel('dynamicContent');
352
        $security = $this->get('mautic.security');
353
        $entity   = $model->getEntity($objectId);
354
355
        //set the page we came from
356
        $page = $this->get('session')->get('mautic.dynamicContent.page', 1);
357
358
        if (null === $entity) {
359
            //set the return URL
360
            $returnUrl = $this->generateUrl('mautic_dynamicContent_index', ['page' => $page]);
361
362
            return $this->postActionRedirect(
363
                [
364
                    'returnUrl'       => $returnUrl,
365
                    'viewParameters'  => ['page' => $page],
366
                    'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:index',
367
                    'passthroughVars' => [
368
                        'activeLink'    => '#mautic_dynamicContent_index',
369
                        'mauticContent' => 'dynamicContent',
370
                    ],
371
                    'flashes' => [
372
                        [
373
                            'type'    => 'error',
374
                            'msg'     => 'mautic.dynamicContent.error.notfound',
375
                            'msgVars' => ['%id%' => $objectId],
376
                        ],
377
                    ],
378
                ]
379
            );
380
        } elseif (!$security->hasEntityAccess(
381
            'dynamiccontent:dynamiccontents:viewown',
382
            'dynamiccontent:dynamiccontents:viewother',
383
            $entity->getCreatedBy()
384
        )
385
        ) {
386
            return $this->accessDenied();
387
        }
388
389
        /* @var DynamicContent $parent */
390
        /* @var DynamicContent[] $children */
391
        list($translationParent, $translationChildren) = $entity->getTranslations();
392
393
        // Audit Log
394
        $logs = $this->getModel('core.auditlog')->getLogForObject('dynamicContent', $entity->getId(), $entity->getDateAdded());
395
396
        // Init the date range filter form
397
        $dateRangeValues = $this->request->get('daterange', []);
398
        $action          = $this->generateUrl('mautic_dynamicContent_action', ['objectAction' => 'view', 'objectId' => $objectId]);
399
        $dateRangeForm   = $this->get('form.factory')->create(DateRangeType::class, $dateRangeValues, ['action' => $action]);
400
        $entityViews     = $model->getHitsLineChartData(
401
            null,
402
            new \DateTime($dateRangeForm->get('date_from')->getData()),
403
            new \DateTime($dateRangeForm->get('date_to')->getData()),
404
            null,
405
            ['dynamic_content_id' => $entity->getId(), 'flag' => 'total_and_unique']
406
        );
407
408
        $trackables = $this->getModel('page.trackable')->getTrackableList('dynamicContent', $entity->getId());
409
410
        return $this->delegateView(
411
            [
412
                'returnUrl'       => $action,
413
                'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:details.html.php',
414
                'passthroughVars' => [
415
                    'activeLink'    => '#mautic_dynamicContent_index',
416
                    'mauticContent' => 'dynamicContent',
417
                ],
418
                'viewParameters' => [
419
                    'entity'       => $entity,
420
                    'permissions'  => $this->getPermissions(),
421
                    'logs'         => $logs,
422
                    'isEmbedded'   => $this->request->get('isEmbedded') ? $this->request->get('isEmbedded') : false,
423
                    'translations' => [
424
                        'parent'   => $translationParent,
425
                        'children' => $translationChildren,
426
                    ],
427
                    'trackables'    => $trackables,
428
                    'entityViews'   => $entityViews,
429
                    'dateRangeForm' => $dateRangeForm->createView(),
430
                ],
431
            ]
432
        );
433
    }
434
435
    /**
436
     * Clone an entity.
437
     *
438
     * @param $objectId
439
     *
440
     * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
441
     */
442
    public function cloneAction($objectId)
443
    {
444
        $model  = $this->getModel('dynamicContent');
445
        $entity = $model->getEntity($objectId);
446
447
        if (null != $entity) {
448
            if (!$this->get('mautic.security')->isGranted('dynamiccontent:dynamiccontents:create')
449
                || !$this->get('mautic.security')->hasEntityAccess(
450
                    'dynamiccontent:dynamiccontents:viewown',
451
                    'dynamiccontent:dynamiccontents:viewother',
452
                    $entity->getCreatedBy()
453
                )
454
            ) {
455
                return $this->accessDenied();
456
            }
457
458
            $entity = clone $entity;
459
        }
460
461
        return $this->newAction($entity);
462
    }
463
464
    /**
465
     * Deletes the entity.
466
     *
467
     * @param $objectId
468
     *
469
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
470
     */
471
    public function deleteAction($objectId)
472
    {
473
        $page      = $this->get('session')->get('mautic.dynamicContent.page', 1);
474
        $returnUrl = $this->generateUrl('mautic_dynamicContent_index', ['page' => $page]);
475
        $flashes   = [];
476
477
        $postActionVars = [
478
            'returnUrl'       => $returnUrl,
479
            'viewParameters'  => ['page' => $page],
480
            'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:index',
481
            'passthroughVars' => [
482
                'activeLink'    => 'mautic_dynamicContent_index',
483
                'mauticContent' => 'dynamicContent',
484
            ],
485
        ];
486
487
        if ('POST' == $this->request->getMethod()) {
488
            $model  = $this->getModel('dynamicContent');
489
            $entity = $model->getEntity($objectId);
490
491
            if (null === $entity) {
492
                $flashes[] = [
493
                    'type'    => 'error',
494
                    'msg'     => 'mautic.dynamicContent.error.notfound',
495
                    'msgVars' => ['%id%' => $objectId],
496
                ];
497
            } elseif (!$this->get('mautic.security')->hasEntityAccess(
498
                'dynamiccontent:dynamiccontents:deleteown',
499
                'dynamiccontent:dynamiccontents:deleteother',
500
                $entity->getCreatedBy()
501
            )
502
            ) {
503
                return $this->accessDenied();
504
            } elseif ($model->isLocked($entity)) {
505
                return $this->isLocked($postActionVars, $entity, 'notification');
506
            }
507
508
            $model->deleteEntity($entity);
509
510
            $flashes[] = [
511
                'type'    => 'notice',
512
                'msg'     => 'mautic.core.notice.deleted',
513
                'msgVars' => [
514
                    '%name%' => $entity->getName(),
515
                    '%id%'   => $objectId,
516
                ],
517
            ];
518
        } //else don't do anything
519
520
        return $this->postActionRedirect(array_merge($postActionVars, ['flashes' => $flashes]));
521
    }
522
523
    /**
524
     * Deletes a group of entities.
525
     *
526
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
527
     */
528
    public function batchDeleteAction()
529
    {
530
        $page      = $this->get('session')->get('mautic.dynamicContent.page', 1);
531
        $returnUrl = $this->generateUrl('mautic_dynamicContent_index', ['page' => $page]);
532
        $flashes   = [];
533
534
        $postActionVars = [
535
            'returnUrl'       => $returnUrl,
536
            'viewParameters'  => ['page' => $page],
537
            'contentTemplate' => 'MauticDynamicContentBundle:DynamicContent:index',
538
            'passthroughVars' => [
539
                'activeLink'    => '#mautic_dynamicContent_index',
540
                'mauticContent' => 'dynamicContent',
541
            ],
542
        ];
543
544
        if ('POST' == $this->request->getMethod()) {
545
            $model = $this->getModel('dynamicContent');
546
            $ids   = json_decode($this->request->query->get('ids', '{}'));
547
548
            $deleteIds = [];
549
550
            // Loop over the IDs to perform access checks pre-delete
551
            foreach ($ids as $objectId) {
552
                $entity = $model->getEntity($objectId);
553
554
                if (null === $entity) {
555
                    $flashes[] = [
556
                        'type'    => 'error',
557
                        'msg'     => 'mautic.dynamicContent.error.notfound',
558
                        'msgVars' => ['%id%' => $objectId],
559
                    ];
560
                } elseif (!$this->get('mautic.security')->hasEntityAccess(
561
                    'dynamiccontent:dynamiccontents:viewown',
562
                    'dynamiccontent:dynamiccontents:viewother',
563
                    $entity->getCreatedBy()
564
                )
565
                ) {
566
                    $flashes[] = $this->accessDenied(true);
567
                } elseif ($model->isLocked($entity)) {
568
                    $flashes[] = $this->isLocked($postActionVars, $entity, 'dynamicContent', true);
569
                } else {
570
                    $deleteIds[] = $objectId;
571
                }
572
            }
573
574
            // Delete everything we are able to
575
            if (!empty($deleteIds)) {
576
                $entities = $model->deleteEntities($deleteIds);
577
578
                $flashes[] = [
579
                    'type'    => 'notice',
580
                    'msg'     => 'mautic.dynamicContent.notice.batch_deleted',
581
                    'msgVars' => [
582
                        '%count%' => count($entities),
583
                    ],
584
                ];
585
            }
586
        } //else don't do anything
587
588
        return $this->postActionRedirect(array_merge($postActionVars, ['flashes' => $flashes]));
589
    }
590
}
591