1 package C4
::Utils
::DataTables
::Members
;
5 use C4
::Utils
::DataTables
;
10 my $searchmember = $params->{searchmember
};
11 my $firstletter = $params->{firstletter
};
12 my $categorycode = $params->{categorycode
};
13 my $branchcode = $params->{branchcode
};
14 my $searchtype = $params->{searchtype
} || 'contain';
15 my $searchfieldstype = $params->{searchfieldstype
} || 'standard';
16 my $dt_params = $params->{dt_params
};
18 unless ( $searchmember ) {
19 $searchmember = $dt_params->{sSearch
} // '';
22 my ($sth, $query, $iTotalRecords, $iTotalDisplayRecords);
23 my $dbh = C4
::Context
->dbh;
24 # Get the iTotalRecords DataTable variable
25 $query = "SELECT COUNT(borrowers.borrowernumber) FROM borrowers";
26 $sth = $dbh->prepare($query);
28 ($iTotalRecords) = $sth->fetchrow_array;
30 if ( $searchfieldstype eq 'dateofbirth' ) {
31 # Return an empty list if the date of birth is not correctly formatted
32 $searchmember = eval { output_pref
( { str
=> $searchmember, dateformat
=> 'iso', dateonly
=> 1 } ); };
33 if ( $@
or not $searchmember ) {
36 iTotalDisplayRecords
=> 0,
42 # If branches are independent and user is not superlibrarian
43 # The search has to be only on the user branch
44 if ( C4
::Context
::only_my_library
) {
45 my $userenv = C4
::Context
->userenv;
46 $branchcode = $userenv->{'branch'};
51 borrowers.borrowernumber, borrowers.surname, borrowers.firstname,
52 borrowers.streetnumber, borrowers.streettype, borrowers.address,
53 borrowers.address2, borrowers.city, borrowers.state, borrowers.zipcode,
54 borrowers.country, cardnumber, borrowers.dateexpiry,
55 borrowers.borrowernotes, borrowers.branchcode, borrowers.email,
56 borrowers.userid, borrowers.dateofbirth, borrowers.categorycode,
57 categories.description AS category_description, categories.category_type,
59 my $from = "FROM borrowers
60 LEFT JOIN branches ON borrowers.branchcode = branches.branchcode
61 LEFT JOIN categories ON borrowers.categorycode = categories.categorycode";
64 if(defined $firstletter and $firstletter ne '') {
65 push @where_strs, "borrowers.surname LIKE ?";
66 push @where_args, "$firstletter%";
68 if(defined $categorycode and $categorycode ne '') {
69 push @where_strs, "borrowers.categorycode = ?";
70 push @where_args, $categorycode;
72 if(defined $branchcode and $branchcode ne '') {
73 push @where_strs, "borrowers.branchcode = ?";
74 push @where_args, $branchcode;
78 standard
=> C4
::Context
->preference('DefaultPatronSearchFields') || 'surname,firstname,othernames,cardnumber,userid',
80 email
=> 'email,emailpro,B_email',
81 borrowernumber
=> 'borrowernumber',
83 phone
=> 'phone,phonepro,B_phone,altcontactphone,mobile',
84 address
=> 'streettype,address,address2,city,state,zipcode,country',
85 dateofbirth
=> 'dateofbirth',
90 # * is replaced with % for sql
91 $searchmember =~ s/\*/%/g;
93 # split into search terms
95 # consider coma as space
96 $searchmember =~ s/,/ /g;
97 if ( $searchtype eq 'contain' ) {
98 @terms = split / /, $searchmember;
100 @terms = ($searchmember);
103 foreach my $term (@terms) {
106 my $term_dt = eval { local $SIG{__WARN__
} = {}; output_pref
( { str
=> $term, dateonly
=> 1, dateformat
=> 'sql' } ); };
111 $term .= '%' # end with anything
113 $term = "%$term" # begin with anythin unless start_with
114 if $searchtype eq 'contain' && $term !~ /^%/;
118 for my $searchfield ( split /,/, $searchfields->{$searchfieldstype} ) {
119 push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfield) . " LIKE ?";
120 push @where_args, $term;
123 if ( C4
::Context
->preference('ExtendedPatronAttributes') and $searchmember ) {
124 my $matching_borrowernumbers = C4
::Members
::Attributes
::SearchIdMatchingAttribute
($searchmember);
126 for my $borrowernumber ( @
$matching_borrowernumbers ) {
127 push @where_strs_or, "borrowers.borrowernumber = ?";
128 push @where_args, $borrowernumber;
132 push @where_strs, '('. join (' OR ', @where_strs_or) . ')'
137 $where = " WHERE " . join (" AND ", @where_strs) if @where_strs;
138 my $orderby = dt_build_orderby
($dt_params);
141 # If iDisplayLength == -1, we want to display all patrons
142 if ( !$dt_params->{iDisplayLength
} || $dt_params->{iDisplayLength
} > -1 ) {
143 # In order to avoid sql injection
144 $dt_params->{iDisplayStart
} =~ s/\D//g if defined($dt_params->{iDisplayStart
});
145 $dt_params->{iDisplayLength
} =~ s/\D//g if defined($dt_params->{iDisplayLength
});
146 $dt_params->{iDisplayStart
} //= 0;
147 $dt_params->{iDisplayLength
} //= 20;
148 $limit = "LIMIT $dt_params->{iDisplayStart},$dt_params->{iDisplayLength}";
153 ($select ?
$select : ""),
154 ($from ?
$from : ""),
155 ($where ?
$where : ""),
156 ($orderby ?
$orderby : ""),
157 ($limit ?
$limit : "")
159 $sth = $dbh->prepare($query);
160 $sth->execute(@where_args);
161 my $patrons = $sth->fetchall_arrayref({});
163 # Get the iTotalDisplayRecords DataTable variable
164 $query = "SELECT COUNT(borrowers.borrowernumber) " . $from . ($where ?
$where : "");
165 $sth = $dbh->prepare($query);
166 $sth->execute(@where_args);
167 ($iTotalDisplayRecords) = $sth->fetchrow_array;
169 # Get some information on patrons
170 foreach my $patron (@
$patrons) {
171 my $patron_object = Koha
::Patrons
->find( $patron->{borrowernumber
} );
172 $patron->{overdues
} = $patron_object->get_overdues->count;
173 $patron->{issues
} = $patron_object->get_issues->count;
174 my $balance = $patron_object->get_account_lines->get_balance;
175 # FIXME Should be formatted from the template
176 $patron->{fines
} = sprintf("%.2f", $balance);
178 if($patron->{dateexpiry
} and $patron->{dateexpiry
} ne '0000-00-00') {
179 $patron->{dateexpiry
} = output_pref
( { dt
=> dt_from_string
( $patron->{dateexpiry
}, 'iso'), dateonly
=> 1} );
181 $patron->{dateexpiry
} = '';
186 iTotalRecords
=> $iTotalRecords,
187 iTotalDisplayRecords
=> $iTotalDisplayRecords,
197 C4::Utils::DataTables::Members - module for using DataTables with patrons
201 This module provides (one for the moment) routines used by the patrons search
207 my $dt_infos = C4::Utils::DataTables::Members->search($params);
209 $params is a hashref with some keys:
215 String to search in the borrowers sql table
219 Introduced to contain 1 letter but can contain more.
220 The search will done on the borrowers.surname field
224 Search patrons with this categorycode
228 Search patrons with this branchcode
232 Can be 'start_with' or 'contain' (default value). Used for the searchmember parameter.
234 =item searchfieldstype
236 Can be 'standard' (default value), 'email', 'borrowernumber', 'phone', 'address' or 'dateofbirth', 'sort1', 'sort2'
240 Is the reference of C4::Utils::DataTables::dt_get_params($input);
248 This file is part of Koha.
250 Copyright 2013 BibLibre
252 Koha is free software; you can redistribute it and/or modify it
253 under the terms of the GNU General Public License as published by
254 the Free Software Foundation; either version 3 of the License, or
255 (at your option) any later version.
257 Koha is distributed in the hope that it will be useful, but
258 WITHOUT ANY WARRANTY; without even the implied warranty of
259 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
260 GNU General Public License for more details.
262 You should have received a copy of the GNU General Public License
263 along with Koha; if not, see <http://www.gnu.org/licenses>.