1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Database\Connection;
13: use Xoops\Core\Kernel\Handlers\XoopsUser;
14: use Xoops\Core\Kernel\XoopsObject;
15: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28:
29: 30: 31: 32:
33: class ProfileField extends XoopsObject
34: {
35: 36: 37:
38: public function __construct()
39: {
40: $this->initVar('field_id', XOBJ_DTYPE_INT, null);
41: $this->initVar('cat_id', XOBJ_DTYPE_INT, null, true);
42: $this->initVar('field_type', XOBJ_DTYPE_TXTBOX);
43: $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true);
44: $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true);
45: $this->initVar('field_title', XOBJ_DTYPE_TXTBOX);
46: $this->initVar('field_description', XOBJ_DTYPE_TXTAREA);
47: $this->initVar('field_required', XOBJ_DTYPE_INT, 0);
48: $this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0);
49: $this->initVar('field_weight', XOBJ_DTYPE_INT, 0);
50: $this->initVar('field_default', XOBJ_DTYPE_TXTAREA, "");
51: $this->initVar('field_notnull', XOBJ_DTYPE_INT, 1);
52: $this->initVar('field_edit', XOBJ_DTYPE_INT, 0);
53: $this->initVar('field_show', XOBJ_DTYPE_INT, 0);
54: $this->initVar('field_config', XOBJ_DTYPE_INT, 0);
55: $this->initVar('field_options', XOBJ_DTYPE_ARRAY, array());
56: $this->initVar('step_id', XOBJ_DTYPE_INT, 0);
57: }
58:
59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69:
70: public function setVar($key, $value)
71: {
72: if ($key === 'field_options' && is_array($value)) {
73: foreach (array_keys($value) as $idx) {
74: $value[$idx] = base64_encode($value[$idx]);
75: }
76: }
77: parent::setVar($key, $value);
78: }
79:
80: 81: 82: 83: 84:
85: public function getVar($key, $format = 's')
86: {
87: $value = parent::getVar($key, $format);
88: if ($key === 'field_options' && !empty($value)) {
89: foreach (array_keys($value) as $idx) {
90: $value[$idx] = base64_decode($value[$idx]);
91: }
92: }
93: return $value;
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103:
104: public function getEditElement(XoopsUser $user, ProfileProfile $profile)
105: {
106: $xoops = Xoops::getInstance();
107: $value = in_array($this->getVar('field_name'), $this->getUserVars())
108: ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
109:
110: $caption = $this->getVar('field_title');
111: $caption = defined($caption) ? constant($caption) : $caption;
112: $name = $this->getVar('field_name', 'e');
113: $options = $this->getVar('field_options');
114: if (is_array($options)) {
115:
116:
117: foreach (array_keys($options) as $key) {
118: $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
119: $optkey = defined($key) ? constant($key) : $key;
120: unset($options[$key]);
121: $options[$optkey] = $optval;
122: }
123: }
124: switch ($this->getVar('field_type')) {
125: default:
126: case "autotext":
127:
128: $element = new Xoops\Form\Label($caption, $this->getOutputValue($user, $profile));
129: break;
130:
131: case "textbox":
132: $element = new Xoops\Form\Text($caption, $name, 35, $this->getVar('field_maxlength'), $value);
133: break;
134:
135: case "textarea":
136: $element = new Xoops\Form\TextArea($caption, $name, $value, 4, 30);
137: break;
138:
139: case "dhtml":
140: $element = new Xoops\Form\DhtmlTextArea($caption, $name, $value, 10, 30);
141: break;
142:
143: case "select":
144: $element = new Xoops\Form\Select($caption, $name, $value);
145:
146: if (!in_array('', array_keys($options))) {
147: $element->addOption('', XoopsLocale::NONE);
148:
149: $eltmsg = empty($caption) ? sprintf(XoopsLocale::F_ENTER, $name) : sprintf(XoopsLocale::F_ENTER, $caption);
150: $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
151: $element->addCustomValidationCode("\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; }");
152: }
153: $element->addOptionArray($options);
154: break;
155:
156: case "select_multi":
157: $element = new Xoops\Form\Select($caption, $name, $value, 5, true);
158: $element->addOptionArray($options);
159: break;
160:
161: case "radio":
162: $element = new Xoops\Form\Radio($caption, $name, $value);
163: $element->addOptionArray($options);
164: break;
165:
166: case "checkbox":
167: $element = new Xoops\Form\Checkbox($caption, $name, $value);
168: $element->addOptionArray($options);
169: break;
170:
171: case "yesno":
172: $element = new Xoops\Form\RadioYesNo($caption, $name, $value);
173: break;
174:
175: case "group":
176: $element = new Xoops\Form\SelectGroup($caption, $name, true, $value);
177: break;
178:
179: case "group_multi":
180: $element = new Xoops\Form\SelectGroup($caption, $name, true, $value, 5, true);
181: break;
182:
183: case "language":
184: $element = new Xoops\Form\SelectLanguage($caption, $name, $value);
185: break;
186:
187: case "date":
188: $element = new Xoops\Form\DateSelect($caption, $name, $value);
189: break;
190:
191: case "longdate":
192: $element = new Xoops\Form\DateSelect($caption, $name, str_replace("-", "/", $value));
193: break;
194:
195: case "datetime":
196: $element = new Xoops\Form\DateTime($caption, $name, $value);
197: break;
198:
199: case "timezone":
200: $element = new Xoops\Form\SelectTimeZone($caption, $name, $value);
201: break;
202:
203: case "rank":
204: $ranklist = $xoops->service('userrank')->getAssignableUserRankList()->getValue();
205: if ($ranklist !== null) {
206: $element = new Xoops\Form\Select($caption, $name, $value);
207: $element->addOption(0, "--------------");
208: $element->addOptionArray($ranklist);
209: } else {
210: $element = new Xoops\Form\Hidden($name, $value);
211: }
212: break;
213:
214: case 'theme':
215: $element = new Xoops\Form\Select($caption, $name, $value);
216: $element->addOption("0", _PROFILE_MA_SITEDEFAULT);
217: $handle = opendir(\XoopsBaseConfig::get('themes-path') . '/');
218: $dirlist = array();
219: while (false !== ($file = readdir($handle))) {
220: if (is_dir(\XoopsBaseConfig::get('themes-path') . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) !== 'cvs') {
221: if (XoopsLoad::fileExists(\XoopsBaseConfig::get('themes-path') . "/" . $file . "/theme.html") && in_array($file, $xoops->getConfig('theme_set_allowed'))) {
222: $dirlist[$file] = $file;
223: }
224: }
225: }
226: closedir($handle);
227: if (!empty($dirlist)) {
228: asort($dirlist);
229: $element->addOptionArray($dirlist);
230: }
231: break;
232: }
233: if ($this->getVar('field_description') != "") {
234: $element->setDescription($this->getVar('field_description'));
235: }
236: return $element;
237: }
238:
239: 240: 241: 242: 243: 244: 245: 246:
247: public function getOutputValue(XoopsUser $user, ProfileProfile $profile)
248: {
249: $xoops = Xoops::getInstance();
250: $xoops->loadLanguage('modinfo', 'profile');
251:
252: $value = in_array($this->getVar('field_name'), $this->getUserVars())
253: ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
254:
255: switch ($this->getVar('field_type')) {
256: default:
257: case "textbox":
258: if ($this->getVar('field_name') === 'url' && $value != '') {
259: return '<a href="' . $xoops->formatURL($value) . '" rel="external">' . $value . '</a>';
260: } else {
261: return $value;
262: }
263: break;
264: case "textarea":
265: case "dhtml":
266: case 'theme':
267: case "language":
268: case "list":
269: return $value;
270: break;
271:
272: case "select":
273: case "radio":
274: $options = $this->getVar('field_options');
275: if (isset($options[$value])) {
276: $value = htmlspecialchars(
277: defined($options[$value]) ? constant($options[$value]) : $options[$value]
278: );
279: } else {
280: $value = "";
281: }
282: return $value;
283: break;
284:
285: case "select_multi":
286: case "checkbox":
287: $options = $this->getVar('field_options');
288: $ret = array();
289: if (count($options) > 0) {
290: foreach (array_keys($options) as $key) {
291: if (in_array($key, $value)) {
292: $ret[$key] = htmlspecialchars(
293: defined($options[$key]) ? constant($options[$key]) : $options[$key]
294: );
295: }
296: }
297: }
298: return $ret;
299: break;
300:
301: case "group":
302: $member_handler = $xoops->getHandlerMember();
303: $options = $member_handler->getGroupList();
304: $ret = isset($options[$value]) ? $options[$value] : '';
305: return $ret;
306: break;
307:
308: case "group_multi":
309: $member_handler = $xoops->getHandlerMember();
310: $options = $member_handler->getGroupList();
311: $ret = array();
312: foreach (array_keys($options) as $key) {
313: if (in_array($key, $value)) {
314: $ret[$key] = htmlspecialchars($options[$key]);
315: }
316: }
317: return $ret;
318: break;
319:
320: case "longdate":
321:
322:
323: return str_replace("-", "/", $value);
324:
325: case "date":
326: return XoopsLocale::formatTimestamp($value, 's');
327: break;
328:
329: case "datetime":
330: if (!empty($value)) {
331: return XoopsLocale::formatTimestamp($value, 'm');
332: } else {
333: return _PROFILE_MI_NEVER_LOGGED_IN;
334: }
335: break;
336:
337: case "autotext":
338: $value = $user->getVar($this->getVar('field_name'), 'n');
339: $value = str_replace("{X_UID}", $user->getVar("uid"), $value);
340: $value = str_replace("{X_URL}", \XoopsBaseConfig::get('url'), $value);
341: $value = str_replace("{X_UNAME}", $user->getVar("uname"), $value);
342: return $value;
343: break;
344:
345: case "rank":
346: $userrank = $user->rank();
347: $user_rankimage = "";
348: if (isset($userrank['image']) && $userrank['image'] != "") {
349: $user_rankimage = '<img src="' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br />';
350: }
351: return $user_rankimage . $userrank['title'];
352: break;
353:
354: case "yesno":
355: return $value ? XoopsLocale::YES : XoopsLocale::NO;
356: break;
357:
358: case "timezone":
359: $timezones = XoopsLists::getTimeZoneList();
360: $value = empty($value) ? "0" : (string)($value);
361: return $timezones[str_replace('.0', '', $value)];
362: break;
363: }
364: }
365:
366: 367: 368: 369: 370: 371: 372:
373: public function getValueForSave($value)
374: {
375: switch ($this->getVar('field_type')) {
376: default:
377: case "textbox":
378: case "textarea":
379: case "dhtml":
380: case "yesno":
381: case "timezone":
382: case 'theme':
383: case "language":
384: case "list":
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: return $value;
401: break;
402:
403: case "datetime":
404: if (!empty($value)) {
405: return strtotime($value['date']) + (int)($value['time']);
406: }
407: return $value;
408: break;
409: }
410: }
411:
412: 413: 414: 415: 416:
417: public function getUserVars()
418: {
419:
420: $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile');
421: return $profile_handler->getUserVars();
422: }
423: }
424:
425: 426: 427: 428:
429: class ProfileFieldHandler extends XoopsPersistableObjectHandler
430: {
431: 432: 433:
434: public function __construct(Connection $db = null)
435: {
436: parent::__construct($db, 'profile_field', 'ProfileField', 'field_id', 'field_title');
437: }
438:
439: 440: 441: 442: 443: 444: 445:
446: public function loadFields($force_update = false)
447: {
448: static $fields = array();
449: if (!empty($force_update) || count($fields) == 0) {
450: $this->table_link = $this->db2->prefix('profile_category');
451: $criteria = new Criteria('o.field_id', 0, "!=");
452: $criteria->setSort('l.cat_weight ASC, o.field_weight');
453: $field_objs = $this->getByLink($criteria, array('o.*'), true, 'cat_id', 'cat_id');
454:
455: foreach ($field_objs as $field) {
456: $fields[$field->getVar('field_name')] = $field;
457: }
458: }
459: return $fields;
460: }
461:
462: 463: 464: 465: 466: 467: 468:
469: public function insertFields(XoopsObject $obj, $force = false)
470: {
471: $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile');
472: $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
473: $obj->cleanVars();
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())) {
525: if ($obj->isNew()) {
526:
527: $changetype = "ADD";
528: } else {
529:
530: $changetype = "CHANGE `" . $obj->getVar('field_name', 'n') . "`";
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_EMAIL:
539: case XOBJ_DTYPE_TXTBOX:
540: case XOBJ_DTYPE_URL:
541: $type = "varchar";
542:
543: if (!$maxlengthstring) {
544:
545: $maxlengthstring = "(255)";
546: $obj->setVar('field_maxlength', 255);
547: }
548: break;
549:
550: case XOBJ_DTYPE_INT:
551: $type = "int";
552: break;
553:
554: case XOBJ_DTYPE_DECIMAL:
555: $type = "decimal(14,6)";
556: break;
557:
558: case XOBJ_DTYPE_FLOAT:
559: $type = "float(15,9)";
560: break;
561:
562: case XOBJ_DTYPE_OTHER:
563: case XOBJ_DTYPE_TXTAREA:
564: $type = "text";
565: $maxlengthstring = "";
566: break;
567:
568: case XOBJ_DTYPE_MTIME:
569: $type = "date";
570: $maxlengthstring = "";
571: break;
572: }
573:
574: $sql = "ALTER TABLE `" . $profile_handler->table . "` " . $changetype . " `" . $obj->cleanVars['field_name'] . "` " . $type . $maxlengthstring . ' NULL';
575: if (!$this->db2->query($sql)) {
576: return false;
577: }
578: }
579:
580:
581: $obj->setDirty();
582: if (!parent::insert($obj, $force)) {
583: return false;
584: }
585: return $obj->getVar('field_id');
586:
587: }
588:
589: 590: 591: 592: 593: 594: 595:
596: public function deleteFields(XoopsObject $obj, $force = false)
597: {
598: $xoops = Xoops::getInstance();
599: $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile');
600:
601: $sql = "ALTER TABLE " . $profile_handler->table . " DROP `" . $obj->getVar('field_name', 'n') . "`";
602: if ($this->db2->query($sql)) {
603:
604: if (!parent::delete($obj, $force)) {
605: return false;
606: }
607:
608: if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
609: $profile_module = $xoops->getModuleByDirname('profile');
610: if (is_object($profile_module)) {
611:
612: $groupperm_handler = $xoops->getHandlerGroupPermission();
613: $criteria = new CriteriaCompo(new Criteria('gperm_modid', $profile_module->getVar('mid')));
614: $criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
615: return $groupperm_handler->deleteAll($criteria);
616: }
617: }
618: }
619: return false;
620: }
621:
622: 623: 624: 625: 626:
627: public function getUserVars()
628: {
629: return array(
630: 'uid', 'uname', 'name', 'email', 'url', 'user_avatar', 'user_regdate', 'user_icq', 'user_from', 'user_sig',
631: 'user_viewemail', 'actkey', 'user_aim', 'user_yim', 'user_msnm', 'pass', 'posts', 'attachsig', 'rank',
632: 'level', 'theme', 'timezone', 'last_login', 'umode', 'uorder', 'notify_method', 'notify_mode',
633: 'user_occ', 'bio', 'user_intrest', 'user_mailok'
634: );
635: }
636: }
637: