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\FormController; |
||
15 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
16 | |||
17 | /** |
||
18 | * Class DefaultController. |
||
19 | */ |
||
20 | class DefaultController extends FormController |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
21 | { |
||
22 | /** |
||
23 | * Generates the default view. |
||
24 | * |
||
25 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response |
||
26 | */ |
||
27 | public function indexAction() |
||
28 | { |
||
29 | return $this->delegateView([ |
||
30 | 'contentTemplate' => 'MauticCalendarBundle:Default:index.html.php', |
||
31 | 'passthroughVars' => [ |
||
32 | 'activeLink' => '#mautic_calendar_index', |
||
33 | 'mauticContent' => 'calendar', |
||
34 | 'route' => $this->generateUrl('mautic_calendar_index'), |
||
35 | ], |
||
36 | ]); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Generates the modal view. |
||
41 | * |
||
42 | * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response |
||
43 | */ |
||
44 | public function editAction() |
||
45 | { |
||
46 | $source = $this->request->query->get('source'); |
||
47 | $startDate = new \DateTime($this->request->query->get('startDate')); |
||
48 | $entityId = $this->request->query->get('objectId'); |
||
49 | |||
50 | /* @type \Mautic\CalendarBundle\Model\CalendarModel $model */ |
||
51 | $calendarModel = $this->getModel('calendar'); |
||
52 | $event = $calendarModel->editCalendarEvent($source, $entityId); |
||
53 | |||
54 | $model = $event->getModel(); |
||
55 | $entity = $event->getEntity(); |
||
56 | $session = $this->get('session'); |
||
57 | $sourceSession = $this->get('session')->get('mautic.calendar.'.$source, 1); |
||
58 | |||
59 | //set the return URL |
||
60 | $returnUrl = $this->generateUrl('mautic_calendar_index', [$source => $sourceSession]); |
||
61 | |||
62 | $postActionVars = [ |
||
63 | 'returnUrl' => $returnUrl, |
||
64 | 'viewParameters' => [$source => $sourceSession], |
||
65 | 'contentTemplate' => $event->getContentTemplate(), |
||
66 | 'passthroughVars' => [ |
||
67 | 'activeLink' => 'mautic_calendar_index', |
||
68 | 'mauticContent' => $source, |
||
69 | ], |
||
70 | ]; |
||
71 | |||
72 | //not found |
||
73 | if (null === $entity) { |
||
74 | return $this->postActionRedirect( |
||
75 | array_merge($postActionVars, [ |
||
76 | 'flashes' => [ |
||
77 | [ |
||
78 | 'type' => 'error', |
||
79 | 'msg' => 'mautic.'.$source.'.error.notfound', |
||
80 | 'msgVars' => ['%id%' => $entityId], |
||
81 | ], |
||
82 | ], |
||
83 | ]) |
||
84 | ); |
||
85 | } elseif (!$event->hasAccess()) { |
||
86 | return $this->accessDenied(); |
||
87 | } elseif ($model->isLocked($entity)) { |
||
88 | //deny access if the entity is locked |
||
89 | return $this->isLocked($postActionVars, $entity, $source.'.'.$source); |
||
90 | } |
||
91 | |||
92 | //Create the form |
||
93 | $action = $this->generateUrl('mautic_calendar_action', [ |
||
94 | 'objectAction' => 'edit', |
||
95 | 'objectId' => $entity->getId(), |
||
96 | 'source' => $source, |
||
97 | 'startDate' => $startDate->format('Y-m-d H:i:s'), |
||
98 | ]); |
||
99 | $form = $model->createForm($entity, $this->get('form.factory'), $action, ['formName' => $event->getFormName()]); |
||
100 | |||
101 | ///Check for a submitted form and process it |
||
102 | if ('POST' == $this->request->getMethod()) { |
||
103 | $valid = false; |
||
104 | if (!$cancelled = $this->isFormCancelled($form)) { |
||
105 | if ($valid = $this->isFormValid($form)) { |
||
106 | $contentName = 'mautic.'.$source.'builder.'.$entity->getSessionId().'.content'; |
||
107 | $existingContent = $entity->getContent(); |
||
108 | $newContent = $session->get($contentName, []); |
||
109 | $content = array_merge($existingContent, $newContent); |
||
110 | $entity->setContent($content); |
||
111 | |||
112 | //form is valid so process the data |
||
113 | $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked()); |
||
114 | |||
115 | //clear the session |
||
116 | $session->remove($contentName); |
||
117 | |||
118 | $this->addFlash('mautic.core.notice.updated', [ |
||
119 | '%name%' => $entity->getTitle(), |
||
120 | '%menu_link%' => 'mautic_'.$source.'_index', |
||
121 | '%url%' => $this->generateUrl('mautic_'.$source.'_action', [ |
||
122 | 'objectAction' => 'edit', |
||
123 | 'objectId' => $entity->getId(), |
||
124 | ]), |
||
125 | ]); |
||
126 | } |
||
127 | } else { |
||
128 | //clear any modified content |
||
129 | $session->remove('mautic.'.$source.'builder.'.$entityId.'.content'); |
||
130 | //unlock the entity |
||
131 | $model->unlockEntity($entity); |
||
132 | } |
||
133 | |||
134 | if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) { |
||
135 | return new JsonResponse([ |
||
136 | 'mauticContent' => 'calendarModal', |
||
137 | 'closeModal' => 1, |
||
138 | ]); |
||
139 | } |
||
140 | } else { |
||
141 | //lock the entity |
||
142 | $model->lockEntity($entity); |
||
143 | } |
||
144 | |||
145 | $builderComponents = $model->getBuilderComponents($entity); |
||
146 | |||
147 | return $this->delegateView([ |
||
148 | 'viewParameters' => [ |
||
149 | 'form' => $this->setFormTheme($form, $event->getContentTemplate()), |
||
150 | 'tokens' => $builderComponents[$source.'Tokens'], |
||
151 | 'entity' => $entity, |
||
152 | 'model' => $model, |
||
153 | ], |
||
154 | 'contentTemplate' => $event->getContentTemplate(), |
||
155 | 'passthroughVars' => [ |
||
156 | 'activeLink' => '#mautic_calendar_index', |
||
157 | 'mauticContent' => 'calendarModal', |
||
158 | 'route' => $this->generateUrl('mautic_calendar_action', [ |
||
159 | 'objectAction' => 'edit', |
||
160 | 'objectId' => $entity->getId(), |
||
161 | 'source' => $source, |
||
162 | 'startDate' => $startDate->format('Y-m-d H:i:s'), |
||
163 | ]), |
||
164 | ], |
||
165 | ]); |
||
166 | } |
||
167 | } |
||
168 |