From a40362564fa659ffa92d9ad9bd0ca164b592ff55 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 16 Dec 2020 14:11:43 +0100 Subject: [locations] Avoid potentially duplicate ids in ajax fragments --- modules-available/locations/clientscript.js | 54 ++++++----- modules-available/locations/pages/details.inc.php | 11 +-- .../locations/templates/ajax-opening-location.html | 104 ++++++++++----------- .../locations/templates/location-subnets.html | 12 +-- .../locations/templates/locations.html | 8 +- 5 files changed, 97 insertions(+), 92 deletions(-) diff --git a/modules-available/locations/clientscript.js b/modules-available/locations/clientscript.js index 25c255fb..89abcefc 100644 --- a/modules-available/locations/clientscript.js +++ b/modules-available/locations/clientscript.js @@ -61,60 +61,68 @@ function newOpeningTime(vals) { * Convert fields from simple mode view to entries in expert mode. * @returns {Array} */ -function simpleToExpert() { - var retval = []; - if ($('#week-open').val() || $('#week-close').val()) { +function simpleToExpert($form) { + var retval = [], $open, $close; + $open = $form.find('.week-open'); + $close = $form.find('.week-close'); + if ($open.val() || $close.val()) { retval.push({ 'days': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'], - 'openingtime': $('#week-open').val(), - 'closingtime': $('#week-close').val(), - 'tag': '#week' + 'openingtime': $open.val(), + 'closingtime': $close.val(), + 'tag': 'week' }); } - if ($('#saturday-open').val() || $('#saturday-close').val()) { + $open = $form.find('.saturday-open'); + $close = $form.find('.saturday-close'); + if ($open.val() || $close.val()) { retval.push({ 'days': ['Saturday'], - 'openingtime': $('#saturday-open').val(), - 'closingtime': $('#saturday-close').val(), - 'tag': '#saturday' + 'openingtime': $open.val(), + 'closingtime': $close.val(), + 'tag': 'saturday' }); } - if ($('#sunday-open').val() || $('#sunday-close').val()) { + $open = $form.find('.sunday-open'); + $close = $form.find('.sunday-close'); + if ($open.val() || $close.val()) { retval.push({ 'days': ['Sunday'], - 'openingtime': $('#sunday-open').val(), - 'closingtime': $('#sunday-close').val(), - 'tag': '#sunday' + 'openingtime': $open.val(), + 'closingtime': $close.val(), + 'tag': 'sunday' }); } return retval; } /** - * Triggered when the form is submitted + * Triggered when the openingtimes/WOL form is submitted */ -function submitLocationSettings(event) { +function validateOpeningTimes(event) { var schedule, s, e; var badFormat = false; - $('#settings-outer').find('.red-bg').removeClass('red-bg'); - if ($('#week-open').length > 0) { - schedule = simpleToExpert(); + var $form = $(this); + + $form.find('.red-bg').removeClass('red-bg'); + if ($form.find('.week-open').length > 0) { + schedule = simpleToExpert($form); for (var i = 0; i < schedule.length; ++i) { s = getTime(schedule[i].openingtime); e = getTime(schedule[i].closingtime); if (s === false) { - $(schedule[i].tag + '-open').addClass('red-bg'); + $form.find('.' + schedule[i].tag + '-open').addClass('red-bg'); badFormat = true; } if (e === false || e <= s) { - $(schedule[i].tag + '-close').addClass('red-bg'); + $form.find('.' + schedule[i].tag + '-close').addClass('red-bg'); badFormat = true; } } } else { // Serialize schedule = []; - $('#expert-table').find('.expert-row').each(function () { + $form.find('.expert-table .expert-row').each(function () { var $t = $(this); if ($t.find('.i-delete').is(':checked')) return; // Skip marked as delete var entry = { @@ -149,5 +157,5 @@ function submitLocationSettings(event) { if (badFormat) { event.preventDefault(); } - $('#json-openingtimes').val(JSON.stringify(schedule)); + $form.find('input[name="openingtimes"]').val(JSON.stringify(schedule)); } \ No newline at end of file diff --git a/modules-available/locations/pages/details.inc.php b/modules-available/locations/pages/details.inc.php index aa279570..2f444157 100644 --- a/modules-available/locations/pages/details.inc.php +++ b/modules-available/locations/pages/details.inc.php @@ -33,15 +33,16 @@ class SubPage return false; } - private static function updateOpeningTimes() { - $openingTimes = Request::post('openingtimes', '', 'string'); - $locationid = Request::post('locationid', false, 'int'); + private static function updateOpeningTimes() + { + $openingTimes = Request::post('openingtimes', Request::REQUIRED, 'string'); + $locationid = Request::post('locationid', Request::REQUIRED, 'int'); $wol = Request::post('wol', false, 'bool'); $woloffset = Request::post('wol-offset', 0, 'int'); $sd = Request::post('sd', false, 'bool'); $sdoffset = Request::post('sd-offset', 0, 'int'); - User::assertPermission('location.edit', $locationid); + User::assertPermission('location.edit.*', $locationid); // TODO: Introduce permission // Construct opening-times for database if ($openingTimes !== '') { @@ -109,8 +110,6 @@ class SubPage Scheduler::deleteSchedule($locationid, 'sd'); } } - - return true; } private static function getTime($str) diff --git a/modules-available/locations/templates/ajax-opening-location.html b/modules-available/locations/templates/ajax-opening-location.html index 3b2ea7ae..3207fcd7 100644 --- a/modules-available/locations/templates/ajax-opening-location.html +++ b/modules-available/locations/templates/ajax-opening-location.html @@ -1,8 +1,7 @@ - -
+

{{lang_openingTime}}

{{^expertMode}} -
+
{{lang_expertMode}} @@ -22,7 +21,7 @@ - +
@@ -30,7 +29,7 @@ - +
@@ -41,7 +40,7 @@ - +
@@ -49,7 +48,7 @@ - +
@@ -60,7 +59,7 @@ - +
@@ -68,7 +67,7 @@ - + @@ -76,9 +75,9 @@ {{/expertMode}} -
+
-
+
{{lang_openingTime}}
{{lang_closingTime}}
@@ -87,7 +86,7 @@

- + {{lang_openingTime}} @@ -96,7 +95,7 @@
-