From 624c5db5da728fe31c7e5315946e7bd9dda96271 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 6 Nov 2020 11:17:56 +0000
Subject: [PATCH] Bug 17364: Add ability to exclude some columns from selection
 for system preferences

Bug 22844 Added the option to provide a modal for selecting database columns for system preferences

Some system preferences are limited to a subset of the columns allowed. For instance, setting branchcode
in BorrowerUnwantedFields prevents adding new patrons

This patch allows for defining exclusions in the .pref file

To test:
1 - Alter BorroweUnwantedFields and select branchcode
2 - Attempt to add a new patron
3 - Get a message like "Something went wrong. Check the logs"
4 - Apply patches
5 - Alter the preference again
6 - Note that branchcode is unchecked and disabled
7 - Save the pref
8 - Add a patron
9 - It succeeds
---
 admin/preferences.pl                                             | 1 +
 koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt     | 2 +-
 .../intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref | 1 +
 koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js             | 9 ++++++++-
 4 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/admin/preferences.pl b/admin/preferences.pl
index a39b6dd8f9..1353397b9b 100755
--- a/admin/preferences.pl
+++ b/admin/preferences.pl
@@ -67,6 +67,7 @@ sub _get_chunk {
 
     if( $options{'type'} && $options{'type'} eq 'modalselect' ){
         $chunk->{'source'} = $options{'source'};
+        $chunk->{'exclusions'} = $options{'exclusions'} // "";
         $chunk->{'type'} = 'modalselect';
     }
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt
index 977a7834fe..45a12476a0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences.tt
@@ -111,7 +111,7 @@
                         [% END %]
                     </select>
                     [% ELSIF ( CHUNK.type_modalselect ) %]
-                        <input type="text" name="pref_[% CHUNK.name | html %]" id="pref_[% CHUNK.name | html %]" class="modalselect preference preference-[% CHUNK.type | html %]" data-source="[% CHUNK.source | html %]" readonly="readonly" value="[% CHUNK.value | html %]"/>
+                        <input type="text" name="pref_[% CHUNK.name | html %]" id="pref_[% CHUNK.name | html %]" class="modalselect preference preference-[% CHUNK.type | html %]" data-source="[% CHUNK.source | html %]" data-exclusions="[% CHUNK.exclusions %]" readonly="readonly" value="[% CHUNK.value | html %]"/>
                     [% ELSIF ( CHUNK.type_multiple ) %]
                     <select name="pref_[% CHUNK.name | html %]" id="pref_[% CHUNK.name | html %]" class="preference preference-[% CHUNK.class or "choice" | html %]" multiple="multiple">
                         [% FOREACH CHOICE IN CHUNK.CHOICES %][% IF ( CHOICE.selected ) %]<option value="[% CHOICE.value | html %]" selected="selected">[% ELSE %]<option value="[% CHOICE.value | html %]">[% END %][% CHOICE.text | html %]</option>[% END %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref
index b618fdfe4e..71b4d795bf 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref
@@ -197,6 +197,7 @@ Patrons:
          - pref: BorrowerUnwantedField
            type: modalselect
            source: borrowers
+           exclusions: branchcode
      -
          - "Borrowers can have the following titles:"
          - pref: BorrowersTitles
diff --git a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js
index 2ff49b3f3b..b4d243cb2c 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/pages/preferences.js
@@ -225,6 +225,7 @@ $( document ).ready( function () {
 
     $(".modalselect").on("click", function(){
         var datasource = $(this).data("source");
+        var exclusions = $(this).data("exclusions").split('|');
         var pref_name = this.id.replace(/pref_/, '');
         var pref_value = this.value;
         var prefs = pref_value.split("|");
@@ -238,7 +239,13 @@ $( document ).ready( function () {
                 } else {
                     checked = "";
                 }
-                items.push('<label><input class="dbcolumn_selection" type="checkbox" id="' + key + '"' + checked + ' name="pref" value="' + val + '" /> ' + key + '</label>');
+                if( exclusions.indexOf( val ) >= 0 ){
+                    disabled = ' disabled="disabled" ';
+                    checked  = "";
+                } else {
+                    disabled = "";
+                }
+                items.push('<label><input class="dbcolumn_selection" type="checkbox" id="' + key + '"' + checked + disabled + ' name="pref" value="' + val + '" /> ' + key + '</label>');
             });
             $("<div/>", {
                 "class": "columns-2",
-- 
2.11.0