summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/baseconfig/getconfig.inc.php
blob: 1bed5de7f7627f5f08d87367cf0f43c94dc62a9a (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
<?php

/** @var ?string $uuid */
/** @var ?string $ip */

// Location handling: figure out location
$locationId = false;
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');
	}
}

if ($locationId === false) {
	if ($ip === null) // Required at this point, bail out if not given
		return;
	$locationId = Location::getFromIpAndUuid($ip, $uuid);
}

$matchingLocations = array();
if ($locationId !== false) {
	// Get all parents
	$matchingLocations = Location::getLocationRootChain($locationId);
	ConfigHolder::add("SLX_LOCATIONS", implode(' ', $matchingLocations), 100);
}

// Query location specific settings (from bottom to top)
if (!empty($matchingLocations)) {
	// First get all settings for all locations we're in
	$res = Database::simpleQuery("SELECT locationid, setting, value, displayvalue
			FROM setting_location WHERE locationid IN (:list)",
		['list' => $matchingLocations]);
	$tmp = array();
	foreach ($res as $row) {
		$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, $cb);
		foreach ($tmp[$lid] as $setting => $value) {
			ConfigHolder::add($setting, $value, $prio);
		}
		$prio -= 1;
	}
	unset($tmp);
}