summaryrefslogtreecommitdiffstats
path: root/modules-available/locations
diff options
context:
space:
mode:
authorSimon Rettberg2021-03-19 13:54:22 +0100
committerSimon Rettberg2021-03-19 13:54:22 +0100
commit4c0862efe57fbdaa51a69c8dc39f6fc4ae45fa20 (patch)
treecf5d472d2bea7a4ba43339bf892db6dad710e51c /modules-available/locations
parent[locations] Add permission for openingtimes (diff)
downloadslx-admin-4c0862efe57fbdaa51a69c8dc39f6fc4ae45fa20.tar.gz
slx-admin-4c0862efe57fbdaa51a69c8dc39f6fc4ae45fa20.tar.xz
slx-admin-4c0862efe57fbdaa51a69c8dc39f6fc4ae45fa20.zip
[locations/rebootcontrol] Inherit openingtimes for WOL/shutdown
The opening times schedule is now inherited to child locations, so it's easy to toggle WOL or shutdown for individual rooms in a building, where you only have to set the opening times once for the entire building. As of now, WOL and shutdown settings are *not* inherited to child locations, as I'm not sure if you always want to inherit those by default. Closes #3710
Diffstat (limited to 'modules-available/locations')
-rw-r--r--modules-available/locations/inc/openingtimes.inc.php28
-rw-r--r--modules-available/locations/install.inc.php7
-rw-r--r--modules-available/locations/lang/de/template-tags.json1
-rw-r--r--modules-available/locations/lang/en/template-tags.json1
-rw-r--r--modules-available/locations/pages/details.inc.php55
-rw-r--r--modules-available/locations/templates/ajax-opening-location.html17
6 files changed, 69 insertions, 40 deletions
diff --git a/modules-available/locations/inc/openingtimes.inc.php b/modules-available/locations/inc/openingtimes.inc.php
new file mode 100644
index 00000000..3417a213
--- /dev/null
+++ b/modules-available/locations/inc/openingtimes.inc.php
@@ -0,0 +1,28 @@
+<?php
+
+class OpeningTimes
+{
+
+ /**
+ * Get opening times for given location.
+ */
+ public static function forLocation(int $locationId)
+ {
+ static $openingTimesList = false;
+ if ($openingTimesList === false) {
+ $openingTimesList = Database::queryKeyValueList("SELECT locationid, openingtime FROM location
+ WHERE openingtime IS NOT NULL");
+ }
+ $chain = Location::getLocationRootChain($locationId);
+ $openingTimes = null;
+ foreach ($chain as $lid) {
+ if (isset($openingTimesList[$lid])) {
+ if (is_string($openingTimesList[$lid])) {
+ $openingTimesList[$lid] = json_decode($openingTimesList[$lid], true);
+ }
+ return $openingTimesList[$lid];
+ }
+ }
+ return null;
+ }
+} \ No newline at end of file
diff --git a/modules-available/locations/install.inc.php b/modules-available/locations/install.inc.php
index c5fd9688..46a6544c 100644
--- a/modules-available/locations/install.inc.php
+++ b/modules-available/locations/install.inc.php
@@ -15,7 +15,7 @@ $res[] = tableCreate('location', '
`locationid` INT(11) NOT NULL AUTO_INCREMENT,
`parentlocationid` INT(11) NOT NULL,
`locationname` VARCHAR(100) NOT NULL,
- `openingtime` BLOB,
+ `openingtime` BLOB DEFAULT NULL,
PRIMARY KEY (`locationid`),
KEY `locationname` (`locationname`),
KEY `parentlocationid` (`parentlocationid`)
@@ -40,7 +40,7 @@ $res[] = tableAddConstraint('setting_location', 'locationid', 'location', 'locat
// 2020-07-14 Add openingtime column to location table, then migrate data and delete the column from locationinfo
if (!tableHasColumn('location', 'openingtime')) {
- if (Database::exec("ALTER TABLE location ADD openingtime BLOB") === false) {
+ if (Database::exec("ALTER TABLE location ADD openingtime BLOB DEFAULT NULL") === false) {
finalResponse(UPDATE_FAILED, 'Could not create openingtime column');
}
$res[] = UPDATE_DONE;
@@ -60,5 +60,8 @@ if (tableHasColumn('locationinfo_locationconfig', 'openingtime')) {
$res[] = UPDATE_DONE;
}
+// 2021-03-19: Fix this. No idea how this came to be, maybe during dev only? But better be safe...
+Database::exec("UPDATE location SET openingtime = NULL WHERE openingtime = ''");
+
// Create response for browser
responseFromArray($res);
diff --git a/modules-available/locations/lang/de/template-tags.json b/modules-available/locations/lang/de/template-tags.json
index 79bdbac6..94b348d2 100644
--- a/modules-available/locations/lang/de/template-tags.json
+++ b/modules-available/locations/lang/de/template-tags.json
@@ -15,6 +15,7 @@
"lang_endAddress": "Endadresse",
"lang_expertMode": "Expertenmodus",
"lang_fixMachineAssign": "Zuweisungen anzeigen\/aufheben",
+ "lang_inheritOpeningTimes": "Vom \u00fcbergeordneten Ort \u00fcbernehmen",
"lang_ip": "IP-Adresse",
"lang_listOfSubnets": "Liste der Subnetze",
"lang_location": "Ort",
diff --git a/modules-available/locations/lang/en/template-tags.json b/modules-available/locations/lang/en/template-tags.json
index 5790902b..d60aa291 100644
--- a/modules-available/locations/lang/en/template-tags.json
+++ b/modules-available/locations/lang/en/template-tags.json
@@ -15,6 +15,7 @@
"lang_endAddress": "End address",
"lang_expertMode": "Expert mode",
"lang_fixMachineAssign": "Fix or remove assignment",
+ "lang_inheritOpeningTimes": "Inherit from parent location",
"lang_ip": "IP address",
"lang_listOfSubnets": "List of subnets",
"lang_location": "Location",
diff --git a/modules-available/locations/pages/details.inc.php b/modules-available/locations/pages/details.inc.php
index 19a89c88..356620d3 100644
--- a/modules-available/locations/pages/details.inc.php
+++ b/modules-available/locations/pages/details.inc.php
@@ -35,6 +35,7 @@ class SubPage
private static function updateOpeningTimes()
{
+ $otInherited = Request::post('openingtimes-inherited', false, 'bool');
$openingTimes = Request::post('openingtimes', Request::REQUIRED, 'string');
$locationid = Request::post('locationid', Request::REQUIRED, 'int');
$wol = Request::post('wol', false, 'bool');
@@ -45,10 +46,12 @@ class SubPage
User::assertPermission('location.edit.openingtimes', $locationid);
// Construct opening-times for database
- if ($openingTimes !== '') {
+ if ($otInherited || $openingTimes === '') {
+ $openingTimes = null;
+ } else {
$openingTimes = json_decode($openingTimes, true);
if (!is_array($openingTimes)) {
- $openingTimes = '';
+ $openingTimes = null;
} else {
$mangled = array();
foreach (array_keys($openingTimes) as $key) {
@@ -89,32 +92,8 @@ class SubPage
array('locationid' => $locationid, 'openingtime' => $openingTimes));
if (Module::isAvailable('rebootcontrol')) {
- if ($wol || $sd) {
- $options = array();
-
- // Sanity checks
- if ($woloffset > 15) {
- $woloffset = 15;
- } elseif ($woloffset < 0) {
- $woloffset = 0;
- }
- if ($sdoffset > 15) {
- $sdoffset = 15;
- } elseif ($sdoffset < 0) {
- $sdoffset = 0;
- }
-
- // Set options
- $options['wol'] = $wol;
- $options['wol-offset'] = $woloffset;
- $options['sd'] = $sd;
- $options['sd-offset'] = $sdoffset;
-
- Scheduler::updateSchedule($locationid, $options, $openingTimes);
-
- } else {
- Scheduler::deleteSchedule($locationid);
- }
+ // Set options
+ Scheduler::setLocationOptions($locationid, $wol, $sd, $woloffset, $sdoffset);
}
}
@@ -429,25 +408,29 @@ class SubPage
private static function ajaxOpeningTimes($id)
{
User::assertPermission('location.edit.openingtimes', $id);
- $openTimes = Database::queryFirst("SELECT openingtime FROM `location` WHERE locationid = :id", array('id' => $id));
- if ($openTimes !== false) {
+ $data = ['id' => $id];
+ $openTimes = Database::queryFirst("SELECT openingtime FROM `location`
+ WHERE locationid = :id", array('id' => $id));
+ if ($openTimes === false) {
+ Message::addError('invalid-location-id', $id);
+ return;
+ }
+ if ($openTimes['openingtime'] !== null) {
$openingTimes = json_decode($openTimes['openingtime'], true);
+ } else {
+ $openingTimes = OpeningTimes::forLocation($id);
+ $data['openingtimes_inherited'] = 'checked';
}
if (!isset($openingTimes) || !is_array($openingTimes)) {
$openingTimes = array();
}
- $data = array('id' => $id);
$data['expertMode'] = !self::isSimpleMode($openingTimes);
$data['schedule_data'] = json_encode($openingTimes);
$rebootcontrol = Module::isAvailable('rebootcontrol');
$data['rebootcontrol'] = $rebootcontrol;
if ($rebootcontrol) {
- $res = Database::queryFirst("SELECT action, nextexecution, options FROM `reboot_scheduler`
- WHERE locationid = :id", ['id' => $id]);
- if ($res !== false) {
- $data['scheduler-options'] = json_decode($res['options'], true);
- }
+ $data['scheduler-options'] = Scheduler::getLocationOptions($id);
}
echo Render::parse('ajax-opening-location', $data);
diff --git a/modules-available/locations/templates/ajax-opening-location.html b/modules-available/locations/templates/ajax-opening-location.html
index b1aea2e2..5c741857 100644
--- a/modules-available/locations/templates/ajax-opening-location.html
+++ b/modules-available/locations/templates/ajax-opening-location.html
@@ -1,5 +1,10 @@
<div>
<h3>{{lang_openingTime}}</h3>
+ <div class="checkbox">
+ <input id="oi{{id}}" class="openingtimes-inherited"
+ type="checkbox" name="openingtimes-inherited" value="1" {{openingtimes_inherited}}>
+ <label for="oi{{id}}">{{lang_inheritOpeningTimes}}</label>
+ </div>
{{^expertMode}}
<div class="simple-mode">
@@ -135,7 +140,7 @@
<div class="col-sm-8">
<div class="input-group">
<input disabled type="number" id="wol-offset-{{id}}" name="wol-offset" class="form-control"
- value="{{scheduler-options.wol-offset}}" placeholder="0" min="0" max="15">
+ value="{{scheduler-options.wol-offset}}" placeholder="0" min="0" max="60">
<span class="input-group-addon slx-ga2">
<label for="wol-offset-{{id}}">{{lang_offsetEarly}}</label>
</span>
@@ -152,7 +157,7 @@
<div class="col-sm-8">
<div class="input-group">
<input disabled type="number" id="sd-offset-{{id}}" name="sd-offset" class="form-control"
- value="{{scheduler-options.sd-offset}}" placeholder="0" min="0" max="15">
+ value="{{scheduler-options.sd-offset}}" placeholder="0" min="0" max="60">
<span class="input-group-addon slx-ga2">
<label for="sd-offset-{{id}}">{{lang_offsetLate}}</label>
</span>
@@ -204,6 +209,7 @@
$loc.find('.new-openingtime').click(function (e) {
e.preventDefault();
setTimepicker(newOpeningTime($loc, {}).find('.timepicker2'));
+ setInputEnabled();
});
$loc.find('.btn-show-expert').click(function (e) {
@@ -214,9 +220,16 @@
}
$loc.find('.simple-mode').remove();
$loc.find('.expert-mode').show();
+ setInputEnabled();
});
$loc.find('form').submit(validateOpeningTimes);
+ var setInputEnabled = function () {
+ $loc.find('.expert-mode input, .simple-mode input').prop('disabled', $inheritCb.is(':checked') ? 'disabled' : false);
+ };
+ var $inheritCb = $loc.find('.openingtimes-inherited');
+ setInputEnabled();
+ $inheritCb.change(setInputEnabled);
})();
</script>