Passed
Push — master ( 6209eb...36ba5e )
by Michael
51s queued 14s
created

YogurtField::getOutputValue()   F

Complexity

Conditions 39
Paths 160

Size

Total Lines 125
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 89
c 1
b 0
f 0
dl 0
loc 125
rs 3.6666
cc 39
nc 160
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-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 YogurtField 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 boolean and string; 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 'timezone':
191
                $element = new XoopsFormSelectTimezone($caption, $name, $value);
192
                $element->setExtra("style='width: 280px;'");
193
                break;
194
195
            case 'rank':
196
                $element = new XoopsFormSelect($caption, $name, $value);
197
198
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
199
                $ranks = XoopsLists::getUserRankList();
200
                $element->addOption(0, '--------------');
201
                $element->addOptionArray($ranks);
202
                break;
203
204
            case 'theme':
205
                $element = new XoopsFormSelectTheme($caption, $name, $value, 1, true);
206
                break;
207
        }
208
        if ($this->getVar('field_description') != '') {
209
            $element->setDescription($this->getVar('field_description'));
210
        }
211
212
        return $element;
213
    }
214
215
    /**
216
     * Returns a value for output of this field
217
     *
218
     * @param XoopsUser      $user    {@link XoopsUser} object to get the value of
219
     * @param profileProfile $profile object to get the value of
220
     *
221
     * @return mixed
222
     **/
223
    public function getOutputValue(&$user, $profile)
224
    {
225
        if (file_exists($file = $GLOBALS['xoops']->path('modules/yogurt/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) {
226
            include_once $file;
227
        } else {
228
            include_once $GLOBALS['xoops']->path('modules/yogurt/language/english/modinfo.php');
229
        }
230
231
        $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
232
233
        switch ($this->getVar('field_type')) {
234
            default:
235
            case 'textbox':
236
                $value = is_array($value) ? $value[0] : $value;
237
                if ($this->getVar('field_name') === 'url' && $value !== '') {
238
                    return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>';
239
                } else {
240
                    return $value;
241
                }
242
                break;
243
            case 'textarea':
244
            case 'dhtml':
245
            case 'theme':
246
            case 'language':
247
                return $value;
248
                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...
249
250
            case 'select':
251
            case 'radio':
252
                $value = is_array($value) ? $value[0] : $value;
253
                $options = $this->getVar('field_options');
254
                if (isset($options[$value])) {
255
                    $value = htmlspecialchars(defined($options[$value]) ? constant($options[$value]) : $options[$value]);
256
                } else {
257
                    $value = '';
258
                }
259
260
                return $value;
261
                break;
262
263
            case 'select_multi':
264
            case 'checkbox':
265
                $options = $this->getVar('field_options');
266
                $ret     = array();
267
                if (count($options) > 0) {
268
                    foreach (array_keys($options) as $key) {
269
                        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

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

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