XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
field.php
Go to the documentation of this file.
1 <?php
21 defined('XOOPS_ROOT_PATH') or die("XOOPS root path not defined");
22 
28 {
29  function __construct()
30  {
31  $this->initVar('field_id', XOBJ_DTYPE_INT, null);
32  $this->initVar('cat_id', XOBJ_DTYPE_INT, null, true);
33  $this->initVar('field_type', XOBJ_DTYPE_TXTBOX);
34  $this->initVar('field_valuetype', XOBJ_DTYPE_INT, null, true);
35  $this->initVar('field_name', XOBJ_DTYPE_TXTBOX, null, true);
36  $this->initVar('field_title', XOBJ_DTYPE_TXTBOX);
37  $this->initVar('field_description', XOBJ_DTYPE_TXTAREA);
38  $this->initVar('field_required', XOBJ_DTYPE_INT, 0); //0 = no, 1 = yes
39  $this->initVar('field_maxlength', XOBJ_DTYPE_INT, 0);
40  $this->initVar('field_weight', XOBJ_DTYPE_INT, 0);
41  $this->initVar('field_default', XOBJ_DTYPE_TXTAREA, "");
42  $this->initVar('field_notnull', XOBJ_DTYPE_INT, 1);
43  $this->initVar('field_edit', XOBJ_DTYPE_INT, 0);
44  $this->initVar('field_show', XOBJ_DTYPE_INT, 0);
45  $this->initVar('field_config', XOBJ_DTYPE_INT, 0);
46  $this->initVar('field_options', XOBJ_DTYPE_ARRAY, array() );
47  $this->initVar('step_id', XOBJ_DTYPE_INT, 0);
48  }
49 
50  function ProfileField()
51  {
52  $this->__construct();
53  }
54 
59  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  function getVar($key, $format = 's')
70  {
71  $value = parent::getVar($key, $format);
72  if ($key == 'field_options' && !empty($value)) {
73  foreach (array_keys($value) as $idx) {
74  $value[$idx] = base64_decode($value[$idx]);
75  }
76  }
77  return $value;
78  }
79 
88  function getEditElement($user, $profile)
89  {
90  $value = in_array($this->getVar('field_name'), $this->getUserVars() ) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
91 
92  $caption = $this->getVar('field_title');
93  $caption = defined($caption) ? constant($caption) : $caption;
94  $name = $this->getVar('field_name', 'e');
95  $options = $this->getVar('field_options');
96  if (is_array($options)) {
97  //asort($options);
98 
99  foreach (array_keys($options) as $key) {
100  $optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
101  $optkey = defined($key) ? constant($key) : $key;
102  unset($options[$key]);
103  $options[$optkey] = $optval;
104  }
105  }
106  include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
107  switch ($this->getVar('field_type') ) {
108  default:
109  case "autotext":
110  //autotext is not for editing
111  $element = new XoopsFormLabel($caption, $this->getOutputValue($user, $profile));
112  break;
113 
114  case "textbox":
115  $element = new XoopsFormText($caption, $name, 35, $this->getVar('field_maxlength'), $value);
116  break;
117 
118  case "textarea":
119  $element = new XoopsFormTextArea($caption, $name, $value, 4, 30);
120  break;
121 
122  case "dhtml":
123  $element = new XoopsFormDhtmlTextArea($caption, $name, $value, 10, 30);
124  break;
125 
126  case "select":
127  $element = new XoopsFormSelect($caption, $name, $value);
128  // If options do not include an empty element, then add a blank option to prevent any default selection
129  if (!in_array('', array_keys($options))) {
130  $element->addOption('', _NONE);
131 
132  $eltmsg = empty($caption) ? sprintf(_FORM_ENTER, $name) : sprintf( _FORM_ENTER, $caption);
133  $eltmsg = str_replace('"', '\"', stripslashes($eltmsg));
134  $element->customValidationCode[] = "\nvar hasSelected = false; var selectBox = myform.{$name};" .
135  "for (i = 0; i < selectBox.options.length; i++ ) { if ( selectBox.options[i].selected == true && selectBox.options[i].value != '' ) { hasSelected = true; break; } }" .
136  "if ( !hasSelected ) { window.alert(\"{$eltmsg}\"); selectBox.focus(); return false; }";
137  }
138  $element->addOptionArray($options);
139  break;
140 
141  case "select_multi":
142  $element = new XoopsFormSelect($caption, $name, $value, 5, true);
143  $element->addOptionArray($options);
144  break;
145 
146  case "radio":
147  $element = new XoopsFormRadio($caption, $name, $value);
148  $element->addOptionArray($options);
149  break;
150 
151  case "checkbox":
152  $element = new XoopsFormCheckBox($caption, $name, $value);
153  $element->addOptionArray($options);
154  break;
155 
156  case "yesno":
157  $element = new XoopsFormRadioYN($caption, $name, $value);
158  break;
159 
160  case "group":
161  $element = new XoopsFormSelectGroup($caption, $name, true, $value);
162  break;
163 
164  case "group_multi":
165  $element = new XoopsFormSelectGroup($caption, $name, true, $value, 5, true);
166  break;
167 
168  case "language":
169  $element = new XoopsFormSelectLang($caption, $name, $value);
170  break;
171 
172  case "date":
173  $element = new XoopsFormTextDateSelect($caption, $name, 15, $value);
174  break;
175 
176  case "longdate":
177  $element = new XoopsFormTextDateSelect($caption, $name, 15, str_replace("-", "/", $value) );
178  break;
179 
180  case "datetime":
181  $element = new XoopsFormDatetime($caption, $name, 15, $value);
182  break;
183 
184  case "list":
185  $element = new XoopsFormSelectList($caption, $name, $value, 1, $options[0]);
186  break;
187 
188  case "timezone":
189  $element = new XoopsFormSelectTimezone($caption, $name, $value);
190  $element->setExtra("style='width: 280px;'");
191  break;
192 
193  case "rank":
194  $element = new XoopsFormSelect($caption, $name, $value);
195 
196  include_once $GLOBALS['xoops']->path('class/xoopslists.php');
197  $ranks = XoopsLists::getUserRankList();
198  $element->addOption(0, "--------------");
199  $element->addOptionArray($ranks);
200  break;
201 
202  case 'theme':
203  $element = new XoopsFormSelect($caption, $name, $value);
204  $element->addOption("0", _PROFILE_MA_SITEDEFAULT);
205  $handle = opendir(XOOPS_THEME_PATH . '/');
206  $dirlist = array();
207  while (false !== ($file = readdir($handle) ) ) {
208  if (is_dir(XOOPS_THEME_PATH . '/' . $file) && !preg_match("/^[.]{1,2}$/", $file) && strtolower($file) != 'cvs' ) {
209  if (file_exists(XOOPS_THEME_PATH . "/" . $file . "/theme.html") && in_array($file, $GLOBALS['xoopsConfig']['theme_set_allowed'])) {
210  $dirlist[$file] = $file;
211  }
212  }
213  }
214  closedir($handle);
215  if (!empty($dirlist)) {
216  asort($dirlist);
217  $element->addOptionArray($dirlist);
218  }
219  break;
220  }
221  if ($this->getVar('field_description') != "") {
222  $element->setDescription($this->getVar('field_description') );
223  }
224  return $element;
225  }
226 
235  function getOutputValue(&$user, $profile)
236  {
237  if (file_exists($file = $GLOBALS['xoops']->path('modules/profile/language/' . $GLOBALS['xoopsConfig']['language'] . '/modinfo.php'))) {
238  include_once $file;
239  } else {
240  include_once $GLOBALS['xoops']->path('modules/profile/language/english/modinfo.php');
241  }
242 
243  $value = in_array($this->getVar('field_name'), $this->getUserVars() ) ? $user->getVar($this->getVar('field_name') ) : $profile->getVar($this->getVar('field_name'));
244 
245  switch ($this->getVar('field_type') ) {
246  default:
247  case "textbox":
248  if ( $this->getVar('field_name') == 'url' && $value != '') {
249  return '<a href="' . formatURL($value) . '" rel="external">' . $value . '</a>';
250  } else {
251  return $value;
252  }
253  break;
254  case "textarea":
255  case "dhtml":
256  case 'theme':
257  case "language":
258  case "list":
259  return $value;
260  break;
261 
262  case "select":
263  case "radio":
264  $options = $this->getVar('field_options');
265  if (isset($options[$value])) {
266  $value = htmlspecialchars( defined($options[$value]) ? constant($options[$value]) : $options[$value]);
267  } else {
268  $value = "";
269  }
270  return $value;
271  break;
272 
273  case "select_multi":
274  case "checkbox":
275  $options = $this->getVar('field_options');
276  $ret = array();
277  if (count($options) > 0) {
278  foreach (array_keys($options) as $key) {
279  if (in_array($key, $value)) {
280  $ret[$key] = htmlspecialchars( defined($options[$key]) ? constant($options[$key]) : $options[$key]);
281  }
282  }
283  }
284  return $ret;
285  break;
286 
287  case "group":
288  $member_handler = &xoops_gethandler('member');
289  $options = $member_handler->getGroupList();
290  $ret = isset($options[$value]) ? $options[$value] : '';
291  return $ret;
292  break;
293 
294  case "group_multi":
295  $member_handler = &xoops_gethandler('member');
296  $options = $member_handler->getGroupList();
297  $ret = array();
298  foreach (array_keys($options) as $key) {
299  if (in_array($key, $value)) {
300  $ret[$key] = htmlspecialchars($options[$key]);
301  }
302  }
303  return $ret;
304  break;
305 
306  case "longdate":
307  //return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
308  //when we cannot convert it to a UNIX timestamp?
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'); //autotext can have HTML in it
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  return $value;
329  break;
330 
331  case "rank":
332  $userrank = $user->rank();
333  $user_rankimage = "";
334  if (isset($userrank['image']) && $userrank['image'] != "") {
335  $user_rankimage = '<img src="'.XOOPS_UPLOAD_URL . '/' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /><br />';
336  }
337  return $user_rankimage.$userrank['title'];
338  break;
339 
340  case "yesno":
341  return $value ? _YES : _NO;
342  break;
343 
344  case "timezone":
345  include_once $GLOBALS['xoops']->path('class/xoopslists.php');
346  $timezones = XoopsLists::getTimeZoneList();
347  $value = empty($value) ? "0" : strval($value);
348  return $timezones[str_replace('.0', '', $value)];
349  break;
350  }
351  }
352 
360  function getValueForSave($value)
361  {
362  switch ($this->getVar('field_type')) {
363  default:
364  case "textbox":
365  case "textarea":
366  case "dhtml":
367  case "yesno":
368  case "timezone":
369  case 'theme':
370  case "language":
371  case "list":
372  case "select":
373  case "radio":
374  case "select_multi":
375  case "group":
376  case "group_multi":
377  case "longdate":
378  return $value;
379 
380  case "checkbox":
381  return (array) $value;
382 
383  case "date":
384  if ($value != "") {
385  return strtotime($value);
386  }
387  return $value;
388  break;
389 
390  case "datetime":
391  if (!empty($value)) {
392  return strtotime($value['date']) + intval($value['time']);
393  }
394  return $value;
395  break;
396  }
397  }
398 
404  function getUserVars()
405  {
406  $profile_handler = xoops_getmodulehandler('profile', 'profile');
407  return $profile_handler->getUserVars();
408  }
409 }
410 
416 {
418  {
419  $this->__construct($db);
420  }
421 
422  function __construct(&$db)
423  {
424  parent::__construct($db, 'profile_field', "profilefield", "field_id", 'field_title');
425  }
426 
434  function loadFields($force_update = false)
435  {
436  static $fields = array();
437  if (!empty($force_update) || count($fields) == 0) {
438  $this->table_link = $this->db->prefix('profile_category');
439  $criteria = new Criteria('o.field_id', 0, "!=");
440  $criteria->setSort('l.cat_weight ASC, o.field_weight');
441  $field_objs = $this->getByLink($criteria, array('o.*'), true, 'cat_id', 'cat_id');
442  foreach (array_keys($field_objs) as $i ) {
443  $fields[$field_objs[$i]->getVar('field_name')] = $field_objs[$i];
444  }
445  }
446  return $fields;
447  }
448 
457  function insert(&$obj, $force = false)
458  {
459  $profile_handler =& xoops_getmodulehandler('profile', 'profile');
460  $obj->setVar('field_name', str_replace(' ', '_', $obj->getVar('field_name')));
461  $obj->cleanVars();
462  $defaultstring = "";
463  switch ($obj->getVar('field_type') ) {
464  case "datetime":
465  case "date":
466  $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
467  $obj->setVar('field_maxlength', 10);
468  break;
469 
470  case "longdate":
471  $obj->setVar('field_valuetype', XOBJ_DTYPE_MTIME);
472  break;
473 
474  case "yesno":
475  $obj->setVar('field_valuetype', XOBJ_DTYPE_INT);
476  $obj->setVar('field_maxlength', 1);
477  break;
478 
479  case "textbox":
480  if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
481  $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
482  }
483  break;
484 
485  case "autotext":
486  if ($obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
487  $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
488  }
489  break;
490 
491  case "group_multi":
492  case "select_multi":
493  case "checkbox":
494  $obj->setVar('field_valuetype', XOBJ_DTYPE_ARRAY);
495  break;
496 
497  case "language":
498  case "timezone":
499  case "theme":
500  $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
501  break;
502 
503  case "dhtml":
504  case "textarea":
505  $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTAREA);
506  break;
507  }
508 
509  if ($obj->getVar('field_valuetype') == "") {
510  $obj->setVar('field_valuetype', XOBJ_DTYPE_TXTBOX);
511  }
512 
513  if ((!in_array($obj->getVar('field_name'), $this->getUserVars())) && (isset($_REQUEST['field_required']))) {
514  if ($obj->isNew()) {
515  //add column to table
516  $changetype = "ADD";
517  } else {
518  //update column information
519  $changetype = "CHANGE `" . $obj->getVar('field_name', 'n') . "`";
520  }
521  $maxlengthstring = $obj->getVar('field_maxlength') > 0 ? "(" . $obj->getVar('field_maxlength') . ")" : "";
522 
523  //set type
524  switch ($obj->getVar('field_valuetype')) {
525  default:
526  case XOBJ_DTYPE_ARRAY:
528  $type = "mediumtext";
529  break;
533  case XOBJ_DTYPE_EMAIL:
534  case XOBJ_DTYPE_TXTBOX:
535  case XOBJ_DTYPE_URL:
536  $type = "varchar";
537  // varchars must have a maxlength
538  if (!$maxlengthstring) {
539  //so set it to max if maxlength is not set - or should it fail?
540  $maxlengthstring = "(255)";
541  $obj->setVar('field_maxlength', 255);
542  }
543  break;
544 
545  case XOBJ_DTYPE_INT:
546  $type = "int";
547  break;
548 
549  case XOBJ_DTYPE_DECIMAL:
550  $type = "decimal(14,6)";
551  break;
552 
553  case XOBJ_DTYPE_FLOAT:
554  $type = "float(15,9)";
555  break;
556 
557  case XOBJ_DTYPE_OTHER:
559  case XOBJ_DTYPE_TXTAREA:
560  $type = "text";
561  $maxlengthstring = "";
562  break;
563 
564  case XOBJ_DTYPE_MTIME:
565  $type = "date";
566  $maxlengthstring = "";
567  break;
568  }
569 
570  $sql = "ALTER TABLE `" . $profile_handler->table . "` " .
571  $changetype . " `" . $obj->cleanVars['field_name'] . "` " . $type . $maxlengthstring . ' NULL';
572  if (!$this->db->query($sql)) {
573  return false;
574  }
575  }
576 
577  //change this to also update the cached field information storage
578  $obj->setDirty();
579  if (!parent::insert($obj, $force)) {
580  return false;
581  }
582  return $obj->getVar('field_id');
583 
584  }
585 
593  function delete(&$obj, $force = false)
594  {
595  $profile_handler =& xoops_getmodulehandler('profile', 'profile');
596  // remove column from table
597  $sql = "ALTER TABLE " . $profile_handler->table . " DROP `" . $obj->getVar('field_name', 'n') . "`";
598  if ($this->db->query($sql)) {
599  //change this to update the cached field information storage
600  if (!parent::delete($obj, $force)) {
601  return false;
602  }
603 
604  if ($obj->getVar('field_show') || $obj->getVar('field_edit')) {
605  $module_handler =& xoops_gethandler('module');
606  $profile_module = $module_handler->getByDirname('profile');
607  if (is_object($profile_module)) {
608  // Remove group permissions
609  $groupperm_handler =& xoops_gethandler('groupperm');
610  $criteria = new CriteriaCompo(new Criteria('gperm_modid', $profile_module->getVar('mid')));
611  $criteria->add(new Criteria('gperm_itemid', $obj->getVar('field_id')));
612  return $groupperm_handler->deleteAll($criteria);
613  }
614  }
615  }
616  return false;
617  }
618 
624  function getUserVars()
625  {
626  return array('uid', 'uname', 'name', 'email', 'url','user_avatar', 'user_regdate', 'user_icq', 'user_from',
627  'user_sig', 'user_viewemail', 'actkey', 'user_aim', 'user_yim', 'user_msnm', 'pass', 'posts',
628  'attachsig', 'rank', 'level', 'theme', 'timezone_offset', 'last_login', 'umode', 'uorder',
629  'notify_method', 'notify_mode', 'user_occ', 'bio', 'user_intrest', 'user_mailok');
630  }
631 }
632 ?>