summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2016-09-06 18:47:25 +0200
committerSimon Rettberg2016-09-06 18:47:25 +0200
commitc06b302e6d069446fea27691e293082b65013f1d (patch)
tree76c441c71f0097d3786de46d0848cf3f035c2da5
parent[roomplanner] Remove stray html/head/body tags (diff)
downloadslx-admin-c06b302e6d069446fea27691e293082b65013f1d.tar.gz
slx-admin-c06b302e6d069446fea27691e293082b65013f1d.tar.xz
slx-admin-c06b302e6d069446fea27691e293082b65013f1d.zip
[roomplanner] Use AJAX for saving, show error message if unsuccessful
-rw-r--r--modules-available/roomplanner/config.json2
-rw-r--r--modules-available/roomplanner/js/grid.js14
-rw-r--r--modules-available/roomplanner/js/init.js13
-rw-r--r--modules-available/roomplanner/page.inc.php106
-rw-r--r--modules-available/roomplanner/templates/page.html25
5 files changed, 99 insertions, 61 deletions
diff --git a/modules-available/roomplanner/config.json b/modules-available/roomplanner/config.json
index f65b698f..f620405a 100644
--- a/modules-available/roomplanner/config.json
+++ b/modules-available/roomplanner/config.json
@@ -1,3 +1,3 @@
{
- "dependencies": ["js_jqueryui", "js_selectize", "bootstrap_dialog"]
+ "dependencies": ["js_jqueryui", "js_selectize", "bootstrap_dialog", "statistics", "locations"]
}
diff --git a/modules-available/roomplanner/js/grid.js b/modules-available/roomplanner/js/grid.js
index eb845f9f..80273441 100644
--- a/modules-available/roomplanner/js/grid.js
+++ b/modules-available/roomplanner/js/grid.js
@@ -286,11 +286,15 @@ if (!roomplanner) var roomplanner = {
return JSON.stringify(objects);
},
load: function(object) {
- try {
- var objects = JSON.parse(object);
- } catch(e) {
- alert('invalid JSON format');
- return false;
+ if (typeof object === 'string') {
+ try {
+ var objects = JSON.parse(object);
+ } catch (e) {
+ alert('invalid JSON format');
+ return false;
+ }
+ } else {
+ var objects = object;
}
$('#draw-element-area').html('');
diff --git a/modules-available/roomplanner/js/init.js b/modules-available/roomplanner/js/init.js
index efe14fea..da015711 100644
--- a/modules-available/roomplanner/js/init.js
+++ b/modules-available/roomplanner/js/init.js
@@ -28,8 +28,17 @@ function initRoomplanner() {
});
$("#saveBtn").click(function() {
- $('#serializedRoom').val(roomplanner.serialize());
- $('#roomForm').submit();
+ $.post('?do=roomplanner&locationid=' + locationId,
+ { token: TOKEN, action: 'save', serializedRoom: roomplanner.serialize() }
+ ).done(function ( data ) {
+ if (data.indexOf('SUCCESS') !== -1) {
+ window.close();
+ return;
+ }
+ $('#error-msg').text('Error: ' + data).show();
+ }).fail(function () {
+ $('#error-msg').text('AJAX save call failed').show();
+ });
});
$('#zoom-out').click(function() {
diff --git a/modules-available/roomplanner/page.inc.php b/modules-available/roomplanner/page.inc.php
index 58082395..d437fdd0 100644
--- a/modules-available/roomplanner/page.inc.php
+++ b/modules-available/roomplanner/page.inc.php
@@ -2,6 +2,17 @@
class Page_Roomplanner extends Page
{
+
+ /**
+ * @var int locationid of location we're editing
+ */
+ private $locationid;
+
+ /**
+ * @var string action to perform
+ */
+ private $action;
+
protected function doPreprocess()
{
User::load();
@@ -10,48 +21,44 @@ class Page_Roomplanner extends Page
Message::addError('main.no-permission');
Util::redirect('?do=Main');
}
- }
-
- protected function doRender()
- {
- $locationid = Request::get('locationid', null, 'integer');
+ $this->locationid = Request::get('locationid', null, 'integer');
+ $this->action = Request::any('action', 'show', 'string');
- if ($locationid === null) {
- die('please specify locationid');
+ if ($this->locationid === null) {
+ Message::addError('need-locationid');
+ Util::redirect('?do=locations');
}
- $furniture = $this->getFurniture($locationid);
- $subnetMachines = $this->getPotentialMachines($locationid);
- $machinesOnPlan = $this->getMachinesOnPlan($locationid);
-
- $action = Request::any('action', 'show', 'string');
-
- $roomConfig = array_merge($furniture, $machinesOnPlan);
-
+ if ($this->action === 'save') {
+ $this->handleSaveRequest(false);
+ Util::redirect("?do=roomplanner&locationid={$this->locationid}&action=show");
+ }
+ }
- if ($action === 'show') {
+ protected function doRender()
+ {
+ if ($this->action === 'show') {
/* do nothing */
+ $furniture = $this->getFurniture();
+ $subnetMachines = $this->getPotentialMachines();
+ $machinesOnPlan = $this->getMachinesOnPlan();
+ $roomConfig = array_merge($furniture, $machinesOnPlan);
Render::addTemplate('page', [
'subnetMachines' => json_encode($subnetMachines),
- 'locationid' => $locationid,
+ 'locationid' => $this->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");
+ } else {
+ Message::addError('main.invalid-action', $this->action);
}
}
protected function doAjax()
{
- $action = Request::get('action', null, 'string');
+ $this->action = Request::any('action', null, 'string');
- if ($action === 'getmachines') {
+ if ($this->action === 'getmachines') {
$query = Request::get('query', null, 'string');
/* the query could be anything: UUID, IP or macaddr */
@@ -76,10 +83,37 @@ class Page_Roomplanner extends Page
$returnObject['machines'][] = $row;
}
echo json_encode($returnObject);
+ } elseif ($this->action === 'save') {
+ $this->locationid = Request::any('locationid', null, 'integer');
+ if ($this->locationid === null) {
+ die('Missing locationid in save data');
+ }
+ $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);
}
- protected function saveComputerConfig($locationid, $computers, $oldComputers)
+ protected function saveComputerConfig($computers, $oldComputers)
{
$oldUuids = [];
@@ -97,7 +131,7 @@ class Page_Roomplanner extends Page
'itemlook' => $computer['itemlook']]);
Database::exec('UPDATE machine SET position = :position, locationid = :locationid WHERE machineuuid = :muuid',
- ['locationid' => $locationid, 'muuid' => $computer['muuid'], 'position' => $position]);
+ ['locationid' => $this->locationid, 'muuid' => $computer['muuid'], 'position' => $position]);
}
$toDelete = array_diff($oldUuids, $newUuids);
@@ -107,17 +141,17 @@ class Page_Roomplanner extends Page
}
}
- protected function saveRoomConfig($locationid, $furniture)
+ 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' => $locationid,
+ ['locationid' => $this->locationid,
'roomplan' => $obj]);
}
- protected function getFurniture($locationid)
+ protected function getFurniture()
{
- $config = Database::queryFirst('SELECT roomplan FROM location_roomplan WHERE locationid = :locationid', ['locationid' => $locationid]);
+ $config = Database::queryFirst('SELECT roomplan FROM location_roomplan WHERE locationid = :locationid', ['locationid' => $this->locationid]);
if ($config === false) {
return array();
}
@@ -125,10 +159,10 @@ class Page_Roomplanner extends Page
return $config;
}
- protected function getMachinesOnPlan($locationid)
+ protected function getMachinesOnPlan()
{
$result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname, position FROM machine WHERE locationid = :locationid',
- ['locationid' => $locationid]);
+ ['locationid' => $this->locationid]);
$machines = [];
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$machine = [];
@@ -149,11 +183,11 @@ class Page_Roomplanner extends Page
return ['computers' => $machines];
}
- protected function getPotentialMachines($locationid)
+ 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' => $locationid]);
+ . 'WHERE subnet.locationid = :locationid', ['locationid' => $this->locationid]);
$machines = [];
diff --git a/modules-available/roomplanner/templates/page.html b/modules-available/roomplanner/templates/page.html
index 711ecf3b..344191d4 100644
--- a/modules-available/roomplanner/templates/page.html
+++ b/modules-available/roomplanner/templates/page.html
@@ -44,6 +44,8 @@
<!-- berryous raumplaner -->
<h1>{{lang_roomplanner}}</h1>
+<div class="alert alert-danger" style="display:none" id="error-msg"></div>
+
<div id="toolpanel" class="panel panel-default" style="z-index:200;">
<div class="panel-heading">
<h3 class="panel-title">Werkzeuge</h3>
@@ -309,26 +311,16 @@
</div>
- <div class="panel panel-default" style="display:none">
- <div class="panel-heading"><h3 class="panel-title">Store / Restore</h3></div>
- <div class="panel-body">
- <form id="roomForm" method="POST" action="?do=roomplanner&locationid={{locationid}}">
- <input type="hidden" name="token" value="{{token}}">
- <input type="hidden" name="action" value="save">
- <div class="form-group">
- <label for="serializedRoom" class="col-sm-2 control-label">In-/Output</label>
- <textarea class="form-control" rows="5" name="serializedRoom" id="serializedRoom">{{{roomConfiguration}}}</textarea>
- </div>
- </form>
- </div>
- </div>
- <button id="saveBtn" class="btn btn-success" onclick="window.close(); return false">Save</button>
+ <button id="saveBtn" class="btn btn-success">Save</button>
<script type="application/javascript"><!--
+var locationId = '{{locationid}}';
+var subnetMachines, roomConfiguration;
document.addEventListener("DOMContentLoaded", function () {
subnetMachines = {{{subnetMachines}}};
+ roomConfiguration = {{{roomConfiguration}}};
$.when(
$.getScript("modules/roomplanner/js/lib/jquery-collision.js"),
@@ -340,10 +332,9 @@ document.addEventListener("DOMContentLoaded", function () {
).done(function() {
$.getScript("modules/roomplanner/js/init.js", function() {
initRoomplanner();
- roomplanner.load($('#serializedRoom').val());
- console.log(subnetMachines);
+ roomplanner.load(roomConfiguration); // TODO: Filter invalid PCs, they're currently invisible and cannot be removed
});
});
});
-</script>
+// --></script>