Issues (3627)

bundles/FormBundle/Controller/ActionController.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\FormBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController as CommonFormController;
15
use Mautic\FormBundle\Entity\Action;
16
use Mautic\FormBundle\Form\Type\ActionType;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
19
/**
20
 * Class ActionController.
21
 */
22
class ActionController extends CommonFormController
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

22
class ActionController extends /** @scrutinizer ignore-deprecated */ CommonFormController
Loading history...
23
{
24
    /**
25
     * Generates new form and processes post data.
26
     *
27
     * @return JsonResponse
28
     */
29
    public function newAction()
30
    {
31
        $success = 0;
32
        $valid   = $cancelled   = false;
33
        $method  = $this->request->getMethod();
34
        $session = $this->get('session');
35
36
        if ('POST' == $method) {
37
            $formAction = $this->request->request->get('formaction');
38
            $actionType = $formAction['type'];
39
            $formId     = $formAction['formId'];
40
        } else {
41
            $actionType = $this->request->query->get('type');
42
            $formId     = $this->request->query->get('formId');
43
            $formAction = [
44
                'type'   => $actionType,
45
                'formId' => $formId,
46
            ];
47
        }
48
49
        //ajax only for form fields
50
        if (!$actionType ||
51
            !$this->request->isXmlHttpRequest() ||
52
            !$this->get('mautic.security')->isGranted(['form:forms:editown', 'form:forms:editother', 'form:forms:create'], 'MATCH_ONE')
53
        ) {
54
            return $this->modalAccessDenied();
55
        }
56
57
        //fire the form builder event
58
        $customComponents = $this->getModel('form.form')->getCustomComponents();
59
        $form             = $this->get('form.factory')->create(ActionType::class, $formAction, [
60
            'action'   => $this->generateUrl('mautic_formaction_action', ['objectAction' => 'new']),
61
            'settings' => $customComponents['actions'][$actionType],
62
            'formId'   => $formId,
63
        ]);
64
        $form->get('formId')->setData($formId);
65
        $formAction['settings'] = $customComponents['actions'][$actionType];
66
67
        //Check for a submitted form and process it
68
        if ('POST' == $method) {
69
            if (!$cancelled = $this->isFormCancelled($form)) {
70
                if ($valid = $this->isFormValid($form)) {
71
                    $success = 1;
72
73
                    //form is valid so process the data
74
                    $keyId = 'new'.hash('sha1', uniqid(mt_rand()));
75
76
                    //save the properties to session
77
                    $actions          = $session->get('mautic.form.'.$formId.'.actions.modified', []);
78
                    $formData         = $form->getData();
79
                    $formAction       = array_merge($formAction, $formData);
80
                    $formAction['id'] = $keyId;
81
                    if (empty($formAction['name'])) {
82
                        //set it to the event default
83
                        $formAction['name'] = $this->get('translator')->trans($formAction['settings']['label']);
84
                    }
85
                    $actions[$keyId] = $formAction;
86
                    $session->set('mautic.form.'.$formId.'.actions.modified', $actions);
87
                } else {
88
                    $success = 0;
89
                }
90
            }
91
        }
92
93
        $viewParams = ['type' => $actionType];
94
95
        if ($cancelled || $valid) {
96
            $closeModal = true;
97
        } else {
98
            $closeModal                 = false;
99
            $viewParams['tmpl']         = 'action';
100
            $viewParams['form']         = (isset($formAction['settings']['formTheme'])) ? $this->setFormTheme($form, 'MauticFormBundle:Builder:action.html.php', $formAction['settings']['formTheme']) : $form->createView();
101
            $header                     = $formAction['settings']['label'];
102
            $viewParams['actionHeader'] = $this->get('translator')->trans($header);
103
        }
104
105
        $passthroughVars = [
106
            'mauticContent' => 'formAction',
107
            'success'       => $success,
108
            'route'         => false,
109
        ];
110
111
        if (!empty($keyId)) {
112
            //prevent undefined errors
113
            $entity     = new Action();
114
            $blank      = $entity->convertToArray();
115
            $formAction = array_merge($blank, $formAction);
116
117
            $template = (!empty($formAction['settings']['template'])) ? $formAction['settings']['template'] :
118
                'MauticFormBundle:Action:generic.html.php';
119
            $passthroughVars['actionId']   = $keyId;
120
            $passthroughVars['actionHtml'] = $this->renderView($template, [
121
                'inForm' => true,
122
                'action' => $formAction,
123
                'id'     => $keyId,
124
                'formId' => $formId,
125
            ]);
126
        }
127
128
        if ($closeModal) {
129
            //just close the modal
130
            $passthroughVars['closeModal'] = 1;
131
132
            return new JsonResponse($passthroughVars);
133
        }
134
135
        return $this->ajaxAction([
136
            'contentTemplate' => 'MauticFormBundle:Builder:'.$viewParams['tmpl'].'.html.php',
137
            'viewParameters'  => $viewParams,
138
            'passthroughVars' => $passthroughVars,
139
        ]);
140
    }
141
142
    /**
143
     * Generates edit form and processes post data.
144
     *
145
     * @param int $objectId
146
     *
147
     * @return JsonResponse
148
     */
149
    public function editAction($objectId)
150
    {
151
        $session    = $this->get('session');
152
        $method     = $this->request->getMethod();
153
        $formaction = $this->request->request->get('formaction', []);
154
        $formId     = 'POST' === $method ? ($formaction['formId'] ?? '') : $this->request->query->get('formId');
155
        $actions    = $session->get('mautic.form.'.$formId.'.actions.modified', []);
156
        $success    = 0;
157
        $valid      = $cancelled      = false;
158
        $formAction = array_key_exists($objectId, $actions) ? $actions[$objectId] : null;
159
160
        if (null !== $formAction) {
161
            $actionType             = $formAction['type'];
162
            $customComponents       = $this->getModel('form.form')->getCustomComponents();
163
            $formAction['settings'] = $customComponents['actions'][$actionType];
164
165
            //ajax only for form fields
166
            if (!$actionType ||
167
                !$this->request->isXmlHttpRequest() ||
168
                !$this->get('mautic.security')->isGranted(['form:forms:editown', 'form:forms:editother', 'form:forms:create'], 'MATCH_ONE')
169
            ) {
170
                return $this->modalAccessDenied();
171
            }
172
173
            $form = $this->get('form.factory')->create(ActionType::class, $formAction, [
174
                'action'   => $this->generateUrl('mautic_formaction_action', ['objectAction' => 'edit', 'objectId' => $objectId]),
175
                'settings' => $formAction['settings'],
176
                'formId'   => $formId,
177
            ]);
178
            $form->get('formId')->setData($formId);
179
180
            //Check for a submitted form and process it
181
            if ('POST' == $method) {
182
                if (!$cancelled = $this->isFormCancelled($form)) {
183
                    if ($valid = $this->isFormValid($form)) {
184
                        $success = 1;
185
186
                        //form is valid so process the data
187
188
                        //save the properties to session
189
                        $session  = $this->get('session');
190
                        $actions  = $session->get('mautic.form.'.$formId.'.actions.modified');
191
                        $formData = $form->getData();
192
                        //overwrite with updated data
193
                        $formAction = array_merge($actions[$objectId], $formData);
194
                        if (empty($formAction['name'])) {
195
                            //set it to the event default
196
                            $formAction['name'] = $this->get('translator')->trans($formAction['settings']['label']);
197
                        }
198
                        $actions[$objectId] = $formAction;
199
                        $session->set('mautic.form.'.$formId.'.actions.modified', $actions);
200
201
                        //generate HTML for the field
202
                        $keyId = $objectId;
203
204
                        //take note if this is a submit button or not
205
                        if ('button' == $actionType) {
206
                            $submits = $session->get('mautic.formactions.submits', []);
207
                            if ('submit' == $formAction['properties']['type'] && !in_array($keyId, $submits)) {
208
                                //button type updated to submit
209
                                $submits[] = $keyId;
210
                                $session->set('mautic.formactions.submits', $submits);
211
                            } elseif ('submit' != $formAction['properties']['type'] && in_array($keyId, $submits)) {
212
                                //button type updated to something other than submit
213
                                $key = array_search($keyId, $submits);
214
                                unset($submits[$key]);
215
                                $session->set('mautic.formactions.submits', $submits);
216
                            }
217
                        }
218
                    }
219
                }
220
            }
221
222
            $viewParams = ['type' => $actionType];
223
            if ($cancelled || $valid) {
224
                $closeModal = true;
225
            } else {
226
                $closeModal                 = false;
227
                $viewParams['tmpl']         = 'action';
228
                $viewParams['form']         = (isset($formAction['settings']['formTheme'])) ? $this->setFormTheme($form, 'MauticFormBundle:Builder:action.html.php', $formAction['settings']['formTheme']) : $form->createView();
229
                $viewParams['actionHeader'] = $this->get('translator')->trans($formAction['settings']['label']);
230
            }
231
232
            $passthroughVars = [
233
                'mauticContent' => 'formAction',
234
                'success'       => $success,
235
                'route'         => false,
236
            ];
237
238
            if (!empty($keyId)) {
239
                $passthroughVars['actionId'] = $keyId;
240
241
                //prevent undefined errors
242
                $entity     = new Action();
243
                $blank      = $entity->convertToArray();
244
                $formAction = array_merge($blank, $formAction);
245
                $template   = (!empty($formAction['settings']['template'])) ? $formAction['settings']['template'] :
246
                    'MauticFormBundle:Action:generic.html.php';
247
                $passthroughVars['actionHtml'] = $this->renderView($template, [
248
                    'inForm' => true,
249
                    'action' => $formAction,
250
                    'id'     => $keyId,
251
                    'formId' => $formId,
252
                ]);
253
            }
254
255
            if ($closeModal) {
256
                //just close the modal
257
                $passthroughVars['closeModal'] = 1;
258
259
                return new JsonResponse($passthroughVars);
260
            }
261
262
            return $this->ajaxAction([
263
                'contentTemplate' => 'MauticFormBundle:Builder:'.$viewParams['tmpl'].'.html.php',
264
                'viewParameters'  => $viewParams,
265
                'passthroughVars' => $passthroughVars,
266
            ]);
267
        }
268
269
        return new JsonResponse(['success' => 0]);
270
    }
271
272
    /**
273
     * Deletes the entity.
274
     *
275
     * @param $objectId
276
     *
277
     * @return JsonResponse
278
     */
279
    public function deleteAction($objectId)
280
    {
281
        $session = $this->get('session');
282
        $formId  = $this->request->query->get('formId');
283
        $actions = $session->get('mautic.form.'.$formId.'.actions.modified', []);
284
        $delete  = $session->get('mautic.form.'.$formId.'.actions.deleted', []);
285
286
        //ajax only for form fields
287
        if (!$this->request->isXmlHttpRequest() ||
288
            !$this->get('mautic.security')->isGranted(['form:forms:editown', 'form:forms:editother', 'form:forms:create'], 'MATCH_ONE')
289
        ) {
290
            return $this->accessDenied();
291
        }
292
293
        $formAction = (array_key_exists($objectId, $actions)) ? $actions[$objectId] : null;
294
        if ('POST' == $this->request->getMethod() && null !== $formAction) {
295
            //add the field to the delete list
296
            if (!in_array($objectId, $delete)) {
297
                $delete[] = $objectId;
298
                $session->set('mautic.form.'.$formId.'.actions.deleted', $delete);
299
            }
300
301
            //take note if this is a submit button or not
302
            if ('button' == $formAction['type']) {
303
                $submits    = $session->get('mautic.formactions.submits', []);
304
                $properties = $formAction['properties'];
305
                if ('submit' == $properties['type'] && in_array($objectId, $submits)) {
306
                    $key = array_search($objectId, $submits);
307
                    unset($submits[$key]);
308
                    $session->set('mautic.formactions.submits', $submits);
309
                }
310
            }
311
312
            $dataArray = [
313
                'mauticContent' => 'formAction',
314
                'success'       => 1,
315
                'route'         => false,
316
            ];
317
        } else {
318
            $dataArray = ['success' => 0];
319
        }
320
321
        return new JsonResponse($dataArray);
322
    }
323
}
324