Passed
Pull Request — master (#2)
by Michael
07:08 queued 02:34
created

SonglistField   F

Complexity

Total Complexity 143

Size/Duplication

Total Lines 540
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 353
dl 0
loc 540
rs 2
c 0
b 0
f 0
wmc 143

9 Methods

Rating   Name   Duplication   Size   Complexity  
D getValueForSave() 0 34 20
A __construct() 0 18 1
F getOutputValue() 0 107 36
F getSearchElement() 0 144 37
F getEditElement() 0 151 39
A getPostVars() 0 4 1
A setVar() 0 8 4
A getVar() 0 9 4
A SonglistField() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like SonglistField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SonglistField, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
4
defined('XOOPS_ROOT_PATH') or die("XOOPS root path not defined");
5
6
/**
7
 * @package kernel
8
 * @copyright copyright &copy; 2000 XOOPS.org
9
 */
10
class SonglistField extends XoopsObject
11
{
12
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
    {
14
        $this->initVar('field_id', XOBJ_DTYPE_INT, null);
15
        $this->initVar('cids', XOBJ_DTYPE_ARRAY, array(0=>'0'), true);
16
        $this->initVar('field_type', XOBJ_DTYPE_TXTBOX);
17
        $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true);
18
        $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true);
19
        $this->initVar('field_title', XOBJ_DTYPE_TXTBOX);
20
        $this->initVar('field_description', XOBJ_DTYPE_TXTAREA);
21
        $this->initVar('field_required', XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes
22
        $this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0);
23
        $this->initVar('field_weight', XOBJ_DTYPE_INT, 0);
24
        $this->initVar('field_default', XOBJ_DTYPE_TXTAREA, "");
25
        $this->initVar('field_notnull', XOBJ_DTYPE_INT, 1);
26
        $this->initVar('field_edit', XOBJ_DTYPE_INT, 0);
27
        $this->initVar('field_show', XOBJ_DTYPE_INT, 0);
28
        $this->initVar('field_config', XOBJ_DTYPE_INT, 0);
29
        $this->initVar('field_options', XOBJ_DTYPE_ARRAY, array() );
30
    }
31
32
33
	
34
    function SonglistField()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
    {
36
        $this->__construct();
37
    }
38
39
    /**
40
     * Extra treatment dealing with non latin encoding
41
     * Tricky solution
42
     */
43
    function setVar($key, $value, $not_gpc = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
44
    {
45
        if ($key == 'field_options' && is_array($value)) {
46
            foreach (array_keys($value) as $idx ) {
47
                $value[$idx] = base64_encode($value[$idx]);
48
            }
49
        }
50
        parent::setVar($key, $value, $not_gpc);
51
    }
52
53
    function getVar($key, $format = 's')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
54
    {
55
        $value = parent::getVar($key, $format);
56
        if ($key == 'field_options' && !empty($value)) {
57
            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

57
            foreach (array_keys(/** @scrutinizer ignore-type */ $value) as $idx) {
Loading history...
58
                $value[$idx] = base64_decode($value[$idx]);
59
            }
60
        }
61
        return $value;
62
    }
63
64
    /**
65
    * Returns a {@link XoopsFormElement} for editing the value of this field
66
    *
67
    * @param XoopsUser $user {@link XoopsUser} object to edit the value of
68
    * @param ObjectsProfile $profile {@link ObjectsProfile} object to edit the value of
0 ignored issues
show
Bug introduced by
The type 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...
69
    *
70
    * @return XoopsFormElement
71
    **/
72
    function getEditElement($user, $profile)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
    {
74
        $value = in_array($this->getVar('field_name'), $this->getPostVars() ) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
75
        if (is_null($value)) {
76
            $value = $this->getVar('field_default');
77
        }
78
        $caption = $this->getVar('field_title');
79
        $caption = defined($caption) ? constant($caption) : $caption;
80
        $name = $this->getVar('field_name', 'e');
81
        $options = $this->getVar('field_options');
82
        if (is_array($options)) {
83
            //asort($options);
84
85
            foreach (array_keys($options) as $key) {
86
                $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
87
                $optkey = defined($key) ? constant($key) : $key;
88
                unset($options[$key]);
89
                $options[$optkey] = $optval;
90
            }
91
        }
92
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
93
        switch ($this->getVar('field_type')  ) {
94
            default:
95
            case "autotext":
96
                //autotext is not for editing
97
                $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

97
                $element = new XoopsFormLabel($caption, /** @scrutinizer ignore-type */ $this->getOutputValue($user, $profile));
Loading history...
98
                break;
99
100
            case "textbox":
101
                $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
102
                break;
103
104
            case "textarea":
105
                $element = new XoopsFormTextArea($caption, $name, $value, 4, 30);
106
                break;
107
108
            case "dhtml":
109
                $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30);
110
                break;
111
112
            case "editor":
113
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
122
            case "select":
123
                $element = new XoopsFormSelect($caption, $name, $value);
124
                // If options do not include an empty element, then add a blank option to prevent any default selection
125
                if (!in_array('', array_keys($options))) {
126
                    $element->addOption('', _NONE);
127
                    //trabis
128
                    if ($this->getVar('field_required') == 1) {
129
                        $eltmsg = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf( _FORM_ENTER, $caption);
130
                        $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
131
                        $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" .
132
                            "for (i = 0; i < selectBox.options.length; i++  ) { if ( selectBox.options[i].selected == true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }" .
133
                            "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
134
                    }
135
                }
136
                $element->addOptionArray($options);
137
                break;
138
139
            case "select_multi":
140
                $element = new XoopsFormSelect($caption, $name, $value, 5, true);
141
                $element->addOptionArray($options);
142
                break;
143
144
            case "radio":
145
                $element = new XoopsFormRadio($caption, $name, $value);
146
                $element->addOptionArray($options);
147
                break;
148
149
            case "checkbox":
150
                $element = new XoopsFormCheckBox($caption, $name, $value);
151
                $element->addOptionArray($options);
152
                break;
153
154
            case "yesno":
155
                $element = new XoopsFormRadioYN($caption, $name, $value);
156
                break;
157
158
            case "group":
159
                $element = new XoopsFormSelectGroup($caption, $name, true, $value);
160
                break;
161
162
            case "group_multi":
163
                $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
164
                break;
165
166
            case "language":
167
                $element = new XoopsFormSelectLang($caption, $name, $value);
168
                break;
169
170
            case "date":
171
                $element = new XoopsFormTextDateSelect($caption, $name, 15, $value);
172
                break;
173
174
            case "longdate":
175
                $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace("-", "/", $value) );
176
                break;
177
178
            case "datetime":
179
                $element = new XoopsFormDatetime($caption, $name, 15, $value);
180
                break;
181
182
            case "list":
183
                $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]);
0 ignored issues
show
Bug introduced by
The type XoopsFormSelectList 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...
184
                break;
185
186
            case "timezone":
187
                $element = new XoopsFormSelectTimezone($caption, $name, $value);
188
                $element->setExtra("style='width: 280px;'");
189
                break;
190
191
            case "rank":
192
                $element = new XoopsFormSelect($caption, $name, $value);
193
194
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
195
                $ranks = XoopsLists::getUserRankList();
196
                $element->addOption(0, "--------------");
197
                $element->addOptionArray($ranks);
198
                break;
199
200
            case 'theme':
201
                $element = new XoopsFormSelect($caption, $name, $value);
202
                $element->addOption("0", _OBJS_MF_SITEDEFAULT);
0 ignored issues
show
Bug introduced by
The constant _OBJS_MF_SITEDEFAULT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
203
                $handle = opendir(XOOPS_THEME_PATH . '/');
204
                $dirlist = array();
205
                while (false !== ($file = readdir($handle) ) ) {
206
                    if (is_dir(XOOPS_THEME_PATH . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) != 'cvs' ) {
207
                        if (file_exists(XOOPS_THEME_PATH . "/" . $file . "/theme.html") && in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'])) {
208
                            $dirlist[$file] = $file;
209
                        }
210
                    }
211
                }
212
                closedir($handle);
213
                if (!empty($dirlist)) {
214
                    asort($dirlist);
215
                    $element->addOptionArray($dirlist);
216
                }
217
                break;
218
        }
219
        if ($this->getVar('field_description') != "") {
220
            $element->setDescription($this->getVar('field_description') );
221
        }
222
        return $element;
223
    }
224
225
    /**
226
    * Returns a {@link XoopsFormElement} for editing the value of this field
227
    *
228
    * @param XoopsUser $user {@link XoopsUser} object to edit the value of
229
    * @param ObjectsProfile $profile {@link ObjectsProfile} object to edit the value of
230
    *
231
    * @return XoopsFormElement
232
    **/
233
    function getSearchElement()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
234
    {
235
        $caption = $this->getVar('field_title');
236
        $caption = defined($caption) ? constant($caption) : $caption;
237
        $name = $this->getVar('field_name', 'e');
238
        $options = $this->getVar('field_options');
239
        if (is_array($options)) {
240
            //asort($options);
241
242
            foreach (array_keys($options) as $key) {
243
                $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
244
                $optkey = defined($key) ? constant($key) : $key;
245
                unset($options[$key]);
246
                $options[$optkey] = $optval;
247
            }
248
        }
249
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
250
        switch ($this->getVar('field_type')  ) {
251
            default:
252
            case "autotext":
253
                //autotext is not for editing
254
                $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $profile seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
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

254
                $element = new XoopsFormLabel($caption, /** @scrutinizer ignore-type */ $this->getOutputValue($user, $profile));
Loading history...
255
                break;
256
257
            case "textbox":
258
                $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...
259
                break;
260
261
            case "textarea":
262
                $element = new XoopsFormTextArea($caption, $name, $value, 4, 30);
263
                break;
264
265
            case "dhtml":
266
                $element = new XoopsFormText($caption, $name, 35, 255, $value);
267
                break;
268
269
            case "select":
270
                $element = new XoopsFormSelect($caption, $name, $value);
271
                // If options do not include an empty element, then add a blank option to prevent any default selection
272
                if (!in_array('', array_keys($options))) {
273
                    $element->addOption('', _NONE);
274
                    //trabis
275
                    if ($this->getVar('field_required') == 1) {
276
                        $eltmsg = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf( _FORM_ENTER, $caption);
277
                        $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
278
                        $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" .
279
                            "for (i = 0; i < selectBox.options.length; i++  ) { if ( selectBox.options[i].selected == true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }" .
280
                            "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
281
                    }
282
                    //end
283
                }
284
                $element->addOptionArray($options);
285
                break;
286
287
288
            case "editor":
289
290
                $element = new XoopsFormText($caption, $name, 35, 255, $value);
291
                break;
292
293
            case "select_multi":
294
                $element = new XoopsFormSelect($caption, $name, $value, 5, true);
295
                $element->addOptionArray($options);
296
                break;
297
298
            case "radio":
299
                $element = new XoopsFormRadio($caption, $name, $value);
300
                $element->addOptionArray($options);
301
                break;
302
303
            case "checkbox":
304
                $element = new XoopsFormCheckBox($caption, $name, $value);
305
                $element->addOptionArray($options);
306
                break;
307
308
            case "yesno":
309
                $element = new XoopsFormRadioYN($caption, $name, $value);
310
                break;
311
312
            case "group":
313
                $element = new XoopsFormSelectGroup($caption, $name, true, $value);
314
                break;
315
316
            case "group_multi":
317
                $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
318
                break;
319
320
            case "language":
321
                $element = new XoopsFormSelectLang($caption, $name, $value);
322
                break;
323
324
            case "date":
325
                $element = new XoopsFormTextDateSelect($caption, $name, 15, $value);
326
                break;
327
328
            case "longdate":
329
                $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace("-", "/", $value) );
330
                break;
331
332
            case "datetime":
333
                $element = new XoopsFormDatetime($caption, $name, 15, $value);
334
                break;
335
336
            case "list":
337
                $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]);
338
                break;
339
340
            case "timezone":
341
                $element = new XoopsFormSelectTimezone($caption, $name, $value);
342
                $element->setExtra("style='width: 280px;'");
343
                break;
344
345
            case "rank":
346
                $element = new XoopsFormSelect($caption, $name, $value);
347
348
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
349
                $ranks = XoopsLists::getUserRankList();
350
                $element->addOption(0, "--------------");
351
                $element->addOptionArray($ranks);
352
                break;
353
354
            case 'theme':
355
                $element = new XoopsFormSelect($caption, $name, $value);
356
                $element->addOption("0", _OBJS_MF_SITEDEFAULT);
0 ignored issues
show
Bug introduced by
The constant _OBJS_MF_SITEDEFAULT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
357
                $handle = opendir(XOOPS_THEME_PATH . '/');
358
                $dirlist = array();
359
                while (false !== ($file = readdir($handle) ) ) {
360
                    if (is_dir(XOOPS_THEME_PATH . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) != 'cvs' ) {
361
                        if (file_exists(XOOPS_THEME_PATH . "/" . $file . "/theme.html") && in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'])) {
362
                            $dirlist[$file] = $file;
363
                        }
364
                    }
365
                }
366
                closedir($handle);
367
                if (!empty($dirlist)) {
368
                    asort($dirlist);
369
                    $element->addOptionArray($dirlist);
370
                }
371
                break;
372
        }
373
        if ($this->getVar('field_description') != "") {
374
            $element->setDescription($this->getVar('field_description') );
375
        }
376
        return $element;
377
    }
378
379
    /**
380
    * Returns a value for output of this field
381
    *
382
    * @param XoopsUser $user {@link XoopsUser} object to get the value of
383
    * @param ObjectsProfile $profile object to get the value of
384
    *
385
    * @return mixed
386
    **/
387
    function getOutputValue($user, $profile)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
388
    {
389
        if (file_exists($file = $GLOBALS['xoops']->path('modules/objects/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) {
390
            include_once $file;
391
        } else {
392
            include_once $GLOBALS['xoops']->path('modules/objects/language/english/modinfo.php');
393
        }
394
395
        $value = in_array($this->getVar('field_name'), $this->getPostVars() ) ? $user->getVar($this->getVar('field_name') ) : $profile->getVar($this->getVar('field_name'));
396
397
        switch ($this->getVar('field_type')  ) {
398
            default:
399
            case "textbox":
400
                if ( $this->getVar('field_name') == 'url' && $value != '') {
401
                     return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>';
402
                   } else {
403
                     return $value;
404
                }
405
                break;
406
			case "editor":
407
            case "textarea":
408
            case "dhtml":
409
            case 'theme':
410
            case "language":
411
            case "list":
412
                return $value;
413
                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...
414
415
            case "select":
416
            case "radio":
417
                $options = $this->getVar('field_options');
418
                if (isset($options[$value])) {
419
                    $value = htmlspecialchars( defined($options[$value]) ? constant($options[$value]) : $options[$value]);
420
                } else {
421
                    $value = "";
422
                }
423
                return $value;
424
                break;
425
426
            case "select_multi":
427
            case "checkbox":
428
                $options = $this->getVar('field_options');
429
                $ret = array();
430
                if (count($options) > 0) {
431
                    foreach (array_keys($options) as $key) {
432
                        if (in_array($key, $value)) {
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

432
                        if (in_array($key, /** @scrutinizer ignore-type */ $value)) {
Loading history...
433
                            $$ret[$key] = htmlspecialchars( defined($options[$key]) ? constant($options[$key]) : $options[$key]);
434
                        }
435
                    }
436
                }
437
                return $ret;
438
                break;
439
440
            case "group":
441
                //change to retrieve groups and return name of group
442
                return $value;
443
                break;
444
445
            case "group_multi":
446
                //change to retrieve groups and return array of group names
447
                return "";
448
                break;
449
450
            case "longdate":
451
                //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
452
                //when we cannot convert it to a UNIX timestamp?
453
                return str_replace("-", "/", $value);
454
455
            case "date":
456
                return formatTimestamp($value, 's');
457
                break;
458
459
            case "datetime":
460
                if (!empty($value)) {
461
                       return formatTimestamp($value, 'm');
462
                   } else {
463
                       return $value = _XFORUM_MI_DATENOTSET;
0 ignored issues
show
Bug introduced by
The constant _XFORUM_MI_DATENOTSET was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
464
                   }
465
                break;
466
467
            case "autotext":
468
                $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it
469
                $value = str_replace("{X_UID}", $user->getVar("uid"), $value);
470
                $value = str_replace("{X_URL}", XOOPS_URL, $value );
471
                $value = str_replace("{X_UNAME}", $user->getVar("uname"), $value);
472
                return $value;
473
                break;
474
475
            case "rank":
476
                $userrank = $user->rank();
477
                $user_rankimage = "";
478
                if (isset($userrank['image']) && $userrank['image'] != "") {
479
                    $user_rankimage = '<img src="'.XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br />';
480
                }
481
                return $user_rankimage.$userrank['title'];
482
                break;
483
484
            case "yesno":
485
                return $value ? _YES : _NO;
486
                break;
487
488
            case "timezone":
489
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
490
                $timezones = XoopsLists::getTimeZoneList();
491
                $value = empty($value) ? "0" : strval($value);
492
                return $timezones[str_replace('.0', '', $value)];
493
                break;
494
        }
495
    }
496
497
    /**
498
    * Returns a value ready to be saved in the database
499
    *
500
    * @param mixed $value Value to format
501
    *
502
    * @return mixed
503
    */
504
    function getValueForSave($value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
505
    {
506
        switch ($this->getVar('field_type')) {
507
            default:
508
            case "textbox":
509
            case "textarea":
510
            case "dhtml":
511
            case "yesno":
512
            case "timezone":
513
            case 'theme':
514
            case "language":
515
            case "list":
516
            case "select":
517
            case "radio":
518
            case "select_multi":
519
            case "checkbox":
520
            case "group":
521
            case "group_multi":
522
            case "longdate":
523
                return $value;
524
525
            case "date":
526
                if ($value != "") {
527
                    return strtotime($value);
528
                }
529
                return $value;
530
                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...
531
532
            case "datetime":
533
                if (!empty($value)) {
534
                    return strtotime($value['date']) + intval($value['time']);
535
                }
536
                return $value;
537
                break;
538
        }
539
    }
540
541
    /**
542
     * Get names of user variables
543
     *
544
     * @return array
545
     */
546
    function getPostVars()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
547
    {
548
        $objects_handler = xoops_getmodulehandler('extras', 'songlist');
549
        return $objects_handler->getPostVars();
0 ignored issues
show
Bug introduced by
The method getPostVars() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

549
        return $objects_handler->/** @scrutinizer ignore-call */ getPostVars();
Loading history...
550
    }
551
}
552
553
/**
554
 * @package kernel
555
 * @copyright copyright &copy; 2000 XOOPS.org
556
 */
557
class SonglistFieldHandler extends XoopsPersistableObjectHandler
558
{
559
    function __construct($db)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
560
    {
561
        parent::__construct($db, 'songlist_field', "SonglistField", "field_id", 'field_title');
562
    }
563
564
    /**
565
    * Read field information from cached storage
566
    *
567
    * @param bool   $force_update   read fields from database and not cached storage
568
    *
569
    * @return array
570
    */
571
    function loadFields($force_update = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
572
    {
573
        static $fields = array();
574
        if (!empty($force_update) || count($fields) == 0) {
575
            $criteria = new Criteria('field_id', 0, "!=");
576
            $criteria->setSort('field_weight');
577
            if ($this->getCount($criteria)==0)
578
            	return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
579
            $field_objs = $this->getObjects($criteria);
580
            foreach (array_keys($field_objs) as $i ) {
581
                $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i];
582
            }
583
        }
584
        return $fields;
585
    }
586
587
    /**
588
    * save a profile field in the database
589
    *
590
    * @param object $obj reference to the object
591
    * @param bool $force whether to force the query execution despite security settings
592
    * @param bool $checkObject check if the object is dirty and clean the attributes
593
    * @return bool FALSE if failed, TRUE if already present and unchanged or successful
594
    */
595
    function insert($obj, $force = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
596
    {
597
        $objects_handler = xoops_getmodulehandler('extras', 'songlist');
598
        $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
599
        $obj->cleanVars();
600
        $defaultstring = "";
601
        switch ($obj->getVar('field_type')  ) {
602
            case "datetime":
603
            case "date":
604
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
605
                $obj->setVar('field_maxlength', 10);
606
                break;
607
608
            case "longdate":
609
                $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME);
610
                break;
611
612
            case "yesno":
613
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
614
                $obj->setVar('field_maxlength', 1);
615
                break;
616
617
            case "textbox":
618
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
619
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
620
                }
621
                break;
622
623
            case "autotext":
624
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
625
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
626
                }
627
                break;
628
629
            case "group_multi":
630
            case "select_multi":
631
            case "checkbox":
632
                $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY);
633
                break;
634
635
            case "language":
636
            case "timezone":
637
            case "theme":
638
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
639
                break;
640
641
            case "dhtml":
642
            case "textarea":
643
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
644
                break;
645
        }
646
647
        if ($obj->getVar('field_valuetype') == "") {
648
            $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
649
        }
650
651
        if (!in_array($obj->getVar('field_name'), $this->getPostVars())) {
652
            if ($obj->isNew()) {
653
                //add column to table
654
                $changetype = "ADD";
655
            } else {
656
                //update column information
657
                $changetype = "CHANGE `" . $obj->getVar('field_name', 'n') . "`";
658
            }
659
            $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? "(" . $obj->getVar('field_maxlength') . ")" : "";
660
            $notnullstring = " NOT NULL";
661
            //set type
662
            switch ($obj->getVar('field_valuetype')) {
663
                default:
664
                case XOBJ_DTYPE_ARRAY:
665
                case XOBJ_DTYPE_UNICODE_ARRAY:
666
                    $type = "mediumtext";
667
                    break;
668
                case XOBJ_DTYPE_UNICODE_EMAIL:
669
                case XOBJ_DTYPE_UNICODE_TXTBOX:
670
                case XOBJ_DTYPE_UNICODE_URL:
671
                case XOBJ_DTYPE_EMAIL:
672
                case XOBJ_DTYPE_TXTBOX:
673
                case XOBJ_DTYPE_URL:
674
                    $type = "varchar";
675
                    // varchars must have a maxlength
676
                    if (!$maxlengthstring) {
677
                        //so set it to max if maxlength is not set - or should it fail?
678
                        $maxlengthstring = "(255)";
679
                        $obj->setVar('field_maxlength', 255);
680
                    }
681
                    //if ( $obj->getVar('field_default')  ) {
682
                        $defaultstring = " DEFAULT " . $this->db->quote($obj->cleanVars['field_default']);
683
                    //}
684
                    break;
685
686
                case XOBJ_DTYPE_INT:
687
                    $type = "int";
688
                    if ($obj->getVar('field_default') || $obj->getVar('field_default') !== '') {
689
                        $defaultstring = " DEFAULT '" . intval($obj->cleanVars['field_default']) . "'";
690
                        $obj->setVar('field_default', intval($obj->cleanVars['field_default']));
691
                    }
692
                    break;
693
694
                case XOBJ_DTYPE_DECIMAL:
695
                    $type = "decimal(14,6)";
696
                    if ($obj->getVar('field_default') || $obj->getVar('field_default') !== '') {
697
                        $defaultstring = " DEFAULT '" . doubleval($obj->cleanVars['field_default']) . "'";
698
                        $obj->setVar('field_default', doubleval($obj->cleanVars['field_default']));
699
                    }
700
                    break;
701
702
                case XOBJ_DTYPE_FLOAT:
703
                    $type = "float(15,9)";
704
                    if ($obj->getVar('field_default') || $obj->getVar('field_default') !== '') {
705
                        $defaultstring = " DEFAULT '" . floatval($obj->cleanVars['field_default']) . "'";
706
                        $obj->setVar('field_default', floatval($obj->cleanVars['field_default']));
707
                    }
708
                    break;
709
710
                case XOBJ_DTYPE_OTHER:
711
                case XOBJ_DTYPE_UNICODE_TXTAREA:
712
                case XOBJ_DTYPE_TXTAREA:
713
                    $type = "text";
714
                    $maxlengthstring = "";
715
                    $notnullstring = "";
716
                    break;
717
718
                case XOBJ_DTYPE_MTIME:
719
                    $type = "date";
720
                    $maxlengthstring = "";
721
                    break;
722
            }
723
724
            $sql = "ALTER TABLE `" . $objects_handler->table . "` " .
725
            $changetype . " `" . $obj->cleanVars['field_name'] . "` " . $type . $maxlengthstring . $notnullstring . $defaultstring;
726
            if (!$this->db->query($sql)) {
727
                return false;
728
            }
729
        }
730
731
        //change this to also update the cached field information storage
732
        $obj->setDirty();
733
        if (!parent::insert($obj, $force)) {
734
            return false;
735
        }
736
        return $obj->getVar('field_id');
737
738
    }
739
740
    /**
741
    * delete a profile field from the database
742
    *
743
    * @param object $obj reference to the object to delete
744
    * @param bool $force
745
    * @return bool FALSE if failed.
746
    **/
747
    function delete($obj, $force = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
748
    {
749
        $objects_handler = xoops_getmodulehandler('extras', 'songlist');
750
        // remove column from table
751
        $sql = "ALTER TABLE " . $objects_handler->table . " DROP `" . $obj->getVar('field_name', 'n') . "`";
752
        if ($this->db->query($sql)) {
753
            //change this to update the cached field information storage
754
            if (!parent::delete($obj, $force)) {
755
                return false;
756
            }
757
758
            if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
759
                $module_handler = xoops_gethandler('module');
760
                $objects_module = $module_handler->getByDirname('profile');
0 ignored issues
show
Bug introduced by
The method getByDirname() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

760
                /** @scrutinizer ignore-call */ 
761
                $objects_module = $module_handler->getByDirname('profile');
Loading history...
761
                if (is_object($objects_module)) {
762
                    // Remove group permissions
763
                    $groupperm_handler = xoops_gethandler('groupperm');
764
                    $criteria = new CriteriaCompo(new Criteria('gperm_modid', $objects_module->getVar('mid')));
765
                    $criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
766
                    return $groupperm_handler->deleteAll($criteria);
0 ignored issues
show
Bug introduced by
The method deleteAll() does not exist on XoopsObjectHandler. Did you maybe mean delete()? ( Ignorable by Annotation )

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

766
                    return $groupperm_handler->/** @scrutinizer ignore-call */ deleteAll($criteria);

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...
767
                }
768
            }
769
        }
770
        return false;
771
    }
772
773
    /**
774
     * Get array of standard variable names (song table)
775
     *
776
     * @return array
777
     */
778
    function getPostVars()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
779
    {
780
        return array('sid', 'cid', 'gid', 'aids', 'abid', 'songid', 'traxid', 'title', 'lyrics', 'hits', 'rank', 'votes', 'tags',
781
 					 'created', 'updated');
782
    }
783
}
784
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...