Issues (36)

app/Helpers/BSForm.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace App\Helpers;
4
5
class BSForm
6
{
7
    public static function show($method, $action, $data, $submit = [])
8
    {
9
        echo '<form method="'.$method.'"';
10
        if (! empty($action) || strlen($action) > 0) {
11
            echo ' action="'.$action.'"';
12
        }
13
        echo '>';
14
        foreach ($data as $arr) {
15
            $fn = $arr[0];
16
            unset($arr[0]);
17
            self::$fn($arr);
18
        }
19
        echo '<button type="submit" class="btn ';
20
        if (isset($submit['class'])) {
21
            echo $submit['class'];
22
        } else {
23
            echo 'btn-primary';
24
        }
25
        echo '"';
26
        if (isset($submit['extra'])) {
27
            echo ' '.$submit['extra'];
28
        }
29
        echo '>';
30
        if (isset($submit['value'])) {
31
            echo $submit['value'];
32
        } else {
33
            echo 'Submit';
34
        }
35
        echo '</button>';
36
        echo '</form>';
37
    }
38
39
    public static function open($method, $action = '', $data = []): string
40
    {
41
        $str = '<form action="'.$action.'" method="'.($method == 'PUT' ? 'POST' : $method).'" '.self::printAttr($data).'>';
42
        $str .= '<input type="hidden" name="_token" value="'.csrf_token().'">';
43
        if ($method == 'PUT') {
44
            $str .= '<input type="hidden" name="_method" value="PUT">';
45
        }
46
47
        return $str;
48
    }
49
50
    public static function close($submit, $text = 'Submit', $class = 'btn btn-success', $data = []): string
51
    {
52
        if (! $submit) {
53
            return '</form>';
54
        }
55
56
        return self::submit($text, $class, $data).'</form>';
57
    }
58
59
    public static function printAttr($data)
60
    {
61
        $str = '';
62
        foreach ($data as $k => $v) {
63
            $str .= $k.'="'.$v.'" ';
64
        }
65
66
        return $str;
67
    }
68
69
    public static function multiInput($datas)
70
    {
71
        $str = '';
72
        foreach ($datas as $data) {
73
            $str .= self::input(
74
                $data[0],
75
                $data[1],
76
                isset($data[2]) ? $data[2] : '',
77
                isset($data[3]) ? $data[3] : '',
78
                isset($data[4]) ? $data[4] : []
79
            );
80
        }
81
82
        return $str;
83
    }
84
85
    public static function multi($datas)
86
    {
87
        $str = '';
88
        foreach ($datas as $data) {
89
            $func = $data[0];
90
            $dc = count($data);
91
            switch ($dc) {
92
                case 0:
93
                case 1:
94
                case 2:
95
                    break;
96
97
                case 3:
98
                    $str .= self::$func($data[1], $data[2]);
99
                    break;
100
101
                case 4:
102
                    $str .= self::$func($data[1], $data[2], $data[3]);
103
                    break;
104
105
                case 5:
106
                    $str .= self::$func($data[1], $data[2], $data[3], $data[4]);
107
                    break;
108
109
                default:
110
                    $str .= self::$func($data[1], $data[2], $data[3], $data[4], $data[5]);
111
                    break;
112
            }
113
        }
114
115
        return $str;
116
    }
117
118
    public static function multiText($datas)
119
    {
120
        $str = '';
121
        foreach ($datas as $data) {
122
            $str .= self::input(
123
                'text',
124
                $data[0],
125
                isset($data[1]) ? $data[1] : '',
126
                isset($data[2]) ? $data[2] : '',
127
                isset($data[3]) ? $data[3] : []
128
            );
129
        }
130
131
        return $str;
132
    }
133
134
    public static function input($type, $name, $label = '', $value = '', $data = []): string
135
    {
136
        $str = '
137
        <div class="mb-3">';
138
139
        if (! empty($label)) {
140
            $str .= '<label for="'.$type.'" class="form-label">'.$label.':</label>';
141
        }
142
143
        $str .= '<input type="'.$type.'" class="form-control" name="'.$name.'" value="'.old($name, $value).'" '.self::printAttr($data).'>';
0 ignored issues
show
Are you sure old($name, $value) of type array|null|string can be used in concatenation? ( Ignorable by Annotation )

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

143
        $str .= '<input type="'.$type.'" class="form-control" name="'.$name.'" value="'./** @scrutinizer ignore-type */ old($name, $value).'" '.self::printAttr($data).'>';
Loading history...
144
        if ($type == 'file') {
145
            $str .= '<img src="'.$value.'"/>';
146
        }
147
        $str .= '</div>';
148
149
        return $str;
150
    }
151
152
    public static function select($name, $options, $label = '', $value = '', $data = []): string
153
    {
154
        $str = '
155
        <div class="mb-3">';
156
157
        if (! empty($label)) {
158
            $str .= '<label for="select" class="form-label">'.$label.':</label>';
159
        }
160
161
        $str .= '<select class="form-control" name="'.$name.'" '.self::printAttr($data).'>';
162
        foreach ($options as $k => $v) {
163
            $str .= '<option value="'.$k.'"'.($k == old($name, $value) ? ' selected' : '').'>'.$v.'</option>';
164
        }
165
        $str .= '
166
            </select>
167
        </div>';
168
169
        return $str;
170
    }
171
172
    public static function selectM($name, $options, $label = '', $value = [], $data = [])
173
    {
174
        $str = '
175
        <div class="mb-3">';
176
177
        if (! empty($label)) {
178
            $str .= '<label class="form-label" for="select">'.$label.':</label>';
179
        }
180
181
        $str .= '<select class="form-control" multiple="true" name="'.$name.'[]" '.self::printAttr($data).'>';
182
        $value = old($name, $value);
183
        foreach ($options as $k => $v) {
184
            $str .= '<option value="'.$k.'"'.(in_array($k, $value) ? ' selected' : '').'>'.$v.'</option>';
185
        }
186
        $str .= '
187
            </select>
188
        </div>';
189
190
        return $str;
191
    }
192
193
    public static function textarea($name, $label = '', $value = '', $data = [])
194
    {
195
        $str = '
196
        <div class="mb-3">';
197
198
        if (! empty($label)) {
199
            $str .= '<label for="textarea" class="form-label">'.$label.':</label>';
200
        }
201
202
        $str .= '<textarea class="form-control" name="'.$name.'" '.self::printAttr($data).'>'.old($name, $value).'</textarea>
0 ignored issues
show
Are you sure old($name, $value) of type array|null|string can be used in concatenation? ( Ignorable by Annotation )

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

202
        $str .= '<textarea class="form-control" name="'.$name.'" '.self::printAttr($data).'>'./** @scrutinizer ignore-type */ old($name, $value).'</textarea>
Loading history...
203
        </div>';
204
205
        return $str;
206
    }
207
208
    // Input Types
209
210
    public static function hidden($name, $value = ''): string
211
    {
212
        return "<input type=\"hidden\" name=\"$name\" value=\"$value\" />";
213
    }
214
215
    public static function text($name, $label = '', $value = '', $data = [])
216
    {
217
        return self::input('text', $name, $label, $value, $data);
218
    }
219
220
    public static function password($name, $label = '', $value = '', $data = [])
221
    {
222
        return self::input('password', $name, $label, $value, $data);
223
    }
224
225
    public static function email($name, $label = '', $value = '', $data = [])
226
    {
227
        return self::input('email', $name, $label, $value, $data);
228
    }
229
230
    public static function number($name, $label = '', $value = '', $data = [])
231
    {
232
        return self::input('number', $name, $label, $value, $data);
233
    }
234
235
    public static function date($name, $label = '', $value = '', $data = [])
236
    {
237
        return self::input('date', $name, $label, $value, $data);
238
    }
239
240
    public static function datetime_local($name, $label = '', $value = '', $data = [])
241
    {
242
        return self::input('datetime-local', $name, $label, $value, $data);
243
    }
244
245
    public static function range($name, $label = '', $value = '', $data = [])
246
    {
247
        return self::input('range', $name, $label, $value, $data);
248
    }
249
250
    public static function file($name, $label = '', $value = '', $data = [])
251
    {
252
        return self::input('file', $name, $label, $value, $data);
253
    }
254
255
    public static function checkbox($name, $label = '', $value = '', $data = []): string
256
    {
257
        $checked = old($name, $value) ? 'checked' : '';
258
259
        return '
260
        <div class="mb-3 form-check">
261
            <input type="checkbox" class="form-check-input" name="'.$name.'" '.$checked.' '.self::printAttr($data).'>
262
            <label for="checkbox" class="form-check-label">'.$label.'</label>
263
        </div>';
264
    }
265
266
    // Submit
267
268
    public static function submit($text, $class = 'btn btn-success', $data = []): string
269
    {
270
        return '<button type="submit" class="'.$class.'" '.self::printAttr($data).'>'.$text.'</button>';
271
    }
272
}
273