XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
field.php
Go to the documentation of this file.
1 <?php
20 include 'admin_header.php';
23 
24 $indexAdmin->addItemButton(_ADD . ' ' . _PROFILE_AM_FIELD, 'field.php?op=new', 'add', '');
25 
26 echo $indexAdmin->addNavigation('field.php');
27 echo $indexAdmin->renderButton('right', '');
28 
29 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : (isset($_REQUEST['id']) ? "edit" : 'list');
30 
32 
33 switch ($op) {
34  default:
35  case "list":
36  $fields = $profilefield_handler->getObjects(null, true, false);
37 
38  $module_handler =& xoops_gethandler('module');
39  $modules = $module_handler->getObjects(null, true);
40 
41  $cat_handler =& xoops_getmodulehandler('category');
42  $criteria = new CriteriaCompo();
43  $criteria->setSort('cat_weight');
44  $cats = $cat_handler->getObjects($criteria, true);
45  unset($criteria);
46 
47  $categories[0] = _PROFILE_AM_DEFAULT;
48  if (count($cats) > 0) {
49  foreach (array_keys($cats) as $i) {
50  $categories[$cats[$i]->getVar('cat_id')] = $cats[$i]->getVar('cat_title');
51  }
52  }
53  $GLOBALS['xoopsTpl']->assign('categories', $categories);
54  unset($categories);
55  $valuetypes = array( XOBJ_DTYPE_ARRAY => _PROFILE_AM_ARRAY,
63 
64  $fieldtypes = array('checkbox' => _PROFILE_AM_CHECKBOX,
65  'group' => _PROFILE_AM_GROUP,
66  'group_multi' => _PROFILE_AM_GROUPMULTI,
67  'language' => _PROFILE_AM_LANGUAGE,
68  'radio' => _PROFILE_AM_RADIO,
69  'select' => _PROFILE_AM_SELECT,
70  'select_multi' => _PROFILE_AM_SELECTMULTI,
71  'textarea' => _PROFILE_AM_TEXTAREA,
72  'dhtml' => _PROFILE_AM_DHTMLTEXTAREA,
73  'textbox' => _PROFILE_AM_TEXTBOX,
74  'timezone' => _PROFILE_AM_TIMEZONE,
75  'yesno' => _PROFILE_AM_YESNO,
76  'date' => _PROFILE_AM_DATE,
77  'datetime' => _PROFILE_AM_DATETIME,
78  'longdate' => _PROFILE_AM_LONGDATE,
79  'theme' => _PROFILE_AM_THEME,
80  'autotext' => _PROFILE_AM_AUTOTEXT,
81  'rank' => _PROFILE_AM_RANK);
82 
83  foreach (array_keys($fields) as $i) {
84  $fields[$i]['canEdit']=$fields[$i]['field_config'] || $fields[$i]['field_show'] || $fields[$i]['field_edit'];
85  $fields[$i]['canDelete'] = $fields[$i]['field_config'];
86  $fields[$i]['fieldtype'] = $fieldtypes[$fields[$i]['field_type']];
87  $fields[$i]['valuetype'] = $valuetypes[$fields[$i]['field_valuetype']];
88  $categories[$fields[$i]['cat_id']][] = $fields[$i];
89  $weights[$fields[$i]['cat_id']][] = $fields[$i]['field_weight'];
90 
91  }
92  //sort fields order in categories
93  foreach (array_keys($categories) as $i) {
94  array_multisort($weights[$i], SORT_ASC, array_keys($categories[$i]), SORT_ASC, $categories[$i]);
95  }
96  ksort($categories);
97  $GLOBALS['xoopsTpl']->assign('fieldcategories', $categories);
98  $GLOBALS['xoopsTpl']->assign('token', $GLOBALS['xoopsSecurity']->getTokenHTML());
99  $template_main = "profile_admin_fieldlist.html";
100  break;
101 
102  case "new":
103  include_once('../include/forms.php');
104  $obj =& $profilefield_handler->create();
105  $form = profile_getFieldForm($obj);
106  $form->display();
107  break;
108 
109  case "edit":
110  $obj =& $profilefield_handler->get($_REQUEST['id']);
111  if (!$obj->getVar('field_config') && !$obj->getVar('field_show') && !$obj->getVar('field_edit')) { //If no configs exist
113  }
114  include_once('../include/forms.php');
115  $form = profile_getFieldForm($obj);
116  $form->display();
117  break;
118 
119  case "reorder":
120  if (!$GLOBALS['xoopsSecurity']->check()) {
121  redirect_header('field.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
122  }
123  if (isset($_POST['field_ids']) && count($_POST['field_ids']) > 0) {
124  $oldweight = $_POST['oldweight'];
125  $oldcat = $_POST['oldcat'];
126  $category = $_POST['category'];
127  $weight = $_POST['weight'];
128  $ids = array();
129  foreach ($_POST['field_ids'] as $field_id) {
130  if ($oldweight[$field_id] != $weight[$field_id] || $oldcat[$field_id] != $category[$field_id]) {
131  //if field has changed
132  $ids[] = intval($field_id);
133  }
134  }
135  if (count($ids) > 0) {
136  $errors = array();
137  //if there are changed fields, fetch the fieldcategory objects
139  $fields = $field_handler->getObjects(new Criteria('field_id', "(" . implode(',', $ids) . ")", "IN"), true);
140  foreach ($ids as $i) {
141  $fields[$i]->setVar('field_weight', intval($weight[$i]));
142  $fields[$i]->setVar('cat_id', intval($category[$i]));
143  if (!$field_handler->insert($fields[$i])) {
144  $errors = array_merge($errors, $fields[$i]->getErrors());
145  }
146  }
147  if (count($errors) == 0) {
148  //no errors
150  } else {
151  redirect_header('field.php', 3, implode('<br />', $errors));
152  }
153  }
154  }
155  break;
156 
157  case "save":
158  if (!$GLOBALS['xoopsSecurity']->check()) {
159  redirect_header('field.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
160  }
161  $redirect_to_edit = false;
162  if (isset($_REQUEST['id'])) {
163  $obj =& $profilefield_handler->get($_REQUEST['id']);
164  if (!$obj->getVar('field_config') && !$obj->getVar('field_show') && !$obj->getVar('field_edit')) { //If no configs exist
166  }
167  } else {
168  $obj =& $profilefield_handler->create();
169  $obj->setVar('field_name', $_REQUEST['field_name']);
170  $obj->setVar('field_moduleid', $GLOBALS['xoopsModule']->getVar('mid'));
171  $obj->setVar('field_show', 1);
172  $obj->setVar('field_edit', 1);
173  $obj->setVar('field_config', 1);
174  $redirect_to_edit = true;
175  }
176  $obj->setVar('field_title', $_REQUEST['field_title']);
177  $obj->setVar('field_description', $_REQUEST['field_description']);
178  if ($obj->getVar('field_config')) {
179  $obj->setVar('field_type', $_REQUEST['field_type']);
180  if (isset($_REQUEST['field_valuetype'])) {
181  $obj->setVar('field_valuetype', $_REQUEST['field_valuetype']);
182  }
183  $options = $obj->getVar('field_options');
184 
185  if (isset($_REQUEST['removeOptions']) && is_array($_REQUEST['removeOptions'])) {
186  foreach ($_REQUEST['removeOptions'] as $index) {
187  unset($options[$index]);
188  }
189  $redirect_to_edit = true;
190  }
191 
192  if (!empty($_REQUEST['addOption'])) {
193  foreach ($_REQUEST['addOption'] as $option) {
194  if (empty($option['value'])) continue;
195  $options[$option['key']] = $option['value'];
196  $redirect_to_edit = true;
197  }
198  }
199  $obj->setVar('field_options', $options);
200  }
201  if ($obj->getVar('field_edit')) {
202  $required = isset($_REQUEST['field_required']) ? $_REQUEST['field_required'] : 0;
203  $obj->setVar('field_required', $required); //0 = no, 1 = yes
204  if (isset($_REQUEST['field_maxlength'])) {
205  $obj->setVar('field_maxlength', $_REQUEST['field_maxlength']);
206  }
207  if (isset($_REQUEST['field_default'])) {
208  $field_default = $obj->getValueForSave($_REQUEST['field_default']);
209  //Check for multiple selections
210  if (is_array($field_default)) {
211  $obj->setVar('field_default', serialize($field_default));
212  } else {
213  $obj->setVar('field_default', $field_default);
214  }
215  }
216  }
217 
218  if ($obj->getVar('field_show')) {
219  $obj->setVar('field_weight', $_REQUEST['field_weight']);
220  $obj->setVar('cat_id', $_REQUEST['field_category']);
221  }
222  if ( /*$obj->getVar('field_edit') && */isset($_REQUEST['step_id']) ) {
223  $obj->setVar('step_id', $_REQUEST['step_id']);
224  }
225  if ($profilefield_handler->insert($obj)) {
226  $groupperm_handler =& xoops_gethandler('groupperm');
227 
228  $perm_arr = array();
229  if ($obj->getVar('field_show')) {
230  $perm_arr[] = 'profile_show';
231  $perm_arr[] = 'profile_visible';
232  }
233  if ($obj->getVar('field_edit')) {
234  $perm_arr[] = 'profile_edit';
235  }
236  if ($obj->getVar('field_edit') || $obj->getVar('field_show')) {
237  $perm_arr[] = 'profile_search';
238  }
239  if (count($perm_arr) > 0) {
240  foreach ($perm_arr as $perm) {
241  $criteria = new CriteriaCompo(new Criteria('gperm_name', $perm));
242  $criteria->add(new Criteria('gperm_itemid', intval($obj->getVar('field_id'))));
243  $criteria->add(new Criteria('gperm_modid', intval($GLOBALS['xoopsModule']->getVar('mid'))));
244  if (isset($_REQUEST[$perm]) && is_array($_REQUEST[$perm])) {
245  $perms = $groupperm_handler->getObjects($criteria);
246  if (count($perms) > 0) {
247  foreach (array_keys($perms) as $i) {
248  $groups[$perms[$i]->getVar('gperm_groupid')] =& $perms[$i];
249  }
250  } else {
251  $groups = array();
252  }
253  foreach ($_REQUEST[$perm] as $groupid) {
254  $groupid = intval($groupid);
255  if (!isset($groups[$groupid])) {
256  $perm_obj =& $groupperm_handler->create();
257  $perm_obj->setVar('gperm_name', $perm);
258  $perm_obj->setVar('gperm_itemid', intval($obj->getVar('field_id')));
259  $perm_obj->setVar('gperm_modid', $GLOBALS['xoopsModule']->getVar('mid'));
260  $perm_obj->setVar('gperm_groupid', $groupid);
261  $groupperm_handler->insert($perm_obj);
262  unset($perm_obj);
263  }
264  }
265  $removed_groups = array_diff(array_keys($groups), $_REQUEST[$perm]);
266  if (count($removed_groups) > 0) {
267  $criteria->add(new Criteria('gperm_groupid', "(" . implode(',', $removed_groups) . ")", "IN"));
268  $groupperm_handler->deleteAll($criteria);
269  }
270  unset($groups);
271 
272  } else {
273  $groupperm_handler->deleteAll($criteria);
274  }
275  unset($criteria);
276  }
277  }
278  $url = $redirect_to_edit ? 'field.php?op=edit&amp;id=' . $obj->getVar('field_id') : 'field.php';
279  redirect_header($url, 3, sprintf(_PROFILE_AM_SAVEDSUCCESS, _PROFILE_AM_FIELD));
280  }
281  include_once('../include/forms.php');
282  echo $obj->getHtmlErrors();
283  $form = profile_getFieldForm($obj);
284  $form->display();
285  break;
286 
287  case "delete":
288  $obj =& $profilefield_handler->get($_REQUEST['id']);
289  if (!$obj->getVar('field_config')) {
291  }
292  if (isset($_REQUEST['ok']) && $_REQUEST['ok'] == 1) {
293  if (!$GLOBALS['xoopsSecurity']->check()) {
294  redirect_header('field.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
295  }
296  if ($profilefield_handler->delete($obj)) {
297  redirect_header('field.php', 3, sprintf(_PROFILE_AM_DELETEDSUCCESS, _PROFILE_AM_FIELD));
298  } else {
299  echo $obj->getHtmlErrors();
300  }
301  } else {
302  xoops_confirm(array('ok' => 1,'id' => $_REQUEST['id'],'op' => 'delete'), $_SERVER['REQUEST_URI'], sprintf(_PROFILE_AM_RUSUREDEL, $obj->getVar('field_title')));
303  }
304  break;
305 
306  case 'toggle':
307  if (isset($_REQUEST['field_id'])) {
308  $field_id = intval($_REQUEST['field_id']);
309  if (isset($_REQUEST['field_required'])) {
310  $field_required = intval($_REQUEST['field_required']);
311  profile_visible_toggle($field_id, $field_required);
312  }
313  }
314  break;
315 }
316 
317 if (isset($template_main)) {
318  $GLOBALS['xoopsTpl']->display("db:{$template_main}");
319 }
320 
321 function profile_visible_toggle($field_id, $field_required)
322 {
323  $field_required = ($field_required == 1) ? 0 : 1;
324  $this_handler =& xoops_getModuleHandler('field', 'profile');
325  $obj = $this_handler->get($field_id);
326  $obj->setVar('field_required', $field_required);
327  if ($this_handler->insert($obj, true)) {
329  } else {
331  }
332 }
333 
334 
335 include 'admin_footer.php';