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

(-)a/C4/Utils/DataTables/Members.pm (-9 / +21 lines)
Lines 13-19 sub search { Link Here
13
    my $firstletter = $params->{firstletter};
13
    my $firstletter = $params->{firstletter};
14
    my $categorycode = $params->{categorycode};
14
    my $categorycode = $params->{categorycode};
15
    my $branchcode = $params->{branchcode};
15
    my $branchcode = $params->{branchcode};
16
    my $searchtype = $params->{searchtype};
16
    my $searchtype = $params->{searchtype} || 'start_with';
17
    my $searchfieldstype = $params->{searchfieldstype} || 'standard';
17
    my $searchfieldstype = $params->{searchfieldstype} || 'standard';
18
    my $dt_params = $params->{dt_params};
18
    my $dt_params = $params->{dt_params};
19
19
Lines 59-66 sub search { Link Here
59
        push @where_args, $branchcode;
59
        push @where_args, $branchcode;
60
    }
60
    }
61
61
62
    # split on coma
63
    $searchmember =~ s/,/ /g if $searchmember;
64
    my $searchfields = {
62
    my $searchfields = {
65
        standard => 'surname,firstname,othernames,cardnumber,userid',
63
        standard => 'surname,firstname,othernames,cardnumber,userid',
66
        email => 'email,emailpro,B_email',
64
        email => 'email,emailpro,B_email',
Lines 72-85 sub search { Link Here
72
        sort1 => 'sort1',
70
        sort1 => 'sort1',
73
        sort2 => 'sort2',
71
        sort2 => 'sort2',
74
    };
72
    };
75
    foreach my $term ( split / /, $searchmember) {
73
74
    # * is replaced with % for sql
75
    $searchmember =~ s/\*/%/g;
76
77
    # split into search terms
78
    my @terms;
79
    # consider coma as space
80
    $searchmember =~ s/,/ /g;
81
    if ( $searchtype eq 'contain' ) {
82
       @terms = split / /, $searchmember;
83
    } else {
84
       @terms = ($searchmember);
85
    }
86
87
    foreach my $term (@terms) {
76
        next unless $term;
88
        next unless $term;
77
        $searchmember =~ s/\*/%/g; # * is replaced with % for sql
89
78
        $term .= '%' # end with anything
90
        $term .= '%' # end with anything
79
            if $term !~ /%$/;
91
            if $term !~ /%$/;
80
        $term = "%$term" # begin with anythin unless start_with
92
        $term = "%$term" # begin with anythin unless start_with
81
            if (defined $searchtype) && $searchtype eq "contain"
93
            if $searchtype eq 'contain' && $term !~ /^%/;
82
                && $term !~ /^%/;
94
83
        my @where_strs_or;
95
        my @where_strs_or;
84
        for my $searchfield ( split /,/, $searchfields->{$searchfieldstype} ) {
96
        for my $searchfield ( split /,/, $searchfields->{$searchfieldstype} ) {
85
            push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfield) . " LIKE ?";
97
            push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfield) . " LIKE ?";
Lines 197-207 $params is a hashref with some keys: Link Here
197
209
198
=item searchtype
210
=item searchtype
199
211
200
  Can be 'contain' or 'start_with'. Used for the searchmember parameter.
212
  Can be 'contain' or 'start_with' (default value). Used for the searchmember parameter.
201
213
202
=item searchfieldstype
214
=item searchfieldstype
203
215
204
  Can be 'standard', 'email', 'borrowernumber', 'phone', 'address' or 'dateofbirth', 'sort1', 'sort2'
216
  Can be 'standard' (default value), 'email', 'borrowernumber', 'phone', 'address' or 'dateofbirth', 'sort1', 'sort2'
205
217
206
=item dt_params
218
=item dt_params
207
219
(-)a/t/DataTables/Members.t (-1 / +1 lines)
Lines 6-12 use_ok( "C4::Utils::DataTables::Members" ); Link Here
6
my $patrons = C4::Utils::DataTables::Members::search({
6
my $patrons = C4::Utils::DataTables::Members::search({
7
    searchmember => "Doe",
7
    searchmember => "Doe",
8
    searchfieldstype => 'standard',
8
    searchfieldstype => 'standard',
9
    searchtype => 'contains'
9
    searchtype => 'contain'
10
});
10
});
11
11
12
isnt( $patrons->{iTotalDisplayRecords}, undef, "The iTotalDisplayRecords key is defined");
12
isnt( $patrons->{iTotalDisplayRecords}, undef, "The iTotalDisplayRecords key is defined");
(-)a/t/db_dependent/Utils/Datatables_Members.t (-9 / +8 lines)
Lines 104-110 my %dt_params = ( Link Here
104
my $search_results = C4::Utils::DataTables::Members::search({
104
my $search_results = C4::Utils::DataTables::Members::search({
105
    searchmember     => "John Doe",
105
    searchmember     => "John Doe",
106
    searchfieldstype => 'standard',
106
    searchfieldstype => 'standard',
107
    searchtype       => 'contains',
107
    searchtype       => 'contain',
108
    branchcode       => $branchcode,
108
    branchcode       => $branchcode,
109
    dt_params        => \%dt_params
109
    dt_params        => \%dt_params
110
});
110
});
Lines 120-126 ok( $search_results->{ patrons }[0]->{ cardnumber } eq $john_doe{ cardnumber } Link Here
120
$search_results = C4::Utils::DataTables::Members::search({
120
$search_results = C4::Utils::DataTables::Members::search({
121
    searchmember     => "Jane Doe",
121
    searchmember     => "Jane Doe",
122
    searchfieldstype => 'standard',
122
    searchfieldstype => 'standard',
123
    searchtype       => 'contains',
123
    searchtype       => 'contain',
124
    branchcode       => $branchcode,
124
    branchcode       => $branchcode,
125
    dt_params        => \%dt_params
125
    dt_params        => \%dt_params
126
});
126
});
Lines 136-142 is( $search_results->{ patrons }[0]->{ cardnumber }, Link Here
136
$search_results = C4::Utils::DataTables::Members::search({
136
$search_results = C4::Utils::DataTables::Members::search({
137
    searchmember     => "John",
137
    searchmember     => "John",
138
    searchfieldstype => 'standard',
138
    searchfieldstype => 'standard',
139
    searchtype       => 'contains',
139
    searchtype       => 'contain',
140
    branchcode       => $branchcode,
140
    branchcode       => $branchcode,
141
    dt_params        => \%dt_params
141
    dt_params        => \%dt_params
142
});
142
});
Lines 156-162 is( $search_results->{ patrons }[1]->{ cardnumber }, Link Here
156
$search_results = C4::Utils::DataTables::Members::search({
156
$search_results = C4::Utils::DataTables::Members::search({
157
    searchmember     => "Doe",
157
    searchmember     => "Doe",
158
    searchfieldstype => 'standard',
158
    searchfieldstype => 'standard',
159
    searchtype       => 'contains',
159
    searchtype       => 'contain',
160
    branchcode       => $branchcode,
160
    branchcode       => $branchcode,
161
    dt_params        => \%dt_params
161
    dt_params        => \%dt_params
162
});
162
});
Lines 176-182 is( $search_results->{ patrons }[1]->{ cardnumber }, Link Here
176
$search_results = C4::Utils::DataTables::Members::search({
176
$search_results = C4::Utils::DataTables::Members::search({
177
    searchmember     => "john.doe",
177
    searchmember     => "john.doe",
178
    searchfieldstype => 'standard',
178
    searchfieldstype => 'standard',
179
    searchtype       => 'contains',
179
    searchtype       => 'contain',
180
    branchcode       => $branchcode,
180
    branchcode       => $branchcode,
181
    dt_params        => \%dt_params
181
    dt_params        => \%dt_params
182
});
182
});
Lines 187-193 is( $search_results->{ iTotalDisplayRecords }, 1, Link Here
187
$search_results = C4::Utils::DataTables::Members::search({
187
$search_results = C4::Utils::DataTables::Members::search({
188
    searchmember     => "john.doe",
188
    searchmember     => "john.doe",
189
    searchfieldstype => 'userid',
189
    searchfieldstype => 'userid',
190
    searchtype       => 'contains',
190
    searchtype       => 'contain',
191
    branchcode       => $branchcode,
191
    branchcode       => $branchcode,
192
    dt_params        => \%dt_params
192
    dt_params        => \%dt_params
193
});
193
});
Lines 211-217 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1); Link Here
211
$search_results = C4::Utils::DataTables::Members::search({
211
$search_results = C4::Utils::DataTables::Members::search({
212
    searchmember     => "common user",
212
    searchmember     => "common user",
213
    searchfieldstype => 'standard',
213
    searchfieldstype => 'standard',
214
    searchtype       => 'contains',
214
    searchtype       => 'contain',
215
    branchcode       => $branchcode,
215
    branchcode       => $branchcode,
216
    dt_params        => \%dt_params
216
    dt_params        => \%dt_params
217
});
217
});
Lines 222-228 t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0); Link Here
222
$search_results = C4::Utils::DataTables::Members::search({
222
$search_results = C4::Utils::DataTables::Members::search({
223
    searchmember     => "common user",
223
    searchmember     => "common user",
224
    searchfieldstype => 'standard',
224
    searchfieldstype => 'standard',
225
    searchtype       => 'contains',
225
    searchtype       => 'contain',
226
    branchcode       => $branchcode,
226
    branchcode       => $branchcode,
227
    dt_params        => \%dt_params
227
    dt_params        => \%dt_params
228
});
228
});
229
- 

Return to bug 15252