Bugzilla – Attachment 151961 Details for
Bug 23817
Normalize phone number when searching patrons
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 23817: Normalize phone number when searching
Bug-23817-Normalize-phone-number-when-searching.patch (text/plain), 5.18 KB, created by
David Cook
on 2023-06-02 03:03:46 UTC
(
hide
)
Description:
Bug 23817: Normalize phone number when searching
Filename:
MIME Type:
Creator:
David Cook
Created:
2023-06-02 03:03:46 UTC
Size:
5.18 KB
patch
obsolete
>From 855289cb38efafcb0ed6073b30480c9c2683ea40 Mon Sep 17 00:00:00 2001 >From: David Cook <dcook@prosentient.com.au> >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
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 23817
:
140610
|
140648
|
145219
|
145221
|
151961
|
151962
|
152450
|
152451