XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
forms.php
Go to the documentation of this file.
1 <?php
21 defined('XOOPS_ROOT_PATH') or die("XOOPS root path not defined");
22 
31 function profile_getFieldForm(&$field, $action = false)
32 {
33  if ( $action === false ) {
34  $action = $_SERVER['REQUEST_URI'];
35  }
36  $title = $field->isNew() ? sprintf(_PROFILE_AM_ADD, _PROFILE_AM_FIELD) : sprintf(_PROFILE_AM_EDIT, _PROFILE_AM_FIELD);
37 
38  include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
39  $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
40 
41  $form->addElement(new XoopsFormText(_PROFILE_AM_TITLE, 'field_title', 35, 255, $field->getVar('field_title', 'e')));
42  $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DESCRIPTION, 'field_description', $field->getVar('field_description', 'e')));
43 
44  if (!$field->isNew()) {
45  $fieldcat_id = $field->getVar('cat_id');
46  } else {
47  $fieldcat_id = 0;
48  }
49  $category_handler =& xoops_getmodulehandler('category');
50  $cat_select = new XoopsFormSelect(_PROFILE_AM_CATEGORY, 'field_category', $fieldcat_id);
51  $cat_select->addOption(0, _PROFILE_AM_DEFAULT);
52  $cat_select->addOptionArray($category_handler->getList());
53  $form->addElement($cat_select);
54  $form->addElement(new XoopsFormText(_PROFILE_AM_WEIGHT, 'field_weight', 10, 10, $field->getVar('field_weight', 'e')));
55  if ($field->getVar('field_config') || $field->isNew()) {
56  if (!$field->isNew()) {
57  $form->addElement(new XoopsFormLabel(_PROFILE_AM_NAME, $field->getVar('field_name')));
58  $form->addElement(new XoopsFormHidden('id', $field->getVar('field_id')));
59  } else {
60  $form->addElement(new XoopsFormText(_PROFILE_AM_NAME, 'field_name', 35, 255, $field->getVar('field_name', 'e')));
61  }
62 
63  //autotext and theme left out of this one as fields of that type should never be changed (valid assumption, I think)
64  $fieldtypes = array(
65  'checkbox' => _PROFILE_AM_CHECKBOX,
66  'date' => _PROFILE_AM_DATE,
67  'datetime' => _PROFILE_AM_DATETIME,
68  'longdate' => _PROFILE_AM_LONGDATE,
69  'group' => _PROFILE_AM_GROUP,
70  'group_multi' => _PROFILE_AM_GROUPMULTI,
71  'language' => _PROFILE_AM_LANGUAGE,
72  'radio' => _PROFILE_AM_RADIO,
73  'select' => _PROFILE_AM_SELECT,
74  'select_multi' => _PROFILE_AM_SELECTMULTI,
75  'textarea' => _PROFILE_AM_TEXTAREA,
76  'dhtml' => _PROFILE_AM_DHTMLTEXTAREA,
77  'textbox' => _PROFILE_AM_TEXTBOX,
78  'timezone' => _PROFILE_AM_TIMEZONE,
79  'yesno' => _PROFILE_AM_YESNO);
80 
81  $element_select = new XoopsFormSelect(_PROFILE_AM_TYPE, 'field_type', $field->getVar('field_type', 'e'));
82  $element_select->addOptionArray($fieldtypes);
83 
84  $form->addElement($element_select);
85 
86  switch ($field->getVar('field_type')) {
87  case "textbox":
88  $valuetypes = array(
103 
104  $type_select = new XoopsFormSelect(_PROFILE_AM_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
105  $type_select->addOptionArray($valuetypes);
106  $form->addElement($type_select);
107  break;
108 
109  case "select":
110  case "radio":
111  $valuetypes = array(
126 
127  $type_select = new XoopsFormSelect(_PROFILE_AM_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
128  $type_select->addOptionArray($valuetypes);
129  $form->addElement($type_select);
130  break;
131  }
132 
133  //$form->addElement(new XoopsFormRadioYN(_PROFILE_AM_NOTNULL, 'field_notnull', $field->getVar('field_notnull', 'e') ));
134 
135  if ($field->getVar('field_type') == "select" || $field->getVar('field_type') == "select_multi" || $field->getVar('field_type') == "radio" || $field->getVar('field_type') == "checkbox") {
136  $options = $field->getVar('field_options');
137  if (count($options) > 0) {
138  $remove_options = new XoopsFormCheckBox(_PROFILE_AM_REMOVEOPTIONS, 'removeOptions');
139  $remove_options->columns = 3;
140  asort($options);
141  foreach (array_keys($options) as $key) {
142  $options[$key] .= "[{$key}]";
143  }
144  $remove_options->addOptionArray($options);
145  $form->addElement($remove_options);
146  }
147 
148  $option_text = "<table cellspacing='1'><tr><td class='width20'>" . _PROFILE_AM_KEY . "</td><td>" . _PROFILE_AM_VALUE . "</td></tr>";
149  for ($i = 0; $i < 3; $i++) {
150  $option_text .= "<tr><td><input type='text' name='addOption[{$i}][key]' id='addOption[{$i}][key]' size='15' /></td><td><input type='text' name='addOption[{$i}][value]' id='addOption[{$i}][value]' size='35' /></td></tr>";
151  $option_text .= "<tr height='3px'><td colspan='2'> </td></tr>";
152  }
153  $option_text .= "</table>";
154  $form->addElement(new XoopsFormLabel(_PROFILE_AM_ADDOPTION, $option_text) );
155  }
156  }
157 
158  if ($field->getVar('field_edit')) {
159  switch ($field->getVar('field_type')) {
160  case "textbox":
161  case "textarea":
162  case "dhtml":
163  $form->addElement(new XoopsFormText(_PROFILE_AM_MAXLENGTH, 'field_maxlength', 35, 35, $field->getVar('field_maxlength', 'e')));
164  $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
165  break;
166 
167  case "checkbox":
168  case "select_multi":
169  $def_value = $field->getVar('field_default', 'e') != null ? unserialize($field->getVar('field_default', 'n')) : null;
170  $element = new XoopsFormSelect(_PROFILE_AM_DEFAULT, 'field_default', $def_value, 8, true);
171  $options = $field->getVar('field_options');
172  asort($options);
173  // If options do not include an empty element, then add a blank option to prevent any default selection
174  if (!in_array('', array_keys($options))) {
175  $element->addOption('', _NONE);
176  }
177  $element->addOptionArray($options);
178  $form->addElement($element);
179  break;
180 
181  case "select":
182  case "radio":
183  $def_value = $field->getVar('field_default', 'e') != null ? $field->getVar('field_default') : null;
184  $element = new XoopsFormSelect(_PROFILE_AM_DEFAULT, 'field_default', $def_value);
185  $options = $field->getVar('field_options');
186  asort($options);
187  // If options do not include an empty element, then add a blank option to prevent any default selection
188  if (!in_array('', array_keys($options))) {
189  $element->addOption('', _NONE);
190  }
191  $element->addOptionArray($options);
192  $form->addElement($element);
193  break;
194 
195  case "date":
196  $form->addElement(new XoopsFormTextDateSelect(_PROFILE_AM_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
197  break;
198 
199  case "longdate":
200  $form->addElement(new XoopsFormTextDateSelect(_PROFILE_AM_DEFAULT, 'field_default', 15, strtotime($field->getVar('field_default', 'e'))));
201  break;
202 
203  case "datetime":
204  $form->addElement(new XoopsFormDateTime(_PROFILE_AM_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
205  break;
206 
207  case "yesno":
208  $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
209  break;
210 
211  case "timezone":
212  $form->addElement(new XoopsFormSelectTimezone(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
213  break;
214 
215  case "language":
216  $form->addElement(new XoopsFormSelectLang(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
217  break;
218 
219  case "group":
220  $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, $field->getVar('field_default', 'e')));
221  break;
222 
223  case "group_multi":
224  $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, unserialize($field->getVar('field_default', 'n')), 5, true));
225  break;
226 
227  case "theme":
228  $form->addElement(new XoopsFormSelectTheme(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
229  break;
230 
231  case "autotext":
232  $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
233  break;
234  }
235  }
236 
237  $groupperm_handler =& xoops_gethandler('groupperm');
238  $searchable_types = array(
239  'textbox',
240  'select',
241  'radio',
242  'yesno',
243  'date',
244  'datetime',
245  'timezone',
246  'language');
247  if (in_array($field->getVar('field_type'), $searchable_types)) {
248  $search_groups = $groupperm_handler->getGroupIds('profile_search', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
249  $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_PROF_SEARCH, 'profile_search', true, $search_groups, 5, true) );
250  }
251  if ($field->getVar('field_edit') || $field->isNew()) {
252  if (!$field->isNew()) {
253  //Load groups
254  $editable_groups = $groupperm_handler->getGroupIds('profile_edit', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
255  } else {
256  $editable_groups = array();
257  }
258  $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_PROF_EDITABLE, 'profile_edit', false, $editable_groups, 5, true));
259  $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_REQUIRED, 'field_required', $field->getVar('field_required', 'e')));
260  $regstep_select = new XoopsFormSelect(_PROFILE_AM_PROF_REGISTER, 'step_id', $field->getVar('step_id', 'e'));
261  $regstep_select->addOption(0, _NO);
263  $regstep_select->addOptionArray($regstep_handler->getList());
264  $form->addElement($regstep_select);
265  }
266  $form->addElement(new XoopsFormHidden('op', 'save') );
267  $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
268 
269  return $form;
270 }
280 function profile_getRegisterForm(&$user, $profile, $step = null)
281 {
282  include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
283  if (empty($GLOBALS['xoopsConfigUser'])) {
284  $config_handler =& xoops_gethandler('config');
285  $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
286  }
287  $action = $_SERVER['REQUEST_URI'];
288  $step_no = $step['step_no'];
289  $use_token = $step['step_no'] > 0 ? true : false;
290  $reg_form = new XoopsThemeForm($step['step_name'], 'regform', $action, 'post', $use_token);
291 
292  if ($step['step_desc']) {
293  $reg_form->addElement(new XoopsFormLabel('', $step['step_desc']));
294  }
295 
296  if ($step_no == 1) {
297  //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
298 
299  $elements[0][] = array('element' => new XoopsFormText(_US_NICKNAME, 'uname', 35, $GLOBALS['xoopsConfigUser']['maxuname'], $user->getVar('uname', 'e')), 'required' => true);
300  $weights[0][] = 0;
301 
302  $elements[0][] = array('element' => new XoopsFormText(_US_EMAIL, 'email', 35, 255, $user->getVar('email', 'e') ), 'required' => true);
303  $weights[0][] = 0;
304 
305  $elements[0][] = array('element' => new XoopsFormPassword(_US_PASSWORD, 'pass', 35, 32, ''), 'required' => true);
306  $weights[0][] = 0;
307 
308  $elements[0][] = array('element' => new XoopsFormPassword(_US_VERIFYPASS, 'vpass', 35, 32, ''), 'required' => true);
309  $weights[0][] = 0;
310  }
311 
312  // Dynamic fields
314  $fields = $profile_handler->loadFields();
315  $_SESSION['profile_required'] = array();
316  foreach (array_keys($fields) as $i) {
317  if ($fields[$i]->getVar('step_id') == $step['step_id']) {
318  $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile);
319  //assign and check (=)
320  if ($fieldinfo['required'] = $fields[$i]->getVar('field_required')) {
321  $_SESSION['profile_required'][$fields[$i]->getVar('field_name')] = $fields[$i]->getVar('field_title');
322  }
323 
324  $key = $fields[$i]->getVar('cat_id');
325  $elements[$key][] = $fieldinfo;
326  $weights[$key][] = $fields[$i]->getVar('field_weight');
327  }
328  }
329  ksort($elements);
330 
331  // Get categories
332  $cat_handler =& xoops_getmodulehandler('category');
333  $categories = $cat_handler->getObjects(null, true, false);
334 
335  foreach (array_keys($elements) as $k) {
336  array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
337  //$title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT;
338  //$desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
339  //$reg_form->insertBreak("<p>{$title}</p>{$desc}");
340  //$reg_form->addElement(new XoopsFormLabel("<h2>".$title."</h2>", $desc), false);
341  foreach (array_keys($elements[$k]) as $i) {
342  $reg_form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
343  }
344  }
345  //end of Dynamic User fields
346 
347  if ($step_no == 1 && $GLOBALS['xoopsConfigUser']['reg_dispdsclmr'] != 0 && $GLOBALS['xoopsConfigUser']['reg_disclaimer'] != '') {
348  $disc_tray = new XoopsFormElementTray(_US_DISCLAIMER, '<br />');
349  $disc_text = new XoopsFormLabel("", "<div class=\"pad5\">" . $GLOBALS["myts"]->displayTarea($GLOBALS['xoopsConfigUser']['reg_disclaimer'], 1) . "</div>");
350  $disc_tray->addElement($disc_text);
351  $agree_chk = new XoopsFormCheckBox('', 'agree_disc');
352  $agree_chk->addOption(1, _US_IAGREE);
353  $disc_tray->addElement($agree_chk);
354  $reg_form->addElement($disc_tray);
355  }
356 
357  if ($step_no == 1) {
358  $reg_form->addElement(new XoopsFormCaptcha(), true);
359  }
360 
361  $reg_form->addElement(new XoopsFormHidden('uid', $user->getVar('uid')));
362  $reg_form->addElement(new XoopsFormHidden('step', $step_no) );
363  $reg_form->addElement(new XoopsFormButton('', 'submitButton', _SUBMIT, 'submit'));
364  return $reg_form;
365 }
366 
367 
375 function profile_getUserForm(&$user, $profile = null, $action = false)
376 {
377  if ($action === false) {
378  $action = $_SERVER['REQUEST_URI'];
379  }
380  if (empty($GLOBALS['xoopsConfigUser'])) {
381  $config_handler =& xoops_gethandler('config');
382  $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
383  }
384 
385  include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
386 
388 
389  $form = new XoopsThemeForm($title, 'userinfo', $action, 'post', true);
390 
392  // Dynamic fields
393  if (!$profile) {
394  $profile_handler =& xoops_getmodulehandler('profile', 'profile');
395  $profile = $profile_handler->get($user->getVar('uid') );
396  }
397  // Get fields
398  $fields = $profile_handler->loadFields();
399  // Get ids of fields that can be edited
400  $gperm_handler =& xoops_gethandler('groupperm');
401  $editable_fields = $gperm_handler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid') );
402 
403  if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) {
404  $elements[0][] = array('element' => new XoopsFormText(_US_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], $user->getVar('uname', 'e') ), 'required' => 1);
405  $email_text = new XoopsFormText('', 'email', 30, 60, $user->getVar('email'));
406  } else {
407  $elements[0][] = array('element' => new XoopsFormLabel(_US_NICKNAME, $user->getVar('uname') ), 'required' => 0);
408  $email_text = new XoopsFormLabel('', $user->getVar('email') );
409  }
410  $email_tray = new XoopsFormElementTray(_US_EMAIL, '<br />');
411  $email_tray->addElement($email_text, ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin() ) ? 1 : 0);
412  $weights[0][] = 0;
413  $elements[0][] = array('element' => $email_tray, 'required' => 0);
414  $weights[0][] = 0;
415 
416  if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) {
417  //If the user is an admin and is editing someone else
418  $pwd_text = new XoopsFormPassword('', 'password', 10, 32);
419  $pwd_text2 = new XoopsFormPassword('', 'vpass', 10, 32);
420  $pwd_tray = new XoopsFormElementTray(_US_PASSWORD . '<br />' . _US_TYPEPASSTWICE);
421  $pwd_tray->addElement($pwd_text);
422  $pwd_tray->addElement($pwd_text2);
423  $elements[0][] = array('element' => $pwd_tray, 'required' => 0); //cannot set an element tray required
424  $weights[0][] = 0;
425 
426  $level_radio = new XoopsFormRadio(_PROFILE_MA_USERLEVEL, 'level', $user->getVar('level'));
427  $level_radio->addOption(1, _PROFILE_MA_ACTIVE);
428  $level_radio->addOption(0, _PROFILE_MA_INACTIVE);
429  //$level_radio->addOption(-1, _PROFILE_MA_DISABLED);
430  $elements[0][] = array('element' => $level_radio, 'required' => 0);
431  $weights[0][] = 0;
432  }
433 
434  $elements[0][] = array('element' => new XoopsFormHidden('uid', $user->getVar('uid') ), 'required' => 0);
435  $weights[0][] = 0;
436  $elements[0][] = array('element' => new XoopsFormHidden('op', 'save'), 'required' => 0);
437  $weights[0][] = 0;
438 
439  $cat_handler = xoops_getmodulehandler('category');
440  $categories = array();
441  $all_categories = $cat_handler->getObjects(null, true, false);
442  $count_fields = count($fields);
443 
444  foreach (array_keys($fields) as $i ) {
445  if ( in_array($fields[$i]->getVar('field_id'), $editable_fields) ) {
446  // Set default value for user fields if available
447  if ($user->isNew()) {
448  $default = $fields[$i]->getVar('field_default');
449  if ($default !== '' && $default !== null) {
450  $user->setVar($fields[$i]->getVar('field_name'), $default);
451  }
452  }
453 
454  if ($profile->getVar($fields[$i]->getVar('field_name'), 'n') === null) {
455  $default = $fields[$i]->getVar('field_default', 'n');
456  $profile->setVar($fields[$i]->getVar('field_name'), $default);
457  }
458 
459  $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile);
460  $fieldinfo['required'] = $fields[$i]->getVar('field_required');
461 
462  $key = @$all_categories[$fields[$i]->getVar('cat_id')]['cat_weight'] * $count_fields + $fields[$i]->getVar('cat_id');
463  $elements[$key][] = $fieldinfo;
464  $weights[$key][] = $fields[$i]->getVar('field_weight');
465  $categories[$key] = @$all_categories[$fields[$i]->getVar('cat_id')];
466  }
467  }
468 
469  if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
470  xoops_loadLanguage('admin', 'profile');
471  $gperm_handler =& xoops_gethandler('groupperm');
472  //If user has admin rights on groups
473  include_once $GLOBALS['xoops']->path('modules/system/constants.php');
474  if ($gperm_handler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) {
475  //add group selection
476  $group_select = new XoopsFormSelectGroup(_US_GROUPS, 'groups', false, $user->getGroups(), 5, true);
477  $elements[0][] = array('element' => $group_select, 'required' => 0);
478  //set as latest;
479  $weights[0][] = $count_fields +1;
480  }
481  }
482 
483  ksort($elements);
484  foreach (array_keys($elements) as $k) {
485  array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
486  $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT;
487  $desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
488  $form->addElement(new XoopsFormLabel("<h3>{$title}</h3>", $desc), false);
489  foreach (array_keys($elements[$k]) as $i) {
490  $form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
491  }
492  }
493 
494  $form->addElement(new XoopsFormHidden('uid', $user->getVar('uid') ));
495  $form->addElement(new XoopsFormButton('', 'submit', _US_SAVECHANGES, 'submit'));
496  return $form;
497 }
498 
506 function profile_getStepForm($step = null, $action = false)
507 {
508  if ($action === false) {
509  $action = $_SERVER['REQUEST_URI'];
510  }
511  if (empty($GLOBALS['xoopsConfigUser'])) {
512  $config_handler =& xoops_gethandler('config');
513  $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
514  }
515  include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
516 
517  $form = new XoopsThemeForm(_PROFILE_AM_STEP, 'stepform', 'step.php', 'post', true);
518 
519  if (!$step->isNew()) {
520  $form->addElement(new XoopsFormHidden('id', $step->getVar('step_id') ));
521  }
522  $form->addElement(new XoopsFormHidden('op', 'save') );
523  $form->addElement(new XoopsFormText(_PROFILE_AM_STEPNAME, 'step_name', 25, 255, $step->getVar('step_name', 'e')));
524  $form->addElement(new XoopsFormText(_PROFILE_AM_STEPINTRO, 'step_desc', 25, 255, $step->getVar('step_desc', 'e')));
525  $form->addElement(new XoopsFormText(_PROFILE_AM_STEPORDER, 'step_order', 10, 10, $step->getVar('step_order', 'e')));
526  $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_STEPSAVE, 'step_save', $step->getVar('step_save', 'e')));
527  $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
528 
529  return $form;
530 }
531 
532 ?>