From 0c31c9d2d61b87520c197a3b377963ab34d9efc3 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 27 Jun 2023 09:07:49 +0000 Subject: [PATCH] Bug 34058: Add table_prefix argument to buildPatronSearchQuery Signed-off-by: Martin Renvoize --- .../intranet-tmpl/prog/js/staff-global.js | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js index 796f709d55..74a2cb51d9 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js +++ b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js @@ -543,13 +543,15 @@ function expandPatronSearchFields(search_fields) { * Build patron search query * - term: The full search term input by the user * You can then pass a list of options: - * - search_type: String 'contains' or 'starts_with', defaults to DefaultPatronSearchMethod system preference - * - search_fields: String comma-separated list of specific fields, defaults to DefaultPatronSearchFields system preference - * - extended_attribute_types: JSON object containing the patron attribute types to be searched on + * - search_type: (String) 'contains' or 'starts_with', defaults to defaultPatronSearchMethod (see js_includes.inc) + * - search_fields: (String) comma-separated list of specific fields, defaults to defaultPatronSearchFields (see js_includes.inc) + * - extended_attribute_types: (JSON object) contains the patron searchable attribute types to be searched on (see patron-search.inc) + * - table_prefix: (String) table name to prefix the fields with, defaults to 'me' */ function buildPatronSearchQuery(term, options) { let q = []; + let table_prefix; let leading_wildcard; let search_fields; let patterns = term.split(/[\s,]+/).filter(function (s) { return s.length }); @@ -559,15 +561,23 @@ function buildPatronSearchQuery(term, options) { return q; } - // Leading wildcard: If search_type option exists, we use that + // Table prefix: If table_prefix options exists, use that + if (typeof options !== 'undefined' && options.table_prefix) { + table_prefix = options.table_prefix; + // If not, default to 'me' + } else { + table_prefix = 'me'; + } + + // Leading wildcard: If search_type option exists, use that if (typeof options !== 'undefined' && options.search_type) { leading_wildcard = options.search_type === "contains" ? '%' : ''; - // If not, we use DefaultPatronSearchMethod system preference instead + // If not, use DefaultPatronSearchMethod system preference instead } else { leading_wildcard = defaultPatronSearchMethod === 'contains' ? '%' : ''; } - // Search fields: If search_fields option exists, we use that + // Search fields: If search_fields option exists, use that if (typeof options !== 'undefined' && options.search_fields) { search_fields = expandPatronSearchFields(options.search_fields); // If not, we use DefaultPatronSearchFields system preference instead @@ -581,12 +591,12 @@ function buildPatronSearchQuery(term, options) { let pattern_subquery_or = []; search_fields.split('\|').forEach(function (field, i) { pattern_subquery_or.push( - { ["me." + field]: { 'like': leading_wildcard + pattern + '%' } } + { [table_prefix + "." + field]: { 'like': leading_wildcard + pattern + '%' } } ); if (field == 'dateofbirth') { try { let d = $date_to_rfc3339(pattern); - pattern_subquery_or.push({ ["me." + field]: d }); + pattern_subquery_or.push({ [table_prefix + "." + field]: d }); } catch { // Hide the warning if the date is not correct } @@ -600,7 +610,7 @@ function buildPatronSearchQuery(term, options) { let term_subquery_or = []; search_fields.split('\|').forEach(function (field, i) { term_subquery_or.push( - { ["me." + field]: { 'like': leading_wildcard + term + '%' } } + { [table_prefix + "." + field]: { 'like': leading_wildcard + term + '%' } } ); }); q.push({ "-or": term_subquery_or }); -- 2.30.2