forked from XOOPS/XoopsCore
/
field.php
634 lines (564 loc) · 23.1 KB
/
field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<?php
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
use Xoops\Core\Database\Connection;
//use Xoops\Core\Kernel\XoopsObject;
//use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
/**
* Extended User Profile
*
* @copyright XOOPS Project (http://xoops.org)
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
* @package profile
* @since 2.3.0
* @author Jan Pedersen
* @author Taiwen Jiang <phppp@users.sourceforge.net>
* @version $Id$
*/
/**
* @package kernel
* @copyright copyright © 2000 XOOPS.org
*/
class ProfileField extends XoopsObject
{
/**
* Constructor
*/
public function __construct()
{
$this->initVar('field_id', XOBJ_DTYPE_INT, null);
$this->initVar('cat_id', XOBJ_DTYPE_INT, null, true);
$this->initVar('field_type', XOBJ_DTYPE_TXTBOX);
$this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true);
$this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true);
$this->initVar('field_title', XOBJ_DTYPE_TXTBOX);
$this->initVar('field_description', XOBJ_DTYPE_TXTAREA);
$this->initVar('field_required', XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes
$this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0);
$this->initVar('field_weight', XOBJ_DTYPE_INT, 0);
$this->initVar('field_default', XOBJ_DTYPE_TXTAREA, "");
$this->initVar('field_notnull', XOBJ_DTYPE_INT, 1);
$this->initVar('field_edit', XOBJ_DTYPE_INT, 0);
$this->initVar('field_show', XOBJ_DTYPE_INT, 0);
$this->initVar('field_config', XOBJ_DTYPE_INT, 0);
$this->initVar('field_options', XOBJ_DTYPE_ARRAY, array());
$this->initVar('step_id', XOBJ_DTYPE_INT, 0);
}
/**
* Extra treatment dealing with non latin encoding
* Tricky solution
*
* @param string $key
* @param mixed $value
* @param bool $not_gpc
* @return void
*/
public function setVar($key, $value, $not_gpc = false)
{
if ($key == 'field_options' && is_array($value)) {
foreach (array_keys($value) as $idx) {
$value[$idx] = base64_encode($value[$idx]);
}
}
parent::setVar($key, $value, $not_gpc);
}
/**
* @param string $key
* @param string $format
* @return array|mixed
*/
public function getVar($key, $format = 's')
{
$value = parent::getVar($key, $format);
if ($key == 'field_options' && !empty($value)) {
foreach (array_keys($value) as $idx) {
$value[$idx] = base64_decode($value[$idx]);
}
}
return $value;
}
/**
* Returns a {@link Xoops\Form\Element} for editing the value of this field
*
* @param XoopsUser $user {@link XoopsUser} object to edit the value of
* @param ProfileProfile $profile {@link ProfileProfile} object to edit the value of
*
* @return Xoops\Form\Element
**/
public function getEditElement(XoopsUser $user, ProfileProfile $profile)
{
$xoops = Xoops::getInstance();
$value = in_array($this->getVar('field_name'), $this->getUserVars())
? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
$caption = $this->getVar('field_title');
$caption = defined($caption) ? constant($caption) : $caption;
$name = $this->getVar('field_name', 'e');
$options = $this->getVar('field_options');
if (is_array($options)) {
//asort($options);
foreach (array_keys($options) as $key) {
$optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
$optkey = defined($key) ? constant($key) : $key;
unset($options[$key]);
$options[$optkey] = $optval;
}
}
switch ($this->getVar('field_type')) {
default:
case "autotext":
//autotext is not for editing
$element = new Xoops\Form\Label($caption, $this->getOutputValue($user, $profile));
break;
case "textbox":
$element = new Xoops\Form\Text($caption, $name, 35, $this->getVar('field_maxlength'), $value);
break;
case "textarea":
$element = new Xoops\Form\TextArea($caption, $name, $value, 4, 30);
break;
case "dhtml":
$element = new Xoops\Form\DhtmlTextArea($caption, $name, $value, 10, 30);
break;
case "select":
$element = new Xoops\Form\Select($caption, $name, $value);
// If options do not include an empty element, then add a blank option to prevent any default selection
if (!in_array('', array_keys($options))) {
$element->addOption('', XoopsLocale::NONE);
$eltmsg = empty($caption) ? sprintf(XoopsLocale::F_ENTER, $name) : sprintf(XoopsLocale::F_ENTER, $caption);
$eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
$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; }";
}
$element->addOptionArray($options);
break;
case "select_multi":
$element = new Xoops\Form\Select($caption, $name, $value, 5, true);
$element->addOptionArray($options);
break;
case "radio":
$element = new Xoops\Form\Radio($caption, $name, $value);
$element->addOptionArray($options);
break;
case "checkbox":
$element = new Xoops\Form\Checkbox($caption, $name, $value);
$element->addOptionArray($options);
break;
case "yesno":
$element = new Xoops\Form\RadioYesNo($caption, $name, $value);
break;
case "group":
$element = new Xoops\Form\SelectGroup($caption, $name, true, $value);
break;
case "group_multi":
$element = new Xoops\Form\SelectGroup($caption, $name, true, $value, 5, true);
break;
case "language":
$element = new Xoops\Form\SelectLanguage($caption, $name, $value);
break;
case "date":
$element = new Xoops\Form\DateSelect($caption, $name, 15, $value);
break;
case "longdate":
$element = new Xoops\Form\DateSelect($caption, $name, 15, str_replace("-", "/", $value));
break;
case "datetime":
$element = new Xoops\Form\DateTime($caption, $name, 15, $value);
break;
case "timezone":
$element = new Xoops\Form\SelectTimeZone($caption, $name, $value);
$element->setExtra("style='width: 280px;'");
break;
case "rank":
if ($xoops->isActiveModule('userrank')) {
$element = new Xoops\Form\Select($caption, $name, $value);
$ranks = XoopsLists::getUserRankList();
$element->addOption(0, "--------------");
$element->addOptionArray($ranks);
} else {
$element = new Xoops\Form\Hidden($name, $value);
}
break;
case 'theme':
$element = new Xoops\Form\Select($caption, $name, $value);
$element->addOption("0", _PROFILE_MA_SITEDEFAULT);
$handle = opendir(\XoopsBaseConfig::get('themes-path') . '/');
$dirlist = array();
while (false !== ($file = readdir($handle))) {
if (is_dir(\XoopsBaseConfig::get('themes-path') . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) != 'cvs') {
if (XoopsLoad::fileExists(\XoopsBaseConfig::get('themes-path') . "/" . $file . "/theme.html") && in_array($file, $xoops->getConfig('theme_set_allowed'))) {
$dirlist[$file] = $file;
}
}
}
closedir($handle);
if (!empty($dirlist)) {
asort($dirlist);
$element->addOptionArray($dirlist);
}
break;
}
if ($this->getVar('field_description') != "") {
$element->setDescription($this->getVar('field_description'));
}
return $element;
}
/**
* Returns a value for output of this field
*
* @param XoopsUser $user {@link XoopsUser} object to get the value of
* @param profileProfile $profile object to get the value of
*
* @return string
**/
public function getOutputValue(XoopsUser &$user, ProfileProfile $profile)
{
$xoops = Xoops::getInstance();
$xoops->loadLanguage('modinfo', 'profile');
$value = in_array($this->getVar('field_name'), $this->getUserVars())
? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
switch ($this->getVar('field_type')) {
default:
case "textbox":
if ($this->getVar('field_name') == 'url' && $value != '') {
return '<a href="' . $xoops->formatURL($value) . '" rel="external">' . $value . '</a>';
} else {
return $value;
}
break;
case "textarea":
case "dhtml":
case 'theme':
case "language":
case "list":
return $value;
break;
case "select":
case "radio":
$options = $this->getVar('field_options');
if (isset($options[$value])) {
$value = htmlspecialchars(defined($options[$value]) ? constant($options[$value])
: $options[$value]);
} else {
$value = "";
}
return $value;
break;
case "select_multi":
case "checkbox":
$options = $this->getVar('field_options');
$ret = array();
if (count($options) > 0) {
foreach (array_keys($options) as $key) {
if (in_array($key, $value)) {
$ret[$key] = htmlspecialchars(defined($options[$key]) ? constant($options[$key])
: $options[$key]);
}
}
}
return $ret;
break;
case "group":
$member_handler = $xoops->getHandlerMember();
$options = $member_handler->getGroupList();
$ret = isset($options[$value]) ? $options[$value] : '';
return $ret;
break;
case "group_multi":
$member_handler = $xoops->getHandlerMember();
$options = $member_handler->getGroupList();
$ret = array();
foreach (array_keys($options) as $key) {
if (in_array($key, $value)) {
$ret[$key] = htmlspecialchars($options[$key]);
}
}
return $ret;
break;
case "longdate":
//return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
//when we cannot convert it to a UNIX timestamp?
return str_replace("-", "/", $value);
case "date":
return XoopsLocale::formatTimestamp($value, 's');
break;
case "datetime":
if (!empty($value)) {
return XoopsLocale::formatTimestamp($value, 'm');
} else {
return _PROFILE_MI_NEVER_LOGGED_IN;
}
break;
case "autotext":
$value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it
$value = str_replace("{X_UID}", $user->getVar("uid"), $value);
$value = str_replace("{X_URL}", \XoopsBaseConfig::get('url'), $value);
$value = str_replace("{X_UNAME}", $user->getVar("uname"), $value);
return $value;
break;
case "rank":
$userrank = $user->rank();
$user_rankimage = "";
if (isset($userrank['image']) && $userrank['image'] != "") {
$user_rankimage = '<img src="' . \XoopsBaseConfig::get('uploads-url') . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br />';
}
return $user_rankimage . $userrank['title'];
break;
case "yesno":
return $value ? XoopsLocale::YES : XoopsLocale::NO;
break;
case "timezone":
$timezones = XoopsLists::getTimeZoneList();
$value = empty($value) ? "0" : (string)($value);
return $timezones[str_replace('.0', '', $value)];
break;
}
}
/**
* Returns a value ready to be saved in the database
*
* @param mixed $value Value to format
*
* @return mixed
*/
public function getValueForSave($value)
{
switch ($this->getVar('field_type')) {
default:
case "textbox":
case "textarea":
case "dhtml":
case "yesno":
case "timezone":
case 'theme':
case "language":
case "list":
case "select":
case "radio":
case "select_multi":
case "group":
case "group_multi":
case "longdate":
return $value;
case "checkbox":
return (array)$value;
case "date":
if ($value != "") {
return strtotime($value);
}
return $value;
break;
case "datetime":
if (!empty($value)) {
return strtotime($value['date']) + (int)($value['time']);
}
return $value;
break;
}
}
/**
* Get names of user variables
*
* @return array
*/
public function getUserVars()
{
$xoops = Xoops::getInstance();
/* @var $profile_handler ProfileProfileHandler */
$profile_handler = $xoops->getModuleHandler('profile', 'profile');
return $profile_handler->getUserVars();
}
}
/**
* @package kernel
* @copyright copyright © 2000 XOOPS.org
*/
class ProfileFieldHandler extends XoopsPersistableObjectHandler
{
/**
* @param null|Connection $db database
*/
public function __construct(Connection $db = null)
{
parent::__construct($db, 'profile_field', "profilefield", "field_id", 'field_title');
}
/**
* Read field information from cached storage
*
* @param bool $force_update read fields from database and not cached storage
*
* @return array
*/
public function loadFields($force_update = false)
{
static $fields = array();
if (!empty($force_update) || count($fields) == 0) {
$this->table_link = $this->db->prefix('profile_category');
$criteria = new Criteria('o.field_id', 0, "!=");
$criteria->setSort('l.cat_weight ASC, o.field_weight');
$field_objs = $this->getByLink($criteria, array('o.*'), true, 'cat_id', 'cat_id');
/* @var ProfileField $field */
foreach ($field_objs as $field) {
$fields[$field->getVar('field_name')] = $field;
}
}
return $fields;
}
/**
* save a profile field in the database
*
* @param XoopsObject|ProfileField $obj reference to the object
* @param bool $force whether to force the query execution despite security settings
* @return bool FALSE if failed, TRUE if already present and unchanged or successful
*/
public function insertFields(XoopsObject $obj, $force = false)
{
$xoops = Xoops::getInstance();
$profile_handler = $xoops->getModuleHandler('profile', 'profile');
$obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
$obj->cleanVars(false); //Don't quote
switch ($obj->getVar('field_type')) {
case "datetime":
case "date":
$obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
$obj->setVar('field_maxlength', 10);
break;
case "longdate":
$obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME);
break;
case "yesno":
$obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
$obj->setVar('field_maxlength', 1);
break;
case "textbox":
if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
$obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
}
break;
case "autotext":
if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
$obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
}
break;
case "group_multi":
case "select_multi":
case "checkbox":
$obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY);
break;
case "language":
case "timezone":
case "theme":
$obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
break;
case "dhtml":
case "textarea":
$obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
break;
}
if ($obj->getVar('field_valuetype') == "") {
$obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
}
if (!in_array($obj->getVar('field_name'), $this->getUserVars())) {
if ($obj->isNew()) {
//add column to table
$changetype = "ADD";
} else {
//update column information
$changetype = "CHANGE `" . $obj->getVar('field_name', 'n') . "`";
}
$maxlengthstring = $obj->getVar('field_maxlength') > 0 ? "(" . $obj->getVar('field_maxlength') . ")" : "";
//set type
switch ($obj->getVar('field_valuetype')) {
default:
case XOBJ_DTYPE_ARRAY:
case XOBJ_DTYPE_EMAIL:
case XOBJ_DTYPE_TXTBOX:
case XOBJ_DTYPE_URL:
$type = "varchar";
// varchars must have a maxlength
if (!$maxlengthstring) {
//so set it to max if maxlength is not set - or should it fail?
$maxlengthstring = "(255)";
$obj->setVar('field_maxlength', 255);
}
break;
case XOBJ_DTYPE_INT:
$type = "int";
break;
case XOBJ_DTYPE_DECIMAL:
$type = "decimal(14,6)";
break;
case XOBJ_DTYPE_FLOAT:
$type = "float(15,9)";
break;
case XOBJ_DTYPE_OTHER:
case XOBJ_DTYPE_TXTAREA:
$type = "text";
$maxlengthstring = "";
break;
case XOBJ_DTYPE_MTIME:
$type = "date";
$maxlengthstring = "";
break;
}
$sql = "ALTER TABLE `" . $profile_handler->table . "` " . $changetype . " `" . $obj->cleanVars['field_name'] . "` " . $type . $maxlengthstring . ' NULL';
if (!$this->db->query($sql)) {
return false;
}
}
//change this to also update the cached field information storage
$obj->setDirty();
if (!parent::insert($obj, $force)) {
return false;
}
return $obj->getVar('field_id');
}
/**
* delete a profile field from the database
*
* @param XoopsObject|ProfileField $obj reference to the object to delete
* @param bool $force
* @return bool FALSE if failed.
**/
public function deleteFields(XoopsObject $obj, $force = false)
{
$xoops = Xoops::getInstance();
$profile_handler = $xoops->getModuleHandler('profile', 'profile');
// remove column from table
$sql = "ALTER TABLE " . $profile_handler->table . " DROP `" . $obj->getVar('field_name', 'n') . "`";
if ($this->db->query($sql)) {
//change this to update the cached field information storage
if (!parent::delete($obj, $force)) {
return false;
}
if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
$profile_module = $xoops->getModuleByDirname('profile');
if (is_object($profile_module)) {
// Remove group permissions
$groupperm_handler = $xoops->getHandlerGroupperm();
$criteria = new CriteriaCompo(new Criteria('gperm_modid', $profile_module->getVar('mid')));
$criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
return $groupperm_handler->deleteAll($criteria);
}
}
}
return false;
}
/**
* Get array of standard variable names (user table)
*
* @return string[]
*/
public function getUserVars()
{
return array(
'uid', 'uname', 'name', 'email', 'url', 'user_avatar', 'user_regdate', 'user_icq', 'user_from', 'user_sig',
'user_viewemail', 'actkey', 'user_aim', 'user_yim', 'user_msnm', 'pass', 'posts', 'attachsig', 'rank',
'level', 'theme', 'timezone_offset', 'last_login', 'umode', 'uorder', 'notify_method', 'notify_mode',
'user_occ', 'bio', 'user_intrest', 'user_mailok'
);
}
}