Field::getSearchElement()   F
last analyzed

Complexity

Conditions 37
Paths 200

Size

Total Lines 124
Code Lines 104

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 37
eloc 104
c 1
b 0
f 0
nc 200
nop 0
dl 0
loc 124
rs 2.6666

How to fix   Long Method    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 declare(strict_types=1);
2
3
namespace XoopsModules\Songlist;
4
5
use function base64_decode;
6
use function base64_encode;
7
8
9
10
/**
11
 * @copyright copyright &copy; 2000 XOOPS.org
12
 */
13
class Field extends \XoopsObject
14
{
15
    public function __construct()
16
    {
17
        $this->initVar('field_id', \XOBJ_DTYPE_INT, null);
18
        $this->initVar('cids', \XOBJ_DTYPE_ARRAY, [0 => '0'], true);
19
        $this->initVar('field_type', \XOBJ_DTYPE_TXTBOX);
20
        $this->initVar('field_valuetype', \XOBJ_DTYPE_INT, null, true);
21
        $this->initVar('field_name', \XOBJ_DTYPE_TXTBOX, null, true);
22
        $this->initVar('field_title', \XOBJ_DTYPE_TXTBOX);
23
        $this->initVar('field_description', \XOBJ_DTYPE_TXTAREA);
24
        $this->initVar('field_required', \XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes
25
        $this->initVar('field_maxlength', \XOBJ_DTYPE_INT, 0);
26
        $this->initVar('field_weight', \XOBJ_DTYPE_INT, 0);
27
        $this->initVar('field_default', \XOBJ_DTYPE_TXTAREA, '');
28
        $this->initVar('field_notnull', \XOBJ_DTYPE_INT, 1);
29
        $this->initVar('field_edit', \XOBJ_DTYPE_INT, 0);
30
        $this->initVar('field_show', \XOBJ_DTYPE_INT, 0);
31
        $this->initVar('field_config', \XOBJ_DTYPE_INT, 0);
32
        $this->initVar('field_options', \XOBJ_DTYPE_ARRAY, []);
33
    }
34
35
    /**
36
     * Extra treatment dealing with non latin encoding
37
     * Tricky solution
38
     * @param string $key
39
     * @param mixed  $value
40
     * @param bool   $not_gpc
41
     */
42
    public function setVar($key, $value, $not_gpc = false): void
43
    {
44
        if ('field_options' === $key && \is_array($value)) {
45
            foreach (\array_keys($value) as $idx) {
46
                $value[$idx] = base64_encode($value[$idx]);
47
            }
48
        }
49
        parent::setVar($key, $value, $not_gpc);
50
    }
51
52
    /**
53
     * @param string $key
54
     * @param string $format
55
     * @return mixed
56
     */
57
    public function getVar($key, $format = 's')
58
    {
59
        $value = parent::getVar($key, $format);
60
        if ('field_options' === $key && !empty($value)) {
61
            foreach (\array_keys($value) as $idx) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type boolean and string; however, parameter $array of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

61
            foreach (\array_keys(/** @scrutinizer ignore-type */ $value) as $idx) {
Loading history...
62
                $value[$idx] = base64_decode($value[$idx], true);
63
            }
64
        }
65
66
        return $value;
67
    }
68
69
    /**
70
     * Returns a {@link XoopsFormElement} for editing the value of this field
71
     *
72
     * @param \XoopsUser     $user    {@link XoopsUser} object to edit the value of
73
     * @param ObjectsProfile $profile {@link ObjectsProfile} object to edit the value of
0 ignored issues
show
Bug introduced by
The type XoopsModules\Songlist\ObjectsProfile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
     *
75
     * @return \\XoopsFormDhtmlTextArea|\\XoopsFormEditor|\\XoopsFormLabel|\\XoopsFormSelect|\\XoopsFormText|\\XoopsFormTextArea
0 ignored issues
show
Bug introduced by
The type \XoopsFormDhtmlTextArea was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \XoopsFormLabel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \XoopsFormEditor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \XoopsFormTextArea was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \XoopsFormSelect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type \XoopsFormText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
     */
77
    public function getEditElement($user, $profile)
78
    {
79
        $value = \in_array($this->getVar('field_name'), $this->getPostVars(), true) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
80
        if (null === $value) {
81
            $value = $this->getVar('field_default');
82
        }
83
        $caption = $this->getVar('field_title');
84
        $caption = \defined($caption) ? \constant($caption) : $caption;
85
        $name    = $this->getVar('field_name', 'e');
86
        $options = $this->getVar('field_options');
87
        if (\is_array($options)) {
88
            //asort($options);
89
90
            foreach (\array_keys($options) as $key) {
91
                $optval = \defined($options[$key]) ? \constant($options[$key]) : $options[$key];
92
                $optkey = \defined((string)$key) ? \constant($key) : $key;
93
                unset($options[$key]);
94
                $options[$optkey] = $optval;
95
            }
96
        }
97
        require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
98
        switch ($this->getVar('field_type')) {
99
            default:
100
            case 'autotext':
101
                //autotext is not for editing
102
                $element = new \XoopsFormLabel($caption, $this->getOutputValue($user, $profile));
0 ignored issues
show
Bug introduced by
It seems like $this->getOutputValue($user, $profile) can also be of type array and array; however, parameter $value of XoopsFormLabel::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

102
                $element = new \XoopsFormLabel($caption, /** @scrutinizer ignore-type */ $this->getOutputValue($user, $profile));
Loading history...
103
                break;
104
            case 'textbox':
105
                $element = new \XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
106
                break;
107
            case 'textarea':
108
                $element = new \XoopsFormTextArea($caption, $name, $value, 4, 30);
109
                break;
110
            case 'dhtml':
111
                $element = new \XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30);
112
                break;
113
            case 'editor':
114
                $editor_config['name']   = $name;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$editor_config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $editor_config = array(); before regardless.
Loading history...
115
                $editor_config['editor'] = $GLOBALS['songlistModuleConfig']['editor'];
116
                $editor_config['value']  = $value;
117
                $editor_config['width']  = $GLOBALS['songlistModuleConfig']['editor_width'];
118
                $editor_config['height'] = $GLOBALS['songlistModuleConfig']['editor_height'];
119
                $element                 = new \XoopsFormEditor($caption, $name, $editor_config);
120
                break;
121
            case 'select':
122
                $element = new \XoopsFormSelect($caption, $name, $value);
123
                // If options do not include an empty element, then add a blank option to prevent any default selection
124
                if (!\array_key_exists('', $options)) {
125
                    $element->addOption('', _NONE);
126
                    //trabis
127
                    if (1 == $this->getVar('field_required')) {
128
                        $eltmsg                          = empty($caption) ? \sprintf(_FORM_ENTER, $name) : \sprintf(_FORM_ENTER, $caption);
129
                        $eltmsg                          = \str_replace('"', '\"', \stripslashes($eltmsg));
130
                        $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};"
131
                                                           . "for (i = 0; i < selectBox.options.length; i++  ) { if ( selectBox.options[i].selected === true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }"
132
                                                           . "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
133
                    }
134
                }
135
                $element->addOptionArray($options);
136
                break;
137
            case 'select_multi':
138
                $element = new \XoopsFormSelect($caption, $name, $value, 5, true);
139
                $element->addOptionArray($options);
140
                break;
141
            case 'radio':
142
                $element = new \XoopsFormRadio($caption, $name, $value);
143
                $element->addOptionArray($options);
144
                break;
145
            case 'checkbox':
146
                $element = new \XoopsFormCheckBox($caption, $name, $value);
147
                $element->addOptionArray($options);
148
                break;
149
            case 'yesno':
150
                $element = new \XoopsFormRadioYN($caption, $name, $value);
151
                break;
152
            case 'group':
153
                $element = new \XoopsFormSelectGroup($caption, $name, true, $value);
154
                break;
155
            case 'group_multi':
156
                $element = new \XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
157
                break;
158
            case 'language':
159
                $element = new \XoopsFormSelectLang($caption, $name, $value);
160
                break;
161
            case 'date':
162
                $element = new \XoopsFormTextDateSelect($caption, $name, 15, $value);
163
                break;
164
            case 'longdate':
165
                $element = new \XoopsFormTextDateSelect($caption, $name, 15, \str_replace('-', '/', $value));
166
                break;
167
            case 'datetime':
168
                $element = new XoopsFormDatetime($caption, $name, 15, $value);
0 ignored issues
show
Bug introduced by
The type XoopsModules\Songlist\XoopsFormDatetime was not found. Did you mean XoopsFormDatetime? If so, make sure to prefix the type with \.
Loading history...
169
                break;
170
            case 'list':
171
                $element = new \XoopsFormSelect($caption, $name, $value, 1, $options[0]);
172
                break;
173
            case 'timezone':
174
                $element = new \XoopsFormSelectTimezone($caption, $name, $value);
175
                $element->setExtra("style='width: 280px;'");
176
                break;
177
            case 'rank':
178
                $element = new \XoopsFormSelect($caption, $name, $value);
179
180
                require_once $GLOBALS['xoops']->path('class/xoopslists.php');
181
                $ranks = \XoopsLists::getUserRankList();
182
                $element->addOption(0, '--------------');
183
                $element->addOptionArray($ranks);
184
                break;
185
            case 'theme':
186
                $element = new \XoopsFormSelect($caption, $name, $value);
187
                $element->addOption('0', \_AM_SONGLIST_OBJS_MF_SITEDEFAULT);
188
                $handle  = \opendir(XOOPS_THEME_PATH . '/');
189
                $dirlist = [];
190
                while (false !== ($file = \readdir($handle))) {
191
                    if (\is_dir(XOOPS_THEME_PATH . '/' . $file) && !\preg_match('/^[.]{1,2}$/', $file) && 'cvs' !== \mb_strtolower($file)) {
192
                        if (\file_exists(XOOPS_THEME_PATH . '/' . $file . '/theme.tpl') && \in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'], true)) {
193
                            $dirlist[$file] = $file;
194
                        }
195
                    }
196
                }
197
                \closedir($handle);
198
                if (!empty($dirlist)) {
199
                    \asort($dirlist);
200
                    $element->addOptionArray($dirlist);
201
                }
202
                break;
203
        }
204
        if ('' != $this->getVar('field_description')) {
205
            $element->setDescription($this->getVar('field_description'));
206
        }
207
208
        return $element;
209
    }
210
211
    /**
212
     * Returns a {@link XoopsFormElement} for editing the value of this field
213
     * @return \\XoopsFormLabel|\\XoopsFormSelect|\\XoopsFormText|\\XoopsFormTextArea
214
     * @internal param XoopsUser $user <a href='psi_element://XoopsUser'>XoopsUser</a> object to edit the value of object to edit the value of
215
     * @internal param ObjectsProfile $profile <a href='psi_element://ObjectsProfile'>ObjectsProfile</a> object to edit the value of object to edit the value of
216
     */
217
    public function getSearchElement()
218
    {
219
        $caption = $this->getVar('field_title');
220
        $caption = \defined($caption) ? \constant($caption) : $caption;
221
        $name    = $this->getVar('field_name', 'e');
222
        $options = $this->getVar('field_options');
223
        if (\is_array($options)) {
224
            //asort($options);
225
226
            foreach (\array_keys($options) as $key) {
227
                $optval = \defined($options[$key]) ? \constant($options[$key]) : $options[$key];
228
                $optkey = \defined($key) ? \constant($key) : $key;
229
                unset($options[$key]);
230
                $options[$optkey] = $optval;
231
            }
232
        }
233
        require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
234
        switch ($this->getVar('field_type')) {
235
            default:
236
            case 'autotext':
237
                //autotext is not for editing
238
                $element = new \XoopsFormLabel($caption, $this->getOutputValue($user, $profile));
0 ignored issues
show
Bug introduced by
It seems like $this->getOutputValue($user, $profile) can also be of type array; however, parameter $value of XoopsFormLabel::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

238
                $element = new \XoopsFormLabel($caption, /** @scrutinizer ignore-type */ $this->getOutputValue($user, $profile));
Loading history...
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $profile seems to be never defined.
Loading history...
239
                break;
240
            case 'textbox':
241
                $element = new \XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $value seems to be never defined.
Loading history...
242
                break;
243
            case 'textarea':
244
                $element = new \XoopsFormTextArea($caption, $name, $value, 4, 30);
245
                break;
246
            case 'dhtml':
247
                $element = new \XoopsFormText($caption, $name, 35, 255, $value);
248
                break;
249
            case 'select':
250
                $element = new \XoopsFormSelect($caption, $name, $value);
251
                // If options do not include an empty element, then add a blank option to prevent any default selection
252
                if (!\array_key_exists('', $options)) {
253
                    $element->addOption('', _NONE);
254
                    //trabis
255
                    if (1 == $this->getVar('field_required')) {
256
                        $eltmsg                          = empty($caption) ? \sprintf(_FORM_ENTER, $name) : \sprintf(_FORM_ENTER, $caption);
257
                        $eltmsg                          = \str_replace('"', '\"', \stripslashes($eltmsg));
258
                        $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};"
259
                                                           . "for (i = 0; i < selectBox.options.length; i++  ) { if ( selectBox.options[i].selected === true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }"
260
                                                           . "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
261
                    }
262
                    //end
263
                }
264
                $element->addOptionArray($options);
265
                break;
266
            case 'editor':
267
                $element = new \XoopsFormText($caption, $name, 35, 255, $value);
268
                break;
269
            case 'select_multi':
270
                $element = new \XoopsFormSelect($caption, $name, $value, 5, true);
271
                $element->addOptionArray($options);
272
                break;
273
            case 'radio':
274
                $element = new \XoopsFormRadio($caption, $name, $value);
275
                $element->addOptionArray($options);
276
                break;
277
            case 'checkbox':
278
                $element = new \XoopsFormCheckBox($caption, $name, $value);
279
                $element->addOptionArray($options);
280
                break;
281
            case 'yesno':
282
                $element = new \XoopsFormRadioYN($caption, $name, $value);
283
                break;
284
            case 'group':
285
                $element = new \XoopsFormSelectGroup($caption, $name, true, $value);
286
                break;
287
            case 'group_multi':
288
                $element = new \XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
289
                break;
290
            case 'language':
291
                $element = new \XoopsFormSelectLang($caption, $name, $value);
292
                break;
293
            case 'date':
294
                $element = new \XoopsFormTextDateSelect($caption, $name, 15, $value);
295
                break;
296
            case 'longdate':
297
                $element = new \XoopsFormTextDateSelect($caption, $name, 15, \str_replace('-', '/', $value));
298
                break;
299
            case 'datetime':
300
                $element = new XoopsFormDatetime($caption, $name, 15, $value);
301
                break;
302
            case 'list':
303
                $element = new \XoopsFormSelect($caption, $name, $value, 1, $options[0]);
304
                break;
305
            case 'timezone':
306
                $element = new \XoopsFormSelectTimezone($caption, $name, $value);
307
                $element->setExtra("style='width: 280px;'");
308
                break;
309
            case 'rank':
310
                $element = new \XoopsFormSelect($caption, $name, $value);
311
312
                require_once $GLOBALS['xoops']->path('class/xoopslists.php');
313
                $ranks = \XoopsLists::getUserRankList();
314
                $element->addOption(0, '--------------');
315
                $element->addOptionArray($ranks);
316
                break;
317
            case 'theme':
318
                $element = new \XoopsFormSelect($caption, $name, $value);
319
                $element->addOption('0', \_AM_SONGLIST_OBJS_MF_SITEDEFAULT);
320
                $handle  = \opendir(XOOPS_THEME_PATH . '/');
321
                $dirlist = [];
322
                while (false !== ($file = \readdir($handle))) {
323
                    if (\is_dir(XOOPS_THEME_PATH . '/' . $file) && !\preg_match('/^[.]{1,2}$/', $file) && 'cvs' !== \mb_strtolower($file)) {
324
                        if (\file_exists(XOOPS_THEME_PATH . '/' . $file . '/theme.tpl') && \in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'], true)) {
325
                            $dirlist[$file] = $file;
326
                        }
327
                    }
328
                }
329
                \closedir($handle);
330
                if (!empty($dirlist)) {
331
                    \asort($dirlist);
332
                    $element->addOptionArray($dirlist);
333
                }
334
                break;
335
        }
336
        if ('' != $this->getVar('field_description')) {
337
            $element->setDescription($this->getVar('field_description'));
338
        }
339
340
        return $element;
341
    }
342
343
    /**
344
     * Returns a value for output of this field
345
     *
346
     * @param \XoopsUser     $user    {@link XoopsUser} object to get the value of
347
     * @param ObjectsProfile $profile object to get the value of
348
     *
349
     * @return mixed
350
     **/
351
    public function getOutputValue($user, $profile)
352
    {
353
        \xoops_loadLanguage('modinfo', 'objects');
354
355
        $value = \in_array($this->getVar('field_name'), $this->getPostVars(), true) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
356
357
        switch ($this->getVar('field_type')) {
358
            default:
359
            case 'textbox':
360
                if ('url' === $this->getVar('field_name') && '' != $value) {
361
                    return '<a href="' . \formatURL($value) . '" rel="external">' . $value . '</a>';
362
                }
363
364
                return $value;
365
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
366
            case 'editor':
367
            case 'textarea':
368
            case 'dhtml':
369
            case 'theme':
370
            case 'language':
371
            case 'list':
372
                return $value;
373
                break;
374
            case 'select':
375
            case 'radio':
376
                $options = $this->getVar('field_options');
377
                if (isset($options[$value])) {
378
                    $value = \htmlspecialchars(\defined($options[$value]) ? \constant($options[$value]) : $options[$value], \ENT_QUOTES | \ENT_HTML5);
379
                } else {
380
                    $value = '';
381
                }
382
383
                return $value;
384
                break;
385
            case 'select_multi':
386
            case 'checkbox':
387
                $options = $this->getVar('field_options');
388
                $ret     = [];
389
                if (\count($options) > 0) {
390
                    foreach (\array_keys($options) as $key) {
391
                        if (\in_array($key, $value, true)) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type boolean and null and string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

391
                        if (\in_array($key, /** @scrutinizer ignore-type */ $value, true)) {
Loading history...
392
                            $$ret[$key] = \htmlspecialchars(\defined($options[$key]) ? \constant($options[$key]) : $options[$key], \ENT_QUOTES | \ENT_HTML5);
393
                        }
394
                    }
395
                }
396
397
                return $ret;
398
                break;
399
            case 'group':
400
                //change to retrieve groups and return name of group
401
                return $value;
402
                break;
403
            case 'group_multi':
404
                //change to retrieve groups and return array of group names
405
                return '';
406
                break;
407
            case 'longdate':
408
                //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
409
                //when we cannot convert it to a UNIX timestamp?
410
                return \str_replace('-', '/', $value);
411
            case 'date':
412
                return \formatTimestamp($value, 's');
413
                break;
414
            case 'datetime':
415
                if (!empty($value)) {
416
                    return \formatTimestamp($value, 'm');
417
                }
418
419
                return $value = \_MI_SONGLIST_DATENOTSET;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
420
                break;
421
            case 'autotext':
422
                $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it
423
                $value = \str_replace('{X_UID}', $user->getVar('uid'), $value);
424
                $value = \str_replace('{X_URL}', XOOPS_URL, $value);
425
                $value = \str_replace('{X_UNAME}', $user->getVar('uname'), $value);
426
427
                return $value;
428
                break;
429
            case 'rank':
430
                $userrank       = $user->rank();
431
                $user_rankimage = '';
432
                if (isset($userrank['image']) && '' != $userrank['image']) {
433
                    $user_rankimage = '<img src="' . XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '"><br>';
434
                }
435
436
                return $user_rankimage . $userrank['title'];
437
                break;
438
            case 'yesno':
439
                return $value ? _YES : _NO;
440
                break;
441
            case 'timezone':
442
                require_once $GLOBALS['xoops']->path('class/xoopslists.php');
443
                $timezones = \XoopsLists::getTimeZoneList();
444
                $value     = empty($value) ? '0' : (string)$value;
445
446
                return $timezones[\str_replace('.0', '', $value)];
447
                break;
448
        }
449
    }
450
451
    /**
452
     * Returns a value ready to be saved in the database
453
     *
454
     * @param mixed $value Value to format
455
     *
456
     * @return mixed
457
     */
458
    public function getValueForSave($value)
459
    {
460
        switch ($this->getVar('field_type')) {
461
            default:
462
            case 'textbox':
463
            case 'textarea':
464
            case 'dhtml':
465
            case 'yesno':
466
            case 'timezone':
467
            case 'theme':
468
            case 'language':
469
            case 'list':
470
            case 'select':
471
            case 'radio':
472
            case 'select_multi':
473
            case 'checkbox':
474
            case 'group':
475
            case 'group_multi':
476
            case 'longdate':
477
                return $value;
478
            case 'date':
479
                if ('' != $value) {
480
                    return \strtotime((string)$value);
481
                }
482
483
                return $value;
484
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
485
            case 'datetime':
486
                if (!empty($value)) {
487
                    return \strtotime($value['date']??'') + (int)$value['time'];
488
                }
489
490
                return $value;
491
                break;
492
        }
493
    }
494
495
    /**
496
     * Get names of user variables
497
     *
498
     * @return array
499
     */
500
    public function getPostVars(): array
501
    {
502
        $objectsHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Extras');
503
504
        return $objectsHandler->getPostVars();
505
    }
506
}
507