From 84e8f93ef254ec0b011ef92d8fe61b5c1e9ce59a Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Mon, 26 Jun 2023 12:51:13 +0000 Subject: [PATCH] Bug 34092: Move search-patron.inc logic into buildPatronSearchQuery --- .../prog/en/includes/js_includes.inc | 1 + .../prog/en/includes/patron-search.inc | 63 +++++-------------- .../intranet-tmpl/prog/js/staff-global.js | 52 +++++++++++++-- 3 files changed, 63 insertions(+), 53 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc index 1e687f8ab7..39e34d72df 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/js_includes.inc @@ -71,6 +71,7 @@ diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc index 9cead35acc..ee09b35eb2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc @@ -19,6 +19,8 @@ [%# You can then pass a list of filters %] [%# - branch: select library list %] [%# - category: select patron category list %] +[%# - sort1: select patron sort1 field %] +[%# - sort2: select patron sort2 field %] [%# - search_field: select patron field list %] [%# - search_type: select 'contain' or 'start with' %] [% BLOCK patron_search_filters %] @@ -315,6 +317,20 @@ }, "-and": function(){ let filters = []; + + let search_type = $("#searchtype_filter").val() || "contain"; + let search_fields = $("#searchfieldstype_filter").val(); + let pattern = $("#search_patron_filter").val(); + + filters = buildPatronSearchQuery( + pattern, + { + search_type: search_type, + search_fields: search_fields, + extended_attribute_types: extended_attribute_types + } + ); + let f_sort1 = $("#sort1_filter").val(); if ( f_sort1 ) { filters.push({ @@ -328,53 +344,6 @@ }); } - let pattern = $("#search_patron_filter").val(); - if (!pattern) { - if ( filters.length == 0 ) { - return ""; - } - else { - return filters; - } - } - let patterns = pattern.split(/[\s,]+/).filter(function(s){ return s.length }); - - let search_type = $("#searchtype_filter").val() || "contain"; - let search_fields = $("#searchfieldstype_filter").val(); - if ( !search_fields ) { - search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]"; - } - - let subquery_and = []; - patterns.forEach(function(pattern,i){ - let sub_or = []; - search_fields.split(',').forEach(function(attr,ii){ - sub_or.push({["me."+attr]:{"like":(search_type == "contain" ? "%" : "" ) + pattern + "%"}}); - if ( attr == 'dateofbirth' ) { - try { - let d = $date_to_rfc3339(pattern); - sub_or.push({["me."+attr]:d}); - } catch { - // Hide the warning if the date is not correct - } - } - }); - subquery_and.push(sub_or); - }); - filters.push({"-and": subquery_and}); - - [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %] - subquery_and = []; - patterns.forEach(function(pattern,i){ - let sub_or = []; - sub_or.push({ - "extended_attributes.value": { "like": "%" + pattern + (search_type == "contain" ? "%" : "" )}, - "extended_attributes.code": extended_attribute_types - }); - subquery_and.push(sub_or); - }); - filters.push({"-and": subquery_and}); - [% END %] return filters; } }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js index 3203109d23..4c0e3c0c5c 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js +++ b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js @@ -514,19 +514,47 @@ function patron_autocomplete(node, options) { function buildPatronSearchQuery(term, options) { let q = []; - let leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; - let search_fields = defaultPatronSearchFields; + let leading_wildcard; + let search_fields; + let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); + + // Bail if no patterns + if (patterns.length == 0) { + return; + } + + // Leading wildcard: If search_type option exists, we use that + if (typeof options !== 'undefined' && options.search_type) { + leading_wildcard = options.search_type === "contain" ? '%' : ''; + // If not, we use DefaultPatronSearchMethod system preference instead + } else { + leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; + } + + // Search fields: If search_fields option exists, we use that + if (typeof options !== 'undefined' && options.search_fields) { + search_fields = 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 = []; - term.split(/[\s,]+/) - .filter(function (s) { return s.length }) - .forEach(function (pattern, i) { + patterns.forEach(function (pattern, i) { let pattern_subquery_or = []; search_fields.split(',').forEach(function (field, i) { pattern_subquery_or.push( { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } ); + if (field == 'dateofbirth') { + try { + let d = $date_to_rfc3339(pattern); + pattern_subquery_or.push({ ["me." + field]: d }); + } catch { + // Hide the warning if the date is not correct + } + } }); pattern_subquery_and.push(pattern_subquery_or); }); @@ -541,6 +569,18 @@ 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) { + 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": extended_attribute_types + }); + extended_attribute_subquery_and.push(extended_attribute_sub_or); + }); + q.push({ "-and": extended_attribute_subquery_and }); + } return q; } -- 2.30.2