-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsettings.php
376 lines (326 loc) · 12.2 KB
/
settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
// $Id$
// --------------------------------------------------------------
// Red México Common Utilities
// A framework for Red México Modules
// Author: Eduardo Cortés <i.bitcero@gmail.com>
// Email: i.bitcero@gmail.com
// License: GPL 2.0
// --------------------------------------------------------------
require_once dirname(__DIR__) . '/../include/cp_header.php';
//require_once XOOPS_ROOT_PATH . '/modules/rmcommon/admin-loader.php';
$common->location = 'cu-settings';
class AjaxResponse
{
use RMModuleAjax;
}
/**
* Shows all modules that can be configured with this tool
*/
function show_configurable_items()
{
global $xoopsModule, $cuSettings, $xoopsModuleConfig, $rmTpl;
$rmTpl->add_style('settings.css', 'rmcommon', ['footer' => 1]);
$db = XoopsDatabaseFactory::getDatabaseConnection();
$sql = 'SELECT mid FROM ' . $db->prefix('modules') . " WHERE dirname='system' OR hasconfig=1 ORDER BY name ASC";
$result = $db->query($sql);
$mh = xoops_getHandler('module');
$modules = [];
while (false !== ($row = $db->fetchArray($result))) {
$mod = $mh->get($row['mid']);
$modules[] = [
'id' => $mod->mid(),
'name' => $mod->getVar('name'),
'logo' => XOOPS_URL . '/modules/' . $mod->getVar('dirname') . '/' . $mod->getInfo('image'),
];
}
$rmTpl->header();
include $rmTpl->get_template('rmc-settings.php', 'module', 'rmcommon');
$rmTpl->footer();
}
/**
* Show the preferences for a specific module
*/
function show_module_preferences()
{
global $rmTpl, $xoopsSecurity, $cuIcons;
$quick = RMHttpRequest::get('popup', 'integer', 0);
$token = RMHttpRequest::get('CUTOKEN_REQUEST', 'string', '');
$ajax = new AjaxResponse();
$is_popup = 1 == $quick && '' != $token;
if ($is_popup) {
$ajax->prepare();
if (!$xoopsSecurity->validateToken(false, true, 'CUTOKEN')) {
$ajax->response(
__('Unauthorized action', 'rmcommon'),
1,
0,
['reload' => true]
);
}
}
$mod = RMHttpRequest::get('mod', 'integer', 0);
if ($mod <= 0) {
RMUris::redirect_with_message(
__('You have not specified a module!', 'rmcommon'),
'settings.php',
RMMSG_WARN
);
}
$mh = xoops_getHandler('module');
$module = $mh->get($mod);
if ($module->isNew()) {
RMUris::redirect_with_message(
__('You have not specified a valid module ID!', 'rmcommon'),
'settings.php',
RMMSG_ERROR
);
}
/**
* Verify if module is rmcommon native or not
*/
if (1 != $module->getInfo('rmnative')) {
RMUris::redirect_with_message(
__('This module can not be configured with Common Utilities', 'rmcommon'),
XOOPS_URL . '/modules/system/admin.php?fct=preferences&op=showmod&mod=' . $mod,
RMMSG_INFO
);
}
/*
Cargamos los valores y los datos para formar los campos
*/
$values = RMSettings::module_settings($module->getVar('dirname'));
$configs = $module->getInfo('config');
$settings_categories = $module->getInfo('categories');
$categories = [];
if (empty($settings_categories)) {
$categories = [
'all' => [
'caption' => __('Preferences', 'rmcommon'),
],
];
} else {
foreach ($settings_categories as $category => $caption) {
// Verify if category has been provided in array format
if (is_array($caption) && array_key_exists('caption', $caption)) {
$categories[$category] = $caption;
} else {
$categories[$category] = ['caption' => $caption];
}
}
}
unset($settings_categories);
$fields = []; // Container for all fields and values
foreach ($configs as $option) {
$name = ucfirst($module->getVar('dirname')) . '[' . $option['name'] . ']';
$field = new stdClass();
$field->name = $name;
$field->id = $option['name'];
$field->value = isset($values->{$option['name']}) ? $values->{$option['name']} : $option['default'];
$field->caption = defined($option['title']) ? constant($option['title']) : $option['title'];
$field->description = defined($option['description']) ? constant($option['description']) : $option['description'];
$field->field = $option['formtype'];
$field->type = $option['valuetype'];
$field->options = isset($option['options']) ? $option['options'] : null;
$category = isset($option['category']) ? $option['category'] : 'all';
if (isset($categories[$category])) {
$categories[$category]['fields'][$field->id] = $field;
} else {
if (!isset($categories['all'])) {
$categories['all'] = ['caption' => __('Preferences', 'rmcommon')];
}
$categories['all']['fields'][$field->id] = $field;
}
}
$categories = RMEvents::get()->trigger('rmcommon.settings.fields', $categories, $module);
$rmTpl->add_style('settings.css', 'rmcommon', ['footer' => 1]);
/* Breadcrumb */
$bc = RMBreadCrumb::get();
if ($module->getVar('hasadmin')) {
$bc->add_crumb($module->getVar('name'), XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/' . $module->getVar('adminindex'));
} else {
$bc->add_crumb($module->getVar('name'), '');
}
$bc->add_crumb(__('Settings', 'rmcommon'));
$rmTpl->assign('xoops_pagetitle', sprintf(__('%s Settings', 'rmcommon'), $module->getVar('name')));
if (!$is_popup) {
$rmTpl->header();
require $rmTpl->get_template('rmc-settings-form.php', 'module', 'rmcommon');
$rmTpl->footer();
} else {
ob_start();
require $rmTpl->get_template('rmc-settings-form.php', 'module', 'rmcommon');
$response = ob_get_clean();
$ajax->ajax_response(
sprintf(__('%s Settings', 'rmcommon'), $module->getVar('name')),
0,
1,
[
'content' => $response,
'width' => 'large',
'closeButton' => 1,
'id' => 'cu-settings-form',
'color' => 'primary',
'solid' => true,
]
);
}
}
/**
* Save module settings
*/
function save_module_settings()
{
global $xoopsSecurity, $xoopsDB, $common;
$mod = RMHttpRequest::post('mod', 'string', '');
$via_ajax = RMHttpRequest::post('via_ajax', 'integer', 0);
if ($via_ajax) {
$ajax = new AjaxResponse();
$common->ajax()->prepare();
}
if ('' == $mod) {
RMUris::redirect_with_message(__('A module has not been specified!', 'rmcommon'), 'settings.php', RMMSG_ERROR);
}
//echo RMHttpRequest::request('CUTOKEN_REQUEST', 'string', '') . ' ' . print_r($_SESSION['CUTOKEN_SESSION'], true); die();
if (!$xoopsSecurity->check(true, false, $via_ajax ? 'CUTOKEN' : 'XOOPS_TOKEN')) {
if ($via_ajax) {
$ajax->ajax_response(
__('Session token expired. Please try again.', 'rmcommon'),
1,
0
);
} else {
RMUris::redirect_with_message(__('Session token expired. Please try again.', 'rmcommon'), 'settings.php', RMMSG_WARN);
}
}
$module = RMModules::load_module($mod);
if (!$module) {
if ($via_ajax) {
$ajax->ajax_response(
__('The specified module does not exists.', 'rmcommon'),
1,
1
);
} else {
RMUris::redirect_with_message(__('The specified module does not exists.', 'rmcommon'), 'settings.php', RMMSG_ERROR);
}
}
$current_settings = (array) RMSettings::module_settings($module->getVar('dirname'));
$new_settings = RMHttpRequest::post(ucfirst($module->getVar('dirname')), 'array', []);
$configs = &$module->getInfo('config');
$fields = []; // Container for all fields and values
foreach ($configs as $option) {
$id = $option['name'];
$field = new stdClass();
$field->id = $id;
$field->value = isset($values->$id) ? $values->$id : $option['default'];
$field->caption = defined($option['title']) ? constant($option['title']) : $option['title'];
$field->description = defined($option['description']) ? constant($option['description']) : $option['description'];
$field->field = $option['formtype'];
$field->type = $option['valuetype'];
$field->options = isset($option['options']) ? $option['options'] : null;
$category = isset($option['category']) ? $option['category'] : 'all';
$fields[$id] = $field;
}
/**
* This keys already exists in database
*/
$to_save = array_intersect_key($new_settings, $current_settings);
/**
* This settings will be added to database beacause don't exists in table
*/
$to_add = array_diff_key($new_settings, $current_settings);
/**
* This keys has been removed from xoops_version.php file and then
* must be removed from table
*/
$to_delete = array_diff_key($current_settings, $new_settings);
$errors = ''; // Errors ocurred while saving
/**
* First for all, remove unused items
*/
$keys = array_keys($to_delete);
if (!empty($keys)) {
$sql = 'DELETE FROM ' . $xoopsDB->prefix('config') . ' WHERE conf_modid = ' . $module->mid() . " AND (conf_name = '" . implode("' OR conf_name='", $keys) . "')";
if (!$xoopsDB->queryF($sql)) {
$errors .= $xoopsDB->error() . '<br>';
}
}
/**
* Save existing items
*/
if (!empty($to_save)) {
foreach ($to_save as $name => $value) {
$item = new Rmcommon_Config_Item($name, $module->mid());
if (isset($fields[$name])) {
$item->setVar('conf_valuetype', $fields[$name]->type);
$item->setVar('conf_title', $fields[$name]->caption);
$item->setVar('conf_desc', $fields[$name]->description);
$item->setVar('conf_formtype', $fields[$name]->field);
}
$item->set_value($value, $item->getVar('conf_valuetype'));
$item->save();
}
}
/**
* Add new items
*/
if (!empty($to_add)) {
foreach ($to_add as $name => $value) {
$item = new Rmcommon_Config_Item($name, $module->mid());
if (isset($fields[$name])) {
$item->setVar('conf_modid', $module->mid());
$item->setVar('conf_name', $name);
$item->setVar('conf_valuetype', $fields[$name]->type);
$item->setVar('conf_title', $fields[$name]->caption);
$item->setVar('conf_desc', $fields[$name]->description);
$item->setVar('conf_formtype', $fields[$name]->field);
}
$item->set_value($value, $item->getVar('conf_valuetype'));
$item->save();
}
}
/**
* Notify to system events
*/
RMEvents::get()->trigger('rmcommon.saved.settings', $module->dirname(), $to_save, $to_add, $to_delete);
if ($module->getInfo('hasAdmin')) {
$goto = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/' . $module->getInfo('adminindex');
} else {
$goto = 'settings.php';
}
if ($via_ajax) {
$ajax->ajax_response(
__('Settings saved successfully!', 'rmcommon'),
0,
1,
[
'closeWindow' => '#cu-settings-form',
'notify' => [
'icon' => 'svg-rmcommon-ok-circle',
'type' => 'alert-success',
],
]
);
} else {
RMUris::redirect_with_message(__('Settings saved successfully!', 'rmcommon'), $goto, RMMSG_SUCCESS, 'fa fa-check');
}
}
$action = RMHttpRequest::request('action', 'string', '');
switch ($action) {
/**
* Show options for a specific element
*/
case 'configure':
show_module_preferences();
break;
/**
* Save settings for a single module
*/
case 'save-settings':
save_module_settings();
break;
default:
show_configurable_items();
break;
}