From 4d3e3272ea29e28442c4f73b2b6c49e3cd554e78 Mon Sep 17 00:00:00 2001
From: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Date: Tue, 27 Jun 2023 09:07:49 +0000
Subject: [PATCH] Bug 34058: Add table_prefix argument to
 buildPatronSearchQuery

---
 .../intranet-tmpl/prog/js/staff-global.js     | 31 +++++++++++++------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
index 02bc39b8f8..9a679d355b 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/staff-global.js
@@ -514,13 +514,15 @@ function patron_autocomplete(node, options) {
  * 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 });
@@ -530,18 +532,26 @@ function buildPatronSearchQuery(term, options) {
         return;
     }
 
-    // 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 = options.search_fields;
-    // If not, we use DefaultPatronSearchFields system preference instead
+    // If not, use DefaultPatronSearchFields system preference instead
     } else {
         search_fields = defaultPatronSearchFields;
     }
@@ -552,12 +562,12 @@ function buildPatronSearchQuery(term, options) {
             let pattern_subquery_or = [];
             defaultPatronSearchFields.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
                     }
@@ -571,12 +581,13 @@ function buildPatronSearchQuery(term, options) {
     let term_subquery_or = [];
     defaultPatronSearchFields.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 });
 
     // Add each pattern for each extended patron attributes
+    // extendedPatronAttributes: ExtendedPatronAttributes system preference (see js_includes.inc)
     if (typeof options !== 'undefined' && options.extended_attribute_types && extendedPatronAttributes) {
         extended_attribute_subquery_and = [];
         patterns.forEach(function (pattern, i) {
-- 
2.30.2