diff options
Diffstat (limited to 'modules-available/roomplanner/page.inc.php')
-rw-r--r-- | modules-available/roomplanner/page.inc.php | 119 |
1 files changed, 62 insertions, 57 deletions
diff --git a/modules-available/roomplanner/page.inc.php b/modules-available/roomplanner/page.inc.php index 52ad34f3..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'); } @@ -79,7 +79,12 @@ class Page_Roomplanner extends Page private function showLeafEditor() { - $config = Database::queryFirst('SELECT roomplan, managerip, tutoruuid FROM location_roomplan WHERE locationid = :locationid', ['locationid' => $this->locationid]); + $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; @@ -90,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']); @@ -103,13 +102,15 @@ 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), 'edit_disabled' => $canEdit ? '' : 'disabled', - 'statistics_disabled' => Module::get('statistics') !== false && User::hasPermission('.statistics.machine.view-details') ? '' : 'disabled', + 'statistics_disabled' => + (Module::get('statistics') !== false && User::hasPermission('.statistics.machine.view-details')) + ? '' : 'disabled', ]; Render::addTemplate('header', $params); if ($canEdit) { @@ -122,9 +123,10 @@ class Page_Roomplanner extends Page private function showComposedEditor() { // Load settings - $row = Database::queryFirst("SELECT locationid, roomplan FROM location_roomplan WHERE locationid = :lid", [ - 'lid' => $this->locationid, - ]); + $row = Database::queryFirst("SELECT locationid, roomplan + FROM location_roomplan + WHERE locationid = :lid", + ['lid' => $this->locationid]); $room = new ComposedRoom($row); $params = [ 'location' => $this->location, @@ -161,7 +163,7 @@ class Page_Roomplanner extends Page $this->action = Request::any('action', false, 'string'); if ($this->action === 'getmachines') { - + // Load suggestions when typing in the search box of the "add machine" pop-up User::load(); $locations = User::getAllowedLocations('edit'); if (empty($locations)) { @@ -190,8 +192,7 @@ class Page_Roomplanner extends Page $returnObject = ['machines' => []]; - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { - error_log("$roomLocationId, {$row['subnetlocationid']}"); + foreach ($result as $row) { if (!Location::isFixedLocationValid($roomLocationId, $row['subnetlocationid'])) continue; if (empty($row['hostname'])) { @@ -203,11 +204,12 @@ class Page_Roomplanner extends Page } echo json_encode($returnObject); } 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); @@ -224,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); @@ -245,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)) { @@ -258,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); @@ -277,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"); } } @@ -293,11 +290,15 @@ class Page_Roomplanner extends Page } } - protected function saveComputerConfig($computers, $oldComputers) + /** + * @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(array $computers, array $oldComputers) { $oldUuids = []; - /* collect all uuids from the old computers */ + /* collect all uuids from the old roomplan */ foreach ($oldComputers['computers'] as $c) { $oldUuids[] = $c['muuid']; } @@ -314,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'], @@ -330,6 +331,7 @@ class Page_Roomplanner extends Page ['locationid' => $this->locationid, 'muuid' => $computer['muuid'], 'position' => $position]); } + // Get all computers that were removed from the roomplan and reset their data in DB $toDelete = array_diff($oldUuids, $newUuids); foreach ($toDelete as $d) { @@ -337,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'); @@ -348,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'); @@ -366,22 +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', + $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'])) { @@ -408,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 @@ -417,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']; } |