1 | <?php |
||
2 | /** |
||
3 | * Extended User Profile |
||
4 | * |
||
5 | * You may not change or alter any portion of this comment or credits |
||
6 | * of supporting developers from this source code or any supporting source code |
||
7 | * which is considered copyrighted (c) material of the original comment or credit authors. |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
11 | * |
||
12 | * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
||
13 | * @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html) |
||
14 | * @package profile |
||
15 | * @since 2.3.0 |
||
16 | * @author Jan Pedersen |
||
17 | * @author Taiwen Jiang <[email protected]> |
||
18 | */ |
||
19 | |||
20 | use XoopsModules\Suico; |
||
21 | |||
22 | include_once __DIR__ . '/admin_header.php'; |
||
23 | xoops_cp_header(); |
||
24 | $adminObject->addItemButton(_AM_SUICO_FIELD, 'fieldslist.php?op=new', 'add'); |
||
25 | $adminObject->displayNavigation(basename(__FILE__)); |
||
26 | $adminObject->displayButton('left'); |
||
27 | $op = $_REQUEST['op'] ?? (isset($_REQUEST['id']) ? 'edit' : 'list'); |
||
28 | /* @var Suico\FieldHandler $fieldHandler */ |
||
29 | $fieldHandler = $helper->getHandler('Field'); |
||
30 | switch ($op) { |
||
31 | default: |
||
32 | case 'list': |
||
33 | $fields = $fieldHandler->getObjects(null, true, false); |
||
34 | /* @var XoopsModuleHandler $moduleHandler */ |
||
35 | $moduleHandler = xoops_getHandler('module'); |
||
36 | $modules = $moduleHandler->getObjects(null, true); |
||
37 | /* @var Suico\CategoryHandler $categoryHandler */ |
||
38 | $categoryHandler = $helper->getHandler('Category'); |
||
39 | $criteria = new CriteriaCompo(); |
||
40 | $criteria->setSort('cat_weight'); |
||
41 | $cats = $categoryHandler->getObjects($criteria, true); |
||
42 | unset($criteria); |
||
43 | $categories[0] = _AM_SUICO_DEFAULT; |
||
44 | if (count($cats) > 0) { |
||
45 | foreach (array_keys($cats) as $i) { |
||
46 | $categories[$cats[$i]->getVar('cat_id')] = $cats[$i]->getVar('cat_title'); |
||
47 | } |
||
48 | } |
||
49 | $GLOBALS['xoopsTpl']->assign('categories', $categories); |
||
50 | unset($categories); |
||
51 | $valuetypes = [ |
||
52 | XOBJ_DTYPE_ARRAY => _AM_SUICO_ARRAY, |
||
53 | XOBJ_DTYPE_EMAIL => _AM_SUICO_EMAIL, |
||
54 | XOBJ_DTYPE_INT => _AM_SUICO_INT, |
||
55 | XOBJ_DTYPE_TXTAREA => _AM_SUICO_TXTAREA, |
||
56 | XOBJ_DTYPE_TXTBOX => _AM_SUICO_TXTBOX, |
||
57 | XOBJ_DTYPE_URL => _AM_SUICO_URL, |
||
58 | XOBJ_DTYPE_OTHER => _AM_SUICO_OTHER, |
||
59 | XOBJ_DTYPE_MTIME => _AM_SUICO_DATE, |
||
60 | ]; |
||
61 | $fieldtypes = [ |
||
62 | 'checkbox' => _AM_SUICO_CHECKBOX, |
||
63 | 'group' => _AM_SUICO_GROUP, |
||
64 | 'group_multi' => _AM_SUICO_GROUPMULTI, |
||
65 | 'language' => _AM_SUICO_LANGUAGE, |
||
66 | 'radio' => _AM_SUICO_RADIO, |
||
67 | 'select' => _AM_SUICO_SELECT, |
||
68 | 'select_multi' => _AM_SUICO_SELECTMULTI, |
||
69 | 'textarea' => _AM_SUICO_TEXTAREA, |
||
70 | 'dhtml' => _AM_SUICO_DHTMLTEXTAREA, |
||
71 | 'textbox' => _AM_SUICO_TEXTBOX, |
||
72 | 'timezone' => _AM_SUICO_TIMEZONE, |
||
73 | 'yesno' => _AM_SUICO_YESNO, |
||
74 | 'date' => _AM_SUICO_DATE, |
||
75 | 'datetime' => _AM_SUICO_DATETIME, |
||
76 | 'longdate' => _AM_SUICO_LONGDATE, |
||
77 | 'theme' => _AM_SUICO_THEME, |
||
78 | 'autotext' => _AM_SUICO_AUTOTEXT, |
||
79 | 'rank' => _AM_SUICO_RANK, |
||
80 | ]; |
||
81 | foreach (array_keys($fields) as $i) { |
||
82 | $fields[$i]['canEdit'] = $fields[$i]['field_config'] || $fields[$i]['field_show'] || $fields[$i]['field_edit']; |
||
83 | $fields[$i]['canDelete'] = $fields[$i]['field_config']; |
||
84 | $fields[$i]['fieldtype'] = $fieldtypes[$fields[$i]['field_type']]; |
||
85 | $fields[$i]['valuetype'] = $valuetypes[$fields[$i]['field_valuetype']]; |
||
86 | $categories[$fields[$i]['cat_id']][] = $fields[$i]; |
||
87 | $weights[$fields[$i]['cat_id']][] = $fields[$i]['field_weight']; |
||
88 | } |
||
89 | //sort fields order in categories |
||
90 | foreach (array_keys($categories) as $i) { |
||
91 | array_multisort($weights[$i], SORT_ASC, array_keys($categories[$i]), SORT_ASC, $categories[$i]); |
||
92 | } |
||
93 | ksort($categories); |
||
94 | $GLOBALS['xoopsTpl']->assign('fieldcategories', $categories); |
||
95 | $GLOBALS['xoopsTpl']->assign('token', $GLOBALS['xoopsSecurity']->getTokenHTML()); |
||
96 | $template_main = 'admin/suico_admin_fieldslist.tpl'; |
||
97 | break; |
||
98 | case 'new': |
||
99 | include_once dirname(__DIR__) . '/include/forms.php'; |
||
100 | $obj = $fieldHandler->create(); |
||
101 | $form = suico_getFieldForm($obj); |
||
102 | $form->display(); |
||
103 | break; |
||
104 | case 'edit': |
||
105 | $obj = $fieldHandler->get($_REQUEST['id']); |
||
106 | if (!$obj->getVar('field_config') && !$obj->getVar('field_show') && !$obj->getVar('field_edit')) { //If no configs exist |
||
107 | redirect_header('fieldslist.php', 2, _AM_SUICO_FIELDNOTCONFIGURABLE); |
||
108 | } |
||
109 | include_once dirname(__DIR__) . '/include/forms.php'; |
||
110 | $form = suico_getFieldForm($obj); |
||
111 | $form->display(); |
||
112 | break; |
||
113 | case 'reorder': |
||
114 | if (!$GLOBALS['xoopsSecurity']->check()) { |
||
115 | redirect_header('fieldslist.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
||
116 | } |
||
117 | if (isset($_POST['field_ids']) && count($_POST['field_ids']) > 0) { |
||
118 | $oldweight = $_POST['oldweight']; |
||
119 | $oldcat = $_POST['oldcat']; |
||
120 | $category = $_POST['category']; |
||
121 | $weight = $_POST['weight']; |
||
122 | $ids = []; |
||
123 | foreach ($_POST['field_ids'] as $field_id) { |
||
124 | if ($oldweight[$field_id] != $weight[$field_id] || $oldcat[$field_id] != $category[$field_id]) { |
||
125 | //if field has changed |
||
126 | $ids[] = (int)$field_id; |
||
127 | } |
||
128 | } |
||
129 | if (count($ids) > 0) { |
||
130 | $errors = []; |
||
131 | //if there are changed fields, fetch the fieldcategory objects |
||
132 | /* @var XoopsModuleHandler $fieldHandler */ |
||
133 | $fieldHandler = $helper->getHandler('Field'); |
||
134 | $fields = $fieldHandler->getObjects(new Criteria('field_id', '(' . implode(',', $ids) . ')', 'IN'), true); |
||
135 | foreach ($ids as $i) { |
||
136 | $fields[$i]->setVar('field_weight', (int)$weight[$i]); |
||
137 | $fields[$i]->setVar('cat_id', (int)$category[$i]); |
||
138 | if (!$fieldHandler->insert($fields[$i])) { |
||
139 | $errors = array_merge($errors, $fields[$i]->getErrors()); |
||
140 | } |
||
141 | } |
||
142 | if (0 == count($errors)) { |
||
143 | //no errors |
||
144 | redirect_header('fieldslist.php', 2, sprintf(_AM_SUICO_SAVEDSUCCESS, _AM_SUICO_FIELDS)); |
||
145 | } else { |
||
146 | redirect_header('fieldslist.php', 3, implode('<br>', $errors)); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | break; |
||
151 | case 'save': |
||
152 | if (!$GLOBALS['xoopsSecurity']->check()) { |
||
153 | redirect_header('fieldslist.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
||
154 | } |
||
155 | $redirect_to_edit = false; |
||
156 | if (isset($_REQUEST['id'])) { |
||
157 | $obj = $fieldHandler->get($_REQUEST['id']); |
||
158 | if (!$obj->getVar('field_config') && !$obj->getVar('field_show') && !$obj->getVar('field_edit')) { //If no configs exist |
||
159 | redirect_header('admin.php', 2, _AM_SUICO_FIELDNOTCONFIGURABLE); |
||
160 | } |
||
161 | } else { |
||
162 | $obj = $fieldHandler->create(); |
||
163 | $obj->setVar('field_name', $_REQUEST['field_name']); |
||
164 | $obj->setVar('field_moduleid', $GLOBALS['xoopsModule']->getVar('mid')); |
||
165 | $obj->setVar('field_show', 1); |
||
166 | $obj->setVar('field_edit', 1); |
||
167 | $obj->setVar('field_config', 1); |
||
168 | $redirect_to_edit = true; |
||
169 | } |
||
170 | $obj->setVar('field_title', $_REQUEST['field_title']); |
||
171 | $obj->setVar('field_description', $_REQUEST['field_description']); |
||
172 | if ($obj->getVar('field_config')) { |
||
173 | $obj->setVar('field_type', $_REQUEST['field_type']); |
||
174 | if (isset($_REQUEST['field_valuetype'])) { |
||
175 | $obj->setVar('field_valuetype', $_REQUEST['field_valuetype']); |
||
176 | } |
||
177 | $options = $obj->getVar('field_options'); |
||
178 | if (isset($_REQUEST['removeOptions']) && is_array($_REQUEST['removeOptions'])) { |
||
179 | foreach ($_REQUEST['removeOptions'] as $index) { |
||
180 | unset($options[$index]); |
||
181 | } |
||
182 | $redirect_to_edit = true; |
||
183 | } |
||
184 | if (!empty($_REQUEST['addOption'])) { |
||
185 | foreach ($_REQUEST['addOption'] as $option) { |
||
186 | if (empty($option['value'])) { |
||
187 | continue; |
||
188 | } |
||
189 | $options[$option['key']] = $option['value']; |
||
190 | $redirect_to_edit = true; |
||
191 | } |
||
192 | } |
||
193 | $obj->setVar('field_options', $options); |
||
194 | } |
||
195 | if ($obj->getVar('field_edit')) { |
||
196 | $required = $_REQUEST['field_required'] ?? 0; |
||
197 | $obj->setVar('field_required', $required); //0 = no, 1 = yes |
||
198 | if (isset($_REQUEST['field_maxlength'])) { |
||
199 | $obj->setVar('field_maxlength', $_REQUEST['field_maxlength']); |
||
200 | } |
||
201 | if (isset($_REQUEST['field_default'])) { |
||
202 | $field_default = $obj->getValueForSave($_REQUEST['field_default']); |
||
203 | //Check for multiple selections |
||
204 | if (is_array($field_default)) { |
||
205 | $obj->setVar('field_default', serialize($field_default)); |
||
206 | } else { |
||
207 | $obj->setVar('field_default', $field_default); |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | if ($obj->getVar('field_show')) { |
||
212 | $obj->setVar('field_weight', $_REQUEST['field_weight']); |
||
213 | $obj->setVar('cat_id', $_REQUEST['field_category']); |
||
214 | } |
||
215 | if (/*$obj->getVar('field_edit') && */ |
||
216 | isset($_REQUEST['step_id'])) { |
||
217 | $obj->setVar('step_id', $_REQUEST['step_id']); |
||
218 | } |
||
219 | if ($fieldHandler->insert($obj)) { |
||
220 | /* @var XoopsGroupPermHandler $grouppermHandler */ |
||
221 | $grouppermHandler = xoops_getHandler('groupperm'); |
||
222 | $perm_arr = []; |
||
223 | if ($obj->getVar('field_show')) { |
||
224 | $perm_arr[] = 'profile_show'; |
||
225 | $perm_arr[] = 'profile_visible'; |
||
226 | } |
||
227 | if ($obj->getVar('field_edit')) { |
||
228 | $perm_arr[] = 'profile_edit'; |
||
229 | } |
||
230 | if ($obj->getVar('field_edit') || $obj->getVar('field_show')) { |
||
231 | $perm_arr[] = 'profile_search'; |
||
232 | } |
||
233 | if (count($perm_arr) > 0) { |
||
234 | foreach ($perm_arr as $perm) { |
||
235 | $criteria = new CriteriaCompo(new Criteria('gperm_name', $perm)); |
||
236 | $criteria->add(new Criteria('gperm_itemid', (int)$obj->getVar('field_id'))); |
||
237 | $criteria->add(new Criteria('gperm_modid', (int)$GLOBALS['xoopsModule']->getVar('mid'))); |
||
238 | if (isset($_REQUEST[$perm]) && is_array($_REQUEST[$perm])) { |
||
239 | $perms = $grouppermHandler->getObjects($criteria); |
||
240 | if (count($perms) > 0) { |
||
241 | foreach (array_keys($perms) as $i) { |
||
242 | $groups[$perms[$i]->getVar('gperm_groupid')] = &$perms[$i]; |
||
243 | } |
||
244 | } else { |
||
245 | $groups = []; |
||
246 | } |
||
247 | foreach ($_REQUEST[$perm] as $groupid) { |
||
248 | $groupid = (int)$groupid; |
||
249 | if (!isset($groups[$groupid])) { |
||
250 | $perm_obj = $grouppermHandler->create(); |
||
251 | $perm_obj->setVar('gperm_name', $perm); |
||
252 | $perm_obj->setVar('gperm_itemid', (int)$obj->getVar('field_id')); |
||
253 | $perm_obj->setVar('gperm_modid', $GLOBALS['xoopsModule']->getVar('mid')); |
||
254 | $perm_obj->setVar('gperm_groupid', $groupid); |
||
255 | $grouppermHandler->insert($perm_obj); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
256 | unset($perm_obj); |
||
257 | } |
||
258 | } |
||
259 | $removed_groups = array_diff(array_keys($groups), $_REQUEST[$perm]); |
||
260 | if (count($removed_groups) > 0) { |
||
261 | $criteria->add(new Criteria('gperm_groupid', '(' . implode(',', $removed_groups) . ')', 'IN')); |
||
262 | $grouppermHandler->deleteAll($criteria); |
||
263 | } |
||
264 | unset($groups); |
||
265 | } else { |
||
266 | $grouppermHandler->deleteAll($criteria); |
||
267 | } |
||
268 | unset($criteria); |
||
269 | } |
||
270 | } |
||
271 | $url = $redirect_to_edit ? 'fieldslist.php?op=edit&id=' . $obj->getVar('field_id') : 'fieldslist.php'; |
||
272 | redirect_header($url, 3, sprintf(_AM_SUICO_SAVEDSUCCESS, _AM_SUICO_FIELD)); |
||
273 | } |
||
274 | include_once dirname(__DIR__) . '/include/forms.php'; |
||
275 | echo $obj->getHtmlErrors(); |
||
276 | $form = suico_getFieldForm($obj); |
||
277 | $form->display(); |
||
278 | break; |
||
279 | case 'delete': |
||
280 | $obj = $fieldHandler->get($_REQUEST['id']); |
||
281 | if (!$obj->getVar('field_config')) { |
||
282 | redirect_header('index.php', 2, _AM_SUICO_FIELDNOTCONFIGURABLE); |
||
283 | } |
||
284 | if (isset($_REQUEST['ok']) && 1 == $_REQUEST['ok']) { |
||
285 | if (!$GLOBALS['xoopsSecurity']->check()) { |
||
286 | redirect_header('fieldslist.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
||
287 | } |
||
288 | if ($fieldHandler->delete($obj)) { |
||
289 | redirect_header('fieldslist.php', 3, sprintf(_AM_SUICO_DELETEDSUCCESS, _AM_SUICO_FIELD)); |
||
290 | } else { |
||
291 | echo $obj->getHtmlErrors(); |
||
292 | } |
||
293 | } else { |
||
294 | xoops_confirm( |
||
295 | [ |
||
296 | 'ok' => 1, |
||
297 | 'id' => $_REQUEST['id'], |
||
298 | 'op' => 'delete', |
||
299 | ], |
||
300 | $_SERVER['REQUEST_URI'], |
||
301 | sprintf(_AM_SUICO_RUSUREDEL, $obj->getVar('field_title')) |
||
302 | ); |
||
303 | } |
||
304 | break; |
||
305 | case 'toggle': |
||
306 | if (isset($_REQUEST['field_id'])) { |
||
307 | $field_id = (int)$_REQUEST['field_id']; |
||
308 | if (isset($_REQUEST['field_required'])) { |
||
309 | $field_required = (int)$_REQUEST['field_required']; |
||
310 | suico_visible_toggle($field_id, $field_required, $helper); |
||
311 | } |
||
312 | } |
||
313 | break; |
||
314 | } |
||
315 | if (isset($template_main)) { |
||
316 | $GLOBALS['xoopsTpl']->display("db:{$template_main}"); |
||
317 | } |
||
318 | /** |
||
319 | * @param $field_id |
||
320 | * @param $field_required |
||
321 | */ |
||
322 | function suico_visible_toggle($field_id, $field_required, $helper) |
||
323 | { |
||
324 | $field_required = (1 == $field_required) ? 0 : 1; |
||
325 | $fieldHandler = $helper->getHandler('Field'); |
||
326 | $obj = $fieldHandler->get($field_id); |
||
327 | $obj->setVar('field_required', $field_required); |
||
328 | if ($fieldHandler->insert($obj, true)) { |
||
329 | redirect_header('fieldslist.php', 1, _AM_SUICO_REQUIRED_TOGGLE_SUCCESS); |
||
330 | } else { |
||
331 | redirect_header('fieldslist.php', 1, _AM_SUICO_REQUIRED_TOGGLE_FAILED); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | include_once __DIR__ . '/admin_footer.php'; |
||
336 |