Passed
Push — master ( 4761c2...696f77 )
by
unknown
05:50 queued 19s
created

class/Field.php (2 issues)

Labels
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
6
7
/**
8
 * Extended User Profile
9
 *
10
 * You may not change or alter any portion of this comment or credits
11
 * of supporting developers from this source code or any supporting source code
12
 * which is considered copyrighted (c) material of the original comment or credit authors.
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
18
 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @package             profile
20
 * @since               2.3.0
21
 * @author              Jan Pedersen
22
 * @author              Taiwen Jiang <[email protected]>
23
 */
24
// defined('XOOPS_ROOT_PATH') || exit("XOOPS root path not defined");
25
26
/**
27
 * @package             kernel
28
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
29
 */
30
class Field extends \XoopsObject
31
{
32
    public function __construct()
33
    {
34
        $this->initVar('field_id', \XOBJ_DTYPE_INT, null);
35
        $this->initVar('cat_id', \XOBJ_DTYPE_INT, null, true);
36
        $this->initVar('field_type', \XOBJ_DTYPE_TXTBOX);
37
        $this->initVar('field_valuetype', \XOBJ_DTYPE_INT, null, true);
38
        $this->initVar('field_name', \XOBJ_DTYPE_TXTBOX, null, true);
39
        $this->initVar('field_title', \XOBJ_DTYPE_TXTBOX);
40
        $this->initVar('field_description', \XOBJ_DTYPE_TXTAREA);
41
        $this->initVar('field_required', \XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes
42
        $this->initVar('field_maxlength', \XOBJ_DTYPE_INT, 0);
43
        $this->initVar('field_weight', \XOBJ_DTYPE_INT, 0);
44
        $this->initVar('field_default', \XOBJ_DTYPE_TXTAREA, '');
45
        $this->initVar('field_notnull', \XOBJ_DTYPE_INT, 1);
46
        $this->initVar('field_edit', \XOBJ_DTYPE_INT, 0);
47
        $this->initVar('field_show', \XOBJ_DTYPE_INT, 0);
48
        $this->initVar('field_config', \XOBJ_DTYPE_INT, 0);
49
        $this->initVar('field_options', \XOBJ_DTYPE_ARRAY, []);
50
        $this->initVar('step_id', \XOBJ_DTYPE_INT, 0);
51
    }
52
53
    /**
54
     * Extra treatment dealing with non latin encoding
55
     * Tricky solution
56
     * @param string $key
57
     * @param mixed  $value
58
     * @param bool   $not_gpc
59
     */
60
    public function setVar($key, $value, $not_gpc = false)
61
    {
62
        if ('field_options' === $key && \is_array($value)) {
63
            foreach (\array_keys($value) as $idx) {
64
                $value[$idx] = \base64_encode($value[$idx]);
65
            }
66
        }
67
        parent::setVar($key, $value, $not_gpc);
68
    }
69
70
    /**
71
     * @param string $key
72
     * @param string $format
73
     *
74
     * @return mixed
75
     */
76
    public function getVar($key, $format = 's')
77
    {
78
        $value = parent::getVar($key, $format);
79
        if ('field_options' === $key && !empty($value)) {
80
            foreach (\array_keys($value) as $idx) {
81
                $value[$idx] = \base64_decode($value[$idx]);
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 Profile    $profile {@link Profile} object to edit the value of
92
     *
93
     * @return \XoopsFormCheckBox|\XoopsFormDatetime|\XoopsFormDhtmlTextArea|\XoopsFormLabel|\XoopsFormRadio|\XoopsFormRadioYN|\XoopsFormSelect|\XoopsFormSelectGroup|\XoopsFormSelectLang|\XoopsFormSelectTheme|\XoopsFormSelectTimezone|\XoopsFormText|\XoopsFormTextArea|\XoopsFormTextDateSelect
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
        $caption = $this->getVar('field_title');
99
        $caption = \defined($caption) ? \constant($caption) : $caption;
100
        $name    = $this->getVar('field_name', 'e');
101
        $options = $this->getVar('field_options');
102
        if (\is_array($options)) {
103
            //asort($options);
104
            foreach (\array_keys($options) as $key) {
105
                $optval = \defined($options[$key]) ? \constant($options[$key]) : $options[$key];
106
                $optkey = \defined((string)$key) ? \constant($key) : $key;
107
                unset($options[$key]);
108
                $options[$optkey] = $optval;
109
            }
110
        }
111
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
112
        switch ($this->getVar('field_type')) {
113
            default:
114
            case 'autotext':
115
                //autotext is not for editing
116
                $element = new \XoopsFormLabel($caption, $this->getOutputValue($user, $profile));
117
                break;
118
            case 'textbox':
119
                $element = new \XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
120
                break;
121
            case 'textarea':
122
                $element = new \XoopsFormTextArea($caption, $name, $value, 4, 30);
123
                break;
124
            case 'dhtml':
125
                $element = new \XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30);
126
                break;
127
            case 'select':
128
                $element = new \XoopsFormSelect($caption, $name, $value);
129
                // If options do not include an empty element, then add a blank option to prevent any default selection
130
                //                if (!in_array('', array_keys($options))) {
131
                if (!\array_key_exists('', $options)) {
132
                    $element->addOption('', \_NONE);
133
                    $eltmsg                          = empty($caption) ? \sprintf(\_FORM_ENTER, $name) : \sprintf(\_FORM_ENTER, $caption);
134
                    $eltmsg                          = \str_replace('"', '\"', \stripslashes($eltmsg));
135
                    $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};"
136
                                                       . "for (i = 0; i < selectBox.options.length; i++) { if (selectBox.options[i].selected == true && selectBox.options[i].value != '') { hasSelected = true; break; } }"
137
                                                       . "if (!hasSelected) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
138
                }
139
                $element->addOptionArray($options);
140
                break;
141
            case 'select_multi':
142
                $element = new \XoopsFormSelect($caption, $name, $value, 5, true);
143
                $element->addOptionArray($options);
144
                break;
145
            case 'radio':
146
                $element = new \XoopsFormRadio($caption, $name, $value);
147
                $element->addOptionArray($options);
148
                break;
149
            case 'checkbox':
150
                $element = new \XoopsFormCheckBox($caption, $name, $value);
151
                $element->addOptionArray($options);
152
                break;
153
            case 'yesno':
154
                $element = new \XoopsFormRadioYN($caption, $name, $value);
155
                break;
156
            case 'group':
157
                $element = new \XoopsFormSelectGroup($caption, $name, true, $value);
158
                break;
159
            case 'group_multi':
160
                $element = new \XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
161
                break;
162
            case 'language':
163
                $element = new \XoopsFormSelectLang($caption, $name, $value);
164
                break;
165
            case 'date':
166
                $element = new \XoopsFormTextDateSelect($caption, $name, 15, $value);
167
                break;
168
            case 'longdate':
169
                $element = new \XoopsFormTextDateSelect($caption, $name, 15, \str_replace('-', '/', $value));
170
                break;
171
            case 'datetime':
172
                $element = new \XoopsFormDatetime($caption, $name, 15, $value);
173
                break;
174
            case 'timezone':
175
                $element = new \XoopsFormSelectTimezone($caption, $name, $value);
176
                $element->setExtra("style='width: 280px;'");
177
                break;
178
            case 'rank':
179
                $element = new \XoopsFormSelect($caption, $name, $value);
180
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
181
                $ranks = \XoopsLists::getUserRankList();
182
                $element->addOption(0, '--------------');
183
                $element->addOptionArray($ranks);
184
                break;
185
            case 'theme':
186
                $element = new \XoopsFormSelectTheme($caption, $name, $value, 1, true);
187
                break;
188
        }
189
        if ('' != $this->getVar('field_description')) {
190
            $element->setDescription($this->getVar('field_description'));
191
        }
192
        return $element;
193
    }
194
195
    /**
196
     * Returns a value for output of this field
197
     *
198
     * @param XoopsUser      $user    {@link XoopsUser} object to get the value of
0 ignored issues
show
The type XoopsModules\Suico\XoopsUser was not found. Did you mean XoopsUser? If so, make sure to prefix the type with \.
Loading history...
199
     * @param profileProfile $profile object to get the value of
0 ignored issues
show
The type XoopsModules\Suico\profileProfile was not found. Did you mean profileProfile? If so, make sure to prefix the type with \.
Loading history...
200
     *
201
     * @return mixed
202
     **/
203
    public function getOutputValue($user, $profile)
204
    {
205
        if (\file_exists($file = $GLOBALS['xoops']->path('modules/suico/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) {
206
            include_once $file;
207
        } else {
208
            include_once $GLOBALS['xoops']->path('modules/suico/language/english/modinfo.php');
209
        }
210
        $value = \in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
211
        switch ($this->getVar('field_type')) {
212
            default:
213
            case 'textbox':
214
                $value = \is_array($value) ? $value[0] : $value;
215
                if ('url' === $this->getVar('field_name') && '' !== $value) {
216
                    return '<a href="' . \formatURL($value) . '" rel="external">' . $value . '</a>';
217
                }
218
                return $value;
219
                break;
220
            case 'textarea':
221
            case 'dhtml':
222
            case 'theme':
223
            case 'language':
224
                return $value;
225
                break;
226
            case 'select':
227
            case 'radio':
228
                $value   = \is_array($value) ? $value[0] : $value;
229
                $options = $this->getVar('field_options');
230
                if (isset($options[$value])) {
231
                    $value = \htmlspecialchars(\defined($options[$value]) ? \constant($options[$value]) : $options[$value]);
232
                } else {
233
                    $value = '';
234
                }
235
                return $value;
236
                break;
237
            case 'select_multi':
238
            case 'checkbox':
239
                $options = $this->getVar('field_options');
240
                $ret     = [];
241
                if (\count($options) > 0) {
242
                    foreach (\array_keys($options) as $key) {
243
                        if (\in_array($key, $value)) {
244
                            $ret[$key] = \htmlspecialchars(\defined($options[$key]) ? \constant($options[$key]) : $options[$key]);
245
                        }
246
                    }
247
                }
248
                return $ret;
249
                break;
250
            case 'group':
251
                /* @var XoopsMemberHandler $memberHandler */ $memberHandler = \xoops_getHandler('member');
252
                $options                                                    = $memberHandler->getGroupList();
253
                $ret                                                        = $options[$value] ?? '';
254
                return $ret;
255
                break;
256
            case 'group_multi':
257
                /* @var XoopsMemberHandler $memberHandler */ $memberHandler = \xoops_getHandler('member');
258
                $options                                                    = $memberHandler->getGroupList();
259
                $ret                                                        = [];
260
                foreach (\array_keys($options) as $key) {
261
                    if (\in_array($key, $value)) {
262
                        $ret[$key] = \htmlspecialchars($options[$key]);
263
                    }
264
                }
265
                return $ret;
266
                break;
267
            case 'longdate':
268
                //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
269
                //when we cannot convert it to a UNIX timestamp?
270
                return \str_replace('-', '/', $value);
271
            case 'date':
272
                return \formatTimestamp($value, 's');
273
                break;
274
            case 'datetime':
275
                if (!empty($value)) {
276
                    return \formatTimestamp($value, 'm');
277
                }
278
                return $value = _MI_SUICO_NEVER_LOGGED_IN;
279
                break;
280
            case 'autotext':
281
                $value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it
282
                $value = \str_replace('{X_UID}', $user->getVar('uid'), $value);
283
                $value = \str_replace('{X_URL}', XOOPS_URL, $value);
284
                $value = \str_replace('{X_UNAME}', $user->getVar('uname'), $value);
285
                return $value;
286
                break;
287
            case 'rank':
288
                $userrank       = $user->rank();
289
                $user_rankimage = '';
290
                if (isset($userrank['image']) && '' !== $userrank['image']) {
291
                    $user_rankimage = '<img src="' . \XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /> ';
292
                }
293
                return $user_rankimage . $userrank['title'];
294
                break;
295
            case 'yesno':
296
                return $value ? \_YES : \_NO;
297
                break;
298
            case 'timezone':
299
                include_once $GLOBALS['xoops']->path('class/xoopslists.php');
300
                $timezones = \XoopsLists::getTimeZoneList();
301
                $value     = empty($value) ? '0' : (string)$value;
302
                return $timezones[\str_replace('.0', '', $value)];
303
                break;
304
        }
305
    }
306
307
    /**
308
     * Returns a value ready to be saved in the database
309
     *
310
     * @param mixed $value Value to format
311
     *
312
     * @return mixed
313
     */
314
    public function getValueForSave($value)
315
    {
316
        switch ($this->getVar('field_type')) {
317
            default:
318
            case 'textbox':
319
            case 'textarea':
320
            case 'dhtml':
321
            case 'yesno':
322
            case 'timezone':
323
            case 'theme':
324
            case 'language':
325
            case 'select':
326
            case 'radio':
327
            case 'select_multi':
328
            case 'group':
329
            case 'group_multi':
330
            case 'longdate':
331
                return $value;
332
            case 'checkbox':
333
                return (array)$value;
334
            case 'date':
335
                if ('' !== $value) {
336
                    return \strtotime($value);
337
                }
338
                return $value;
339
                break;
340
            case 'datetime':
341
                if (!empty($value)) {
342
                    return \strtotime($value['date']) + (int)$value['time'];
343
                }
344
                return $value;
345
                break;
346
        }
347
    }
348
349
    /**
350
     * Get names of user variables
351
     *
352
     * @return array
353
     */
354
    public function getUserVars()
355
    {
356
        /* @var Suico\ProfileHandler $profileHandler */
357
        $helper         = \XoopsModules\Suico\Helper::getInstance();
358
        $profileHandler = $helper->getHandler('Profile');
359
        return $profileHandler->getUserVars();
360
    }
361
}
362