1 package C4::Utils::DataTables::Members;
5 use C4::Utils::DataTables;
6 use C4::Members::Attributes;
8 use C4::Members::Attributes qw(SearchIdMatchingAttribute );
12 my $searchmember = $params->{searchmember};
13 my $firstletter = $params->{firstletter};
14 my $categorycode = $params->{categorycode};
15 my $branchcode = $params->{branchcode};
16 my $searchtype = $params->{searchtype} || 'contain';
17 my $searchfieldstype = $params->{searchfieldstype} || 'standard';
18 my $dt_params = $params->{dt_params};
20 unless ( $searchmember ) {
21 $searchmember = $dt_params->{sSearch} // '';
24 # If branches are independent and user is not superlibrarian
25 # The search has to be only on the user branch
26 my $userenv = C4::Context->userenv;
27 my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
28 my @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
30 my ($sth, $query, $iTotalQuery, $iTotalRecords, $iTotalDisplayRecords);
31 my $dbh = C4::Context->dbh;
32 # Get the iTotalRecords DataTable variable
33 $query = $iTotalQuery = "SELECT COUNT(borrowers.borrowernumber) FROM borrowers";
34 if ( @restricted_branchcodes ) {
35 $iTotalQuery .= " WHERE borrowers.branchcode IN (" . join( ',', ('?') x @restricted_branchcodes ) . ")";
37 ($iTotalRecords) = $dbh->selectrow_array( $iTotalQuery, undef, @restricted_branchcodes );
39 # Do that after iTotalQuery!
40 if ( defined $branchcode and $branchcode ) {
41 @restricted_branchcodes = @restricted_branchcodes
42 ? grep { /^$branchcode$/ } @restricted_branchcodes
44 : (undef) # Do not return any results
48 if ( $searchfieldstype eq 'dateofbirth' ) {
49 # Return an empty list if the date of birth is not correctly formatted
50 $searchmember = eval { output_pref( { str => $searchmember, dateformat => 'iso', dateonly => 1 } ); };
51 if ( $@ or not $searchmember ) {
53 iTotalRecords => $iTotalRecords,
54 iTotalDisplayRecords => 0,
61 borrowers.borrowernumber, borrowers.surname, borrowers.firstname,
62 borrowers.streetnumber, borrowers.streettype, borrowers.address,
63 borrowers.address2, borrowers.city, borrowers.state, borrowers.zipcode,
64 borrowers.country, cardnumber, borrowers.dateexpiry,
65 borrowers.borrowernotes, borrowers.branchcode, borrowers.email,
66 borrowers.userid, borrowers.dateofbirth, borrowers.categorycode,
67 categories.description AS category_description, categories.category_type,
68 branches.branchname, borrowers.phone";
69 my $from = "FROM borrowers
70 LEFT JOIN branches ON borrowers.branchcode = branches.branchcode
71 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode";
74 if(defined $firstletter and $firstletter ne '') {
75 push @where_strs, "borrowers.surname LIKE ?";
76 push @where_args, "$firstletter%";
78 if(defined $categorycode and $categorycode ne '') {
79 push @where_strs, "borrowers.categorycode = ?";
80 push @where_args, $categorycode;
82 if(@restricted_branchcodes ) {
83 push @where_strs, "borrowers.branchcode IN (" . join( ',', ('?') x @restricted_branchcodes ) . ")";
84 push @where_args, @restricted_branchcodes;
88 standard => C4::Context->preference('DefaultPatronSearchFields') || 'surname,firstname,othernames,cardnumber,userid',
90 email => 'email,emailpro,B_email',
91 borrowernumber => 'borrowernumber',
93 phone => 'phone,phonepro,B_phone,altcontactphone,mobile',
94 address => 'streettype,address,address2,city,state,zipcode,country',
95 dateofbirth => 'dateofbirth',
100 # * is replaced with % for sql
101 $searchmember =~ s/\*/%/g;
103 # split into search terms
105 # consider coma as space
106 $searchmember =~ s/,/ /g;
107 if ( $searchtype eq 'contain' ) {
108 @terms = split / /, $searchmember;
110 @terms = ($searchmember);
113 foreach my $term (@terms) {
116 my $term_dt = eval { local $SIG{__WARN__} = {}; output_pref( { str => $term, dateonly => 1, dateformat => 'sql' } ); };
121 $term .= '%' # end with anything
123 $term = "%$term" # begin with anythin unless start_with
124 if $searchtype eq 'contain' && $term !~ /^%/;
128 for my $searchfield ( split /,/, $searchfields->{$searchfieldstype} ) {
129 push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfield) . " LIKE ?";
130 push @where_args, $term;
133 if ( $searchfieldstype eq 'standard' and C4::Context->preference('ExtendedPatronAttributes') and $searchmember ) {
134 my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($searchmember);
136 for my $borrowernumber ( @$matching_borrowernumbers ) {
137 push @where_strs_or, "borrowers.borrowernumber = ?";
138 push @where_args, $borrowernumber;
142 push @where_strs, '('. join (' OR ', @where_strs_or) . ')'
147 $where = " WHERE " . join (" AND ", @where_strs) if @where_strs;
148 my $orderby = dt_build_orderby($dt_params);
151 # If iDisplayLength == -1, we want to display all patrons
152 if ( !$dt_params->{iDisplayLength} || $dt_params->{iDisplayLength} > -1 ) {
153 # In order to avoid sql injection
154 $dt_params->{iDisplayStart} =~ s/\D//g if defined($dt_params->{iDisplayStart});
155 $dt_params->{iDisplayLength} =~ s/\D//g if defined($dt_params->{iDisplayLength});
156 $dt_params->{iDisplayStart} //= 0;
157 $dt_params->{iDisplayLength} //= 20;
158 $limit = "LIMIT $dt_params->{iDisplayStart},$dt_params->{iDisplayLength}";
163 ($select ? $select : ""),
164 ($from ? $from : ""),
165 ($where ? $where : ""),
166 ($orderby ? $orderby : ""),
167 ($limit ? $limit : "")
169 $sth = $dbh->prepare($query);
170 $sth->execute(@where_args);
171 my $patrons = $sth->fetchall_arrayref({});
173 # Get the iTotalDisplayRecords DataTable variable
174 $query = "SELECT COUNT(borrowers.borrowernumber) " . $from . ($where ? $where : "");
175 $sth = $dbh->prepare($query);
176 $sth->execute(@where_args);
177 ($iTotalDisplayRecords) = $sth->fetchrow_array;
179 # Get some information on patrons
180 foreach my $patron (@$patrons) {
181 my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
182 $patron->{overdues} = $patron_object->get_overdues->count;
183 $patron->{issues} = $patron_object->checkouts->count;
184 my $balance = $patron_object->account->balance;
185 # FIXME Should be formatted from the template
186 $patron->{fines} = sprintf("%.2f", $balance);
188 if( $patron->{dateexpiry} ) {
189 # FIXME We should not format the date here, do it in template-side instead
190 $patron->{dateexpiry} = output_pref( { dt => scalar dt_from_string( $patron->{dateexpiry}, 'iso'), dateonly => 1} );
192 $patron->{dateexpiry} = '';
197 iTotalRecords => $iTotalRecords,
198 iTotalDisplayRecords => $iTotalDisplayRecords,
208 C4::Utils::DataTables::Members - module for using DataTables with patrons
212 This module provides (one for the moment) routines used by the patrons search
218 my $dt_infos = C4::Utils::DataTables::Members->search($params);
220 $params is a hashref with some keys:
226 String to search in the borrowers sql table
230 Introduced to contain 1 letter but can contain more.
231 The search will done on the borrowers.surname field
235 Search patrons with this categorycode
239 Search patrons with this branchcode
243 Can be 'start_with' or 'contain' (default value). Used for the searchmember parameter.
245 =item searchfieldstype
247 Can be 'standard' (default value), 'email', 'borrowernumber', 'phone', 'address' or 'dateofbirth', 'sort1', 'sort2'
251 Is the reference of C4::Utils::DataTables::dt_get_params($input);
259 This file is part of Koha.
261 Copyright 2013 BibLibre
263 Koha is free software; you can redistribute it and/or modify it
264 under the terms of the GNU General Public License as published by
265 the Free Software Foundation; either version 3 of the License, or
266 (at your option) any later version.
268 Koha is distributed in the hope that it will be useful, but
269 WITHOUT ANY WARRANTY; without even the implied warranty of
270 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
271 GNU General Public License for more details.
273 You should have received a copy of the GNU General Public License
274 along with Koha; if not, see <http://www.gnu.org/licenses>.