Issues (3627)

CalendarBundle/Controller/AjaxController.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\CalendarBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\AjaxController as CommonAjaxController;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * Class AjaxController.
20
 */
21
class AjaxController extends CommonAjaxController
22
{
23
    /**
24
     * Generates the calendar data.
25
     *
26
     * @return \Symfony\Component\HttpFoundation\JsonResponse
27
     */
28
    public function generateDataAction(Request $request)
29
    {
30
        $dates = [
31
            'start_date' => $request->query->get('start'),
32
            'end_date'   => $request->query->get('end'),
33
        ];
34
35
        /* @type \Mautic\CalendarBundle\Model\CalendarModel $model */
36
        $model  = $this->getModel('calendar');
37
        $events = $model->getCalendarEvents($dates);
38
39
        $this->checkEventPermissions($events);
40
41
        // Can't use $this->sendJsonResponse, because it converts arrays to objects and Fullcalendar doesn't render events then.
42
        $response = new Response();
43
        $response->setContent(json_encode($events));
44
        $response->headers->set('Content-Type', 'application/json');
45
46
        return $response;
47
    }
48
49
    /**
50
     * Updates an event on dragging the event around the calendar.
51
     *
52
     * @return \Symfony\Component\HttpFoundation\JsonResponse
53
     */
54
    public function updateEventAction(Request $request)
55
    {
56
        $entityId  = $request->request->get('entityId');
57
        $source    = $request->request->get('entityType');
58
        $setter    = 'set'.$request->request->get('setter');
59
        $dateValue = new \DateTime($request->request->get('startDate'));
60
        $response  = ['success' => false];
61
62
        /* @type \Mautic\CalendarBundle\Model\CalendarModel $model */
63
        $calendarModel = $this->getModel('calendar');
64
        $event         = $calendarModel->editCalendarEvent($source, $entityId);
0 ignored issues
show
The method editCalendarEvent() 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\CalendarBundle\Model\CalendarModel. ( Ignorable by Annotation )

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

64
        /** @scrutinizer ignore-call */ 
65
        $event         = $calendarModel->editCalendarEvent($source, $entityId);
Loading history...
65
66
        $model  = $event->getModel();
67
        $entity = $event->getEntity();
68
69
        //not found
70
        if (null === $entity) {
71
            $this->addFlash('mautic.core.error.notfound', 'error');
72
        } elseif (!$event->hasAccess()) {
73
            $this->addFlash('mautic.core.error.accessdenied', 'error');
74
        } elseif ($model->isLocked($entity)) {
75
            $this->addFlash(
76
                'mautic.core.error.locked',
77
                [
78
                    '%name%'      => $entity->getTitle(),
79
                    '%menu_link%' => 'mautic_'.$source.'_index',
80
                    '%url%'       => $this->generateUrl(
81
                        'mautic_'.$source.'_action',
82
                        [
83
                            'objectAction' => 'edit',
84
                            'objectId'     => $entity->getId(),
85
                        ]
86
                    ),
87
                ]
88
            );
89
        } elseif ('POST' == $this->request->getMethod()) {
90
            $entity->$setter($dateValue);
91
            $model->saveEntity($entity);
92
            $response['success'] = true;
93
94
            $this->addFlash(
95
                'mautic.core.notice.updated',
96
                [
97
                    '%name%'      => $entity->getTitle(),
98
                    '%menu_link%' => 'mautic_'.$source.'_index',
99
                    '%url%'       => $this->generateUrl(
100
                        'mautic_'.$source.'_action',
101
                        [
102
                            'objectAction' => 'edit',
103
                            'objectId'     => $entity->getId(),
104
                        ]
105
                    ),
106
                ]
107
            );
108
        }
109
110
        //render flashes
111
        $response['flashes'] = $this->getFlashContent();
112
113
        return $this->sendJsonResponse($response);
114
    }
115
116
    /**
117
     * @param $events
118
     */
119
    public function checkEventPermissions(&$events)
120
    {
121
        $security     = $this->get('mautic.security');
122
        $modelFactory = $this->get('mautic.model.factory');
123
124
        foreach ($events as $key => $event) {
125
            //make sure the user has view access to the entities
126
            foreach ($event as $eventKey => $eventValue) {
127
                if ('_id' === substr($eventKey, -3)) {
128
                    $modelName = substr($eventKey, 0, -3);
129
                    if ($modelFactory->hasModel($modelName)) {
130
                        $model = $modelFactory->getModel($modelName);
131
                        $base  = $model->getPermissionBase();
132
                        if (!$security->isGranted([$base.':viewown', $base.':viewother'], 'MATCH_ONE')) {
133
                            unset($events[$key]);
134
                        }
135
                    }
136
137
                    break;
138
                }
139
            }
140
        }
141
    }
142
}
143