From 50d28a0ad8dcf5d9fe697278b6ea05aa00f8fb87 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 5 Nov 2019 18:01:37 +0100 Subject: [baseconfig] Overhaul hook system This enables us to finally properly show the inheritance flow of all the config variables when editing the baseconfig for a certain location or machine. --- .../locations/baseconfig/getconfig.inc.php | 25 +++++++++++++++---- modules-available/locations/baseconfig/hook.json | 2 +- .../locations/inc/locationhooks.inc.php | 28 ++++++++++------------ 3 files changed, 34 insertions(+), 21 deletions(-) (limited to 'modules-available/locations') diff --git a/modules-available/locations/baseconfig/getconfig.inc.php b/modules-available/locations/baseconfig/getconfig.inc.php index f21503f1..26e43ed8 100644 --- a/modules-available/locations/baseconfig/getconfig.inc.php +++ b/modules-available/locations/baseconfig/getconfig.inc.php @@ -2,7 +2,9 @@ // Location handling: figure out location $locationId = false; -if (Request::any('force', 0, 'int') === 1 && Request::any('module', false, 'string') === 'locations') { +if (BaseConfig::hasOverride('locationid')) { + $locationId = BaseConfig::getOverride('locationid'); +} elseif (Request::any('force', 0, 'int') === 1 && Request::any('module', false, 'string') === 'locations') { // Force location for testing, but require logged in admin if (User::load()) { $locationId = Request::any('value', 0, 'int'); @@ -10,6 +12,8 @@ if (Request::any('force', 0, 'int') === 1 && Request::any('module', false, 'stri } if ($locationId === false) { + if (!$ip) // Required at this point, bail out if not given + return; $locationId = Location::getFromIpAndUuid($ip, $uuid); } @@ -23,19 +27,30 @@ if ($locationId !== false) { // Query location specific settings (from bottom to top) if (!empty($matchingLocations)) { // First get all settings for all locations we're in - $list = implode(',', $matchingLocations); - $res = Database::simpleQuery("SELECT locationid, setting, value FROM setting_location WHERE locationid IN ($list)"); + $res = Database::simpleQuery("SELECT locationid, setting, value, displayvalue + FROM setting_location WHERE locationid IN (:list)", + ['list' => $matchingLocations]); $tmp = array(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $tmp[(int)$row['locationid']][$row['setting']] = $row['value']; + $tmp[(int)$row['locationid']][$row['setting']] = $row; // Put whole row so we have value and displayvalue } + // Callback for pretty printing + $cb = function($id) { + if (substr($id, 0, 9) !== 'location-') + return ['name' => $id, 'locationid' => 0]; + $lid = (int)substr($id, 9); + return [ + 'name' => Location::getName($lid), + 'locationid' => $lid, + ]; + }; // $matchingLocations contains the location ids sorted from closest to furthest, so we use it to make sure the order // in which they override is correct (closest setting wins, e.g. room setting beats department setting) $prio = count($matchingLocations) + 1; foreach ($matchingLocations as $lid) { if (!isset($tmp[$lid])) continue; - ConfigHolder::setContext('location-' . $lid); + ConfigHolder::setContext('location-' . $lid, $cb); foreach ($tmp[$lid] as $setting => $value) { ConfigHolder::add($setting, $value, $prio); } diff --git a/modules-available/locations/baseconfig/hook.json b/modules-available/locations/baseconfig/hook.json index 1d69647e..fabb4686 100644 --- a/modules-available/locations/baseconfig/hook.json +++ b/modules-available/locations/baseconfig/hook.json @@ -3,5 +3,5 @@ "field": "locationid", "locationResolver": "LocationHooks::baseconfigLocationResolver", "tostring": "Location::getName", - "getfallback": "LocationHooks::getBaseconfigParent" + "getInheritance": "LocationHooks::baseconfigInheritance" } \ No newline at end of file diff --git a/modules-available/locations/inc/locationhooks.inc.php b/modules-available/locations/inc/locationhooks.inc.php index c15c34ab..5ce3bbfe 100644 --- a/modules-available/locations/inc/locationhooks.inc.php +++ b/modules-available/locations/inc/locationhooks.inc.php @@ -4,28 +4,26 @@ class LocationHooks { /** - * Used for baseconfig hook - * @param $locationId - * @return bool|array ('value' => x, 'display' => y), false if no parent or unknown id + * Resolve baseconfig id to locationid -- noop in this case */ - public static function getBaseconfigParent($locationId) + public static function baseconfigLocationResolver($id) { - $locationId = (int)$locationId; - $assoc = Location::getLocationsAssoc(); - if (!isset($assoc[$locationId])) - return false; - $locationId = (int)$assoc[$locationId]['parentlocationid']; - if (!isset($assoc[$locationId])) - return false; - return array('value' => $locationId, 'display' => $assoc[$locationId]['locationname']); + return $id; } /** - * Resolve baseconfig id to locationid -- noop in this case + * Hook to get inheritance tree for all config vars + * @param int $id Locationid currently being edited */ - public static function baseconfigLocationResolver($id) + public static function baseconfigInheritance($id) { - return $id; + $locs = Location::getLocationsAssoc(); + if ($locs === false || !isset($locs[$id])) + return []; + BaseConfig::prepareWithOverrides([ + 'locationid' => $locs[$id]['parentlocationid'] + ]); + return ConfigHolder::getRecursiveConfig(true); } } \ No newline at end of file -- cgit v1.2.3-55-g7522