summaryrefslogtreecommitdiffstats
path: root/modules-available/baseconfig/page.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/baseconfig/page.inc.php')
-rw-r--r--modules-available/baseconfig/page.inc.php140
1 files changed, 75 insertions, 65 deletions
diff --git a/modules-available/baseconfig/page.inc.php b/modules-available/baseconfig/page.inc.php
index 837a3b67..5d684a8e 100644
--- a/modules-available/baseconfig/page.inc.php
+++ b/modules-available/baseconfig/page.inc.php
@@ -23,11 +23,7 @@ class Page_BaseConfig extends Page
$newValues = Request::post('setting');
if (is_array($newValues)) {
- if ($this->targetModule === 'locations') {
- User::assertPermission('edit', $this->qry_extra['field_value']);
- } else {
- User::assertPermission('edit', 0);
- }
+ User::assertPermission('edit', $this->getPermissionLocationId());
// Build variables for specific sub-settings
if ($this->targetModule === false || empty($this->qry_extra['field'])) {
// Global, or Module specific, but module doesn't have an extra field
@@ -58,8 +54,7 @@ class Page_BaseConfig extends Page
BaseConfigUtil::markShadowedVars($vars, $newValues);
// Validate input
foreach ($vars as $key => $var) {
- if (isset($var['shadowed']))
- continue;
+ // Delete entries where we disabled override
if ($this->targetModule !== false) {
// Module mode
if (is_array($override) && (!isset($override[$key]) || $override[$key] !== 'on')) {
@@ -69,8 +64,11 @@ class Page_BaseConfig extends Page
continue;
}
}
+ // Only after that, check if variable is shadowed (disabled)
+ if (isset($var['shadowed']))
+ continue;
$validator = $var['validator'];
- $displayValue = (isset($newValues[$key]) ? $newValues[$key] : '');
+ $displayValue = $newValues[$key] ?? '';
// Validate data first!
$mangledValue = Validator::validate($validator, $displayValue);
if ($mangledValue === false) {
@@ -90,11 +88,11 @@ class Page_BaseConfig extends Page
}
Message::addSuccess('settings-updated');
if ($this->targetModule === false) {
- Util::redirect('?do=BaseConfig');
+ Util::redirect('?do=BaseConfig', true);
} elseif (empty($this->qry_extra['field'])) {
- Util::redirect('?do=BaseConfig&module=' . $this->targetModule);
+ Util::redirect('?do=BaseConfig&module=' . $this->targetModule, true);
} else {
- Util::redirect('?do=BaseConfig&module=' . $this->targetModule . '&' . $this->qry_extra['field'] . '=' . $this->qry_extra['field_value']);
+ Util::redirect('?do=BaseConfig&module=' . $this->targetModule . '&' . $this->qry_extra['field'] . '=' . $this->qry_extra['field_value'], true);
}
}
// Load categories so we can define them as sub menu items
@@ -103,7 +101,7 @@ class Page_BaseConfig extends Page
foreach ($this->categories as $catid => $val) {
Dashboard::addSubmenu(
'#category_' . $catid,
- Dictionary::translateFileModule($this->categories[$catid]['module'], 'config-variable-categories', $catid, true)
+ Dictionary::translateFileModule($this->categories[$catid]['module'], 'config-variable-categories', $catid)
);
}
}
@@ -118,76 +116,59 @@ class Page_BaseConfig extends Page
Util::redirect('?do=BaseConfig');
}
}
- if ($this->targetModule === 'locations') {
- User::assertPermission('view', $this->qry_extra['field_value']);
- $editForbidden = !User::hasPermission('edit', $this->qry_extra['field_value']);
- } else {
- User::assertPermission('view', 0);
- $editForbidden = !User::hasPermission('edit', 0);
- }
+ $lid = $this->getPermissionLocationId();
+ User::assertPermission('view', $lid);
+ $editForbidden = !User::hasPermission('edit', $lid);
// Get stuff that's set in DB already
+ $fields = '';
if ($this->targetModule !== false && isset($this->qry_extra['field'])) {
- $fields = '';
$where = " WHERE {$this->qry_extra['field']} = :field_value";
$params = array('field_value' => $this->qry_extra['field_value']);
} else {
- $fields = '';
$where = '';
$params = array();
}
+ $parents = $this->getInheritanceData();
// List config options
$settings = array();
- $vars = BaseConfigUtil::getVariables();
+ $varsFromJson = BaseConfigUtil::getVariables();
// Remember missing variables
- $missing = $vars;
+ $missing = $varsFromJson;
// Populate structure with existing config from db
- $this->fillSettings($vars, $settings, $missing, $this->qry_extra['table'], $fields, $where, $params, false);
- if (isset($this->qry_extra['getfallback']) && !empty($missing)) {
- $method = explode('::', $this->qry_extra['getfallback']);
- $fieldValue = $this->qry_extra['field_value'];
- $tries = 0;
- while (++$tries < 100 && !empty($missing)) {
- $ret = call_user_func($method, $fieldValue);
- if ($ret === false)
- break;
- $fieldValue = $ret['value'];
- $params = array('field_value' => $fieldValue);
- $this->fillSettings($vars, $settings, $missing, $this->qry_extra['table'], $fields, $where, $params, $ret['display']);
- }
- }
- if ($this->targetModule !== false && !empty($missing)) {
- $this->fillSettings($vars, $settings, $missing, 'setting_global', '', '', array(), 'Global');
- }
+ $this->fillSettings($varsFromJson, $settings, $missing, $this->qry_extra['table'], $fields, $where, $params);
// Add entries that weren't in the db (global), setup override checkbox (module specific)
- foreach ($vars as $key => $var) {
+ foreach ($varsFromJson as $key => $var) {
if ($this->targetModule !== false && !isset($settings[$var['catid']]['settings'][$key])) {
// Module specific - value is not set in DB
- $settings[$var['catid']]['settings'][$key] = $var + array(
+ $settings[$var['catid']]['settings'][$key] = array(
'setting' => $key
);
}
- if (!isset($settings[$var['catid']]['settings'][$key]['displayvalue'])) {
- $settings[$var['catid']]['settings'][$key]['displayvalue'] = $var['defaultvalue'];
- }
- if (!isset($settings[$var['catid']]['settings'][$key]['defaultvalue'])) {
- $settings[$var['catid']]['settings'][$key]['defaultvalue'] = $var['defaultvalue'];
+ $entry =& $settings[$var['catid']]['settings'][$key];
+ if (!isset($entry['displayvalue'])) {
+ if (isset($parents[$key][0]['value'])) {
+ $entry['displayvalue'] = $parents[$key][0]['value'];
+ } else {
+ $entry['displayvalue'] = $var['defaultvalue'];
+ }
}
- if (!isset($settings[$var['catid']]['settings'][$key]['shadows'])) {
- $settings[$var['catid']]['settings'][$key]['shadows'] = isset($var['shadows']) ? $var['shadows'] : null;
+ if (!isset($entry['shadows'])) {
+ $entry['shadows'] = $var['shadows'] ?? null;
}
- $settings[$var['catid']]['settings'][$key] += array(
+ $entry += array(
'item' => $this->makeInput(
$var['validator'],
$key,
- $settings[$var['catid']]['settings'][$key]['displayvalue'],
- $settings[$var['catid']]['settings'][$key]['shadows'],
+ $entry['displayvalue'],
+ $entry['shadows'],
$editForbidden
),
'description' => Util::markup(Dictionary::translateFileModule($var['module'], 'config-variables', $key)),
'setting' => $key,
+ 'tree' => $parents[$key] ?? false,
);
}
- //die();
+ unset($entry);
// Sort categories
@@ -209,25 +190,23 @@ class Page_BaseConfig extends Page
'categories' => array_values($settings),
'target_module' => $this->targetModule,
'edit_disabled' => $editForbidden ? 'disabled' : '',
+ 'redirect' => Request::get('redirect'),
) + $this->qry_extra);
}
- private function fillSettings($vars, &$settings, &$missing, $table, $fields, $where, $params, $sourceName)
+ private function fillSettings($vars, &$settings, &$missing, $table, $fields, $where, $params): void
{
$res = Database::simpleQuery("SELECT setting, value, displayvalue $fields FROM $table "
. " {$where} ORDER BY setting ASC", $params);
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ foreach ($res as $row) {
if (!isset($missing[$row['setting']]))
continue;
if (!isset($vars[$row['setting']]) || !is_array($vars[$row['setting']])) {
- $unknown[] = $row['setting'];
+ //$unknown[] = $row['setting'];
continue;
}
unset($missing[$row['setting']]);
- if ($sourceName !== false) {
- $row['defaultvalue'] = '';
- $row['defaultsource'] = $sourceName;
- } elseif ($this->targetModule !== false) {
+ if ($this->targetModule !== false) {
$row['checked'] = 'checked';
}
$row += $vars[$row['setting']];
@@ -280,13 +259,29 @@ class Page_BaseConfig extends Page
$this->targetModule = $module;
$this->qry_extra = $hook;
}
+
+ private function getPermissionLocationId(): int
+ {
+ if (!isset($this->qry_extra['locationResolver']) || !isset($this->qry_extra['field_value']))
+ return 0;
+ $func = explode('::', $this->qry_extra['locationResolver']);
+ return (int)call_user_func($func, $this->qry_extra['field_value']);
+ }
+
+ private function getInheritanceData()
+ {
+ if (!isset($this->qry_extra['getInheritance']) || !isset($this->qry_extra['field_value'])) {
+ BaseConfig::prepareDefaults();
+ return ConfigHolder::getRecursiveConfig(true);
+ }
+ $func = explode('::', $this->qry_extra['getInheritance']);
+ return call_user_func($func, $this->qry_extra['field_value']);
+ }
/**
* Create html snippet for setting, based on given validator
- * @param string $validator
- * @return boolean
*/
- private function makeInput($validator, $setting, $current, $shadows, $disabled)
+ private function makeInput(string $validator, string $setting, string $current, ?array $shadows, bool $disabled): string
{
/* for the html snippet we need: */
$args = array('class' => 'form-control', 'name' => "setting[$setting]", 'id' => $setting);
@@ -296,7 +291,7 @@ class Page_BaseConfig extends Page
if ($disabled) {
$args['disabled'] = true;
}
- $inner = "";
+ $extra = $inner = "";
/* -- */
$parts = explode(':', $validator, 2);
@@ -316,6 +311,21 @@ class Page_BaseConfig extends Page
unset($args['type']);
$current = '';
+ } elseif ($parts[0] === 'suggestions') {
+
+ $extra = '<datalist id="list-' . $setting . '">';
+ $items = explode('|', $parts[1]);
+ foreach ($items as $item) {
+ $extra .= '<option>' . htmlspecialchars($item) . '</option>';
+ }
+ $extra .= '</datalist>';
+
+ $tag = 'input';
+ $args['value'] = $current;
+ $args['type'] = 'text';
+ $args['list'] = 'list-' . $setting;
+ $current = '';
+
} elseif ($parts[0] == 'multilist') {
$items = explode('|', $parts[1]);
@@ -364,7 +374,7 @@ class Page_BaseConfig extends Page
$output .= '>' . $inner . "</$tag>";
}
- return $output;
+ return $output . $extra;
}
}