summaryrefslogtreecommitdiffstats
path: root/modules-available/baseconfig/inc/baseconfigutil.inc.php
blob: a48eb93b91436a78043238af4bae4eea9ad5e310 (plain) (blame)
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
<?php

class BaseConfigUtil
{

	/**
	 * Return all config variables to be handled directly by the baseconfig edit module.
	 * The array will contain a list of mapping of type:
	 * VARNAME => array(
	 *     catid => xx,
	 *     defaultvalue => xx,
	 *     permissions => xx,
	 *     validator => xx,
	 * )
	 *
	 * @param \Module $module optional, only consider given module, not all enabled modules
	 * @return array all known config variables
	 */
	public static function getVariables($module = false)
	{
		$settings = array();
		if ($module === false) {
			$module = '*';
		} else {
			$module = $module->getIdentifier();
		}
		foreach (glob("modules/{$module}/baseconfig/settings.json", GLOB_NOSORT) as $file) {
			$data = json_decode(file_get_contents($file), true);
			if (!is_array($data))
				continue;
			preg_match('#^modules/([^/]+)/#', $file, $out);
			foreach ($data as &$entry) {
				$entry['module'] = $out[1];
			}
			$settings += $data;
		}
		return $settings;
	}

	/**
	 * Get configuration categories for given module, or all modules if false is passed.
	 *
	 * @param \Module $module
	 * @return array
	 */
	public static function getCategories($module = false)
	{
		$categories = array();
		if ($module === false) {
			$module = '*';
		} elseif (is_object($module)) {
			$module = $module->getIdentifier();
		}
		foreach (glob("modules/{$module}/baseconfig/categories.json", GLOB_NOSORT) as $file) {
			$data = json_decode(file_get_contents($file), true);
			if (!is_array($data))
				continue;
			preg_match('#^modules/([^/]+)/#', $file, $out);
			foreach ($data as &$entry) {
				$entry = array('module' => $out[1], 'sortpos' => $entry);
			}
			$categories += $data;
		}
		return $categories;
	}

	/**
	 * Mark variables that would be shadowed according to the given values.
	 *
	 * @param array $vars list of vars as obtained from BaseConfigUtil::getVariables()
	 * @param array $values key-value-pairs of variable assignments to work with
	 */
	public static function markShadowedVars(&$vars, $values) {
		foreach ($vars as $key => &$var) {
			if (!isset($var['shadows']))
				continue;
			foreach ($var['shadows'] as $triggerVal => $destSettings) {
				if (isset($values[$key]) && $values[$key] !== $triggerVal)
					continue;
				foreach ($destSettings as $destSetting) {
					if (isset($vars[$destSetting])) {
						$vars[$destSetting]['shadowed'] = true;
					}
				}
			}
		}
	}

}