Issues (281)

Branch: master

src/Backend/Modules/FormBuilder/Engine/Helper.php (1 issue)

1
<?php
2
3
namespace Backend\Modules\FormBuilder\Engine;
4
5
use Backend\Core\Engine\Form as BackendForm;
6
use Backend\Core\Engine\Model as BackendModel;
7
use Backend\Core\Engine\TwigTemplate as BackendTemplate;
8
9
/**
10
 * Helper class for the form_builder module.
11
 *
12
 * Dieter Vanden Eynde <[email protected]>
13
 */
14
class Helper
15
{
16
    /**
17
     * Parse a field and return the HTML.
18
     *
19
     * @param array $field Field data.
20
     *
21
     * @return string
22
     */
23
    public static function parseField(array $field): string
24
    {
25
        if (!empty($field)) {
26
            // init
27
            $form = new BackendForm('tmp', '');
28
            $tpl = (
29
                BackendModel::getContainer()->has('template') ?
30
                BackendModel::getContainer()->get('template') :
31
                new BackendTemplate()
32
            );
33
            $fieldHTML = '';
34
            $fieldName = 'field' . $field['id'];
35
            $values = (isset($field['settings']['values']) ? $field['settings']['values'] : null);
36
            $defaultValues = (
37
                isset($field['settings']['default_values']) ?
38
                $field['settings']['default_values'] :
39
                null
40
            );
41
            $placeholder = (isset($field['settings']['placeholder']) ? $field['settings']['placeholder'] : null);
42
43
            /*
44
             * Create form and parse to HTML
45
             */
46
            // dropdown
47
            if ($field['type'] === 'dropdown') {
48
                // values and labels are the same
49
                $values = array_combine($values, $values);
50
51
                // get index of selected item
52
                $defaultIndex = array_search($defaultValues, $values, true);
53
                if ($defaultIndex === false) {
54
                    $defaultIndex = null;
55
                }
56
57
                // create element
58
                $ddm = $form->addDropdown($fieldName, $values, $defaultIndex);
59
60
                // empty default element
61
                $ddm->setDefaultElement('');
62
63
                // get content
64
                $fieldHTML = $ddm->parse();
65
            } elseif ($field['type'] === 'datetime') {
66
                // create element
67
                if ($field['settings']['input_type'] === 'date') {
68
                    // calculate default value
69
                    $amount = $field['settings']['value_amount'];
70
                    $type = $field['settings']['value_type'];
71
72
                    if ($type != '') {
73
                        switch ($type) {
74
                            case 'today':
75
                                $defaultValues = date('d/m/Y');
76
                                break;
77
                            case 'day':
78
                            case 'week':
79
                            case 'month':
80
                            case 'year':
81
                                if ($amount != '') {
82
                                    $defaultValues = date('d/m/Y', strtotime('+' . $amount . ' ' . $type));
83
                                }
84
                                break;
85
                        }
86
                    }
87
88
                    $datetime = $form->addText($fieldName, $defaultValues);
89
                } else {
90
                    $datetime = $form->addTime($fieldName, $defaultValues);
91
                }
92
                $datetime->setAttribute('disabled', 'disabled');
93
94
                // get content
95
                $fieldHTML = $datetime->parse();
96
            } elseif ($field['type'] === 'radiobutton') {
97
                // create element
98
                $rbt = $form->addRadiobutton($fieldName, $values, $defaultValues);
99
100
                // get content
101
                $fieldHTML = $rbt->parse();
102
            } elseif ($field['type'] === 'checkbox') {
103
                // rebuild values
104
                $newValues = [];
105
                foreach ((array) $values as $value) {
106
                    $newValues[] = ['label' => $value, 'value' => $value];
107
                }
108
109
                // create element
110
                $chk = $form->addMultiCheckbox($fieldName, $newValues, $defaultValues);
111
112
                // get content
113
                $fieldHTML = $chk->parse();
114
            } elseif ($field['type'] === 'textbox') {
115
                // create element
116
                $txt = $form->addText($fieldName, $defaultValues);
117
                $txt->setAttribute('disabled', 'disabled');
118
                $txt->setAttribute('placeholder', $placeholder);
119
120
                // get content
121
                $fieldHTML = $txt->parse();
122
            } elseif ($field['type'] === 'textarea') {
123
                // create element
124
                $txt = $form->addTextarea($fieldName, $defaultValues);
125
                $txt->setAttribute('cols', 30);
126
                $txt->setAttribute('disabled', 'disabled');
127
                $txt->setAttribute('placeholder', $placeholder);
128
129
                // get content
130
                $fieldHTML = $txt->parse();
131
            } elseif ($field['type'] === 'heading') {
132
                $fieldHTML = '<h3>' . $values . '</h3>';
133
            } elseif ($field['type'] === 'paragraph') {
134
                $fieldHTML = $values;
135
            } elseif ($field['type'] === 'recaptcha') {
136
                $fieldHTML = '<p>reCAPTCHA</p>';
137
            }
138
139
            /*
140
             * Parse the field into the template
141
             */
142
            // init
143
            $tpl->assign('plaintext', false);
0 ignored issues
show
The method assign() does not exist on null. ( Ignorable by Annotation )

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

143
            $tpl->/** @scrutinizer ignore-call */ 
144
                  assign('plaintext', false);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
            $tpl->assign('simple', false);
145
            $tpl->assign('multiple', false);
146
            $tpl->assign('id', $field['id']);
147
            $tpl->assign('required', isset($field['validations']['required']));
148
149
            // plaintext items
150
            if (in_array($field['type'], ['heading', 'paragraph', 'recaptcha'])) {
151
                // assign
152
                $tpl->assign('content', $fieldHTML);
153
                $tpl->assign('plaintext', true);
154
            } elseif (in_array($field['type'], ['checkbox', 'radiobutton'])) {
155
                // name (prefixed by type)
156
                $name = ($field['type'] === 'checkbox') ?
157
                    'chk' . \SpoonFilter::ucfirst($fieldName) :
158
                    'rbt' . \SpoonFilter::ucfirst($fieldName)
159
                ;
160
161
                // rebuild so the html is stored in a general name (and not rbtName)
162
                foreach ($fieldHTML as &$item) {
163
                    $item['field'] = $item[$name];
164
                }
165
166
                // show multiple
167
                $tpl->assign('label', $field['settings']['label']);
168
                $tpl->assign('items', $fieldHTML);
169
                $tpl->assign('multiple', true);
170
            } else {
171
                // assign
172
                $tpl->assign('label', $field['settings']['label']);
173
                $tpl->assign('field', $fieldHTML);
174
                $tpl->assign('simple', true);
175
            }
176
177
            return $tpl->getContent(BACKEND_MODULES_PATH . '/FormBuilder/Layout/Templates/Field.html.twig');
178
        }
179
180
        // empty field so return empty string
181
        return '';
182
    }
183
}
184