Issues (281)

Branch: master

Backend/Modules/FormBuilder/Ajax/DeleteField.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
 * Delete a field via ajax.
11
 */
12
class DeleteField 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
        // submit button cannot be deleted
38
        if ($field['type'] == 'submit') {
39
            $this->output(Response::HTTP_BAD_REQUEST, null, 'submit button cannot be deleted');
40
41
            return;
42
        }
43
        // delete field
44
        BackendFormBuilderModel::deleteField($fieldId);
45
46
        // success output
47
        $this->output(Response::HTTP_OK, null, 'field deleted');
48
    }
49
}
50