diff options
Diffstat (limited to 'modules-available/roomplanner/page.inc.php')
-rw-r--r-- | modules-available/roomplanner/page.inc.php | 89 |
1 files changed, 41 insertions, 48 deletions
diff --git a/modules-available/roomplanner/page.inc.php b/modules-available/roomplanner/page.inc.php index 8c3beace..94bc3f78 100644 --- a/modules-available/roomplanner/page.inc.php +++ b/modules-available/roomplanner/page.inc.php @@ -4,14 +4,14 @@ class Page_Roomplanner extends Page { /** - * @var int locationid of location we're editing + * @var ?int locationid of location we're editing, or null if unknown/not set */ - private $locationid = false; + private $locationid = null; /** * @var array location data from location table */ - private $location = false; + private $location = null; /** * @var string action to perform @@ -25,8 +25,8 @@ class Page_Roomplanner extends Page private function loadRequestedLocation() { - $this->locationid = Request::get('locationid', false, 'integer'); - if ($this->locationid !== false) { + $this->locationid = Request::get('locationid', null, 'integer'); + if ($this->locationid !== null) { $locs = Location::getLocationsAssoc(); if (isset($locs[$this->locationid])) { $this->location = $locs[$this->locationid]; @@ -46,11 +46,11 @@ class Page_Roomplanner extends Page $this->action = Request::any('action', 'show', 'string'); $this->loadRequestedLocation(); - if ($this->locationid === false) { + if ($this->locationid === null) { Message::addError('need-locationid'); Util::redirect('?do=locations'); } - if ($this->location === false) { + if ($this->location === null) { Message::addError('locations.invalid-location-id', $this->locationid); Util::redirect('?do=locations'); } @@ -82,6 +82,9 @@ class Page_Roomplanner extends Page $config = Database::queryFirst('SELECT roomplan, managerip, tutoruuid FROM location_roomplan WHERE locationid = :locationid', ['locationid' => $this->locationid]); + if ($config === false) { + $config = ['managerip' => '', 'tutoruuid' => '']; + } $runmode = RunMode::getForMode(Page::getModule(), $this->locationid, true); if (empty($runmode)) { $config['dedicatedmgr'] = false; @@ -92,12 +95,6 @@ class Page_Roomplanner extends Page $data = json_decode($runmode['modedata'], true); $config['dedicatedmgr'] = (isset($data['dedicatedmgr']) && $data['dedicatedmgr']); } - if ($config !== false) { - $managerIp = $config['managerip']; - $dediMgr = $config['dedicatedmgr'] ? 'checked' : ''; - } else { - $dediMgr = $managerIp = ''; - } $furniture = $this->getFurniture($config); $subnetMachines = $this->getPotentialMachines(); $machinesOnPlan = $this->getMachinesOnPlan($config['tutoruuid']); @@ -105,8 +102,8 @@ class Page_Roomplanner extends Page $canEdit = User::hasPermission('edit', $this->locationid); $params = [ 'location' => $this->location, - 'managerip' => $managerIp, - 'dediMgrChecked' => $dediMgr, + 'managerip' => $config['managerip'], + 'dediMgrChecked' => $config['dedicatedmgr'] ? 'checked' : '', 'subnetMachines' => json_encode($subnetMachines), 'locationid' => $this->locationid, 'roomConfiguration' => json_encode($roomConfig), @@ -195,7 +192,7 @@ class Page_Roomplanner extends Page $returnObject = ['machines' => []]; - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + foreach ($result as $row) { if (!Location::isFixedLocationValid($roomLocationId, $row['subnetlocationid'])) continue; if (empty($row['hostname'])) { @@ -209,10 +206,10 @@ class Page_Roomplanner extends Page } elseif ($this->action === 'save') { // Save roomplan - give feedback if it failed so the window can stay open $this->loadRequestedLocation(); - if ($this->locationid === false) { + if ($this->locationid === null) { die('Missing locationid in save data'); } - if ($this->location === false) { + if ($this->location === null) { die('Location with id ' . $this->locationid . ' does not exist.'); } $this->handleSaveRequest(true); @@ -229,11 +226,9 @@ class Page_Roomplanner extends Page if ($leaf !== $this->isLeaf) { if ($isAjax) { die('Leaf mode mismatch. Did you restructure locations while editing this room?'); - } else { - Message::addError('leaf-mode-mismatch'); - Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } - return; + Message::addError('leaf-mode-mismatch'); + Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } if ($this->isLeaf) { $this->saveLeafRoom($isAjax); @@ -250,10 +245,9 @@ class Page_Roomplanner extends Page if (!is_array($config) || !isset($config['furniture']) || !isset($config['computers'])) { if ($isAjax) { die('JSON data incomplete'); - } else { - Message::addError('json-data-invalid'); - Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } + Message::addError('json-data-invalid'); + Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } $tutorUuid = Request::post('tutoruuid', '', 'string'); if (empty($tutorUuid)) { @@ -263,10 +257,9 @@ class Page_Roomplanner extends Page if ($ret === false) { if ($isAjax) { die('Invalid tutor UUID'); - } else { - Message::addError('invalid-tutor-uuid'); - Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } + Message::addError('invalid-tutor-uuid'); + Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } } $this->saveRoomConfig($config['furniture'], $tutorUuid); @@ -282,10 +275,9 @@ class Page_Roomplanner extends Page if ($res === false) { if ($isAjax) { die('Error writing config to DB'); - } else { - Message::addError('db-error'); - Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } + Message::addError('db-error'); + Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); } } @@ -302,7 +294,7 @@ class Page_Roomplanner extends Page * @param array $computers Deserialized json from browser with all the computers * @param array $oldComputers Deserialized old roomplan from database, used to find removed computers */ - protected function saveComputerConfig($computers, $oldComputers) + protected function saveComputerConfig(array $computers, array $oldComputers) { $oldUuids = []; @@ -323,12 +315,12 @@ class Page_Roomplanner extends Page if (!isset($computer['gridRow'])) { $computer['gridRow'] = 0; } else { - $this->sanitizeNumber($computer['gridRow'], 0, 32 * 4); + Util::clamp($computer['gridRow'], 0, 32 * 4); } if (!isset($computer['gridCol'])) { $computer['gridCol'] = 0; } else { - $this->sanitizeNumber($computer['gridCol'], 0, 32 * 4); + Util::clamp($computer['gridCol'], 0, 32 * 4); } $position = json_encode(['gridRow' => $computer['gridRow'], @@ -347,7 +339,7 @@ class Page_Roomplanner extends Page } } - protected function saveRoomConfig($furniture, $tutorUuid) + protected function saveRoomConfig(?array $furniture, ?string $tutorUuid) { $obj = json_encode(['furniture' => $furniture]); $managerIp = Request::post('managerip', '', 'string'); @@ -358,13 +350,11 @@ class Page_Roomplanner extends Page 'locationid' => $this->locationid, 'roomplan' => $obj, 'managerip' => $managerIp, - 'tutoruuid' => $tutorUuid + 'tutoruuid' => $tutorUuid, ]); // See if the client is known, set run-mode - if (empty($managerIp)) { - RunMode::deleteMode(Page::getModule(), $this->locationid); - } else { - RunMode::deleteMode(Page::getModule(), $this->locationid); + RunMode::deleteMode(Page::getModule(), (string)$this->locationid); + if (!empty($managerIp)) { $pc = Statistics::getMachinesByIp($managerIp, Machine::NO_DATA, 'lastseen DESC'); if (!empty($pc)) { $dedicated = (Request::post('dedimgr') === 'on'); @@ -376,24 +366,27 @@ class Page_Roomplanner extends Page } } - protected function getFurniture($config) + protected function getFurniture(array $config): array { - if ($config === false) - return array(); + if (empty($config['roomplan'])) + return []; $config = json_decode($config['roomplan'], true); if (!is_array($config)) - return array(); + return []; return $config; } - protected function getMachinesOnPlan($tutorUuid) + /** + * @return array{computers: array} + */ + protected function getMachinesOnPlan(?string $tutorUuid): array { $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname, position FROM machine WHERE fixedlocationid = :locationid', ['locationid' => $this->locationid]); $machines = []; - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + foreach ($result as $row) { $machine = []; $pos = json_decode($row['position'], true); if ($pos === false || !isset($pos['gridRow']) || !isset($pos['gridCol'])) { @@ -420,7 +413,7 @@ class Page_Roomplanner extends Page return ['computers' => $machines]; } - protected function getPotentialMachines() + protected function getPotentialMachines(): array { $result = Database::simpleQuery('SELECT m.machineuuid, m.macaddr, m.clientip, m.hostname, l.locationname AS otherroom, m.fixedlocationid FROM machine m @@ -429,7 +422,7 @@ class Page_Roomplanner extends Page $machines = []; - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + foreach ($result as $row) { if (empty($row['hostname'])) { $row['hostname'] = $row['clientip']; } |