1: <?php
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: class ProfileField extends XoopsObject
27: {
28: 29: 30:
31: public function __construct()
32: {
33: $this->initVar('field_id', XOBJ_DTYPE_INT, null);
34: $this->initVar('cat_id', XOBJ_DTYPE_INT, null, true);
35: $this->initVar('field_type', XOBJ_DTYPE_TXTBOX);
36: $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true);
37: $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true);
38: $this->initVar('field_title', XOBJ_DTYPE_TXTBOX);
39: $this->initVar('field_description', XOBJ_DTYPE_TXTAREA);
40: $this->initVar('field_required', XOBJ_DTYPE_INT, 0);
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: 54: 55: 56: 57: 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: 71: 72: 73: 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) {
80: $value[$idx] = base64_decode($value[$idx]);
81: }
82: }
83:
84: return $value;
85: }
86:
87: 88: 89: 90: 91: 92: 93: 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:
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:
118: $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile));
119: break;
120:
121: case 'textbox':
122: $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
123: break;
124:
125: case 'textarea':
126: $element = new XoopsFormTextArea($caption, $name, $value, 4, 30);
127: break;
128:
129: case 'dhtml':
130: $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30);
131: break;
132:
133: case 'select':
134: $element = new XoopsFormSelect($caption, $name, $value);
135:
136:
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);
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);
164: break;
165:
166: case 'group':
167: $element = new XoopsFormSelectGroup($caption, $name, true, $value);
168: break;
169:
170: case 'group_multi':
171: $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
172: break;
173:
174: case 'language':
175: $element = new XoopsFormSelectLang($caption, $name, $value);
176: break;
177:
178: case 'date':
179: $element = new XoopsFormTextDateSelect($caption, $name, 15, $value);
180: break;
181:
182: case 'longdate':
183: $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace('-', '/', $value));
184: break;
185:
186: case 'datetime':
187: $element = new XoopsFormDatetime($caption, $name, 15, $value);
188: break;
189:
190: case 'list':
191: $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]);
192: break;
193:
194: case 'timezone':
195: $element = new XoopsFormSelectTimezone($caption, $name, $value);
196: $element->setExtra("style='width: 280px;'");
197: break;
198:
199: case 'rank':
200: $element = new XoopsFormSelect($caption, $name, $value);
201:
202: include_once $GLOBALS['xoops']->path('class/xoopslists.php');
203: $ranks = XoopsLists::getUserRankList();
204: $element->addOption(0, '--------------');
205: $element->addOptionArray($ranks);
206: break;
207:
208: case 'theme':
209: $element = new XoopsFormSelectTheme($caption, $name, $value, 1, true);
210: break;
211: }
212: if ($this->getVar('field_description') != '') {
213: $element->setDescription($this->getVar('field_description'));
214: }
215:
216: return $element;
217: }
218:
219: 220: 221: 222: 223: 224: 225: 226:
227: public function getOutputValue(&$user, $profile)
228: {
229: if (file_exists($file = $GLOBALS['xoops']->path('modules/profile/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) {
230: include_once $file;
231: } else {
232: include_once $GLOBALS['xoops']->path('modules/profile/language/english/modinfo.php');
233: }
234:
235: $value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
236:
237: switch ($this->getVar('field_type')) {
238: default:
239: case 'textbox':
240: $value = is_array($value) ? $value[0] : $value;
241: if ($this->getVar('field_name') === 'url' && $value !== '') {
242: return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>';
243: } else {
244: return $value;
245: }
246: break;
247: case 'textarea':
248: case 'dhtml':
249: case 'theme':
250: case 'language':
251: case 'list':
252: return $value;
253: break;
254:
255: case 'select':
256: case 'radio':
257: $value = is_array($value) ? $value[0] : $value;
258: $options = $this->getVar('field_options');
259: if (isset($options[$value])) {
260: $value = htmlspecialchars(defined($options[$value]) ? constant($options[$value]) : $options[$value]);
261: } else {
262: $value = '';
263: }
264:
265: return $value;
266: break;
267:
268: case 'select_multi':
269: case 'checkbox':
270: $options = $this->getVar('field_options');
271: $ret = array();
272: if (count($options) > 0) {
273: foreach (array_keys($options) as $key) {
274: if (in_array($key, $value)) {
275: $ret[$key] = htmlspecialchars(defined($options[$key]) ? constant($options[$key]) : $options[$key]);
276: }
277: }
278: }
279:
280: return $ret;
281: break;
282:
283: case 'group':
284:
285: $member_handler = xoops_getHandler('member');
286: $options = $member_handler->getGroupList();
287: $ret = isset($options[$value]) ? $options[$value] : '';
288:
289: return $ret;
290: break;
291:
292: case 'group_multi':
293:
294: $member_handler = xoops_getHandler('member');
295: $options = $member_handler->getGroupList();
296: $ret = array();
297: foreach (array_keys($options) as $key) {
298: if (in_array($key, $value)) {
299: $ret[$key] = htmlspecialchars($options[$key]);
300: }
301: }
302:
303: return $ret;
304: break;
305:
306: case 'longdate':
307:
308:
309: return str_replace('-', '/', $value);
310:
311: case 'date':
312: return formatTimestamp($value, 's');
313: break;
314:
315: case 'datetime':
316: if (!empty($value)) {
317: return formatTimestamp($value, 'm');
318: } else {
319: return $value = _PROFILE_MI_NEVER_LOGGED_IN;
320: }
321: break;
322:
323: case 'autotext':
324: $value = $user->getVar($this->getVar('field_name'), 'n');
325: $value = str_replace('{X_UID}', $user->getVar('uid'), $value);
326: $value = str_replace('{X_URL}', XOOPS_URL, $value);
327: $value = str_replace('{X_UNAME}', $user->getVar('uname'), $value);
328:
329: return $value;
330: break;
331:
332: case 'rank':
333: $userrank = $user->rank();
334: $user_rankimage = '';
335: if (isset($userrank['image']) && $userrank['image'] !== '') {
336: $user_rankimage = '<img src="' . XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br>';
337: }
338:
339: return $user_rankimage . $userrank['title'];
340: break;
341:
342: case 'yesno':
343: return $value ? _YES : _NO;
344: break;
345:
346: case 'timezone':
347: include_once $GLOBALS['xoops']->path('class/xoopslists.php');
348: $timezones = XoopsLists::getTimeZoneList();
349: $value = empty($value) ? '0' : (string)$value;
350:
351: return $timezones[str_replace('.0', '', $value)];
352: break;
353: }
354: }
355:
356: 357: 358: 359: 360: 361: 362:
363: public function getValueForSave($value)
364: {
365: switch ($this->getVar('field_type')) {
366: default:
367: case 'textbox':
368: case 'textarea':
369: case 'dhtml':
370: case 'yesno':
371: case 'timezone':
372: case 'theme':
373: case 'language':
374: case 'list':
375: case 'select':
376: case 'radio':
377: case 'select_multi':
378: case 'group':
379: case 'group_multi':
380: case 'longdate':
381: return $value;
382:
383: case 'checkbox':
384: return (array)$value;
385:
386: case 'date':
387: if ($value !== '') {
388: return strtotime($value);
389: }
390:
391: return $value;
392: break;
393:
394: case 'datetime':
395: if (!empty($value)) {
396: return strtotime($value['date']) + (int)$value['time'];
397: }
398:
399: return $value;
400: break;
401: }
402: }
403:
404: 405: 406: 407: 408:
409: public function getUserVars()
410: {
411:
412: $profile_handler = xoops_getModuleHandler('profile', 'profile');
413:
414: return $profile_handler->getUserVars();
415: }
416: }
417:
418: 419: 420: 421:
422: class ProfileFieldHandler extends XoopsPersistableObjectHandler
423: {
424: 425: 426:
427: public function __construct(XoopsDatabase $db)
428: {
429: parent::__construct($db, 'profile_field', 'profilefield', 'field_id', 'field_title');
430: }
431:
432: 433: 434: 435: 436: 437: 438:
439: public function loadFields($force_update = false)
440: {
441: static $fields = array();
442: if (!empty($force_update) || count($fields) == 0) {
443: $this->table_link = $this->db->prefix('profile_category');
444: $criteria = new Criteria('o.field_id', 0, '!=');
445: $criteria->setSort('l.cat_weight ASC, o.field_weight');
446: $field_objs =& $this->getByLink($criteria, array('o.*'), true, 'cat_id', 'cat_id');
447: foreach (array_keys($field_objs) as $i) {
448: $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i];
449: }
450: }
451:
452: return $fields;
453: }
454:
455: 456: 457: 458: 459: 460: 461: 462: 463:
464: public function insert(XoopsObject $obj, $force = false)
465: {
466: if (!($obj instanceof $this->className)) {
467: return false;
468: }
469:
470: $profile_handler = xoops_getModuleHandler('profile', 'profile');
471: $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
472: $obj->cleanVars();
473: $defaultstring = '';
474: switch ($obj->getVar('field_type')) {
475: case 'datetime':
476: case 'date':
477: $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
478: $obj->setVar('field_maxlength', 10);
479: break;
480:
481: case 'longdate':
482: $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME);
483: break;
484:
485: case 'yesno':
486: $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
487: $obj->setVar('field_maxlength', 1);
488: break;
489:
490: case 'textbox':
491: if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
492: $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
493: }
494: break;
495:
496: case 'autotext':
497: if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
498: $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
499: }
500: break;
501:
502: case 'group_multi':
503: case 'select_multi':
504: case 'checkbox':
505: $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY);
506: break;
507:
508: case 'language':
509: case 'timezone':
510: case 'theme':
511: $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
512: break;
513:
514: case 'dhtml':
515: case 'textarea':
516: $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
517: break;
518: }
519:
520: if ($obj->getVar('field_valuetype') === '') {
521: $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
522: }
523:
524: if ((!in_array($obj->getVar('field_name'), $this->getUserVars())) && isset($_REQUEST['field_required'])) {
525: if ($obj->isNew()) {
526:
527: $changetype = 'ADD';
528: } else {
529:
530: $changetype = 'MODIFY COLUMN';
531: }
532: $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? '(' . $obj->getVar('field_maxlength') . ')' : '';
533:
534:
535: switch ($obj->getVar('field_valuetype')) {
536: default:
537: case XOBJ_DTYPE_ARRAY:
538: case XOBJ_DTYPE_UNICODE_ARRAY:
539: $type = 'mediumtext';
540: $maxlengthstring = '';
541: break;
542: case XOBJ_DTYPE_UNICODE_EMAIL:
543: case XOBJ_DTYPE_UNICODE_TXTBOX:
544: case XOBJ_DTYPE_UNICODE_URL:
545: case XOBJ_DTYPE_EMAIL:
546: case XOBJ_DTYPE_TXTBOX:
547: case XOBJ_DTYPE_URL:
548: $type = 'varchar';
549:
550: if (!$maxlengthstring) {
551:
552: $maxlengthstring = '(255)';
553: $obj->setVar('field_maxlength', 255);
554: }
555: break;
556:
557: case XOBJ_DTYPE_INT:
558: $type = 'int';
559: break;
560:
561: case XOBJ_DTYPE_DECIMAL:
562: $type = 'decimal(14,6)';
563: break;
564:
565: case XOBJ_DTYPE_FLOAT:
566: $type = 'float(15,9)';
567: break;
568:
569: case XOBJ_DTYPE_OTHER:
570: case XOBJ_DTYPE_UNICODE_TXTAREA:
571: case XOBJ_DTYPE_TXTAREA:
572: $type = 'text';
573: $maxlengthstring = '';
574: break;
575:
576: case XOBJ_DTYPE_MTIME:
577: $type = 'date';
578: $maxlengthstring = '';
579: break;
580: }
581:
582: $sql = 'ALTER TABLE `' . $profile_handler->table . '` ' . $changetype . ' `' . $obj->cleanVars['field_name'] . '` ' . $type . $maxlengthstring . ' NULL';
583: $result = $force ? $this->db->queryF($sql) : $this->db->query($sql);
584: if (!$result) {
585: $obj->setErrors($this->db->error());
586: return false;
587: }
588: }
589:
590:
591: $obj->setDirty();
592: if (!parent::insert($obj, $force)) {
593: return false;
594: }
595:
596: return $obj->getVar('field_id');
597: }
598:
599: 600: 601: 602: 603: 604: 605:
606: public function delete(XoopsObject $obj, $force = false)
607: {
608: if (!($obj instanceof $this->className)) {
609: return false;
610: }
611:
612: $profile_handler = xoops_getModuleHandler('profile', 'profile');
613:
614: $sql = 'ALTER TABLE ' . $profile_handler->table . ' DROP `' . $obj->getVar('field_name', 'n') . '`';
615: if ($this->db->query($sql)) {
616:
617: if (!parent::delete($obj, $force)) {
618: return false;
619: }
620:
621: if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
622:
623: $module_handler = xoops_getHandler('module');
624: $profile_module = $module_handler->getByDirname('profile');
625: if (is_object($profile_module)) {
626:
627:
628: $groupperm_handler = xoops_getHandler('groupperm');
629: $criteria = new CriteriaCompo(new Criteria('gperm_modid', $profile_module->getVar('mid')));
630: $criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
631:
632: return $groupperm_handler->deleteAll($criteria);
633: }
634: }
635: }
636:
637: return false;
638: }
639:
640: 641: 642: 643: 644:
645: public function getUserVars()
646: {
647: return array(
648: 'uid',
649: 'uname',
650: 'name',
651: 'email',
652: 'url',
653: 'user_avatar',
654: 'user_regdate',
655: 'user_icq',
656: 'user_from',
657: 'user_sig',
658: 'user_viewemail',
659: 'actkey',
660: 'user_aim',
661: 'user_yim',
662: 'user_msnm',
663: 'pass',
664: 'posts',
665: 'attachsig',
666: 'rank',
667: 'level',
668: 'theme',
669: 'timezone_offset',
670: 'last_login',
671: 'umode',
672: 'uorder',
673: 'notify_method',
674: 'notify_mode',
675: 'user_occ',
676: 'bio',
677: 'user_intrest',
678: 'user_mailok');
679: }
680: }
681: