@@ -, +, @@ when searching --- Koha/REST/Plugin/Objects.pm | 7 +++ Koha/REST/V1/Patrons.pm | 48 +++++++++++++++++++ .../prog/en/includes/patron-search.inc | 3 ++ 3 files changed, 58 insertions(+) --- a/Koha/REST/Plugin/Objects.pm +++ a/Koha/REST/Plugin/Objects.pm @@ -190,6 +190,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; --- a/Koha/REST/V1/Patrons.pm +++ a/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; --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc +++ a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc @@ -318,6 +318,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){ --