Test Failed
Push — master ( 398493...d4ef72 )
by Michael
11:04
created

ProfileField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 19
rs 9.4285
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
        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
        if ($key === 'field_options' && !empty($value)) {
79
            foreach (array_keys($value) as $idx) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string and boolean; however, parameter $input 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

79
            foreach (array_keys(/** @scrutinizer ignore-type */ $value) as $idx) {
Loading history...
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
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));
0 ignored issues
show
Bug introduced by
It seems like $this->getOutputValue($user, $profile) can also be of type array and 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

118
                $element = new XoopsFormLabel($caption, /** @scrutinizer ignore-type */ $this->getOutputValue($user, $profile));
Loading history...
119
                break;
120
121
            case 'textbox':
122
                $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array and array; however, parameter $value of XoopsFormText::__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

122
                $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), /** @scrutinizer ignore-type */ $value);
Loading history...
123
                break;
124
125
            case 'textarea':
126
                $element = new XoopsFormTextArea($caption, $name, $value, 4, 30);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array and array; however, parameter $value of XoopsFormTextArea::__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

126
                $element = new XoopsFormTextArea($caption, $name, /** @scrutinizer ignore-type */ $value, 4, 30);
Loading history...
127
                break;
128
129
            case 'dhtml':
130
                $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array and array; however, parameter $value of XoopsFormDhtmlTextArea::__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

130
                $element = new XoopsFormDhtmlTextArea($caption, $name, /** @scrutinizer ignore-type */ $value, 10, 30);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array and array; however, parameter $value of XoopsFormRadio::__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

153
                $element = new XoopsFormRadio($caption, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array and array; however, parameter $value of XoopsFormRadioYN::__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

163
                $element = new XoopsFormRadioYN($caption, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
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]);
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...
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 XoopsFormSelectTheme($caption, $name, $value, 1, true);
210
                break;
211
        }
212
        if ($this->getVar('field_description') != '') {
213
            $element->setDescription($this->getVar('field_description'));
214
        }
215
216
        return $element;
217
    }
218
219
    /**
220
     * Returns a value for output of this field
221
     *
222
     * @param XoopsUser      $user    {@link XoopsUser} object to get the value of
223
     * @param profileProfile $profile object to get the value of
224
     *
225
     * @return mixed
226
     **/
227
    public function getOutputValue(&$user, $profile)
228
    {
229
        if (file_exists($file = $GLOBALS['xoops']->path('modules/profile/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) {
230
            include_once $file;
231
        } else {
232
            include_once $GLOBALS['xoops']->path('modules/profile/language/english/modinfo.php');
233
        }
234
235
        $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
236
237
        switch ($this->getVar('field_type')) {
238
            default:
239
            case 'textbox':
240
                $value = is_array($value) ? $value[0] : $value;
241
                if ($this->getVar('field_name') === 'url' && $value !== '') {
242
                    return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>';
243
                } else {
244
                    return $value;
245
                }
246
                break;
247
            case 'textarea':
248
            case 'dhtml':
249
            case 'theme':
250
            case 'language':
251
            case 'list':
252
                return $value;
253
                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...
254
255
            case 'select':
256
            case 'radio':
257
                $value = is_array($value) ? $value[0] : $value;
258
                $options = $this->getVar('field_options');
259
                if (isset($options[$value])) {
260
                    $value = htmlspecialchars(defined($options[$value]) ? constant($options[$value]) : $options[$value]);
261
                } else {
262
                    $value = '';
263
                }
264
265
                return $value;
266
                break;
267
268
            case 'select_multi':
269
            case 'checkbox':
270
                $options = $this->getVar('field_options');
271
                $ret     = array();
272
                if (count($options) > 0) {
273
                    foreach (array_keys($options) as $key) {
274
                        if (in_array($key, $value)) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null and string and boolean; 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

274
                        if (in_array($key, /** @scrutinizer ignore-type */ $value)) {
Loading history...
275
                            $ret[$key] = htmlspecialchars(defined($options[$key]) ? constant($options[$key]) : $options[$key]);
276
                        }
277
                    }
278
                }
279
280
                return $ret;
281
                break;
282
283
            case 'group':
284
                /* @var $member_handler XoopsMemberHandler */
285
                $member_handler = xoops_getHandler('member');
286
                $options        = $member_handler->getGroupList();
287
                $ret            = isset($options[$value]) ? $options[$value] : '';
288
289
                return $ret;
290
                break;
291
292
            case 'group_multi':
293
                /* @var $member_handler XoopsMemberHandler */
294
                $member_handler = xoops_getHandler('member');
295
                $options        = $member_handler->getGroupList();
296
                $ret            = array();
297
                foreach (array_keys($options) as $key) {
298
                    if (in_array($key, $value)) {
299
                        $ret[$key] = htmlspecialchars($options[$key]);
300
                    }
301
                }
302
303
                return $ret;
304
                break;
305
306
            case 'longdate':
307
                //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
308
                //when we cannot convert it to a UNIX timestamp?
309
                return str_replace('-', '/', $value);
310
311
            case 'date':
312
                return formatTimestamp($value, 's');
313
                break;
314
315
            case 'datetime':
316
                if (!empty($value)) {
317
                    return formatTimestamp($value, 'm');
318
                } else {
319
                    return $value = _PROFILE_MI_NEVER_LOGGED_IN;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
320
                }
321
                break;
322
323
            case 'autotext':
324
                $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it
325
                $value = str_replace('{X_UID}', $user->getVar('uid'), $value);
326
                $value = str_replace('{X_URL}', XOOPS_URL, $value);
327
                $value = str_replace('{X_UNAME}', $user->getVar('uname'), $value);
328
329
                return $value;
330
                break;
331
332
            case 'rank':
333
                $userrank       = $user->rank();
334
                $user_rankimage = '';
335
                if (isset($userrank['image']) && $userrank['image'] !== '') {
336
                    $user_rankimage = '<img src="' . XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br>';
337
                }
338
339
                return $user_rankimage . $userrank['title'];
340
                break;
341
342
            case 'yesno':
343
                return $value ? _YES : _NO;
344
                break;
345
346
            case 'timezone':
347
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
348
                $timezones = XoopsLists::getTimeZoneList();
349
                $value     = empty($value) ? '0' : (string)$value;
350
351
                return $timezones[str_replace('.0', '', $value)];
352
                break;
353
        }
354
    }
355
356
    /**
357
     * Returns a value ready to be saved in the database
358
     *
359
     * @param mixed $value Value to format
360
     *
361
     * @return mixed
362
     */
363
    public function getValueForSave($value)
364
    {
365
        switch ($this->getVar('field_type')) {
366
            default:
367
            case 'textbox':
368
            case 'textarea':
369
            case 'dhtml':
370
            case 'yesno':
371
            case 'timezone':
372
            case 'theme':
373
            case 'language':
374
            case 'list':
375
            case 'select':
376
            case 'radio':
377
            case 'select_multi':
378
            case 'group':
379
            case 'group_multi':
380
            case 'longdate':
381
                return $value;
382
383
            case 'checkbox':
384
                return (array)$value;
385
386
            case 'date':
387
                if ($value !== '') {
388
                    return strtotime($value);
389
                }
390
391
                return $value;
392
                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...
393
394
            case 'datetime':
395
                if (!empty($value)) {
396
                    return strtotime($value['date']) + (int)$value['time'];
397
                }
398
399
                return $value;
400
                break;
401
        }
402
    }
403
404
    /**
405
     * Get names of user variables
406
     *
407
     * @return array
408
     */
409
    public function getUserVars()
410
    {
411
        /* @var $profile_handler ProfileProfileHandler */
412
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
413
414
        return $profile_handler->getUserVars();
415
    }
416
}
417
418
/**
419
 * @package             kernel
420
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
421
 */
422
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...
423
{
424
    /**
425
     * @param null|XoopsDatabase $db
426
     */
427
    public function __construct(XoopsDatabase $db)
428
    {
429
        parent::__construct($db, 'profile_field', 'profilefield', 'field_id', 'field_title');
430
    }
431
432
    /**
433
     * Read field information from cached storage
434
     *
435
     * @param bool $force_update read fields from database and not cached storage
436
     *
437
     * @return array
438
     */
439
    public function loadFields($force_update = false)
440
    {
441
        static $fields = array();
442
        if (!empty($force_update) || count($fields) == 0) {
443
            $this->table_link = $this->db->prefix('profile_category');
0 ignored issues
show
Bug Best Practice introduced by
The property table_link does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
444
            $criteria         = new Criteria('o.field_id', 0, '!=');
445
            $criteria->setSort('l.cat_weight ASC, o.field_weight');
446
            $field_objs =& $this->getByLink($criteria, array('o.*'), true, 'cat_id', 'cat_id');
447
            foreach (array_keys($field_objs) as $i) {
448
                $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i];
449
            }
450
        }
451
452
        return $fields;
453
    }
454
455
    /**
456
     * save a profile field in the database
457
     *
458
     * @param XoopsObject|ProfileField $obj   reference to the object
459
     * @param bool                     $force whether to force the query execution despite security settings
460
     *
461
     * @internal param bool $checkObject check if the object is dirty and clean the attributes
462
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
463
     */
464
    public function insert(XoopsObject $obj, $force = false)
465
    {
466
        if (!($obj instanceof $this->className)) {
467
            return false;
468
        }
469
         /* @var $profile_handler ProfileProfileHandler */
470
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
471
        $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
472
        $obj->cleanVars();
473
        $defaultstring = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $defaultstring is dead and can be removed.
Loading history...
474
        switch ($obj->getVar('field_type')) {
475
            case 'datetime':
476
            case 'date':
477
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
478
                $obj->setVar('field_maxlength', 10);
479
                break;
480
481
            case 'longdate':
482
                $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME);
483
                break;
484
485
            case 'yesno':
486
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
487
                $obj->setVar('field_maxlength', 1);
488
                break;
489
490
            case 'textbox':
491
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
492
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
493
                }
494
                break;
495
496
            case 'autotext':
497
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
498
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
499
                }
500
                break;
501
502
            case 'group_multi':
503
            case 'select_multi':
504
            case 'checkbox':
505
                $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY);
506
                break;
507
508
            case 'language':
509
            case 'timezone':
510
            case 'theme':
511
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
512
                break;
513
514
            case 'dhtml':
515
            case 'textarea':
516
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
517
                break;
518
        }
519
520
        if ($obj->getVar('field_valuetype') === '') {
521
            $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
522
        }
523
524
        if ((!in_array($obj->getVar('field_name'), $this->getUserVars())) && isset($_REQUEST['field_required'])) {
525
            if ($obj->isNew()) {
526
                //add column to table
527
                $changetype = 'ADD';
528
            } else {
529
                //update column information
530
                $changetype = 'MODIFY COLUMN';
531
            }
532
            $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? '(' . $obj->getVar('field_maxlength') . ')' : '';
533
534
            //set type
535
            switch ($obj->getVar('field_valuetype')) {
536
                default:
537
                case XOBJ_DTYPE_ARRAY:
538
                case XOBJ_DTYPE_UNICODE_ARRAY:
539
                    $type = 'mediumtext';
540
                    $maxlengthstring = '';
541
                    break;
542
                case XOBJ_DTYPE_UNICODE_EMAIL:
543
                case XOBJ_DTYPE_UNICODE_TXTBOX:
544
                case XOBJ_DTYPE_UNICODE_URL:
545
                case XOBJ_DTYPE_EMAIL:
546
                case XOBJ_DTYPE_TXTBOX:
547
                case XOBJ_DTYPE_URL:
548
                    $type = 'varchar';
549
                    // varchars must have a maxlength
550
                    if (!$maxlengthstring) {
551
                        //so set it to max if maxlength is not set - or should it fail?
552
                        $maxlengthstring = '(255)';
553
                        $obj->setVar('field_maxlength', 255);
554
                    }
555
                    break;
556
557
                case XOBJ_DTYPE_INT:
558
                    $type = 'int';
559
                    break;
560
561
                case XOBJ_DTYPE_DECIMAL:
562
                    $type = 'decimal(14,6)';
563
                    break;
564
565
                case XOBJ_DTYPE_FLOAT:
566
                    $type = 'float(15,9)';
567
                    break;
568
569
                case XOBJ_DTYPE_OTHER:
570
                case XOBJ_DTYPE_UNICODE_TXTAREA:
571
                case XOBJ_DTYPE_TXTAREA:
572
                    $type            = 'text';
573
                    $maxlengthstring = '';
574
                    break;
575
576
                case XOBJ_DTYPE_MTIME:
577
                    $type            = 'date';
578
                    $maxlengthstring = '';
579
                    break;
580
            }
581
582
            $sql = 'ALTER TABLE `' . $profile_handler->table . '` ' . $changetype . ' `' . $obj->cleanVars['field_name'] . '` ' . $type . $maxlengthstring . ' NULL';
583
            $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
0 ignored issues
show
Bug introduced by
The method queryF() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

583
            $result = $force ? $this->db->/** @scrutinizer ignore-call */ queryF($sql) : $this->db->query($sql);
Loading history...
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

583
            $result = $force ? $this->db->queryF($sql) : $this->db->/** @scrutinizer ignore-call */ query($sql);
Loading history...
584
            if (!$result) {
585
                $obj->setErrors($this->db->error());
0 ignored issues
show
Bug introduced by
The method error() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

585
                $obj->setErrors($this->db->/** @scrutinizer ignore-call */ error());
Loading history...
586
                return false;
587
            }
588
        }
589
590
        //change this to also update the cached field information storage
591
        $obj->setDirty();
592
        if (!parent::insert($obj, $force)) {
593
            return false;
594
        }
595
596
        return $obj->getVar('field_id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $obj->getVar('field_id') also could return the type string|array which is incompatible with the documented return type boolean.
Loading history...
597
    }
598
599
    /**
600
     * delete a profile field from the database
601
     *
602
     * @param XoopsObject|ProfileField $obj reference to the object to delete
603
     * @param bool   $force
604
     * @return bool FALSE if failed.
605
     **/
606
    public function delete(XoopsObject $obj, $force = false)
607
    {
608
        if (!($obj instanceof $this->className)) {
609
            return false;
610
        }
611
         /* @var $profile_handler ProfileProfileHandler */
612
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
613
        // remove column from table
614
        $sql = 'ALTER TABLE ' . $profile_handler->table . ' DROP `' . $obj->getVar('field_name', 'n') . '`';
615
        if ($this->db->query($sql)) {
616
            //change this to update the cached field information storage
617
            if (!parent::delete($obj, $force)) {
618
                return false;
619
            }
620
621
            if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
622
                /* @var $module_handler XoopsModuleHandler */
623
                $module_handler = xoops_getHandler('module');
624
                $profile_module = $module_handler->getByDirname('profile');
625
                if (is_object($profile_module)) {
626
                    // Remove group permissions
627
                    /* @var $groupperm_handler XoopsGroupPermHandler  */
628
                    $groupperm_handler = xoops_getHandler('groupperm');
629
                    $criteria          = new CriteriaCompo(new Criteria('gperm_modid', $profile_module->getVar('mid')));
0 ignored issues
show
Bug introduced by
It seems like $profile_module->getVar('mid') can also be of type array and array; however, parameter $value of Criteria::__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

629
                    $criteria          = new CriteriaCompo(new Criteria('gperm_modid', /** @scrutinizer ignore-type */ $profile_module->getVar('mid')));
Loading history...
630
                    $criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
631
632
                    return $groupperm_handler->deleteAll($criteria);
633
                }
634
            }
635
        }
636
637
        return false;
638
    }
639
640
    /**
641
     * Get array of standard variable names (user table)
642
     *
643
     * @return array
644
     */
645
    public function getUserVars()
646
    {
647
        return array(
648
            'uid',
649
            'uname',
650
            'name',
651
            'email',
652
            'url',
653
            'user_avatar',
654
            'user_regdate',
655
            'user_icq',
656
            'user_from',
657
            'user_sig',
658
            'user_viewemail',
659
            'actkey',
660
            'user_aim',
661
            'user_yim',
662
            'user_msnm',
663
            'pass',
664
            'posts',
665
            'attachsig',
666
            'rank',
667
            'level',
668
            'theme',
669
            'timezone_offset',
670
            'last_login',
671
            'umode',
672
            'uorder',
673
            'notify_method',
674
            'notify_mode',
675
            'user_occ',
676
            'bio',
677
            'user_intrest',
678
            'user_mailok');
679
    }
680
}
681