From 287a6eacfa6b59849bd8343d0d13691fea2df128 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 11 Aug 2023 15:57:42 +0000 Subject: [PATCH] Bug 26916: Show searchable patron attributes in patron search dropdown This patch adds attributes that have been marked searchable to the patron search dropdowns To test: 1 - Define some new patron attribute types and make then staff_searchable 2 - Add some values to patrons 3 - Confirm they are searched in 'Standard' search, but there is no other way to search them 4 - Apply patch 5 - Confirm the fields now show in patorn search dropdowns 6 - Search using 'standard' and confirm the searhc works 7 - Search specific attributes and confirm the searhc works --- .../prog/en/includes/patronfields.inc | 9 ++++++++ .../intranet-tmpl/prog/js/staff-global.js | 23 ++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc index 777cc300bd..d3e111eb3d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patronfields.inc @@ -99,5 +99,14 @@ [% END %] [% END %] + [% IF Koha.Preference('ExtendedPatronAttributes') %] + [% FOREACH attribute IN ExtendedAttributeTypes.all( staff_searchable => 1 ) %] + [% IF searchfieldstype == "_ATTR_" _ attribute.code %] + + [% ELSE %] + + [% END %] + [% END %] + [% END %] [%- END -%] diff --git a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js index cb03827e4f..b254bd69a0 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js +++ b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js @@ -548,7 +548,7 @@ function buildPatronSearchQuery(term, options) { let q = []; let leading_wildcard; - let search_fields; + let search_fields = []; let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); // Bail if no patterns @@ -564,9 +564,18 @@ function buildPatronSearchQuery(term, options) { leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; } + let searched_attribute_fields = []; // Search fields: If search_fields option exists, we use that if (typeof options !== 'undefined' && options.search_fields) { - search_fields = expandPatronSearchFields(options.search_fields); + expand_fields = expandPatronSearchFields(options.search_fields); + expand_fields.split('\|').forEach(function (field, i) { + if( field.startsWith('_ATTR_') ){ + let attr_field = field.replace("_ATTR_",""); + searched_attribute_fields.push( attr_field ); + } else { + search_fields.push( field ); + } + }); // If not, we use DefaultPatronSearchFields system preference instead } else { search_fields = defaultPatronSearchFields; @@ -576,7 +585,7 @@ function buildPatronSearchQuery(term, options) { let pattern_subquery_and = []; patterns.forEach(function (pattern, i) { let pattern_subquery_or = []; - search_fields.split('|').forEach(function (field, i) { + search_fields.forEach(function (field, i) { pattern_subquery_or.push( { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } ); @@ -593,9 +602,10 @@ function buildPatronSearchQuery(term, options) { }); q.push({ "-and": pattern_subquery_and }); + // Add full search term for each search field let term_subquery_or = []; - search_fields.split('\|').forEach(function (field, i) { + search_fields.forEach(function (field, i) { term_subquery_or.push( { ["me." + field]: { 'like': leading_wildcard + term + '%' } } ); @@ -603,13 +613,14 @@ function buildPatronSearchQuery(term, options) { q.push({ "-or": term_subquery_or }); // Add each pattern for each extended patron attributes - if (typeof options !== 'undefined' && options.search_fields == 'standard' && options.extended_attribute_types && extendedPatronAttributes) { + if ( typeof options !== 'undefined' && ( (options.search_fields == 'standard' && options.extended_attribute_types) || searched_attribute_fields ) && extendedPatronAttributes) { + extended_attribute_codes_to_search = (searched_attribute_fields.length > 0) ? searched_attribute_fields : options.extended_attribute_types; extended_attribute_subquery_and = []; patterns.forEach(function (pattern, i) { let extended_attribute_sub_or = []; extended_attribute_sub_or.push({ "extended_attributes.value": { "like": leading_wildcard + pattern + '%' }, - "extended_attributes.code": options.extended_attribute_types + "extended_attributes.code": extended_attribute_codes_to_search }); extended_attribute_subquery_and.push(extended_attribute_sub_or); }); -- 2.30.2