Issues (281)

Branch: master

src/Backend/Modules/FormBuilder/Ajax/GetField.php (1 issue)

1
<?php
2
3
namespace Backend\Modules\FormBuilder\Ajax;
4
5
use Backend\Core\Engine\Base\AjaxAction as BackendBaseAJAXAction;
6
use Backend\Modules\FormBuilder\Engine\Model as BackendFormBuilderModel;
7
use Symfony\Component\HttpFoundation\Response;
8
9
/**
10
 * Get a field via ajax.
11
 */
12
class GetField extends BackendBaseAJAXAction
13
{
14
    public function execute(): void
15
    {
16
        parent::execute();
17
18
        // get parameters
19
        $formId = trim($this->getRequest()->request->getInt('form_id'));
20
        $fieldId = trim($this->getRequest()->request->getInt('field_id'));
21
22
        // invalid form id
23
        if (!BackendFormBuilderModel::exists($formId)) {
0 ignored issues
show
$formId of type string is incompatible with the type integer expected by parameter $id of Backend\Modules\FormBuilder\Engine\Model::exists(). ( Ignorable by Annotation )

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

23
        if (!BackendFormBuilderModel::exists(/** @scrutinizer ignore-type */ $formId)) {
Loading history...
24
            $this->output(Response::HTTP_BAD_REQUEST, null, 'form does not exist');
25
26
            return;
27
        }
28
        // invalid fieldId
29
        if (!BackendFormBuilderModel::existsField($fieldId, $formId)) {
30
            $this->output(Response::HTTP_BAD_REQUEST, null, 'field does not exist');
31
32
            return;
33
        }
34
        // get field
35
        $field = BackendFormBuilderModel::getField($fieldId);
36
37
        if ($field['type'] == 'radiobutton') {
38
            $values = [];
39
40
            foreach ($field['settings']['values'] as $value) {
41
                $values[] = $value['label'];
42
            }
43
44
            $field['settings']['values'] = $values;
45
        }
46
47
        // success output
48
        $this->output(Response::HTTP_OK, ['field' => $field]);
49
    }
50
}
51