ProfileFieldHandler::insert()   F
last analyzed

Complexity

Conditions 42
Paths 14961

Size

Total Lines 133
Code Lines 95

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 133
rs 0
c 0
b 0
f 0
cc 42
nc 14961
nop 2

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
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-2025 XOOPS Project (https://xoops.org)
13
 * @license             GNU GPL 2 (https://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
//if (!defined('XOOPS_ROOT_PATH')) {
21
//    throw new \RuntimeException('XOOPS root path not defined');
22
//}
23
24
/**
25
 * @package             kernel
26
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
27
 */
28
class ProfileField extends XoopsObject
29
{
30
    public $field_id;
31
    public $cat_id;
32
    public $field_type;
33
    public $field_valuetype;
34
    public $field_name;
35
    public $field_title;
36
    public $field_description;
37
    public $field_required; //0 = no, 1 = yes
38
    public $field_maxlength;
39
    public $field_weight;
40
    public $field_default;
41
    public $field_notnull;
42
    public $field_edit;
43
    public $field_show;
44
    public $field_config;
45
    public $field_options;
46
    public $step_id;
47
48
    /**
49
     *
50
     */
51
    public function __construct()
52
    {
53
        $this->initVar('field_id', XOBJ_DTYPE_INT, null);
54
        $this->initVar('cat_id', XOBJ_DTYPE_INT, null, true);
55
        $this->initVar('field_type', XOBJ_DTYPE_TXTBOX);
56
        $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true);
57
        $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true);
58
        $this->initVar('field_title', XOBJ_DTYPE_TXTBOX);
59
        $this->initVar('field_description', XOBJ_DTYPE_TXTAREA);
60
        $this->initVar('field_required', XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes
61
        $this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0);
62
        $this->initVar('field_weight', XOBJ_DTYPE_INT, 0);
63
        $this->initVar('field_default', XOBJ_DTYPE_TXTAREA, '');
64
        $this->initVar('field_notnull', XOBJ_DTYPE_INT, 1);
65
        $this->initVar('field_edit', XOBJ_DTYPE_INT, 0);
66
        $this->initVar('field_show', XOBJ_DTYPE_INT, 0);
67
        $this->initVar('field_config', XOBJ_DTYPE_INT, 0);
68
        $this->initVar('field_options', XOBJ_DTYPE_ARRAY, []);
69
        $this->initVar('step_id', XOBJ_DTYPE_INT, 0);
70
    }
71
72
    /**
73
     * Extra treatment dealing with non latin encoding
74
     * Tricky solution
75
     * @param string $key
76
     * @param mixed  $value
77
     * @param bool   $not_gpc
78
     */
79
    public function setVar($key, $value, $not_gpc = false)
80
    {
81
        if ($key === 'field_options' && \is_array($value)) {
82
            foreach (array_keys($value) as $idx) {
83
                $value[$idx] = base64_encode($value[$idx]);
84
            }
85
        }
86
        parent::setVar($key, $value, $not_gpc);
87
    }
88
89
    /**
90
     * @param string $key
91
     * @param string $format
92
     *
93
     * @return mixed
94
     */
95
    public function getVar($key, $format = 's')
96
    {
97
        $value = parent::getVar($key, $format);
98
        if ($key === 'field_options' && !empty($value)) {
99
            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

99
            foreach (array_keys(/** @scrutinizer ignore-type */ $value) as $idx) {
Loading history...
100
                $value[$idx] = base64_decode($value[$idx]);
101
            }
102
        }
103
104
        return $value;
105
    }
106
107
    /**
108
     * Returns a {@link XoopsFormElement} for editing the value of this field
109
     *
110
     * @param XoopsUser      $user    {@link XoopsUser} object to edit the value of
111
     * @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of
112
     *
113
     * @return XoopsFormElement
114
     **/
115
    public function getEditElement($user, $profile)
116
    {
117
        $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
118
119
        $caption = $this->getVar('field_title');
120
        $caption = defined($caption) ? constant($caption) : $caption;
121
        $name    = $this->getVar('field_name', 'e');
122
        $options = $this->getVar('field_options');
123
        if (is_array($options)) {
124
            //asort($options);
125
126
            foreach (array_keys($options) as $key) {
127
                $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
128
                $optkey = defined($key) ? constant($key) : $key;
129
                unset($options[$key]);
130
                $options[$optkey] = $optval;
131
            }
132
        }
133
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
134
        switch ($this->getVar('field_type')) {
135
            default:
136
            case 'autotext':
137
                //autotext is not for editing
138
                $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

138
                $element = new XoopsFormLabel($caption, /** @scrutinizer ignore-type */ $this->getOutputValue($user, $profile));
Loading history...
139
                break;
140
141
            case 'textbox':
142
                $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

142
                $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), /** @scrutinizer ignore-type */ $value);
Loading history...
143
                break;
144
145
            case 'textarea':
146
                $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

146
                $element = new XoopsFormTextArea($caption, $name, /** @scrutinizer ignore-type */ $value, 4, 30);
Loading history...
147
                break;
148
149
            case 'dhtml':
150
                $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

150
                $element = new XoopsFormDhtmlTextArea($caption, $name, /** @scrutinizer ignore-type */ $value, 10, 30);
Loading history...
151
                break;
152
153
            case 'select':
154
                $element = new XoopsFormSelect($caption, $name, $value);
155
                // If options do not include an empty element, then add a blank option to prevent any default selection
156
                //                if (!in_array('', array_keys($options))) {
157
                if (!array_key_exists('', $options)) {
158
                    $element->addOption('', _NONE);
159
160
                    $eltmsg                          = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf(_FORM_ENTER, $caption);
161
                    $eltmsg                          = str_replace('"', '\"', stripslashes($eltmsg));
162
                    $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; }";
163
                }
164
                $element->addOptionArray($options);
165
                break;
166
167
            case 'select_multi':
168
                $element = new XoopsFormSelect($caption, $name, $value, 5, true);
169
                $element->addOptionArray($options);
170
                break;
171
172
            case 'radio':
173
                $element = new XoopsFormRadio($caption, $name, (string) $value);
174
                $element->addOptionArray($options);
175
                break;
176
177
            case 'checkbox':
178
                $element = new XoopsFormCheckBox($caption, $name, $value);
179
                $element->addOptionArray($options);
180
                break;
181
182
            case 'yesno':
183
                $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

183
                $element = new XoopsFormRadioYN($caption, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
184
                break;
185
186
            case 'group':
187
                $element = new XoopsFormSelectGroup($caption, $name, true, $value);
188
                break;
189
190
            case 'group_multi':
191
                $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
192
                break;
193
194
            case 'language':
195
                $element = new XoopsFormSelectLang($caption, $name, $value);
196
                break;
197
198
            case 'date':
199
                $element = new XoopsFormTextDateSelect($caption, $name, 15, $value);
200
                break;
201
202
            case 'longdate':
203
                $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace('-', '/', $value));
204
                break;
205
206
            case 'datetime':
207
                $element = new XoopsFormDatetime($caption, $name, 15, $value);
208
                break;
209
210
            case 'timezone':
211
                $element = new XoopsFormSelectTimezone($caption, $name, $value);
212
                //$element->setExtra("style='width: 280px;'");
213
                break;
214
215
            case 'rank':
216
                $element = new XoopsFormSelect($caption, $name, $value);
217
218
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
219
                $ranks = XoopsLists::getUserRankList();
220
                $element->addOption(0, '--------------');
221
                $element->addOptionArray($ranks);
222
                break;
223
224
            case 'theme':
225
                $element = new XoopsFormSelectTheme($caption, $name, $value, 1, true);
226
                break;
227
        }
228
        if ($this->getVar('field_description') != '') {
229
            $element->setDescription($this->getVar('field_description'));
230
        }
231
232
        return $element;
233
    }
234
235
    /**
236
     * Returns a value for output of this field
237
     *
238
     * @param XoopsUser      $user    {@link XoopsUser} object to get the value of
239
     * @param profileProfile $profile object to get the value of
240
     *
241
     * @return mixed
242
     **/
243
    public function getOutputValue($user, $profile)
244
    {
245
        xoops_loadLanguage('modinfo', 'profile');
246
247
        $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
248
249
        switch ($this->getVar('field_type')) {
250
            default:
251
            case 'textbox':
252
                $value = is_array($value) ? $value[0] : $value;
253
                if ($this->getVar('field_name') === 'url' && $value !== '') {
254
                    return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>';
255
                } else {
256
                    return $value;
257
                }
258
                break;
259
            case 'textarea':
260
            case 'dhtml':
261
            case 'theme':
262
            case 'language':
263
                return $value;
264
                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...
265
266
            case 'select':
267
            case 'radio':
268
                $value = is_array($value) ? $value[0] : $value;
269
                $options = $this->getVar('field_options');
270
                if (isset($options[$value])) {
271
                    $value = htmlspecialchars(defined($options[$value]) ? constant($options[$value]) : $options[$value], ENT_QUOTES | ENT_HTML5);
272
                } else {
273
                    $value = '';
274
                }
275
276
                return $value;
277
                break;
278
279
            case 'select_multi':
280
            case 'checkbox':
281
                $options = $this->getVar('field_options');
282
                $ret     = [];
283
                if (count($options) > 0) {
284
                    foreach (array_keys($options) as $key) {
285
                        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

285
                        if (in_array($key, /** @scrutinizer ignore-type */ $value)) {
Loading history...
286
                            $ret[$key] = htmlspecialchars(defined($options[$key]) ? constant($options[$key]) : $options[$key], ENT_QUOTES | ENT_HTML5);
287
                        }
288
                    }
289
                }
290
291
                return $ret;
292
                break;
293
294
            case 'group':
295
                /** @var XoopsMemberHandler $member_handler */
296
                $member_handler = xoops_getHandler('member');
297
                $options        = $member_handler->getGroupList();
298
                $ret            = $options[$value] ?? '';
299
300
                return $ret;
301
                break;
302
303
            case 'group_multi':
304
                /** @var XoopsMemberHandler $member_handler */
305
                $member_handler = xoops_getHandler('member');
306
                $options        = $member_handler->getGroupList();
307
                $ret            = [];
308
                foreach (array_keys($options) as $key) {
309
                    if (in_array($key, $value)) {
310
                        $ret[$key] = htmlspecialchars($options[$key], ENT_QUOTES | ENT_HTML5);
311
                    }
312
                }
313
314
                return $ret;
315
                break;
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;
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
The assignment to $value is dead and can be removed.
Loading history...
331
                }
332
                break;
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;
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;
352
353
            case 'yesno':
354
                return $value ? _YES : _NO;
355
                break;
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;
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 'select':
386
            case 'radio':
387
            case 'select_multi':
388
            case 'group':
389
            case 'group_multi':
390
            case 'longdate':
391
                return $value;
392
393
            case 'checkbox':
394
                return (array) $value;
395
396
            case 'date':
397
                if ($value !== '') {
398
                    return strtotime($value);
399
                }
400
401
                return $value;
402
                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...
403
404
            case 'datetime':
405
                if (!empty($value)) {
406
                    return strtotime($value['date']) + (int) $value['time'];
407
                }
408
409
                return $value;
410
                break;
411
        }
412
    }
413
414
    /**
415
     * Get names of user variables
416
     *
417
     * @return array
418
     */
419
    public function getUserVars()
420
    {
421
        /** @var ProfileProfileHandler $profile_handler */
422
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
423
424
        return $profile_handler->getUserVars();
425
    }
426
}
427
428
/**
429
 * @package             kernel
430
 * @copyright       (c) 2000-2025 XOOPS Project (https://xoops.org)
431
 */
432
class ProfileFieldHandler extends XoopsPersistableObjectHandler
433
{
434
    public $table_link;
435
436
    /**
437
     * @param XoopsDatabase|null $db
438
     */
439
    public function __construct(XoopsDatabase $db)
440
    {
441
        parent::__construct($db, 'profile_field', 'profilefield', 'field_id', 'field_title');
442
    }
443
444
    /**
445
     * Read field information from cached storage
446
     *
447
     * @param bool $force_update read fields from database and not cached storage
448
     *
449
     * @return array
450
     */
451
    public function loadFields($force_update = false)
452
    {
453
        static $fields = [];
454
        if (!empty($force_update) || count($fields) == 0) {
455
            $this->table_link = $this->db->prefix('profile_category');
456
            $criteria         = new Criteria('o.field_id', 0, '!=');
457
            $criteria->setSort('l.cat_weight ASC, o.field_weight');
458
            $field_objs = $this->getByLink($criteria, ['o.*'], true, 'cat_id', 'cat_id');
459
            foreach (array_keys($field_objs) as $i) {
460
                $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i];
461
            }
462
        }
463
464
        return $fields;
465
    }
466
467
    /**
468
     * save a profile field in the database
469
     *
470
     * @param XoopsObject|ProfileField $obj   reference to the object
471
     * @param bool                     $force whether to force the query execution despite security settings
472
     *
473
     * @internal param bool $checkObject check if the object is dirty and clean the attributes
474
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
475
     */
476
    public function insert(XoopsObject $obj, $force = false)
477
    {
478
        if (!($obj instanceof $this->className)) {
479
            return false;
480
        }
481
        /** @var ProfileProfileHandler $profile_handler */
482
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
483
        $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
484
        $obj->cleanVars();
485
        $defaultstring = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $defaultstring is dead and can be removed.
Loading history...
486
        switch ($obj->getVar('field_type')) {
487
            case 'datetime':
488
            case 'date':
489
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
490
                $obj->setVar('field_maxlength', 10);
491
                break;
492
493
            case 'longdate':
494
                $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME);
495
                break;
496
497
            case 'yesno':
498
                $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
499
                $obj->setVar('field_maxlength', 1);
500
                break;
501
502
            case 'textbox':
503
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
504
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
505
                }
506
                break;
507
508
            case 'autotext':
509
                if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
510
                    $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
511
                }
512
                break;
513
514
            case 'group_multi':
515
            case 'select_multi':
516
            case 'checkbox':
517
                $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY);
518
                break;
519
520
            case 'language':
521
            case 'timezone':
522
            case 'theme':
523
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
524
                break;
525
526
            case 'dhtml':
527
            case 'textarea':
528
                $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
529
                break;
530
        }
531
532
        if ($obj->getVar('field_valuetype') === '') {
533
            $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
534
        }
535
536
        if ((!in_array($obj->getVar('field_name'), $this->getUserVars())) && isset($_REQUEST['field_required'])) {
537
            if ($obj->isNew()) {
538
                //add column to table
539
                $changetype = 'ADD';
540
            } else {
541
                //update column information
542
                $changetype = 'MODIFY COLUMN';
543
            }
544
            $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? '(' . $obj->getVar('field_maxlength') . ')' : '';
545
546
            //set type
547
            switch ($obj->getVar('field_valuetype')) {
548
                default:
549
                case XOBJ_DTYPE_ARRAY:
550
                case XOBJ_DTYPE_UNICODE_ARRAY:
551
                    $type = 'mediumtext';
552
                    $maxlengthstring = '';
553
                    break;
554
                case XOBJ_DTYPE_UNICODE_EMAIL:
555
                case XOBJ_DTYPE_UNICODE_TXTBOX:
556
                case XOBJ_DTYPE_UNICODE_URL:
557
                case XOBJ_DTYPE_EMAIL:
558
                case XOBJ_DTYPE_TXTBOX:
559
                case XOBJ_DTYPE_URL:
560
                    $type = 'varchar';
561
                    // varchars must have a maxlength
562
                    if (!$maxlengthstring) {
563
                        //so set it to max if maxlength is not set - or should it fail?
564
                        $maxlengthstring = '(255)';
565
                        $obj->setVar('field_maxlength', 255);
566
                    }
567
                    break;
568
569
                case XOBJ_DTYPE_INT:
570
                    $type = 'int';
571
                    break;
572
573
                case XOBJ_DTYPE_DECIMAL:
574
                    $type = 'decimal(14,6)';
575
                    break;
576
577
                case XOBJ_DTYPE_FLOAT:
578
                    $type = 'float(15,9)';
579
                    break;
580
581
                case XOBJ_DTYPE_OTHER:
582
                case XOBJ_DTYPE_UNICODE_TXTAREA:
583
                case XOBJ_DTYPE_TXTAREA:
584
                    $type            = 'text';
585
                    $maxlengthstring = '';
586
                    break;
587
588
                case XOBJ_DTYPE_MTIME:
589
                    $type            = 'date';
590
                    $maxlengthstring = '';
591
                    break;
592
            }
593
594
            $sql = 'ALTER TABLE `' . $profile_handler->table . '` ' . $changetype . ' `' . $obj->cleanVars['field_name'] . '` ' . $type . $maxlengthstring . ' NULL';
595
            $result = $force ? $this->db->exec($sql) : $this->db->query($sql);
596
            if (!$result) {
597
                $obj->setErrors($this->db->error());
598
                return false;
599
            }
600
        }
601
602
        //change this to also update the cached field information storage
603
        $obj->setDirty();
604
        if (!parent::insert($obj, $force)) {
605
            return false;
606
        }
607
608
        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 array|string which is incompatible with the documented return type boolean.
Loading history...
609
    }
610
611
    /**
612
     * delete a profile field from the database
613
     *
614
     * @param XoopsObject|ProfileField $obj reference to the object to delete
615
     * @param bool   $force
616
     * @return bool FALSE if failed.
617
     **/
618
    public function delete(XoopsObject $obj, $force = false)
619
    {
620
        if (!($obj instanceof $this->className)) {
621
            return false;
622
        }
623
        /** @var ProfileProfileHandler $profile_handler */
624
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
625
        // remove column from table
626
        $sql = 'ALTER TABLE ' . $profile_handler->table . ' DROP `' . $obj->getVar('field_name', 'n') . '`';
627
        if ($this->db->query($sql)) {
628
            //change this to update the cached field information storage
629
            if (!parent::delete($obj, $force)) {
630
                return false;
631
            }
632
633
            if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
634
                /** @var XoopsModuleHandler $module_handler */
635
                $module_handler = xoops_getHandler('module');
636
                $profile_module = $module_handler->getByDirname('profile');
637
                if (is_object($profile_module)) {
638
                    // Remove group permissions
639
                    /** @var XoopsGroupPermHandler $groupperm_handler */
640
                    $groupperm_handler = xoops_getHandler('groupperm');
641
                    $criteria          = new CriteriaCompo(new Criteria('gperm_modid', $profile_module->getVar('mid')));
642
                    $criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
643
644
                    return $groupperm_handler->deleteAll($criteria);
645
                }
646
            }
647
        }
648
649
        return false;
650
    }
651
652
    /**
653
     * Get array of standard variable names (user table)
654
     *
655
     * @return array
656
     */
657
    public function getUserVars()
658
    {
659
        return [
660
            'uid',
661
            'uname',
662
            'name',
663
            'email',
664
            'url',
665
            'user_avatar',
666
            'user_regdate',
667
            'user_icq',
668
            'user_from',
669
            'user_sig',
670
            'user_viewemail',
671
            'actkey',
672
            'user_aim',
673
            'user_yim',
674
            'user_msnm',
675
            'pass',
676
            'posts',
677
            'attachsig',
678
            'rank',
679
            'level',
680
            'theme',
681
            'timezone_offset',
682
            'last_login',
683
            'umode',
684
            'uorder',
685
            'notify_method',
686
            'notify_mode',
687
            'user_occ',
688
            'bio',
689
            'user_intrest',
690
            'user_mailok',
691
        ];
692
    }
693
}
694