HTML::form()   C
last analyzed

Complexity

Conditions 13
Paths 64

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 16
nc 64
nop 5
dl 0
loc 32
rs 5.1234
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license MIT; https://www.oscommerce.com/license/mit.txt
7
  */
8
9
namespace OSC\OM;
10
11
use OSC\OM\HTML\Panel;
12
use OSC\OM\OSCOM;
13
use OSC\OM\Registry;
14
15
class HTML
16
{
17
    public static function output($string, $translate = null)
18
    {
19
        if (!isset($translate)) {
20
            $translate = [
21
                '"' => '&quot;'
22
            ];
23
        }
24
25
        return strtr(trim($string), $translate);
26
    }
27
28
    public static function outputProtected($string)
29
    {
30
        return htmlspecialchars(trim($string));
31
    }
32
33
    public static function sanitize($string)
34
    {
35
        $patterns = [
36
            '/ +/',
37
            '/[<>]/'
38
        ];
39
40
        $replace = [
41
            ' ',
42
            '_'
43
        ];
44
45
        return preg_replace($patterns, $replace, trim($string));
46
    }
47
48
    public static function image($src, $alt = null, $width = null, $height = null, $parameters = '', $responsive = false, $bootstrap_css = '')
49
    {
50
        if ((empty($src) || ($src == OSCOM::linkImage(''))) && (IMAGE_REQUIRED == 'false')) {
51
            return false;
52
        }
53
54
// alt is added to the img tag even if it is null to prevent browsers from outputting
55
// the image filename as default
56
        $image = '<img src="' . static::output($src) . '" alt="' . static::output($alt) . '"';
57
58 View Code Duplication
        if (isset($alt) && (strlen($alt) > 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $image .= ' title="' . static::output($alt) . '"';
60
        }
61
62 View Code Duplication
        if (isset($width) && (strlen($width) > 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $image .= ' width="' . static::output($width) . '"';
64
        }
65
66 View Code Duplication
        if (isset($height) && (strlen($height) > 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $image .= ' height="' . static::output($height) . '"';
68
        }
69
70
        $class = [];
71
72
        if ($responsive === true) {
73
            $class[] = 'img-responsive';
74
        }
75
76
        if (!empty($bootstrap_css)) {
77
            $class[] = $bootstrap_css;
78
        }
79
80
        if (!empty($class)) {
81
            $image .= ' class="' . implode(' ', $class) . '"';
82
        }
83
84
        if (!empty($parameters)) {
85
            $image .= ' ' . $parameters;
86
        }
87
88
        $image .= ' />';
89
90
        return $image;
91
    }
92
93
    public static function form($name, $action, $method = 'post', $parameters = '', array $flags = [])
94
    {
95
        if (!isset($flags['tokenize']) || !is_bool($flags['tokenize'])) {
96
            $flags['tokenize'] = false;
97
        }
98
99
        if (!isset($flags['session_id']) || !is_bool($flags['session_id'])) {
100
            $flags['session_id'] = false;
101
        }
102
103
        $form = '<form name="' . static::output($name) . '" action="' . static::output($action) . '" method="' . static::output($method) . '"';
104
105
        if (!empty($parameters)) {
106
            $form .= ' ' . $parameters;
107
        }
108
109
        $form .= '>';
110
111
        if (isset($flags['action'])) {
112
            $form .= static::hiddenField('action', $flags['action']);
0 ignored issues
show
Documentation introduced by
$flags['action'] is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
        }
114
115
        if (($flags['session_id'] === true) && Registry::get('Session')->hasStarted() && (strlen(SID) > 0) && !Registry::get('Session')->isForceCookies()) {
116
            $form .= static::hiddenField(session_name(), session_id());
117
        }
118
119
        if (($flags['tokenize'] === true) && isset($_SESSION['sessiontoken'])) {
120
            $form .= static::hiddenField('formid', $_SESSION['sessiontoken']);
121
        }
122
123
        return $form;
124
    }
125
126
    public static function inputField($name, $value = '', $parameters = '', $type = 'text', $reinsert_value = true, $class = 'form-control')
127
    {
128
        $field = '<input type="' . static::output($type) . '" name="' . static::output($name) . '"';
129
130 View Code Duplication
        if (($reinsert_value == true) && ((isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
131
            if (isset($_GET[$name]) && is_string($_GET[$name])) {
132
                $value = $_GET[$name];
133
            } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
134
                $value = $_POST[$name];
135
            }
136
        }
137
138
        if (strlen($value) > 0) {
139
            $field .= ' value="' . static::output($value) . '"';
140
        }
141
142
        if (!empty($parameters)) {
143
            $field .= ' ' . $parameters;
144
        }
145
146
        if (!empty($class)) {
147
            $field .= ' class="' . $class . '"';
148
        }
149
150
        $field .= ' />';
151
152
        return $field;
153
    }
154
155
    public static function passwordField($name, $value = '', $parameters = 'maxlength="40"')
156
    {
157
        return static::inputField($name, $value, $parameters, 'password', false);
158
    }
159
160
    public static function fileField($name, $parameters = null)
161
    {
162
        return static::inputField($name, null, $parameters, 'file', false);
163
    }
164
165
    protected static function selectionField($name, $type, $value = '', $checked = false, $parameters = '')
166
    {
167
        $selection = '<input type="' . static::output($type) . '" name="' . static::output($name) . '"';
168
169
        if (strlen($value) > 0) {
170
            $selection .= ' value="' . static::output($value) . '"';
171
        }
172
173
        if (($checked == true) || (isset($_GET[$name]) && is_string($_GET[$name]) && (($_GET[$name] == 'on') || ($_GET[$name] == $value))) || (isset($_POST[$name]) && is_string($_POST[$name]) && (($_POST[$name] == 'on') || ($_POST[$name] == $value)))) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
174
            $selection .= ' checked="checked"';
175
        }
176
177
        if (!empty($parameters)) {
178
            $selection .= ' ' . $parameters;
179
        }
180
181
        $selection .= ' />';
182
183
        return $selection;
184
    }
185
186
    public static function checkboxField($name, $value = '', $checked = false, $parameters = '')
187
    {
188
        return static::selectionField($name, 'checkbox', $value, $checked, $parameters);
189
    }
190
191
    public static function radioField($name, $value = '', $checked = false, $parameters = '')
192
    {
193
        return static::selectionField($name, 'radio', $value, $checked, $parameters);
194
    }
195
196
    public static function textareaField($name, $width, $height, $text = '', $parameters = '', $reinsert_value = true, $class = 'form-control')
197
    {
198
        $field = '<textarea name="' . static::output($name) . '" cols="' . static::output($width) . '" rows="' . static::output($height) . '"';
199
200
        if (!empty($parameters)) {
201
            $field .= ' ' . $parameters;
202
        }
203
204
        if (!empty($class)) {
205
            $field .= ' class="' . $class . '"';
206
        }
207
208
        $field .= '>';
209
210
        if (($reinsert_value == true) && ((isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])))) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
211
            if (isset($_GET[$name]) && is_string($_GET[$name])) {
212
                $field .= static::outputProtected($_GET[$name]);
213 View Code Duplication
            } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
                $field .= static::outputProtected($_POST[$name]);
215
            }
216
        } elseif (strlen($text) > 0) {
217
            $field .= static::outputProtected($text);
218
        }
219
220
        $field .= '</textarea>';
221
222
        return $field;
223
    }
224
225
    public static function selectField($name, array $values, $default = null, $parameters = '', $required = false, $class = 'form-control')
226
    {
227
        $group = false;
228
229
        $field = '<select name="' . static::output($name) . '"';
230
231
        if ($required == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
232
            $field .= ' required aria-required="true"';
233
        }
234
235
        if (!empty($parameters)) {
236
            $field .= ' ' . $parameters;
237
        }
238
239
        if (!empty($class)) {
240
            $field .= ' class="' . $class . '"';
241
        }
242
243
        $field .= '>';
244
245
        if ($required == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
246
            $field .= '<option value="">' . OSCOM::getDef('pull_down_default') . '</option>';
247
        }
248
249 View Code Duplication
        if (empty($default) && ((isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
            if (isset($_GET[$name]) && is_string($_GET[$name])) {
251
                $default = $_GET[$name];
252
            } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
253
                $default = $_POST[$name];
254
            }
255
        }
256
257
        $ci = new \CachingIterator(new \ArrayIterator($values), \CachingIterator::TOSTRING_USE_CURRENT); // used for hasNext() below
258
259
        foreach ($ci as $v) {
260
            if (isset($v['group'])) {
261
                if ($group != $v['group']) {
262
                    $group = $v['group'];
263
264
                    $field .= '<optgroup label="' . static::output($v['group']) . '">';
265
                }
266
            }
267
268
            $field .= '<option value="' . static::output($v['id']) . '"';
269
270
            if (isset($default) && ($v['id'] == $default)) {
271
                $field .= ' selected="selected"';
272
            }
273
274
            if (isset($v['params'])) {
275
                $field .= ' ' . $v['params'];
276
            }
277
278
            $field .= '>' . static::output($v['text'], [
279
                '"' => '&quot;',
280
                '\'' => '&#039;',
281
                '<' => '&lt;',
282
                '>' => '&gt;'
283
            ]) . '</option>';
284
285
            if (($group !== false) && (($group != $v['group']) || ($ci->hasNext() === false))) {
286
                $group = false;
287
288
                $field .= '</optgroup>';
289
            }
290
        }
291
292
        $field .= '</select>';
293
294
        return $field;
295
    }
296
297
    public static function hiddenField($name, $value = '', $parameters = '')
298
    {
299
        $field = '<input type="hidden" name="' . static::output($name) . '"';
300
301
        if (strlen($value) > 0) {
302
            $field .= ' value="' . static::output($value) . '"';
303
        } elseif ((isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name]))) {
304
            if (isset($_GET[$name]) && is_string($_GET[$name])) {
305
                $field .= ' value="' . static::output($_GET[$name]) . '"';
306 View Code Duplication
            } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
                $field .= ' value="' . static::output($_POST[$name]) . '"';
308
            }
309
        }
310
311
        if (!empty($parameters)) {
312
            $field .= ' ' . $parameters;
313
        }
314
315
        $field .= ' />';
316
317
        return $field;
318
    }
319
320
    public static function button($title = null, $icon = null, $link = null, $params = null, $class = null)
321
    {
322
        $types = ['submit', 'button', 'reset'];
323
324
        if (!isset($params['type'])) {
325
            $params['type'] = 'submit';
326
        }
327
328
        if (!in_array($params['type'], $types)) {
329
            $params['type'] = 'submit';
330
        }
331
332
        if (($params['type'] == 'submit') && isset($link)) {
333
              $params['type'] = 'button';
334
        }
335
336
        $button = '';
337
338
        if (($params['type'] == 'button') && isset($link)) {
339
            $button .= '<a href="' . $link . '"';
340
341
            if (isset($params['newwindow'])) {
342
                $button .= ' target="_blank"';
343
            }
344
        } else {
345
            $button .= '<button type="' . static::output($params['type']) . '"';
346
        }
347
348
        if (isset($params['params'])) {
349
            $button .= ' ' . $params['params'];
350
        }
351
352
        $button .= ' class="btn ' . (isset($class) ? $class : 'btn-default') . '">';
353
354
        if (isset($icon) && !empty($icon)) {
355
            $button .= '<i class="' . $icon . '"></i> ';
356
        }
357
358
        $button .= $title;
359
360
        if (($params['type'] == 'button') && isset($link)) {
361
            $button .= '</a>';
362
        } else {
363
            $button .= '</button>';
364
        }
365
366
        return $button;
367
    }
368
369
    public static function stars($rating = 0, $meta = false)
370
    {
371
        $stars = str_repeat('<span class="glyphicon glyphicon-star"></span>', (int)$rating) .
372
                 str_repeat('<span class="glyphicon glyphicon-star-empty"></span>', 5-(int)$rating);
373
374
        if ($meta !== false) {
375
            $stars .= '<meta itemprop="rating" content="' . (int)$rating . '" />';
376
        }
377
378
        return $stars;
379
    }
380
381
    public static function panel($heading = null, $body = null, $params = null)
382
    {
383
        return Panel::get($heading, $body, $params);
384
    }
385
}
386