summaryrefslogtreecommitdiffstats
path: root/modules-available/baseconfig/hooks/locations-column.inc.php
blob: ca30d56e4764f410d1ef6c95b524ee1cc1d8b598 (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
<?php

if (!User::hasPermission('.baseconfig.view'))
	return null;

class BaseconfigLocationColumn extends AbstractLocationColumn
{

	private $byLoc;
	private $byMachine;

	public function __construct(array $allowedLocationIds)
	{
		if (in_array(0, $allowedLocationIds)) {
			$extra = 'OR m.locationid IS NULL';
		} else {
			$extra = '';
		}
		// Count overridden config vars
		$this->byLoc = Database::queryKeyValueList("SELECT locationid, Count(*) AS cnt
				FROM `setting_location`
				WHERE locationid IN (:allowedLocationIds)
				GROUP BY locationid", compact('allowedLocationIds'));
		// Confusing because the count might be inaccurate within a branch
		//$this->propagateFields($locationList, '', 'overriddenVars', 'overriddenClass');
		// Count machines with overriden var(s)
		$this->byMachine = Database::queryKeyValueList("SELECT IFNULL(m.locationid, 0), Count(DISTINCT sm.machineuuid) AS cnt
				FROM setting_machine sm
				INNER JOIN machine m USING (machineuuid)
				WHERE (m.locationid IN (:allowedLocationIds) $extra)
				GROUP BY m.locationid", compact('allowedLocationIds'));
		// There WHERE statement drops clients without location - but this cannot be displayed by the locations
		// table anyways as of now - maybe implement some day? Or just encourage everyone to have a root location
	}

	public function getColumnHtml(int $locationId): string
	{
		$ret = '';
		if ($this->byLoc[$locationId] ?? 0) {
			$title = htmlspecialchars(Dictionary::translateFileModule('baseconfig', 'module', 'overriden-vars-for-location'));
			$ret .= <<<EOF
			<span class="badge" title="{$title}">
				<span class="glyphicon glyphicon-home"></span> {$this->byLoc[$locationId]}
			</span>
EOF;
		}
		if ($this->byMachine[$locationId] ?? 0) {
			$title = htmlspecialchars(Dictionary::translateFileModule('baseconfig', 'module', 'overriden-vars-machines'));
			$ret .= <<<EOF
				<span class="badge" title="{$title}">
				<span class="glyphicon glyphicon-tasks"></span> {$this->byMachine[$locationId]}
			</span>
EOF;
		}
		return $ret;
	}

	public function getEditUrl(int $locationId): string
	{
		if (!User::hasPermission('.baseconfig.edit', $locationId))
			return '';
		return '?do=baseconfig&module=locations&locationid=' . $locationId;
	}

	public function header(): string
	{
		return Dictionary::translateFileModule('baseconfig', 'module', 'location-column-header');
	}

	public function priority(): int
	{
		return 1000;
	}
}

return new BaseconfigLocationColumn($allowedLocationIds);