diff options
Diffstat (limited to 'modules-available/locationinfo/inc/infopanel.inc.php')
-rw-r--r-- | modules-available/locationinfo/inc/infopanel.inc.php | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/modules-available/locationinfo/inc/infopanel.inc.php b/modules-available/locationinfo/inc/infopanel.inc.php index 7b0c6fe0..1a0e9b67 100644 --- a/modules-available/locationinfo/inc/infopanel.inc.php +++ b/modules-available/locationinfo/inc/infopanel.inc.php @@ -7,16 +7,16 @@ class InfoPanel * Gets the config of the location. * * @param int $locationID ID of the location - * @param mixed $config the panel config will be returned here - * @return string|bool paneltype, false if not exists + * @param ?array $config the panel config will be returned here + * @return ?string panel type, null if not exists */ - public static function getConfig($paneluuid, &$config) + public static function getConfig(string $paneluuid, ?array &$config): ?string { $panel = Database::queryFirst('SELECT panelname, panelconfig, paneltype, locationids FROM locationinfo_panel WHERE paneluuid = :paneluuid', compact('paneluuid')); if ($panel === false) { - return false; + return null; } $config = LocationInfo::defaultPanelConfig($panel['paneltype']); @@ -65,7 +65,7 @@ class InfoPanel $config['locations'][$lid]['config'] = $overrides[$lid]; } } - self::appendMachineData($config['locations'], $lids, true); + self::appendMachineData($config['locations'], $lids, true, $config['hostname']); } self::appendOpeningTimes($config['locations'], $lids); @@ -87,13 +87,10 @@ class InfoPanel * @param array $array location list to populate with machine data * @param bool $withPosition Defines if coords should be included or not. */ - public static function appendMachineData(&$array, $idList = false, $withPosition = false) + public static function appendMachineData(array &$array, array $idList, bool $withPosition = false, bool $withHostname = false): void { - if (empty($array) && $idList === false) + if (empty($idList)) return; - if ($idList === false) { - $idList = array_keys($array); - } $ignoreList = array(); if (Module::isAvailable('runmode')) { @@ -101,13 +98,21 @@ class InfoPanel $ignoreList = RunMode::getAllClients(false, false); } - $positionCol = $withPosition ? 'm.position,' : ''; - $query = "SELECT m.locationid, m.machineuuid, $positionCol m.logintime, m.lastseen, m.lastboot, m.state FROM machine m + $extraCols = ''; + if ($withPosition) { + $extraCols .= 'm.position,'; + } + if ($withHostname) { + $extraCols .= 'm.hostname,'; + } + $query = "SELECT m.locationid, m.fixedlocationid, m.machineuuid, $extraCols m.logintime, + m.lastseen, m.lastboot, m.state, m.currentrunmode + FROM machine m WHERE m.locationid IN (:idlist)"; $dbquery = Database::simpleQuery($query, array('idlist' => $idList)); // Iterate over matching machines - while ($row = $dbquery->fetch(PDO::FETCH_ASSOC)) { + foreach ($dbquery as $row) { if (isset($ignoreList[$row['machineuuid']])) continue; settype($row['locationid'], 'int'); @@ -119,7 +124,8 @@ class InfoPanel } // Compact the pc data in one array. $pc = array('id' => $row['machineuuid']); - if ($withPosition && !empty($row['position'])) { + if ($withPosition && $row['locationid'] == $row['fixedlocationid'] && !empty($row['position'])) { + // check fixed* == locationid to ignore stale position data in relocated clients $position = json_decode($row['position'], true); if (isset($position['gridCol']) && isset($position['gridRow'])) { $pc['x'] = $position['gridCol']; @@ -129,6 +135,19 @@ class InfoPanel } } } + if ($withHostname) { + if (ip2long($row['hostname']) !== false) { + $pc['host'] = $row['hostname']; + } else { + $i = strpos($row['hostname'], '.'); + if ($i === false) { + $pc['host'] = $row['hostname']; + } else { + $pc['host'] = substr($row['hostname'], 0, $i); + } + } + } + $pc['runmode'] = $row['currentrunmode']; $pc['pcState'] = LocationInfo::getPcState($row); //$pc['pcState'] = ['BROKEN', 'OFFLINE', 'IDLE', 'OCCUPIED', 'STANDBY'][mt_rand(0,4)]; // XXX @@ -143,17 +162,17 @@ class InfoPanel * @param array $array list of locations, indexed by locationId * @param int[] $idList list of locations */ - public static function appendOpeningTimes(&$array, $idList) + public static function appendOpeningTimes(array &$array, array $idList): void { // First, lets get all the parent ids for the given locations // in case we need to get inherited opening times $allIds = self::getLocationsWithParents($idList); if (empty($allIds)) return; - $res = Database::simpleQuery("SELECT locationid, openingtime FROM locationinfo_locationconfig + $res = Database::simpleQuery("SELECT locationid, openingtime FROM location WHERE locationid IN (:lids)", array('lids' => $allIds)); $openingTimes = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $openingTimes[(int)$row['locationid']] = $row; } // Now we got all the calendars for locations and parents @@ -185,7 +204,6 @@ class InfoPanel $currentId = $locations[$currentId]['parentlocationid']; } } - return; } @@ -196,12 +214,12 @@ class InfoPanel * @param int[] $idList location ids * @return int[] more location ids */ - private static function getLocationsWithParents($idList) + private static function getLocationsWithParents(array $idList): array { $locations = Location::getLocationsAssoc(); $allIds = $idList; foreach ($idList as $id) { - if (isset($locations[$id]) && isset($locations[$id]['parents'])) { + if (isset($locations[$id]['parents'])) { $allIds = array_merge($allIds, $locations[$id]['parents']); } } @@ -212,14 +230,14 @@ class InfoPanel /** * Format the openingtime in the frontend needed format. - * One key per week day, wich contains an array of { + * One key per week day, which contains an array of { * 'HourOpen' => hh, 'MinutesOpen' => mm, * 'HourClose' => hh, 'MinutesClose' => mm } * * @param array $openingtime The opening time in the db saved format. - * @return mixed The opening time in the frontend needed format. + * @return array The opening time in the frontend needed format. */ - private static function formatOpeningtime($openingtime) + private static function formatOpeningtime(array $openingtime): array { $result = array(); foreach ($openingtime as $entry) { |