1 package C4::Utils::DataTables::Members;
5 use C4::Utils::DataTables;
7 use C4::Members::Attributes qw(SearchIdMatchingAttribute );
11 my $searchmember = $params->{searchmember};
12 my $firstletter = $params->{firstletter};
13 my $categorycode = $params->{categorycode};
14 my $branchcode = $params->{branchcode};
15 my $searchtype = $params->{searchtype} || 'contain';
16 my $searchfieldstype = $params->{searchfieldstype} || 'standard';
17 my $dt_params = $params->{dt_params};
19 unless ( $searchmember ) {
20 $searchmember = $dt_params->{sSearch} // '';
23 my ($sth, $query, $iTotalRecords, $iTotalDisplayRecords);
24 my $dbh = C4::Context->dbh;
25 # Get the iTotalRecords DataTable variable
26 $query = "SELECT COUNT(borrowers.borrowernumber) FROM borrowers";
27 $sth = $dbh->prepare($query);
29 ($iTotalRecords) = $sth->fetchrow_array;
31 if ( $searchfieldstype eq 'dateofbirth' ) {
32 # Return an empty list if the date of birth is not correctly formatted
33 $searchmember = eval { output_pref( { str => $searchmember, dateformat => 'iso', dateonly => 1 } ); };
34 if ( $@ or not $searchmember ) {
37 iTotalDisplayRecords => 0,
43 # If branches are independent and user is not superlibrarian
44 # The search has to be only on the user branch
45 if ( C4::Context::only_my_library ) {
46 my $userenv = C4::Context->userenv;
47 $branchcode = $userenv->{'branch'};
52 borrowers.borrowernumber, borrowers.surname, borrowers.firstname,
53 borrowers.streetnumber, borrowers.streettype, borrowers.address,
54 borrowers.address2, borrowers.city, borrowers.state, borrowers.zipcode,
55 borrowers.country, cardnumber, borrowers.dateexpiry,
56 borrowers.borrowernotes, borrowers.branchcode, borrowers.email,
57 borrowers.userid, borrowers.dateofbirth, borrowers.categorycode,
58 categories.description AS category_description, categories.category_type,
60 my $from = "FROM borrowers
61 LEFT JOIN branches ON borrowers.branchcode = branches.branchcode
62 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode";
65 if(defined $firstletter and $firstletter ne '') {
66 push @where_strs, "borrowers.surname LIKE ?";
67 push @where_args, "$firstletter%";
69 if(defined $categorycode and $categorycode ne '') {
70 push @where_strs, "borrowers.categorycode = ?";
71 push @where_args, $categorycode;
73 if(defined $branchcode and $branchcode ne '') {
74 push @where_strs, "borrowers.branchcode = ?";
75 push @where_args, $branchcode;
79 standard => C4::Context->preference('DefaultPatronSearchFields') || 'surname,firstname,othernames,cardnumber,userid',
81 email => 'email,emailpro,B_email',
82 borrowernumber => 'borrowernumber',
84 phone => 'phone,phonepro,B_phone,altcontactphone,mobile',
85 address => 'streettype,address,address2,city,state,zipcode,country',
86 dateofbirth => 'dateofbirth',
91 # * is replaced with % for sql
92 $searchmember =~ s/\*/%/g;
94 # split into search terms
96 # consider coma as space
97 $searchmember =~ s/,/ /g;
98 if ( $searchtype eq 'contain' ) {
99 @terms = split / /, $searchmember;
101 @terms = ($searchmember);
104 foreach my $term (@terms) {
107 my $term_dt = eval { local $SIG{__WARN__} = {}; output_pref( { str => $term, dateonly => 1, dateformat => 'sql' } ); };
112 $term .= '%' # end with anything
114 $term = "%$term" # begin with anythin unless start_with
115 if $searchtype eq 'contain' && $term !~ /^%/;
119 for my $searchfield ( split /,/, $searchfields->{$searchfieldstype} ) {
120 push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfield) . " LIKE ?";
121 push @where_args, $term;
124 if ( $searchfieldstype eq 'standard' and C4::Context->preference('ExtendedPatronAttributes') and $searchmember ) {
125 my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($searchmember);
127 for my $borrowernumber ( @$matching_borrowernumbers ) {
128 push @where_strs_or, "borrowers.borrowernumber = ?";
129 push @where_args, $borrowernumber;
133 push @where_strs, '('. join (' OR ', @where_strs_or) . ')'
138 $where = " WHERE " . join (" AND ", @where_strs) if @where_strs;
139 my $orderby = dt_build_orderby($dt_params);
142 # If iDisplayLength == -1, we want to display all patrons
143 if ( !$dt_params->{iDisplayLength} || $dt_params->{iDisplayLength} > -1 ) {
144 # In order to avoid sql injection
145 $dt_params->{iDisplayStart} =~ s/\D//g if defined($dt_params->{iDisplayStart});
146 $dt_params->{iDisplayLength} =~ s/\D//g if defined($dt_params->{iDisplayLength});
147 $dt_params->{iDisplayStart} //= 0;
148 $dt_params->{iDisplayLength} //= 20;
149 $limit = "LIMIT $dt_params->{iDisplayStart},$dt_params->{iDisplayLength}";
154 ($select ? $select : ""),
155 ($from ? $from : ""),
156 ($where ? $where : ""),
157 ($orderby ? $orderby : ""),
158 ($limit ? $limit : "")
160 $sth = $dbh->prepare($query);
161 $sth->execute(@where_args);
162 my $patrons = $sth->fetchall_arrayref({});
164 # Get the iTotalDisplayRecords DataTable variable
165 $query = "SELECT COUNT(borrowers.borrowernumber) " . $from . ($where ? $where : "");
166 $sth = $dbh->prepare($query);
167 $sth->execute(@where_args);
168 ($iTotalDisplayRecords) = $sth->fetchrow_array;
170 # Get some information on patrons
171 foreach my $patron (@$patrons) {
172 my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
173 $patron->{overdues} = $patron_object->get_overdues->count;
174 $patron->{issues} = $patron_object->checkouts->count;
175 my $balance = $patron_object->account->balance;
176 # FIXME Should be formatted from the template
177 $patron->{fines} = sprintf("%.2f", $balance);
179 if( $patron->{dateexpiry} ) {
180 # FIXME We should not format the date here, do it in template-side instead
181 $patron->{dateexpiry} = output_pref( { dt => dt_from_string( $patron->{dateexpiry}, 'iso'), dateonly => 1} );
183 $patron->{dateexpiry} = '';
188 iTotalRecords => $iTotalRecords,
189 iTotalDisplayRecords => $iTotalDisplayRecords,
199 C4::Utils::DataTables::Members - module for using DataTables with patrons
203 This module provides (one for the moment) routines used by the patrons search
209 my $dt_infos = C4::Utils::DataTables::Members->search($params);
211 $params is a hashref with some keys:
217 String to search in the borrowers sql table
221 Introduced to contain 1 letter but can contain more.
222 The search will done on the borrowers.surname field
226 Search patrons with this categorycode
230 Search patrons with this branchcode
234 Can be 'start_with' or 'contain' (default value). Used for the searchmember parameter.
236 =item searchfieldstype
238 Can be 'standard' (default value), 'email', 'borrowernumber', 'phone', 'address' or 'dateofbirth', 'sort1', 'sort2'
242 Is the reference of C4::Utils::DataTables::dt_get_params($input);
250 This file is part of Koha.
252 Copyright 2013 BibLibre
254 Koha is free software; you can redistribute it and/or modify it
255 under the terms of the GNU General Public License as published by
256 the Free Software Foundation; either version 3 of the License, or
257 (at your option) any later version.
259 Koha is distributed in the hope that it will be useful, but
260 WITHOUT ANY WARRANTY; without even the implied warranty of
261 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
262 GNU General Public License for more details.
264 You should have received a copy of the GNU General Public License
265 along with Koha; if not, see <http://www.gnu.org/licenses>.