Issues (3627)

Controller/MobileNotificationController.php (2 issues)

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\NotificationBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController;
15
use Mautic\CoreBundle\Form\Type\DateRangeType;
16
use Mautic\CoreBundle\Helper\InputHelper;
17
use Mautic\LeadBundle\Controller\EntityContactsTrait;
18
use Mautic\NotificationBundle\Entity\Notification;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Response;
21
22
class MobileNotificationController extends FormController
23
{
24
    use EntityContactsTrait;
25
26
    /**
27
     * @param int $page
28
     *
29
     * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
30
     */
31
    public function indexAction($page = 1)
32
    {
33
        /** @var \Mautic\NotificationBundle\Model\NotificationModel $model */
34
        $model = $this->getModel('notification');
35
36
        //set some permissions
37
        $permissions = $this->get('mautic.security')->isGranted(
38
            [
39
                'notification:mobile_notifications:viewown',
40
                'notification:mobile_notifications:viewother',
41
                'notification:mobile_notifications:create',
42
                'notification:mobile_notifications:editown',
43
                'notification:mobile_notifications:editother',
44
                'notification:mobile_notifications:deleteown',
45
                'notification:mobile_notifications:deleteother',
46
                'notification:mobile_notifications:publishown',
47
                'notification:mobile_notifications:publishother',
48
            ],
49
            'RETURN_ARRAY'
50
        );
51
52
        if (!$permissions['notification:mobile_notifications:viewown'] && !$permissions['notification:mobile_notifications:viewother']) {
53
            return $this->accessDenied();
54
        }
55
56
        $session = $this->get('session');
57
58
        //set limits
59
        $limit = $session->get('mautic.mobile_notification.limit', $this->coreParametersHelper->get('default_pagelimit'));
60
        $start = (1 === $page) ? 0 : (($page - 1) * $limit);
61
        if ($start < 0) {
62
            $start = 0;
63
        }
64
65
        $search = $this->request->get('search', $session->get('mautic.mobile_notification.filter', ''));
66
        $session->set('mautic.mobile_notification.filter', $search);
67
68
        $filter = [
69
            'string' => $search,
70
            'where'  => [
71
                [
72
                    'expr' => 'eq',
73
                    'col'  => 'mobile',
74
                    'val'  => 1,
75
                ],
76
            ],
77
        ];
78
79
        if (!$permissions['notification:mobile_notifications:viewother']) {
80
            $filter['force'][] =
81
                ['column' => 'e.createdBy', 'expr' => 'eq', 'value' => $this->user->getId()];
82
        }
83
84
        $orderBy    = $session->get('mautic.mobile_notification.orderby', 'e.name');
85
        $orderByDir = $session->get('mautic.mobile_notification.orderbydir', 'DESC');
86
87
        $notifications = $model->getEntities(
88
            [
89
                'start'      => $start,
90
                'limit'      => $limit,
91
                'filter'     => $filter,
92
                'orderBy'    => $orderBy,
93
                'orderByDir' => $orderByDir,
94
            ]
95
        );
96
97
        $count = count($notifications);
98
        if ($count && $count < ($start + 1)) {
99
            //the number of entities are now less then the current page so redirect to the last page
100
            if (1 === $count) {
101
                $lastPage = 1;
102
            } else {
103
                $lastPage = (floor($count / $limit)) ?: 1;
104
            }
105
106
            $session->set('mautic.mobile_notification.page', $lastPage);
107
            $returnUrl = $this->generateUrl('mautic_mobile_notification_index', ['page' => $lastPage]);
108
109
            return $this->postActionRedirect(
110
                [
111
                    'returnUrl'       => $returnUrl,
112
                    'viewParameters'  => ['page' => $lastPage],
113
                    'contentTemplate' => 'MauticNotificationBundle:MobileNotification:index',
114
                    'passthroughVars' => [
115
                        'activeLink'    => '#mautic_mobile_notification_index',
116
                        'mauticContent' => 'mobile_notification',
117
                    ],
118
                ]
119
            );
120
        }
121
        $session->set('mautic.mobile_notification.page', $page);
122
123
        return $this->delegateView(
124
            [
125
                'viewParameters' => [
126
                    'searchValue' => $search,
127
                    'items'       => $notifications,
128
                    'totalItems'  => $count,
129
                    'page'        => $page,
130
                    'limit'       => $limit,
131
                    'tmpl'        => $this->request->get('tmpl', 'index'),
132
                    'permissions' => $permissions,
133
                    'model'       => $model,
134
                    'security'    => $this->get('mautic.security'),
135
                ],
136
                'contentTemplate' => 'MauticNotificationBundle:MobileNotification:list.html.php',
137
                'passthroughVars' => [
138
                    'activeLink'    => '#mautic_mobile_notification_index',
139
                    'mauticContent' => 'mobile_notification',
140
                    'route'         => $this->generateUrl('mautic_mobile_notification_index', ['page' => $page]),
141
                ],
142
            ]
143
        );
144
    }
145
146
    /**
147
     * Loads a specific form into the detailed panel.
148
     *
149
     * @param $objectId
150
     *
151
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
152
     */
153
    public function viewAction($objectId)
154
    {
155
        /** @var \Mautic\NotificationBundle\Model\NotificationModel $model */
156
        $model    = $this->getModel('notification');
157
        $security = $this->get('mautic.security');
158
159
        /** @var \Mautic\NotificationBundle\Entity\Notification $notification */
160
        $notification = $model->getEntity($objectId);
161
        //set the page we came from
162
        $page = $this->get('session')->get('mautic.mobile_notification.page', 1);
163
164
        if (null === $notification) {
165
            //set the return URL
166
            $returnUrl = $this->generateUrl('mautic_mobile_notification_index', ['page' => $page]);
167
168
            return $this->postActionRedirect(
169
                [
170
                    'returnUrl'       => $returnUrl,
171
                    'viewParameters'  => ['page' => $page],
172
                    'contentTemplate' => 'MauticNotificationBundle:MobileNotification:index',
173
                    'passthroughVars' => [
174
                        'activeLink'    => '#mautic_mobile_notification_index',
175
                        'mauticContent' => 'mobile_notification',
176
                    ],
177
                    'flashes' => [
178
                        [
179
                            'type'    => 'error',
180
                            'msg'     => 'mautic.notification.error.notfound',
181
                            'msgVars' => ['%id%' => $objectId],
182
                        ],
183
                    ],
184
                ]
185
            );
186
        } elseif (!$this->get('mautic.security')->hasEntityAccess(
187
            'notification:mobile_notifications:viewown',
188
            'notification:mobile_notifications:viewother',
189
            $notification->getCreatedBy()
190
        )
191
        ) {
192
            return $this->accessDenied();
193
        }
194
195
        // Audit Log
196
        $logs = $this->getModel('core.auditlog')->getLogForObject('notification', $notification->getId(), $notification->getDateAdded());
197
198
        // Init the date range filter form
199
        $dateRangeValues = $this->request->get('daterange', []);
200
        $action          = $this->generateUrl('mautic_mobile_notification_action', ['objectAction' => 'view', 'objectId' => $objectId]);
201
        $dateRangeForm   = $this->get('form.factory')->create(DateRangeType::class, $dateRangeValues, ['action' => $action]);
202
        $entityViews     = $model->getHitsLineChartData(
203
            null,
204
            new \DateTime($dateRangeForm->get('date_from')->getData()),
205
            new \DateTime($dateRangeForm->get('date_to')->getData()),
206
            null,
207
            ['notification_id' => $notification->getId()]
208
        );
209
210
        // Get click through stats
211
        $trackableLinks = $model->getNotificationClickStats($notification->getId());
212
213
        return $this->delegateView([
214
            'returnUrl'      => $this->generateUrl('mautic_mobile_notification_action', ['objectAction' => 'view', 'objectId' => $notification->getId()]),
215
            'viewParameters' => [
216
                'notification' => $notification,
217
                'trackables'   => $trackableLinks,
218
                'logs'         => $logs,
219
                'permissions'  => $security->isGranted([
220
                    'notification:mobile_notifications:viewown',
221
                    'notification:mobile_notifications:viewother',
222
                    'notification:mobile_notifications:create',
223
                    'notification:mobile_notifications:editown',
224
                    'notification:mobile_notifications:editother',
225
                    'notification:mobile_notifications:deleteown',
226
                    'notification:mobile_notifications:deleteother',
227
                    'notification:mobile_notifications:publishown',
228
                    'notification:mobile_notifications:publishother',
229
                ], 'RETURN_ARRAY'),
230
                'security'    => $security,
231
                'entityViews' => $entityViews,
232
                'contacts'    => $this->forward(
233
                    'MauticNotificationBundle:MobileNotification:contacts',
234
                    [
235
                        'objectId'   => $notification->getId(),
236
                        'page'       => $this->get('session')->get('mautic.mobile_notification.contact.page', 1),
237
                        'ignoreAjax' => true,
238
                    ]
239
                )->getContent(),
240
                'dateRangeForm' => $dateRangeForm->createView(),
241
            ],
242
            'contentTemplate' => 'MauticNotificationBundle:MobileNotification:details.html.php',
243
            'passthroughVars' => [
244
                'activeLink'    => '#mautic_mobile_notification_index',
245
                'mauticContent' => 'mobile_notification',
246
            ],
247
        ]);
248
    }
249
250
    /**
251
     * Generates new form and processes post data.
252
     *
253
     * @param Notification $entity
254
     *
255
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
256
     */
257
    public function newAction($entity = null)
258
    {
259
        /** @var \Mautic\NotificationBundle\Model\NotificationModel $model */
260
        $model = $this->getModel('notification');
261
262
        if (!$entity instanceof Notification) {
263
            /** @var \Mautic\NotificationBundle\Entity\Notification $entity */
264
            $entity = $model->getEntity();
265
        }
266
267
        $method  = $this->request->getMethod();
268
        $session = $this->get('session');
269
270
        if (!$this->get('mautic.security')->isGranted('notification:mobile_notifications:create')) {
271
            return $this->accessDenied();
272
        }
273
274
        //set the page we came from
275
        $page         = $session->get('mautic.mobile_notification.page', 1);
276
        $action       = $this->generateUrl('mautic_mobile_notification_action', ['objectAction' => 'new']);
277
        $notification = $this->request->request->get('notification', []);
278
        $updateSelect = 'POST' === $method
279
            ? ($notification['updateSelect'] ?? false)
280
            : $this->request->get('updateSelect', false);
281
282
        if ($updateSelect) {
283
            $entity->setNotificationType('template');
284
        }
285
286
        // create the form
287
        $form = $model->createForm($entity, $this->get('form.factory'), $action, ['update_select' => $updateSelect]);
288
289
        ///Check for a submitted form and process it
290
        if ('POST' === $method) {
291
            $valid = false;
292
            if (!$cancelled = $this->isFormCancelled($form)) {
293
                if ($valid = $this->isFormValid($form)) {
294
                    //form is valid so process the data
295
                    $model->saveEntity($entity);
296
297
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

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

297
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
298
                        'mautic.core.notice.created',
299
                        [
300
                            '%name%'      => $entity->getName(),
301
                            '%menu_link%' => 'mautic_mobile_notification_index',
302
                            '%url%'       => $this->generateUrl(
303
                                'mautic_mobile_notification_action',
304
                                [
305
                                    'objectAction' => 'edit',
306
                                    'objectId'     => $entity->getId(),
307
                                ]
308
                            ),
309
                        ]
310
                    );
311
312
                    if ($form->get('buttons')->get('save')->isClicked()) {
313
                        $viewParameters = [
314
                            'objectAction' => 'view',
315
                            'objectId'     => $entity->getId(),
316
                        ];
317
                        $returnUrl = $this->generateUrl('mautic_mobile_notification_action', $viewParameters);
318
                        $template  = 'MauticNotificationBundle:MobileNotification:view';
319
                    } else {
320
                        //return edit view so that all the session stuff is loaded
321
                        return $this->editAction($entity->getId(), true);
322
                    }
323
                }
324
            } else {
325
                $viewParameters = ['page' => $page];
326
                $returnUrl      = $this->generateUrl('mautic_mobile_notification_index', $viewParameters);
327
                $template       = 'MauticNotificationBundle:MobileNotification:index';
328
                //clear any modified content
329
                $session->remove('mautic.mobile_notification.'.$entity->getId().'.content');
330
            }
331
332
            $passthrough = [
333
                'activeLink'    => 'mautic_mobile_notification_index',
334
                'mauticContent' => 'mobile_notification',
335
            ];
336
337
            // Check to see if this is a popup
338
            if (isset($form['updateSelect'])) {
339
                $template    = false;
340
                $passthrough = array_merge(
341
                    $passthrough,
342
                    [
343
                        'updateSelect' => $form['updateSelect']->getData(),
344
                        'id'           => $entity->getId(),
345
                        'name'         => $entity->getName(),
346
                        'group'        => $entity->getLanguage(),
347
                    ]
348
                );
349
            }
350
351
            if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) {
352
                return $this->postActionRedirect(
353
                    [
354
                        'returnUrl'       => $returnUrl,
355
                        'viewParameters'  => $viewParameters,
356
                        'contentTemplate' => $template,
357
                        'passthroughVars' => $passthrough,
358
                    ]
359
                );
360
            }
361
        }
362
363
        $integration = $this->get('mautic.helper.integration')->getIntegrationObject('OneSignal');
364
365
        return $this->delegateView(
366
            [
367
                'viewParameters' => [
368
                    'form'         => $this->setFormTheme($form, 'MauticNotificationBundle:MobileNotification:form.html.php', 'MauticNotificationBundle:FormTheme\MobileNotification'),
369
                    'notification' => $entity,
370
                    'integration'  => $integration,
371
                ],
372
                'contentTemplate' => 'MauticNotificationBundle:MobileNotification:form.html.php',
373
                'passthroughVars' => [
374
                    'activeLink'    => '#mautic_mobile_notification_index',
375
                    'mauticContent' => 'mobile_notification',
376
                    'updateSelect'  => InputHelper::clean($this->request->query->get('updateSelect')),
377
                    'route'         => $this->generateUrl(
378
                        'mautic_mobile_notification_action',
379
                        [
380
                            'objectAction' => 'new',
381
                        ]
382
                    ),
383
                ],
384
            ]
385
        );
386
    }
387
388
    /**
389
     * @param      $objectId
390
     * @param bool $ignorePost
391
     * @param bool $forceTypeSelection
392
     *
393
     * @return array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
394
     */
395
    public function editAction($objectId, $ignorePost = false, $forceTypeSelection = false)
396
    {
397
        /** @var \Mautic\NotificationBundle\Model\NotificationModel $model */
398
        $model   = $this->getModel('notification');
399
        $method  = $this->request->getMethod();
400
        $entity  = $model->getEntity($objectId);
401
        $session = $this->get('session');
402
        $page    = $session->get('mautic.mobile_notification.page', 1);
403
404
        //set the return URL
405
        $returnUrl = $this->generateUrl('mautic_mobile_notification_index', ['page' => $page]);
406
407
        $postActionVars = [
408
            'returnUrl'       => $returnUrl,
409
            'viewParameters'  => ['page' => $page],
410
            'contentTemplate' => 'MauticNotificationBundle:MobileNotification:index',
411
            'passthroughVars' => [
412
                'activeLink'    => 'mautic_mobile_notification_index',
413
                'mauticContent' => 'mobile_notification',
414
            ],
415
        ];
416
417
        //not found
418
        if (null === $entity) {
419
            return $this->postActionRedirect(
420
                array_merge(
421
                    $postActionVars,
422
                    [
423
                        'flashes' => [
424
                            [
425
                                'type'    => 'error',
426
                                'msg'     => 'mautic.notification.error.notfound',
427
                                'msgVars' => ['%id%' => $objectId],
428
                            ],
429
                        ],
430
                    ]
431
                )
432
            );
433
        } elseif (!$this->get('mautic.security')->hasEntityAccess(
434
            'notification:mobile_notifications:viewown',
435
            'notification:mobile_notifications:viewother',
436
            $entity->getCreatedBy()
437
        )
438
        ) {
439
            return $this->accessDenied();
440
        } elseif ($model->isLocked($entity)) {
441
            //deny access if the entity is locked
442
            return $this->isLocked($postActionVars, $entity, 'notification');
443
        }
444
445
        //Create the form
446
        $action       = $this->generateUrl('mautic_mobile_notification_action', ['objectAction' => 'edit', 'objectId' => $objectId]);
447
        $notification = $this->request->request->get('notification', []);
448
        $updateSelect = 'POST' === $method
449
            ? ($notification['updateSelect'] ?? false)
450
            : $this->request->get('updateSelect', false);
451
452
        $form = $model->createForm($entity, $this->get('form.factory'), $action, ['update_select' => $updateSelect]);
453
454
        ///Check for a submitted form and process it
455
        if (!$ignorePost && 'POST' == $method) {
456
            $valid = false;
457
            if (!$cancelled = $this->isFormCancelled($form)) {
458
                if ($valid = $this->isFormValid($form)) {
459
                    //form is valid so process the data
460
                    $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked());
461
462
                    $this->addFlash(
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

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

462
                    /** @scrutinizer ignore-deprecated */ $this->addFlash(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
463
                        'mautic.core.notice.updated',
464
                        [
465
                            '%name%'      => $entity->getName(),
466
                            '%menu_link%' => 'mautic_mobile_notification_index',
467
                            '%url%'       => $this->generateUrl(
468
                                'mautic_mobile_notification_action',
469
                                [
470
                                    'objectAction' => 'edit',
471
                                    'objectId'     => $entity->getId(),
472
                                ]
473
                            ),
474
                        ],
475
                        'warning'
476
                    );
477
                }
478
            } else {
479
                //clear any modified content
480
                $session->remove('mautic.mobile_notification.'.$objectId.'.content');
481
                //unlock the entity
482
                $model->unlockEntity($entity);
483
            }
484
485
            $template    = 'MauticNotificationBundle:MobileNotification:view';
486
            $passthrough = [
487
                'activeLink'    => 'mautic_mobile_notification_index',
488
                'mauticContent' => 'mobile_notification',
489
            ];
490
491
            // Check to see if this is a popup
492
            if (isset($form['updateSelect'])) {
493
                $template    = false;
494
                $passthrough = array_merge(
495
                    $passthrough,
496
                    [
497
                        'updateSelect' => $form['updateSelect']->getData(),
498
                        'id'           => $entity->getId(),
499
                        'name'         => $entity->getName(),
500
                        'group'        => $entity->getLanguage(),
501
                    ]
502
                );
503
            }
504
505
            if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) {
506
                $viewParameters = [
507
                    'objectAction' => 'view',
508
                    'objectId'     => $entity->getId(),
509
                ];
510
511
                return $this->postActionRedirect(
512
                    array_merge(
513
                        $postActionVars,
514
                        [
515
                            'returnUrl'       => $this->generateUrl('mautic_mobile_notification_action', $viewParameters),
516
                            'viewParameters'  => $viewParameters,
517
                            'contentTemplate' => $template,
518
                            'passthroughVars' => $passthrough,
519
                        ]
520
                    )
521
                );
522
            }
523
        } else {
524
            //lock the entity
525
            $model->lockEntity($entity);
526
        }
527
528
        $integration = $this->get('mautic.helper.integration')->getIntegrationObject('OneSignal');
529
530
        return $this->delegateView(
531
            [
532
                'viewParameters' => [
533
                    'form'               => $this->setFormTheme($form, 'MauticNotificationBundle:MobileNotification:form.html.php', 'MauticNotificationBundle:FormTheme\MobileNotification'),
534
                    'notification'       => $entity,
535
                    'forceTypeSelection' => $forceTypeSelection,
536
                    'integration'        => $integration,
537
                ],
538
                'contentTemplate' => 'MauticNotificationBundle:MobileNotification:form.html.php',
539
                'passthroughVars' => [
540
                    'activeLink'    => '#mautic_mobile_notification_index',
541
                    'mauticContent' => 'mobile_notification',
542
                    'updateSelect'  => InputHelper::clean($this->request->query->get('updateSelect')),
543
                    'route'         => $this->generateUrl(
544
                        'mautic_mobile_notification_action',
545
                        [
546
                            'objectAction' => 'edit',
547
                            'objectId'     => $entity->getId(),
548
                        ]
549
                    ),
550
                ],
551
            ]
552
        );
553
    }
554
555
    /**
556
     * Clone an entity.
557
     *
558
     * @param $objectId
559
     *
560
     * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
561
     */
562
    public function cloneAction($objectId)
563
    {
564
        $model  = $this->getModel('notification');
565
        $entity = $model->getEntity($objectId);
566
567
        if (null != $entity) {
568
            if (!$this->get('mautic.security')->isGranted('notification:mobile_notifications:create')
569
                || !$this->get('mautic.security')->hasEntityAccess(
570
                    'notification:mobile_notifications:viewown',
571
                    'notification:mobile_notifications:viewother',
572
                    $entity->getCreatedBy()
573
                )
574
            ) {
575
                return $this->accessDenied();
576
            }
577
578
            $entity      = clone $entity;
579
            $session     = $this->get('session');
580
            $contentName = 'mautic.mobile_notification.'.$entity->getId().'.content';
581
582
            $session->set($contentName, $entity->getContent());
583
        }
584
585
        return $this->newAction($entity);
586
    }
587
588
    /**
589
     * Deletes the entity.
590
     *
591
     * @param $objectId
592
     *
593
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
594
     */
595
    public function deleteAction($objectId)
596
    {
597
        $page      = $this->get('session')->get('mautic.mobile_notification.page', 1);
598
        $returnUrl = $this->generateUrl('mautic_mobile_notification_index', ['page' => $page]);
599
        $flashes   = [];
600
601
        $postActionVars = [
602
            'returnUrl'       => $returnUrl,
603
            'viewParameters'  => ['page' => $page],
604
            'contentTemplate' => 'MauticNotificationBundle:MobileNotification:index',
605
            'passthroughVars' => [
606
                'activeLink'    => 'mautic_mobile_notification_index',
607
                'mauticContent' => 'mobile_notification',
608
            ],
609
        ];
610
611
        if ('POST' == $this->request->getMethod()) {
612
            $model  = $this->getModel('notification');
613
            $entity = $model->getEntity($objectId);
614
615
            if (null === $entity) {
616
                $flashes[] = [
617
                    'type'    => 'error',
618
                    'msg'     => 'mautic.notification.error.notfound',
619
                    'msgVars' => ['%id%' => $objectId],
620
                ];
621
            } elseif (!$this->get('mautic.security')->hasEntityAccess(
622
                'notification:mobile_notifications:deleteown',
623
                'notification:mobile_notifications:deleteother',
624
                $entity->getCreatedBy()
625
            )
626
            ) {
627
                return $this->accessDenied();
628
            } elseif ($model->isLocked($entity)) {
629
                return $this->isLocked($postActionVars, $entity, 'notification');
630
            }
631
632
            $model->deleteEntity($entity);
633
634
            $flashes[] = [
635
                'type'    => 'notice',
636
                'msg'     => 'mautic.core.notice.deleted',
637
                'msgVars' => [
638
                    '%name%' => $entity->getName(),
639
                    '%id%'   => $objectId,
640
                ],
641
            ];
642
        } //else don't do anything
643
644
        return $this->postActionRedirect(
645
            array_merge(
646
                $postActionVars,
647
                [
648
                    'flashes' => $flashes,
649
                ]
650
            )
651
        );
652
    }
653
654
    /**
655
     * Deletes a group of entities.
656
     *
657
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse
658
     */
659
    public function batchDeleteAction()
660
    {
661
        $page      = $this->get('session')->get('mautic.mobile_notification.page', 1);
662
        $returnUrl = $this->generateUrl('mautic_mobile_notification_index', ['page' => $page]);
663
        $flashes   = [];
664
665
        $postActionVars = [
666
            'returnUrl'       => $returnUrl,
667
            'viewParameters'  => ['page' => $page],
668
            'contentTemplate' => 'MauticNotificationBundle:MobileNotification:index',
669
            'passthroughVars' => [
670
                'activeLink'    => '#mautic_mobile_notification_index',
671
                'mauticContent' => 'mobile_notification',
672
            ],
673
        ];
674
675
        if ('POST' == $this->request->getMethod()) {
676
            $model = $this->getModel('notification');
677
            $ids   = json_decode($this->request->query->get('ids', '{}'));
678
679
            $deleteIds = [];
680
681
            // Loop over the IDs to perform access checks pre-delete
682
            foreach ($ids as $objectId) {
683
                $entity = $model->getEntity($objectId);
684
685
                if (null === $entity) {
686
                    $flashes[] = [
687
                        'type'    => 'error',
688
                        'msg'     => 'mautic.notification.error.notfound',
689
                        'msgVars' => ['%id%' => $objectId],
690
                    ];
691
                } elseif (!$this->get('mautic.security')->hasEntityAccess(
692
                    'notification:mobile_notifications:viewown',
693
                    'notification:mobile_notifications:viewother',
694
                    $entity->getCreatedBy()
695
                )
696
                ) {
697
                    $flashes[] = $this->accessDenied(true);
698
                } elseif ($model->isLocked($entity)) {
699
                    $flashes[] = $this->isLocked($postActionVars, $entity, 'notification', true);
700
                } else {
701
                    $deleteIds[] = $objectId;
702
                }
703
            }
704
705
            // Delete everything we are able to
706
            if (!empty($deleteIds)) {
707
                $entities = $model->deleteEntities($deleteIds);
708
709
                $flashes[] = [
710
                    'type'    => 'notice',
711
                    'msg'     => 'mautic.notification.notice.batch_deleted',
712
                    'msgVars' => [
713
                        '%count%' => count($entities),
714
                    ],
715
                ];
716
            }
717
        } //else don't do anything
718
719
        return $this->postActionRedirect(
720
            array_merge(
721
                $postActionVars,
722
                [
723
                    'flashes' => $flashes,
724
                ]
725
            )
726
        );
727
    }
728
729
    /**
730
     * @param $objectId
731
     *
732
     * @return JsonResponse|Response
733
     */
734
    public function previewAction($objectId)
735
    {
736
        /** @var \Mautic\NotificationBundle\Model\NotificationModel $model */
737
        $model        = $this->getModel('notification');
738
        $notification = $model->getEntity($objectId);
739
740
        if (null != $notification
741
            && $this->get('mautic.security')->hasEntityAccess(
742
                'notification:mobile_notifications:editown',
743
                'notification:mobile_notifications:editother'
744
            )
745
        ) {
746
        }
747
748
        return $this->delegateView(
749
            [
750
                'viewParameters' => [
751
                    'notification' => $notification,
752
                ],
753
                'contentTemplate' => 'MauticNotificationBundle:MobileNotification:preview.html.php',
754
            ]
755
        );
756
    }
757
758
    /**
759
     * @param     $objectId
760
     * @param int $page
761
     *
762
     * @return JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
763
     */
764
    public function contactsAction($objectId, $page = 1)
765
    {
766
        return $this->generateContactsGrid(
767
            $objectId,
768
            $page,
769
            'notification:mobile_notifications:view',
770
            'mobile_notification',
771
            'push_notification_stats',
772
            'mobile_notification',
773
            'notification_id'
774
        );
775
    }
776
}
777