Completed
Pull Request — master (#133)
by Goffy
16:45
created

ProfileFieldHandler::loadFields()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Extended User Profile
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             profile
15
 * @since               2.3.0
16
 * @author              Jan Pedersen
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit("XOOPS root path not defined");
21
22
/**
23
 * @package             kernel
24
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
25
 */
26
class ProfileField extends XoopsObject
27
{
28
    /**
29
     *
30
     */
31
    public function __construct()
32
    {
33
        $this->initVar('field_id', XOBJ_DTYPE_INT, null);
34
        $this->initVar('cat_id', XOBJ_DTYPE_INT, null, true);
35
        $this->initVar('field_type', XOBJ_DTYPE_TXTBOX);
36
        $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true);
37
        $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true);
38
        $this->initVar('field_title', XOBJ_DTYPE_TXTBOX);
39
        $this->initVar('field_description', XOBJ_DTYPE_TXTAREA);
40
        $this->initVar('field_required', XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes
41
        $this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0);
42
        $this->initVar('field_weight', XOBJ_DTYPE_INT, 0);
43
        $this->initVar('field_default', XOBJ_DTYPE_TXTAREA, '');
44
        $this->initVar('field_notnull', XOBJ_DTYPE_INT, 1);
45
        $this->initVar('field_edit', XOBJ_DTYPE_INT, 0);
46
        $this->initVar('field_show', XOBJ_DTYPE_INT, 0);
47
        $this->initVar('field_config', XOBJ_DTYPE_INT, 0);
48
        $this->initVar('field_options', XOBJ_DTYPE_ARRAY, array());
49
        $this->initVar('step_id', XOBJ_DTYPE_INT, 0);
50
    }
51
52
    /**
53
     * Extra treatment dealing with non latin encoding
54
     * Tricky solution
55
     * @param string $key
56
     * @param mixed  $value
57
     * @param bool   $not_gpc
58
     */
59
    public function setVar($key, $value, $not_gpc = false)
60
    {
61 View Code Duplication
        if ($key === 'field_options' && is_array($value)) {
62
            foreach (array_keys($value) as $idx) {
63
                $value[$idx] = base64_encode($value[$idx]);
64
            }
65
        }
66
        parent::setVar($key, $value, $not_gpc);
67
    }
68
69
    /**
70
     * @param string $key
71
     * @param string $format
72
     *
73
     * @return mixed
74
     */
75
    public function getVar($key, $format = 's')
76
    {
77
        $value = parent::getVar($key, $format);
78 View Code Duplication
        if ($key === 'field_options' && !empty($value)) {
79
            foreach (array_keys($value) as $idx) {
80
                $value[$idx] = base64_decode($value[$idx]);
81
            }
82
        }
83
84
        return $value;
85
    }
86
87
    /**
88
     * Returns a {@link XoopsFormElement} for editing the value of this field
89
     *
90
     * @param XoopsUser      $user    {@link XoopsUser} object to edit the value of
91
     * @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of
92
     *
93
     * @return XoopsFormElement
0 ignored issues
show
Documentation introduced by
Should the return type not be XoopsFormLabel|XoopsForm...ime|XoopsFormSelectList?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
94
     **/
95
    public function getEditElement($user, $profile)
96
    {
97
        $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
98
99
        $caption = $this->getVar('field_title');
100
        $caption = defined($caption) ? constant($caption) : $caption;
101
        $name    = $this->getVar('field_name', 'e');
102
        $options = $this->getVar('field_options');
103
        if (is_array($options)) {
104
            //asort($options);
105
106
            foreach (array_keys($options) as $key) {
107
                $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
108
                $optkey = defined($key) ? constant($key) : $key;
109
                unset($options[$key]);
110
                $options[$optkey] = $optval;
111
            }
112
        }
113
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
114
        switch ($this->getVar('field_type')) {
115
            default:
116
            case 'autotext':
117
                //autotext is not for editing
118
                $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile));
119
                break;
120
121
            case 'textbox':
122
                $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
123
                break;
124
125
            case 'textarea':
126
                $element = new XoopsFormTextArea($caption, $name, $value, 4, 30);
127
                break;
128
129
            case 'dhtml':
130
                $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30);
131
                break;
132
133
            case 'select':
134
                $element = new XoopsFormSelect($caption, $name, $value);
135
                // If options do not include an empty element, then add a blank option to prevent any default selection
136
//                if (!in_array('', array_keys($options))) {
137
                if (!array_key_exists('', $options)) {
138
                    $element->addOption('', _NONE);
139
140
                    $eltmsg                          = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf(_FORM_ENTER, $caption);
141
                    $eltmsg                          = str_replace('"', '\"', stripslashes($eltmsg));
142
                    $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" . "for (i = 0; i < selectBox.options.length; i++) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }" . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
143
                }
144
                $element->addOptionArray($options);
145
                break;
146
147
            case 'select_multi':
148
                $element = new XoopsFormSelect($caption, $name, $value, 5, true);
149
                $element->addOptionArray($options);
150
                break;
151
152
            case 'radio':
153
                $element = new XoopsFormRadio($caption, $name, $value);
154
                $element->addOptionArray($options);
155
                break;
156
157
            case 'checkbox':
158
                $element = new XoopsFormCheckBox($caption, $name, $value);
159
                $element->addOptionArray($options);
160
                break;
161
162
            case 'yesno':
163
                $element = new XoopsFormRadioYN($caption, $name, $value);
164
                break;
165
166
            case 'group':
167
                $element = new XoopsFormSelectGroup($caption, $name, true, $value);
168
                break;
169
170
            case 'group_multi':
171
                $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
172
                break;
173
174
            case 'language':
175
                $element = new XoopsFormSelectLang($caption, $name, $value);
176
                break;
177
178
            case 'date':
179
                $element = new XoopsFormTextDateSelect($caption, $name, 15, $value);
180
                break;
181
182
            case 'longdate':
183
                $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace('-', '/', $value));
184
                break;
185
186
            case 'datetime':
187
                $element = new XoopsFormDatetime($caption, $name, 15, $value);
188
                break;
189
190
            case 'list':
191
                $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]);
192
                break;
193
194
            case 'timezone':
195
                $element = new XoopsFormSelectTimezone($caption, $name, $value);
196
                $element->setExtra("style='width: 280px;'");
197
                break;
198
199
            case 'rank':
200
                $element = new XoopsFormSelect($caption, $name, $value);
201
202
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
203
                $ranks = XoopsLists::getUserRankList();
204
                $element->addOption(0, '--------------');
205
                $element->addOptionArray($ranks);
206
                break;
207
208
            case 'theme':
209
                $element = new XoopsFormSelect($caption, $name, $value);
210
                $element->addOption('0', _PROFILE_MA_SITEDEFAULT);
211
                $handle  = opendir(XOOPS_THEME_PATH . '/');
212
                $dirlist = array();
213
                while (false !== ($file = readdir($handle))) {
214
                    if (is_dir(XOOPS_THEME_PATH . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) !== 'cvs') {
215
                        if (file_exists(XOOPS_THEME_PATH . '/' . $file . '/theme.html') && in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'])) {
216
                            $dirlist[$file] = $file;
217
                        }
218
                    }
219
                }
220
                closedir($handle);
221
                if (!empty($dirlist)) {
222
                    asort($dirlist);
223
                    $element->addOptionArray($dirlist);
224
                }
225
                break;
226
        }
227
        if ($this->getVar('field_description') != '') {
228
            $element->setDescription($this->getVar('field_description'));
229
        }
230
231
        return $element;
232
    }
233
234
    /**
235
     * Returns a value for output of this field
236
     *
237
     * @param XoopsUser      $user    {@link XoopsUser} object to get the value of
238
     * @param profileProfile $profile object to get the value of
239
     *
240
     * @return mixed
241
     **/
242
    public function getOutputValue(&$user, $profile)
243
    {
244 View Code Duplication
        if (file_exists($file = $GLOBALS['xoops']->path('modules/profile/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) {
245
            include_once $file;
246
        } else {
247
            include_once $GLOBALS['xoops']->path('modules/profile/language/english/modinfo.php');
248
        }
249
250
        $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
251
252
        switch ($this->getVar('field_type')) {
253
            default:
254
            case 'textbox':
255
                if ($this->getVar('field_name') === 'url' && $value !== '') {
256
                    return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>';
257
                } else {
258
                    return $value;
259
                }
260
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
261
            case 'textarea':
262
            case 'dhtml':
263
            case 'theme':
264
            case 'language':
265
            case 'list':
266
                return $value;
267
                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...
268
269
            case 'select':
270
            case 'radio':
271
                $options = $this->getVar('field_options');
272 View Code Duplication
                if (isset($options[$value])) {
273
                    $value = htmlspecialchars(defined($options[$value]) ? constant($options[$value]) : $options[$value]);
274
                } else {
275
                    $value = '';
276
                }
277
278
                return $value;
279
                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...
280
281
            case 'select_multi':
282
            case 'checkbox':
283
                $options = $this->getVar('field_options');
284
                $ret     = array();
285
                if (count($options) > 0) {
286
                    foreach (array_keys($options) as $key) {
287 View Code Duplication
                        if (in_array($key, $value)) {
288
                            $ret[$key] = htmlspecialchars(defined($options[$key]) ? constant($options[$key]) : $options[$key]);
289
                        }
290
                    }
291
                }
292
293
                return $ret;
294
                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...
295
296
            case 'group':
297
                $member_handler = xoops_getHandler('member');
298
                $options        = $member_handler->getGroupList();
299
                $ret            = isset($options[$value]) ? $options[$value] : '';
300
301
                return $ret;
302
                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...
303
304
            case 'group_multi':
305
                $member_handler = xoops_getHandler('member');
306
                $options        = $member_handler->getGroupList();
307
                $ret            = array();
308
                foreach (array_keys($options) as $key) {
309
                    if (in_array($key, $value)) {
310
                        $ret[$key] = htmlspecialchars($options[$key]);
311
                    }
312
                }
313
314
                return $ret;
315
                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...
316
317
            case 'longdate':
318
                //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
319
                //when we cannot convert it to a UNIX timestamp?
320
                return str_replace('-', '/', $value);
321
322
            case 'date':
323
                return formatTimestamp($value, 's');
324
                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...
325
326
            case 'datetime':
327
                if (!empty($value)) {
328
                    return formatTimestamp($value, 'm');
329
                } else {
330
                    return $value = _PROFILE_MI_NEVER_LOGGED_IN;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
331
                }
332
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
333
334
            case 'autotext':
335
                $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it
336
                $value = str_replace('{X_UID}', $user->getVar('uid'), $value);
337
                $value = str_replace('{X_URL}', XOOPS_URL, $value);
338
                $value = str_replace('{X_UNAME}', $user->getVar('uname'), $value);
339
340
                return $value;
341
                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...
342
343
            case 'rank':
344
                $userrank       = $user->rank();
345
                $user_rankimage = '';
346
                if (isset($userrank['image']) && $userrank['image'] !== '') {
347
                    $user_rankimage = '<img src="' . XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br>';
348
                }
349
350
                return $user_rankimage . $userrank['title'];
351
                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...
352
353
            case 'yesno':
354
                return $value ? _YES : _NO;
355
                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...
356
357
            case 'timezone':
358
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
359
                $timezones = XoopsLists::getTimeZoneList();
360
                $value     = empty($value) ? '0' : (string)$value;
361
362
                return $timezones[str_replace('.0', '', $value)];
363
                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...
364
        }
365
    }
366
367
    /**
368
     * Returns a value ready to be saved in the database
369
     *
370
     * @param mixed $value Value to format
371
     *
372
     * @return mixed
373
     */
374
    public function getValueForSave($value)
375
    {
376
        switch ($this->getVar('field_type')) {
377
            default:
378
            case 'textbox':
379
            case 'textarea':
380
            case 'dhtml':
381
            case 'yesno':
382
            case 'timezone':
383
            case 'theme':
384
            case 'language':
385
            case 'list':
386
            case 'select':
387
            case 'radio':
388
            case 'select_multi':
389
            case 'group':
390
            case 'group_multi':
391
            case 'longdate':
392
                return $value;
393
394
            case 'checkbox':
395
                return (array)$value;
396
397
            case 'date':
398
                if ($value !== '') {
399
                    return strtotime($value);
400
                }
401
402
                return $value;
403
                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...
404
405
            case 'datetime':
406
                if (!empty($value)) {
407
                    return strtotime($value['date']) + (int)$value['time'];
408
                }
409
410
                return $value;
411
                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...
412
        }
413
    }
414
415
    /**
416
     * Get names of user variables
417
     *
418
     * @return array
419
     */
420
    public function getUserVars()
421
    {
422
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
423
424
        return $profile_handler->getUserVars();
0 ignored issues
show
Bug introduced by
The method getUserVars cannot be called on $profile_handler (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
425
    }
426
}
427
428
/**
429
 * @package             kernel
430
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
431
 */
432
class ProfileFieldHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
433
{
434
    /**
435
     * @param null|XoopsDatabase $db
436
     */
437
    public function __construct(XoopsDatabase $db)
438
    {
439
        parent::__construct($db, 'profile_field', 'profilefield', 'field_id', 'field_title');
440
    }
441
442
    /**
443
     * Read field information from cached storage
444
     *
445
     * @param bool $force_update read fields from database and not cached storage
446
     *
447
     * @return array
448
     */
449
    public function loadFields($force_update = false)
450
    {
451
        static $fields = array();
452
        if (!empty($force_update) || count($fields) == 0) {
453
            $this->table_link = $this->db->prefix('profile_category');
0 ignored issues
show
Bug introduced by
The property table_link does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
454
            $criteria         = new Criteria('o.field_id', 0, '!=');
455
            $criteria->setSort('l.cat_weight ASC, o.field_weight');
456
            $field_objs =& $this->getByLink($criteria, array('o.*'), true, 'cat_id', 'cat_id');
457
            foreach (array_keys($field_objs) as $i) {
458
                $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i];
459
            }
460
        }
461
462
        return $fields;
463
    }
464
465
    /**
466
     * save a profile field in the database
467
     *
468
     * @param XoopsObject|ProfileField $obj   reference to the object
469
     * @param bool                     $force whether to force the query execution despite security settings
470
     *
471
     * @internal param bool $checkObject check if the object is dirty and clean the attributes
472
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
473
     */
474
    public function insert(XoopsObject $obj, $force = false)
475
    {
476
        if (!($obj instanceof $this->className)) {
477
            return false;
478
        }
479
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
480
        $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
481
        $obj->cleanVars();
482
        $defaultstring = '';
0 ignored issues
show
Unused Code introduced by
$defaultstring is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
483
        switch ($obj->getVar('field_type')) {
484
            case 'datetime':
485
            case 'date':
486
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
487
                $obj->setVar('field_maxlength', 10);
488
                break;
489
490
            case 'longdate':
491
                $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME);
492
                break;
493
494
            case 'yesno':
495
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
496
                $obj->setVar('field_maxlength', 1);
497
                break;
498
499
            case 'textbox':
500
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
501
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
502
                }
503
                break;
504
505
            case 'autotext':
506
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
507
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
508
                }
509
                break;
510
511
            case 'group_multi':
512
            case 'select_multi':
513
            case 'checkbox':
514
                $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY);
515
                break;
516
517
            case 'language':
518
            case 'timezone':
519
            case 'theme':
520
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
521
                break;
522
523
            case 'dhtml':
524
            case 'textarea':
525
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
526
                break;
527
        }
528
529
        if ($obj->getVar('field_valuetype') === '') {
530
            $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
531
        }
532
533
        if ((!in_array($obj->getVar('field_name'), $this->getUserVars())) && isset($_REQUEST['field_required'])) {
534
            if ($obj->isNew()) {
535
                //add column to table
536
                $changetype = 'ADD';
537
            } else {
538
                //update column information
539
                $changetype = 'CHANGE `' . $obj->getVar('field_name', 'n') . '`';
540
            }
541
            $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? '(' . $obj->getVar('field_maxlength') . ')' : '';
542
543
            //set type
544
            switch ($obj->getVar('field_valuetype')) {
545
                default:
546
                case XOBJ_DTYPE_ARRAY:
547
                case XOBJ_DTYPE_UNICODE_ARRAY:
548
                    $type = 'mediumtext';
549
                    break;
550
                case XOBJ_DTYPE_UNICODE_EMAIL:
551
                case XOBJ_DTYPE_UNICODE_TXTBOX:
552
                case XOBJ_DTYPE_UNICODE_URL:
553
                case XOBJ_DTYPE_EMAIL:
554
                case XOBJ_DTYPE_TXTBOX:
555
                case XOBJ_DTYPE_URL:
556
                    $type = 'varchar';
557
                    // varchars must have a maxlength
558
                    if (!$maxlengthstring) {
559
                        //so set it to max if maxlength is not set - or should it fail?
560
                        $maxlengthstring = '(255)';
561
                        $obj->setVar('field_maxlength', 255);
562
                    }
563
                    break;
564
565
                case XOBJ_DTYPE_INT:
566
                    $type = 'int';
567
                    break;
568
569
                case XOBJ_DTYPE_DECIMAL:
570
                    $type = 'decimal(14,6)';
571
                    break;
572
573
                case XOBJ_DTYPE_FLOAT:
574
                    $type = 'float(15,9)';
575
                    break;
576
577
                case XOBJ_DTYPE_OTHER:
578
                case XOBJ_DTYPE_UNICODE_TXTAREA:
579
                case XOBJ_DTYPE_TXTAREA:
580
                    $type            = 'text';
581
                    $maxlengthstring = '';
582
                    break;
583
584
                case XOBJ_DTYPE_MTIME:
585
                    $type            = 'date';
586
                    $maxlengthstring = '';
587
                    break;
588
            }
589
590
            $sql = 'ALTER TABLE `' . $profile_handler->table . '` ' . $changetype . ' `' . $obj->cleanVars['field_name'] . '` ' . $type . $maxlengthstring . ' NULL';
591
            $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
592
            if (!$result) {
593
                return false;
594
            }
595
        }
596
597
        //change this to also update the cached field information storage
598
        $obj->setDirty();
599
        if (!parent::insert($obj, $force)) {
600
            return false;
601
        }
602
603
        return $obj->getVar('field_id');
604
    }
605
606
    /**
607
     * delete a profile field from the database
608
     *
609
     * @param XoopsObject|ProfileField $obj reference to the object to delete
610
     * @param bool   $force
611
     * @return bool FALSE if failed.
612
     **/
613
    public function delete(XoopsObject $obj, $force = false)
614
    {
615
        if (!($obj instanceof $this->className)) {
616
            return false;
617
        }
618
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
619
        // remove column from table
620
        $sql = 'ALTER TABLE ' . $profile_handler->table . ' DROP `' . $obj->getVar('field_name', 'n') . '`';
621
        if ($this->db->query($sql)) {
622
            //change this to update the cached field information storage
623
            if (!parent::delete($obj, $force)) {
624
                return false;
625
            }
626
627
            if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
628
                $module_handler = xoops_getHandler('module');
629
                $profile_module = $module_handler->getByDirname('profile');
630
                if (is_object($profile_module)) {
631
                    // Remove group permissions
632
                    $groupperm_handler = xoops_getHandler('groupperm');
633
                    $criteria          = new CriteriaCompo(new Criteria('gperm_modid', $profile_module->getVar('mid')));
634
                    $criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
635
636
                    return $groupperm_handler->deleteAll($criteria);
637
                }
638
            }
639
        }
640
641
        return false;
642
    }
643
644
    /**
645
     * Get array of standard variable names (user table)
646
     *
647
     * @return array
648
     */
649
    public function getUserVars()
650
    {
651
        return array(
652
            'uid',
653
            'uname',
654
            'name',
655
            'email',
656
            'url',
657
            'user_avatar',
658
            'user_regdate',
659
            'user_icq',
660
            'user_from',
661
            'user_sig',
662
            'user_viewemail',
663
            'actkey',
664
            'user_aim',
665
            'user_yim',
666
            'user_msnm',
667
            'pass',
668
            'posts',
669
            'attachsig',
670
            'rank',
671
            'level',
672
            'theme',
673
            'timezone_offset',
674
            'last_login',
675
            'umode',
676
            'uorder',
677
            'notify_method',
678
            'notify_mode',
679
            'user_occ',
680
            'bio',
681
            'user_intrest',
682
            'user_mailok');
683
    }
684
}
685