Issues (3627)

bundles/FormBundle/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\FormBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\AjaxController as CommonAjaxController;
15
use Mautic\CoreBundle\Helper\InputHelper;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Class AjaxController.
20
 */
21
class AjaxController extends CommonAjaxController
22
{
23
    /**
24
     * @param string $name
25
     *
26
     * @return \Symfony\Component\HttpFoundation\JsonResponse
27
     */
28
    protected function reorderFieldsAction(Request $request, $bundle, $name = 'fields')
29
    {
30
        if ('form' === $name) {
31
            $name = 'fields';
32
        }
33
        $dataArray   = ['success' => 0];
34
        $sessionId   = InputHelper::clean($request->request->get('formId'));
35
        $sessionName = 'mautic.form.'.$sessionId.'.'.$name.'.modified';
36
        $session     = $this->get('session');
37
        $orderName   = ('fields' == $name) ? 'mauticform' : 'mauticform_action';
38
        $order       = InputHelper::clean($request->request->get($orderName));
39
        $components  = $session->get($sessionName);
40
41
        if (!empty($order) && !empty($components)) {
42
            $components = array_replace(array_flip($order), $components);
0 ignored issues
show
It seems like $order can also be of type string; however, parameter $array of array_flip() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

42
            $components = array_replace(array_flip(/** @scrutinizer ignore-type */ $order), $components);
Loading history...
43
            $session->set($sessionName, $components);
44
            $dataArray['success'] = 1;
45
        }
46
47
        return $this->sendJsonResponse($dataArray);
48
    }
49
50
    /**
51
     * @return \Symfony\Component\HttpFoundation\JsonResponse
52
     */
53
    protected function reorderActionsAction(Request $request)
54
    {
55
        return $this->reorderFieldsAction($request, 'actions');
56
    }
57
58
    /**
59
     * @return \Symfony\Component\HttpFoundation\JsonResponse
60
     */
61
    protected function updateFormFieldsAction(Request $request)
62
    {
63
        $formId     = (int) $request->request->get('formId');
64
        $dataArray  = ['success' => 0];
65
        $model      = $this->getModel('form');
66
        $entity     = $model->getEntity($formId);
67
        $formFields = empty($entity) ? [] : $entity->getFields();
68
        $fields     = [];
69
70
        foreach ($formFields as $field) {
71
            if ('button' != $field->getType()) {
72
                $properties = $field->getProperties();
73
                $options    = [];
74
75
                if (!empty($properties['list']['list'])) {
76
                    //If the field is a SELECT field then the data gets stored in [list][list]
77
                    $optionList = $properties['list']['list'];
78
                } elseif (!empty($properties['optionlist']['list'])) {
79
                    //If the field is a radio or a checkbox then it will be stored in [optionlist][list]
80
                    $optionList = $properties['optionlist']['list'];
81
                }
82
                if (!empty($optionList)) {
83
                    foreach ($optionList as $listItem) {
84
                        if (is_array($listItem) && isset($listItem['value']) && isset($listItem['label'])) {
85
                            //The select box needs values to be [value] => label format so make sure we have that style then put it in
86
                            $options[$listItem['value']] = $listItem['label'];
87
                        } elseif (!is_array($listItem)) {
88
                            //Keeping for BC
89
                            $options[] = $listItem;
90
                        }
91
                    }
92
                }
93
94
                $fields[] = [
95
                    'id'      => $field->getId(),
96
                    'label'   => $field->getLabel(),
97
                    'alias'   => $field->getAlias(),
98
                    'type'    => $field->getType(),
99
                    'options' => $options,
100
                ];
101
102
                // Be sure to not pollute the symbol table.
103
                unset($optionList);
104
            }
105
        }
106
107
        $dataArray['fields']  = $fields;
108
        $dataArray['success'] = 1;
109
110
        return $this->sendJsonResponse($dataArray);
111
    }
112
113
    /**
114
     * Ajax submit for forms.
115
     *
116
     * @return \Symfony\Component\HttpFoundation\JsonResponse
117
     */
118
    public function submitAction()
119
    {
120
        $response     = $this->forwardWithPost('MauticFormBundle:Public:submit', $this->request->request->all(), [], ['ajax' => true]);
121
        $responseData = json_decode($response->getContent(), true);
122
        $success      = (!in_array($response->getStatusCode(), [404, 500]) && empty($responseData['errorMessage'])
123
            && empty($responseData['validationErrors']));
124
125
        $message = '';
126
        $type    = '';
127
        if (isset($responseData['successMessage'])) {
128
            $message = $responseData['successMessage'];
129
            $type    = 'notice';
130
        } elseif (isset($responseData['errorMessage'])) {
131
            $message = $responseData['errorMessage'];
132
            $type    = 'error';
133
        }
134
135
        $data = array_merge($responseData, ['message' => $message, 'type' => $type, 'success' => $success]);
136
137
        return $this->sendJsonResponse($data);
138
    }
139
}
140