From 855289cb38efafcb0ed6073b30480c9c2683ea40 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 15 Sep 2022 04:36:32 +0000 Subject: [PATCH] Bug 23817: Normalize phone number 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 "All phones" 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 --- 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 94baf03753..a1313957db 100644 --- a/Koha/REST/Plugin/Objects.pm +++ b/Koha/REST/Plugin/Objects.pm @@ -256,6 +256,13 @@ controller, and thus shouldn't be called twice in it. $c->stash('koha.pagination.base_total' => $result_set->count); $c->stash('koha.pagination.query_params' => $args); + # 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); + } + } + # Generate the resultset my $objects_rs = $result_set->search( $filtered_params, $attributes ); # Stash the page total if requires, total otherwise diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 1852d7ccd2..28e186ad7d 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -470,4 +470,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 6ae5ebb9e2..77558d1870 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc @@ -308,6 +308,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.30.2