View | Details | Raw Unified | Return to bug 23817
Collapse All | Expand All

(-)a/Koha/Patrons/DataTables.pm (+85 lines)
Line 0 Link Here
1
package Koha::Patrons::DataTables;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
=head1 NAME
21
22
Koha::Patrons::DataTables
23
24
=head1 API
25
26
=head2 Methods
27
28
=cut
29
30
sub _parse_dbic_query_for_dt {
31
    my ( $class, $q_params, $collector ) = @_;
32
    my %allowed_keys = (
33
        'me.phone'           => 1,
34
        'me.phonepro'        => 1,
35
        'me.B_phone'         => 1,
36
        'me.altcontactphone' => 1,
37
        'me.mobile'          => 1,
38
    );
39
    if ( ref($q_params) && ref($q_params) eq 'HASH' ) {
40
        foreach my $key ( keys %{$q_params} ) {
41
            if ( $allowed_keys{$key} ) {
42
                my $query = $q_params->{$key};
43
                if ( $query->{'like'} ) {
44
                    my $like = $query->{'like'};
45
46
                    #NOTE: Remove the hashref, and add a data structure to add to the WHERE clause via the attributes...
47
                    delete $q_params->{$key};
48
                    my $collection = \[ "regexp_replace($key,?,?) LIKE ?", "[^0-9]", "", $like ];
49
                    push( @$collector, $collection );
50
                }
51
            } else {
52
                $q_params->{$key} = $class->_parse_dbic_query_for_dt( $q_params->{$key}, $collector );
53
            }
54
        }
55
        return $q_params;
56
    } elsif ( ref($q_params) && ref($q_params) eq 'ARRAY' ) {
57
        foreach my $param (@$q_params) {
58
            $param = $class->_parse_dbic_query_for_dt( $param, $collector );
59
        }
60
        return $q_params;
61
    } else {
62
        return $q_params;
63
    }
64
}
65
66
=head3 preprocess_dbic_for_datatables
67
68
Method for preprocessing DBIC queries sent from DataTables, so a WHERE
69
clause can be injected into the query if one doesn't already exist.
70
71
=cut
72
73
sub preprocess_dbic_for_datatables {
74
    my ( $class, $q_params, $attributes ) = @_;
75
    my @collector          = ();
76
    my $rewritten_q_params = $class->_parse_dbic_query_for_dt( $q_params, \@collector );
77
    unless ( $attributes->{where} ) {
78
        foreach my $collection (@collector) {
79
            push( @{ $attributes->{where} }, $collection );
80
        }
81
    }
82
    return 1;
83
}
84
85
1;
(-)a/Koha/REST/Plugin/Objects.pm (+7 lines)
Lines 277-282 controller, and thus shouldn't be called twice in it. Link Here
277
277
278
            $c->dbic_validate_operators( { filtered_params => $filtered_params } );
278
            $c->dbic_validate_operators( { filtered_params => $filtered_params } );
279
279
280
            # Pre-process DBIC queries if controller supports it
281
            if ( $c->stash('koha.request_id') ) {
282
                if ( $c->can('preprocess_dbic_for_datatables') ) {
283
                    $c->preprocess_dbic_for_datatables( $filtered_params, $attributes );
284
                }
285
            }
286
280
            # Generate the resultset
287
            # Generate the resultset
281
            my $objects_rs = $result_set->search( $filtered_params, $attributes );
288
            my $objects_rs = $result_set->search( $filtered_params, $attributes );
282
289
(-)a/Koha/REST/V1/Patrons.pm (+14 lines)
Lines 23-28 use Koha::Database; Link Here
23
use Koha::Exceptions;
23
use Koha::Exceptions;
24
use Koha::Patrons;
24
use Koha::Patrons;
25
use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
25
use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
26
use Koha::Patrons::DataTables;
26
27
27
use List::MoreUtils qw(any);
28
use List::MoreUtils qw(any);
28
use Scalar::Util    qw( blessed );
29
use Scalar::Util    qw( blessed );
Lines 496-499 sub guarantors_can_see_checkouts { Link Here
496
    };
497
    };
497
}
498
}
498
499
500
=head3 preprocess_dbic_for_datatables
501
502
Method for preprocessing DBIC queries sent from DataTables, so a WHERE
503
clause can be injected into the query if one doesn't already exist.
504
505
=cut
506
507
sub preprocess_dbic_for_datatables {
508
    my ( $c, $q_params, $attributes ) = @_;
509
    Koha::Patrons::DataTables->preprocess_dbic_for_datatables( $q_params, $attributes );
510
    return 1;
511
}
512
499
1;
513
1;
(-)a/koha-tmpl/intranet-tmpl/prog/js/staff-global.js (+3 lines)
Lines 916-921 function buildPatronSearchQuery(term, options) { Link Here
916
    let searched_attribute_fields = [];
916
    let searched_attribute_fields = [];
917
    // Search fields: If search_fields option exists, we use that
917
    // Search fields: If search_fields option exists, we use that
918
    if (typeof options !== "undefined" && options.search_fields) {
918
    if (typeof options !== "undefined" && options.search_fields) {
919
        if (options.search_fields == "all_phones") {
920
            patterns = [term.replace(/[^\+0-9]/g, "")];
921
        }
919
        expand_fields = expandPatronSearchFields(options.search_fields);
922
        expand_fields = expandPatronSearchFields(options.search_fields);
920
        expand_fields.split("|").forEach(function (field, i) {
923
        expand_fields.split("|").forEach(function (field, i) {
921
            if (field.startsWith("_ATTR_")) {
924
            if (field.startsWith("_ATTR_")) {
(-)a/t/Koha/Patrons/DataTables.t (-1 / +180 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 3;
22
23
use_ok("Koha::Patrons::DataTables");
24
25
subtest 'Preprocessor tests' => sub {
26
    plan tests => 2;
27
    my $q_params = [
28
        {
29
            '-and' => [
30
                [
31
                    { 'me.phone'           => { 'like' => '%2345678901%' } },
32
                    { 'me.phonepro'        => { 'like' => '%2345678901%' } },
33
                    { 'me.B_phone'         => { 'like' => '%2345678901%' } },
34
                    { 'me.altcontactphone' => { 'like' => '%2345678901%' } },
35
                    { 'me.mobile'          => { 'like' => '%2345678901%' } }
36
                ]
37
            ]
38
        },
39
        {
40
            '-or' => [
41
                { 'me.phone'           => { 'like' => '%234 567 8901%' } },
42
                { 'me.phonepro'        => { 'like' => '%234 567 8901%' } },
43
                { 'me.B_phone'         => { 'like' => '%234 567 8901%' } },
44
                { 'me.altcontactphone' => { 'like' => '%234 567 8901%' } },
45
                { 'me.mobile'          => { 'like' => '%234 567 8901%' } }
46
            ]
47
        }
48
    ];
49
    my $attributes = {
50
        'prefetch' => [
51
            'extended_attributes',
52
            'library'
53
        ],
54
        'order_by' => [
55
            { '-asc' => 'me.surname' },
56
            { '-asc' => 'me.preferred_name' },
57
            { '-asc' => 'me.firstname' },
58
            { '-asc' => 'me.middle_name' },
59
            { '-asc' => 'me.othernames' },
60
            { '-asc' => 'me.streetnumber' },
61
            { '-asc' => 'me.address' },
62
            { '-asc' => 'me.address2' },
63
            { '-asc' => 'me.city' },
64
            { '-asc' => 'me.state' },
65
            { '-asc' => 'me.zipcode' },
66
            { '-asc' => 'me.country' }
67
        ],
68
        'page' => 1,
69
        'rows' => 20
70
    };
71
    my $expected_params = [
72
        {
73
            '-and' => [
74
                [
75
                    {},
76
                    {},
77
                    {},
78
                    {},
79
                    {}
80
                ]
81
            ]
82
        },
83
        {
84
            '-or' => [
85
                {},
86
                {},
87
                {},
88
                {},
89
                {}
90
            ]
91
        }
92
    ];
93
    my $expected_attributes = {
94
        'order_by' => [
95
            { '-asc' => 'me.surname' },
96
            { '-asc' => 'me.preferred_name' },
97
            { '-asc' => 'me.firstname' },
98
            { '-asc' => 'me.middle_name' },
99
            { '-asc' => 'me.othernames' },
100
            { '-asc' => 'me.streetnumber' },
101
            { '-asc' => 'me.address' },
102
            { '-asc' => 'me.address2' },
103
            { '-asc' => 'me.city' },
104
            { '-asc' => 'me.state' },
105
            { '-asc' => 'me.zipcode' },
106
            { '-asc' => 'me.country' }
107
        ],
108
        'page'     => 1,
109
        'rows'     => 20,
110
        'prefetch' => [
111
            'extended_attributes',
112
            'library'
113
        ],
114
        'where' => [
115
            \[
116
                'regexp_replace(me.phone,?,?) LIKE ?',
117
                '[^0-9]',
118
                '',
119
                '%2345678901%'
120
            ],
121
            \[
122
                'regexp_replace(me.phonepro,?,?) LIKE ?',
123
                '[^0-9]',
124
                '',
125
                '%2345678901%'
126
            ],
127
            \[
128
                'regexp_replace(me.B_phone,?,?) LIKE ?',
129
                '[^0-9]',
130
                '',
131
                '%2345678901%'
132
            ],
133
            \[
134
                'regexp_replace(me.altcontactphone,?,?) LIKE ?',
135
                '[^0-9]',
136
                '',
137
                '%2345678901%'
138
            ],
139
            \[
140
                'regexp_replace(me.mobile,?,?) LIKE ?',
141
                '[^0-9]',
142
                '',
143
                '%2345678901%'
144
            ],
145
            \[
146
                'regexp_replace(me.phone,?,?) LIKE ?',
147
                '[^0-9]',
148
                '',
149
                '%234 567 8901%'
150
            ],
151
            \[
152
                'regexp_replace(me.phonepro,?,?) LIKE ?',
153
                '[^0-9]',
154
                '',
155
                '%234 567 8901%'
156
            ],
157
            \[
158
                'regexp_replace(me.B_phone,?,?) LIKE ?',
159
                '[^0-9]',
160
                '',
161
                '%234 567 8901%'
162
            ],
163
            \[
164
                'regexp_replace(me.altcontactphone,?,?) LIKE ?',
165
                '[^0-9]',
166
                '',
167
                '%234 567 8901%'
168
            ],
169
            \[
170
                'regexp_replace(me.mobile,?,?) LIKE ?',
171
                '[^0-9]',
172
                '',
173
                '%234 567 8901%'
174
            ]
175
        ]
176
    };
177
    Koha::Patrons::DataTables->preprocess_dbic_for_datatables( $q_params, $attributes );
178
    is_deeply( $q_params,   $expected_params,     "Params preprocessed as expected" );
179
    is_deeply( $attributes, $expected_attributes, "Attributes preprocessed as expected" );
180
};

Return to bug 23817