From 98ec168e1c09c28117b77f733a25267ca98e140c Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 15 Sep 2022 04:36:32 +0000 Subject: [PATCH] Bug 23817: [Alternative patch] Normalize phone number only when searching This patch rewrites the phone number DBIC query when sent from DataTables like in the Patron module. Test plan: 0. Apply patch and koha-plack --restart kohadev 1. Go to http://localhost:8081/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber=51 2. Input '1-(234)-567-8901' into the 'Primary phone' field 3. Note on the next screen the phone number is shown as '1-(234)-567-8901' 4. Go to http://localhost:8081/cgi-bin/koha/members/members-home.pl 5. Choose "Search field" of "Primary phone" 6. Search for '12345678901' 7. Note you are taken to http://localhost:8081/cgi-bin/koha/members/moremember.pl?borrowernumber=51 8. Try out different permutations like '234 567 8901' or '5678901' 9. Note that every permutation works to find the borrower Signed-off-by: Marius Mandrescu --- Koha/REST/Plugin/Objects.pm | 7 +++ Koha/REST/V1/Patrons.pm | 48 +++++++++++++++++++ .../prog/en/includes/patron-search.inc | 3 ++ 3 files changed, 58 insertions(+) diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm index eba63a4ded..ab3985bc83 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -193,6 +193,13 @@ shouldn't be called twice in it. $result_set = $result_set->search_limited, if $result_set->can('search_limited'); + # Pre-process DBIC queries if controller supports it + if ( $reserved_params->{'x-koha-request-id'} ){ + if ($c->can('preprocess_dbic_for_datatables')){ + $c->preprocess_dbic_for_datatables($filtered_params,$attributes); + } + } + # Perform search my $objects = $result_set->search( $filtered_params, $attributes ); my $total = $result_set->search->count; diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 576e4f8f2c..ab60c83da2 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -458,4 +458,52 @@ sub guarantors_can_see_checkouts { }; } +sub _parse_dbic_query_for_dt { + my ($q_params,$collector) = @_; + if(ref($q_params) && ref($q_params) eq 'HASH') { + foreach my $key (keys %{$q_params}) { + #FIXME: The table preference is always required so we might want to handle other scenarios... + if ($key eq 'me.phone'){ + my $query = $q_params->{$key}; + if ($query->{'like'}){ + my $like = $query->{'like'}; + #NOTE: Remove the hashref, and add a data structure to add to the WHERE clause via the attributes... + delete $q_params->{$key}; + my $collection = \["regexp_replace(me.phone,?,?) LIKE ?","[^0-9]","",$like]; + push(@$collector,$collection); + #NOTE: Rewrite the query to use a subquery where we can perform phone number normalization (but it's slow) + #$q_params->{$key} = { + # '-in' => \["SELECT phone FROM borrowers WHERE regexp_replace(phone,?,?) LIKE ?","[^0-9]","",$query->{'like'}], + #}; + #NOTE: Technically, you could replace the whole hashref, but that would nuke any OR query in the same hashref + #return \["regexp_replace(me.phone,?,?) LIKE ?","[^0-9]","",$query->{'like'}]; + } + } + else { + $q_params->{$key} = _parse_dbic_query_for_dt($q_params->{$key},$collector); + } + } + return $q_params; + } elsif (ref($q_params) && ref($q_params) eq 'ARRAY') { + foreach my $param (@$q_params){ + $param = _parse_dbic_query_for_dt($param,$collector); + } + return $q_params; + } else { + return $q_params; + } +} + +sub preprocess_dbic_for_datatables { + my ($c,$q_params,$attributes) = @_; + my @collector = (); + my $rewritten_q_params = _parse_dbic_query_for_dt($q_params,\@collector); + unless ($attributes->{where}){ + foreach my $collection (@collector){ + push(@{$attributes->{where}},$collection); + } + } + return 1; +} + 1; 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 de4ba90c0d..b10bc385ea 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc @@ -322,6 +322,9 @@ if ( !search_fields ) { search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,middle_name,surname,othernames,cardnumber,userid' | html %]"; } + if (search_fields == 'phone'){ + patterns = [pattern.replace(/[^\+0-9]/g,'')]; + } let subquery_and = []; patterns.forEach(function(pattern,i){ -- 2.34.1