From d1ef38233286b3fb19349dc45f850c22b740bddb Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Thu, 10 Aug 2023 18:58:55 +0000
Subject: [PATCH] Bug 33428: Parse search fields in buildPatronSearchQuery

This patch moves the parsing of standard search_field into the buildPatronQuery subroutine
and adds a check for 'standard' field before adding attributes to the search

To test:
1 - Add a new attribute type and make it searchable
2 - Add a value to a patron
3 - Search for this value using 'Standard' fields, confirm you get the patron
4 - Search for the value using 'Cardnumber' field, confirm you get the patron - BAD!
5 - Apply patch
6 - Repeat cardnumebr search, confirm patron not found - Yay!
7 - Search standard, confirm patron is found
8 - Add a new field to 'DefaultPatronSearchFields
9 - Confirm it appears in patron search dropdown
10 - Confirm a search of this field with the attribute value does not return the patron
---
 .../prog/en/includes/patronfields.inc         | 14 +++++------
 .../intranet-tmpl/prog/js/staff-global.js     | 24 +++++++++++++++++--
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc
index c48fa6c28a..163aa2c690 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc
@@ -1,9 +1,9 @@
 [%- BLOCK patron_fields -%]
      [%- SWITCH name -%]
-     [%- CASE standard -%]<span>Standard</span>
-     [%- CASE full_address -%]<span>Full address</span>
-     [%- CASE all_emails -%]<span>All emails</span>
-     [%- CASE all_phones -%]<span>All phones</span>
+     [%- CASE 'standard' -%]<span>Standard</span>
+     [%- CASE 'full_address' -%]<span>Full address</span>
+     [%- CASE 'all_emails' -%]<span>All emails</span>
+     [%- CASE 'all_phones' -%]<span>All phones</span>
      [%- CASE 'borrowernumber' -%]<span>Borrowernumber</span>
      [%- CASE 'cardnumber' -%]<span>Card number</span>
      [%- CASE 'surname' -%]<span>Surname</span>
@@ -87,10 +87,8 @@
         <select name="searchfieldstype" id="searchfieldstype_filter">
     [% END %]
         [% SET standard = Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' %]
-        [% SET full_address = 'streetnumber,streettype,address,address2,city,state,zipcode,country' %]
-        [% SET all_emails = 'email,emailpro,B_email' %]
-        [% SET all_phones = 'phone,phonepro,B_phone,altcontactphone,mobile' %]
-        [% default_fields = [ standard, 'surname', 'cardnumber', all_emails, 'borrowernumber', 'userid', all_phones, full_address, 'dateofbirth', 'sort1', 'sort2' ] %]
+        [%# Above is needed for adding fields from the DefaultPatronSearchFields preference to the dropdowns %]
+        [% default_fields = [ 'standard', 'surname', 'cardnumber', 'all_emails', 'borrowernumber', 'userid', 'all_phones', 'full_address', 'dateofbirth', 'sort1', 'sort2' ] %]
         [% search_options = default_fields.merge(standard.split(',')).unique %]
         [% FOREACH s_o IN search_options %]
             [% display_name = PROCESS patron_fields name=s_o %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
index abb3193bd7..6b08909a2c 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
@@ -517,6 +517,25 @@ function patron_autocomplete(node, options) {
         };
 }
 
+function expandPatronSearchFields(search_fields) {
+    switch(search_fields) {
+        case 'standard':
+            return defaultPatronSearchFields;
+            break;
+        case 'full_address':
+            return 'streetnumber,streettype,address,address2,city,state,zipcode,country';
+            break;
+        case 'all_emails':
+            return 'email,emailpro,B_email';
+            break;
+        case 'all_phones':
+            return 'phone,phonepro,B_phone,altcontactphone,mobile';
+            break;
+        default:
+            return search_fields;
+    }
+}
+
 /**
  * Build patron search query
  * - term: The full search term input by the user
@@ -547,12 +566,13 @@ function buildPatronSearchQuery(term, options) {
 
     // Search fields: If search_fields option exists, we use that
     if (typeof options !== 'undefined' && options.search_fields) {
-        search_fields = options.search_fields;
+        search_fields = expandPatronSearchFields(options.search_fields);
     // If not, we use DefaultPatronSearchFields system preference instead
     } else {
         search_fields = defaultPatronSearchFields;
     }
 
+
     // Add each pattern for each search field
     let pattern_subquery_and = [];
     patterns.forEach(function (pattern, i) {
@@ -584,7 +604,7 @@ function buildPatronSearchQuery(term, options) {
     q.push({ "-or": term_subquery_or });
 
     // Add each pattern for each extended patron attributes
-    if (typeof options !== 'undefined' && options.extended_attribute_types && extendedPatronAttributes) {
+    if (typeof options !== 'undefined' && options.search_fields == 'standard' && options.extended_attribute_types && extendedPatronAttributes) {
         extended_attribute_subquery_and = [];
         patterns.forEach(function (pattern, i) {
             let extended_attribute_sub_or = [];
-- 
2.30.2