diff options
author | Christian Klinger | 2016-09-07 16:56:39 +0200 |
---|---|---|
committer | Christian Klinger | 2016-09-07 16:56:39 +0200 |
commit | d369776f806ed5f2429046802b5e4e447824465d (patch) | |
tree | ec6ac4d39e679146eb6e7e9600551731bffee52f /modules-available/roomplanner/page.inc.php | |
parent | Added a pvs.ini file generator. (diff) | |
parent | [roomplanner] Sanity checks/fixups when saving computers, more user feedback,... (diff) | |
download | slx-admin-d369776f806ed5f2429046802b5e4e447824465d.tar.gz slx-admin-d369776f806ed5f2429046802b5e4e447824465d.tar.xz slx-admin-d369776f806ed5f2429046802b5e4e447824465d.zip |
Merge branch 'modularization' of git.openslx.org:openslx-ng/slx-admin into modularization
Diffstat (limited to 'modules-available/roomplanner/page.inc.php')
-rw-r--r-- | modules-available/roomplanner/page.inc.php | 369 |
1 files changed, 231 insertions, 138 deletions
diff --git a/modules-available/roomplanner/page.inc.php b/modules-available/roomplanner/page.inc.php index 0d4b7ee9..2d3b5187 100644 --- a/modules-available/roomplanner/page.inc.php +++ b/modules-available/roomplanner/page.inc.php @@ -2,65 +2,95 @@ class Page_Roomplanner extends Page { - protected function doPreprocess() - { - User::load(); - if (!User::hasPermission('superadmin')) { - Message::addError('main.no-permission'); - Util::redirect('?do=Main'); - } - } + /** + * @var int locationid of location we're editing + */ + private $locationid = false; - protected function doRender() - { + /** + * @var array location data from location table + */ + private $location = false; - $locationid = Request::get('locationid', null, 'integer'); + /** + * @var string action to perform + */ + private $action = false; - if ($locationid === null) { die('please specify locationid'); } + private function loadRequestedLocation() + { + $this->locationid = Request::get('locationid', false, 'integer'); + if ($this->locationid !== false) { + $this->location = Location::get($this->locationid); + } + } - if (Request::get('pvs', false, 'bool')) { - /* return a pvs-file */ - echo "<pre>"; - echo PvsGenerator::generate($locationid); - echo "</pre>"; - die(); - } + protected function doPreprocess() + { + User::load(); - $furniture = $this->getFurniture($locationid); - $subnetMachines = $this->getPotentialMachines($locationid); - $machinesOnPlan = $this->getMachinesOnPlan($locationid); + if (!User::hasPermission('superadmin')) { + Message::addError('main.no-permission'); + Util::redirect('?do=Main'); + } - $action = Request::any('action', 'show', 'string'); + $this->action = Request::any('action', 'show', 'string'); + $this->loadRequestedLocation(); + if ($this->locationid === false) { + Message::addError('need-locationid'); + Util::redirect('?do=locations'); + } + if ($this->location === false) { + Message::addError('locations.invalid-location-id', $this->locationid); + Util::redirect('?do=locations'); + } - $roomConfig = array_merge($furniture, $machinesOnPlan); + if ($this->action === 'save') { + $this->handleSaveRequest(false); + Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show"); + } + Render::setTitle($this->location['locationname']); + } + protected function doRender() + { + /* deliver the pvs.ini file temporarily here */ + /* TODO: Move this to api.inc.php */ + if (Request::get('pvs', false, 'bool')) { + /* return a pvs-file */ + echo "<pre>"; + echo PvsGenerator::generate($locationid); + echo "</pre>"; + die(); + } - if ($action === 'show') { - /* do nothing */ - Render::addTemplate('page', [ - 'subnetMachines' => json_encode($subnetMachines), - 'locationid' => $locationid, - 'roomConfiguration' => json_encode($roomConfig)]); - } else if ($action === 'save') { - /* save */ - $config = Request::post('serializedRoom', null, 'string'); - $config = json_decode($config, true); - $this->saveRoomConfig($locationid, $config['furniture']); - $this->saveComputerConfig($locationid, $config['computers'], $machinesOnPlan); - Util::redirect("?do=roomplanner&locationid=$locationid&action=show"); - } + if ($this->action === 'show') { + /* do nothing */ + Dashboard::disable(); + $furniture = $this->getFurniture(); + $subnetMachines = $this->getPotentialMachines(); + $machinesOnPlan = $this->getMachinesOnPlan(); + $roomConfig = array_merge($furniture, $machinesOnPlan); + Render::addTemplate('page', [ + 'location' => $this->location, + 'subnetMachines' => json_encode($subnetMachines), + 'locationid' => $this->locationid, + 'roomConfiguration' => json_encode($roomConfig)]); + } else { + Message::addError('main.invalid-action', $this->action); + } - } + } - protected function doAjax() - { - $action = Request::get('action', null, 'string'); + protected function doAjax() + { + $this->action = Request::any('action', false, 'string'); - if ($action === 'getmachines') { - $query = Request::get('query', null, 'string'); + if ($this->action === 'getmachines') { + $query = Request::get('query', false, 'string'); - /* the query could be anything: UUID, IP or macaddr */ + /* the query could be anything: UUID, IP or macaddr */ // $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' // . ', MATCH (machineuuid, macaddr, clientip, hostname) AGAINST (:query) AS relevance ' // . 'FROM machine ' @@ -69,97 +99,160 @@ class Page_Roomplanner extends Page // . 'LIMIT 5' // , ['query' => $query]); // - $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' - .'FROM machine ' - .'WHERE machineuuid LIKE :query ' - .' OR macaddr LIKE :query ' - .' OR clientip LIKE :query ' - .' OR hostname LIKE :query ', ['query' => "%$query%"]); - - $returnObject = ['machines' => []]; - - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { - $returnObject['machines'][] = $row; - } - echo json_encode($returnObject); - } - } - - protected function saveComputerConfig($locationid, $computers, $oldComputers) { - - $oldUuids = []; - /* collect all uuids from the old computers */ - foreach($oldComputers['computers'] as $c) { - $oldUuids[] = $c['muuid']; - } - - $newUuids = []; - foreach($computers as $computer) { - $newUuids[] = $computer['muuid']; - - $position = json_encode(['gridRow' => $computer['gridRow'], - 'gridCol' => $computer['gridCol'], - 'itemlook' => $computer['itemlook']]); - - Database::exec('UPDATE machine SET position = :position, locationid = :locationid WHERE machineuuid = :muuid', - ['locationid' => $locationid, 'muuid' => $computer['muuid'], 'position' => $position]); - } - - $toDelete = array_diff($oldUuids, $newUuids); - - foreach($toDelete as $d) { - Database::exec("UPDATE machine SET position = '', locationid = NULL WHERE machineuuid = :uuid", ['uuid' => $d]); - } - } - protected function saveRoomConfig($locationid, $furniture) { - $obj = json_encode(['furniture' => $furniture]); - Database::exec('INSERT INTO location_roomplan (locationid, roomplan) VALUES (:locationid, :roomplan) ON DUPLICATE KEY UPDATE roomplan=:roomplan', - ['locationid' => $locationid, - 'roomplan' => $obj]); - } - - protected function getFurniture($locationid) { - $config = Database::queryFirst('SELECT roomplan FROM location_roomplan WHERE locationid = :locationid', ['locationid' => $locationid]); - if ($config == null) { return null; } - $config = json_decode($config['roomplan'], true); - return $config; - } - protected function getMachinesOnPlan($locationid) { - $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname, position FROM machine WHERE locationid = :locationid', - ['locationid' => $locationid]); - $machines = []; - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { - $machine = []; - $pos = json_decode($row['position'], true); - // TODO: Check if pos is valid (has required keys) - - $machine['muuid'] = $row['machineuuid']; - $machine['ip'] = $row['clientip']; - $machine['mac_address'] = $row['macaddr']; - $machine['hostname'] = $row['hostname']; - $machine['gridRow'] = (int) $pos['gridRow']; - $machine['gridCol'] = (int) $pos['gridCol']; - $machine['itemlook'] = $pos['itemlook']; - $machine['data-width'] = 100; - $machine['data-height'] = 100; - $machines[] = $machine; - } - return ['computers' => $machines]; - } - - protected function getPotentialMachines($locationid) - { - $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' - .'FROM machine INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) ' - .'WHERE subnet.locationid = :locationid', ['locationid' => $locationid]); - - $machines = []; - - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { - $row['combined'] = implode(' ', array_values($row)); - $machines[] = $row; - } - - return $machines; - } + $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' + . 'FROM machine ' + . 'WHERE machineuuid LIKE :query ' + . ' OR macaddr LIKE :query ' + . ' OR clientip LIKE :query ' + . ' OR hostname LIKE :query ', ['query' => "%$query%"]); + + $returnObject = ['machines' => []]; + + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + $returnObject['machines'][] = $row; + } + echo json_encode($returnObject); + } elseif ($this->action === 'save') { + $this->loadRequestedLocation(); + if ($this->locationid === false) { + die('Missing locationid in save data'); + } + if ($this->location === false) { + die('Location with id ' . $this->locationid . ' does not exist.'); + } + $this->handleSaveRequest(true); + die('SUCCESS'); + } else { + echo 'Invalid AJAX action'; + } + } + + private function handleSaveRequest($isAjax) + { + /* save */ + $machinesOnPlan = $this->getMachinesOnPlan(); + $config = Request::post('serializedRoom', null, 'string'); + $config = json_decode($config, true); + 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"); + } + } + $this->saveRoomConfig($config['furniture']); + $this->saveComputerConfig($config['computers'], $machinesOnPlan); + } + + private function sanitizeNumber(&$number, $lower, $upper) + { + if (!is_numeric($number) || $number < $lower) { + $number = $lower; + } elseif ($number > $upper) { + $number = $upper; + } + } + + protected function saveComputerConfig($computers, $oldComputers) + { + + $oldUuids = []; + /* collect all uuids from the old computers */ + foreach ($oldComputers['computers'] as $c) { + $oldUuids[] = $c['muuid']; + } + + $newUuids = []; + foreach ($computers as $computer) { + $newUuids[] = $computer['muuid']; + + // Fix/sanitize properties + // TODO: The list of items, computers, etc. in general is copied and pasted in multiple places. We need a central definition with generators for the various formats we need it in + if (!isset($computer['itemlook']) || !in_array($computer['itemlook'], ['pc-north', 'pc-south', 'pc-west', 'pc-east', 'copier', 'telephone'])) { + $computer['itemlook'] = 'pc-north'; + } + if (!isset($computer['gridRow'])) { + $computer['gridRow'] = 0; + } else { + $this->sanitizeNumber($computer['gridRow'], 0, 32 * 4); + } + if (!isset($computer['gridCol'])) { + $computer['gridCol'] = 0; + } else { + $this->sanitizeNumber($computer['gridCol'], 0, 32 * 4); + } + + $position = json_encode(['gridRow' => $computer['gridRow'], + 'gridCol' => $computer['gridCol'], + 'itemlook' => $computer['itemlook']]); + + Database::exec('UPDATE machine SET position = :position, locationid = :locationid WHERE machineuuid = :muuid', + ['locationid' => $this->locationid, 'muuid' => $computer['muuid'], 'position' => $position]); + } + + $toDelete = array_diff($oldUuids, $newUuids); + + foreach ($toDelete as $d) { + Database::exec("UPDATE machine SET position = '', locationid = NULL WHERE machineuuid = :uuid", ['uuid' => $d]); + } + } + + protected function saveRoomConfig($furniture) + { + $obj = json_encode(['furniture' => $furniture]); + Database::exec('INSERT INTO location_roomplan (locationid, roomplan) VALUES (:locationid, :roomplan) ON DUPLICATE KEY UPDATE roomplan=:roomplan', + ['locationid' => $this->locationid, + 'roomplan' => $obj]); + } + + protected function getFurniture() + { + $config = Database::queryFirst('SELECT roomplan FROM location_roomplan WHERE locationid = :locationid', ['locationid' => $this->locationid]); + if ($config === false) { + return array(); + } + $config = json_decode($config['roomplan'], true); + return $config; + } + + protected function getMachinesOnPlan() + { + $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname, position FROM machine WHERE locationid = :locationid', + ['locationid' => $this->locationid]); + $machines = []; + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + $machine = []; + $pos = json_decode($row['position'], true); + // TODO: Check if pos is valid (has required keys) + + $machine['muuid'] = $row['machineuuid']; + $machine['ip'] = $row['clientip']; + $machine['mac_address'] = $row['macaddr']; + $machine['hostname'] = $row['hostname']; + $machine['gridRow'] = (int)$pos['gridRow']; + $machine['gridCol'] = (int)$pos['gridCol']; + $machine['itemlook'] = $pos['itemlook']; + $machine['data-width'] = 100; + $machine['data-height'] = 100; + $machines[] = $machine; + } + return ['computers' => $machines]; + } + + protected function getPotentialMachines() + { + $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' + . 'FROM machine INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) ' + . 'WHERE subnet.locationid = :locationid', ['locationid' => $this->locationid]); + + $machines = []; + + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + $row['combined'] = implode(' ', array_values($row)); + $machines[] = $row; + } + + return $machines; + } } |