From dee6f7955e4caeba111b9d70ae77d57bc7625356 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 31 Jan 2023 20:24:21 +0000 Subject: [PATCH] Bug 32772: [Alternate] Add support for '^' as "Starts with" in patron searching This patch parses each term we send to patron search - if it finds it starts with '^' then the term is searched as a starts with - other terms will still be 'contains' To test: 1 - Make a bunch of williams and fitzwiliams UPDATE borrowers SET surname = 'Fitzwilliams' IF borrowernumber < 25; UPDATE borrowers SET surname = 'Williams' IF borrowernumber > 25; 2 - Enable PatronAutrocomplete syspref 3 - Search for 'Will' in patron search 4 - You get Fitzwilliams (you see them in the automplete too) 5 - Apply patch 6 - Search for '^Will' 7 - Confirm that autocomplete and full search results only have williams, not fitzwilliams 8 - Search for '^Will an' 9 - Confirm you get 'Frances Williams' and 'Annie Williams' - as the 'an' is searched as contains --- koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc | 7 ++++++- koha-tmpl/intranet-tmpl/prog/js/patron-autocomplete.js | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) 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 b4589b1201..74a816ee51 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc @@ -326,8 +326,13 @@ let subquery_and = []; patterns.forEach(function(pattern,i){ let sub_or = []; + let cur_search_type = search_type; + if( pattern.slice(0,1) == '^' ){ + cur_search_type = "starts_with"; + pattern = pattern.substr(1); + } search_fields.split(',').forEach(function(attr,ii){ - sub_or.push({["me."+attr]:{"like":(search_type == "contain" ? "%" : "" ) + pattern + "%"}}); + sub_or.push({["me."+attr]:{"like":(cur_search_type == "contain" ? "%" : "" ) + pattern + "%"}}); if ( attr == 'dateofbirth' ) { try { let d = $date_to_rfc3339(pattern); diff --git a/koha-tmpl/intranet-tmpl/prog/js/patron-autocomplete.js b/koha-tmpl/intranet-tmpl/prog/js/patron-autocomplete.js index f4620896ce..ea44d3d5b6 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/patron-autocomplete.js +++ b/koha-tmpl/intranet-tmpl/prog/js/patron-autocomplete.js @@ -20,9 +20,14 @@ function patron_autocomplete(node, options) { .filter(function(s){ return s.length }) .forEach(function(pattern,i){ let subquery_or = []; + let likeness = "%"; + if( pattern.slice(0,1) == '^' ){ + pattern = pattern.substr(1); + likeness = ""; + } defaultPatronSearchFields.split(',').forEach(function(field,i){ subquery_or.push( - {["me."+field]: {'like': pattern + '%'}} + {["me."+field]: {'like': likeness + pattern + '%'}} ); }); subquery_and.push(subquery_or); -- 2.30.2