From 582f0998c91b9a07a6b4b8051f902c43c5fd0f52 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 4 Apr 2024 12:35:56 +0000 Subject: [PATCH] Bug 36521: Checkbox preferences should be allowed to be submitted empty Visit: http://localhost:8081/cgi-bin/koha/admin/preferences.pl?tab=i18n_l10n Uncheck English on both 'language' and 'OPACLanguages'. Save. Notice "Nothing to save" shows. Apply patch. Repeat. Notice you can now save with all options empty. To my knowledge, these 2 language preferences are the only checkbox preferences of this kind (correct me if I'm wrong please). I need to add a new system preference for bug 35604 listing all installed ILL backends so the user may enable or disable each of the backends. I want that particular system preference to be disabled as a whole if all backends are unchecked, but I was unable to uncheck all before this patch. I understand that for languages checkboxes specifically not allowing everything to be empty may make sense (at least one language should be enabled, after all). But these languages checkbox preferences should be the exception, not the rule, for this behaviour. --- .../prog/js/pages/preferences.js | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js index 5da43dbf572..351e42fc44e 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js +++ b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js @@ -10,10 +10,24 @@ KOHA.Preferences = { let sysprefs = $(form).find('.modified').not('.preference-checkbox').toArray().reduce((map, e) => ({ ...map, [$(e).attr('name')]: [$(e).val()].flat()}), {}); - // language prefs - $(form).find('.modified.preference-checkbox:checked').toArray().forEach((elt) => { - (sysprefs[$(elt).attr('name')] = sysprefs[$(elt).attr('name')] || []).push($(elt).val()); - }); + // checkbox prefs + let modified_boxes = $(form).find(".modified.preference-checkbox"); + let all_boxes_unchecked = true; + if (modified_boxes.length > 0) { + modified_boxes + .toArray() + .filter(elt => $(elt).prop("checked")) + .forEach(elt => { + all_boxes_unchecked = false; + (sysprefs[$(elt).attr("name")] = + sysprefs[$(elt).attr("name")] || []).push($(elt).val()); + }); + if (all_boxes_unchecked) { + modified_boxes.toArray().forEach(elt => { + sysprefs[$(elt).attr("name")] = []; + }); + } + } if ( !Object.keys(sysprefs).length ) { humanMsg.displayAlert( __("Nothing to save") ); -- 2.30.2