|
Lines 43-49
BEGIN {
Link Here
|
| 43 |
#Get data |
43 |
#Get data |
| 44 |
push @EXPORT, qw( |
44 |
push @EXPORT, qw( |
| 45 |
&Search |
45 |
&Search |
| 46 |
&SearchMember |
|
|
| 47 |
&GetMemberDetails |
46 |
&GetMemberDetails |
| 48 |
&GetMemberRelatives |
47 |
&GetMemberRelatives |
| 49 |
&GetMember |
48 |
&GetMember |
|
Lines 140-317
This module contains routines for adding, modifying and deleting members/patrons
Link Here
|
| 140 |
|
139 |
|
| 141 |
=head1 FUNCTIONS |
140 |
=head1 FUNCTIONS |
| 142 |
|
141 |
|
| 143 |
=head2 SearchMember |
142 |
=head2 Search |
| 144 |
|
|
|
| 145 |
($count, $borrowers) = &SearchMember($searchstring, $type, |
| 146 |
$category_type, $filter, $showallbranches); |
| 147 |
|
143 |
|
| 148 |
Looks up patrons (borrowers) by name. |
144 |
$borrowers_result_array_ref = &Search($filter,$orderby, $limit, |
|
|
145 |
$columns_out, $search_on_fields,$searchtype); |
| 149 |
|
146 |
|
| 150 |
BUGFIX 499: C<$type> is now used to determine type of search. |
147 |
Looks up patrons (borrowers) on filter. A wrapper for SearchInTable('borrowers'). |
| 151 |
if $type is "simple", search is performed on the first letter of the |
|
|
| 152 |
surname only. |
| 153 |
|
148 |
|
| 154 |
$category_type is used to get a specified type of user. |
149 |
For C<$filter>, C<$orderby>, C<$limit>, C<&columns_out>, C<&search_on_fields> and C<&searchtype> |
| 155 |
(mainly adults when creating a child.) |
150 |
refer to C4::SQLHelper:SearchInTable(). |
| 156 |
|
151 |
|
| 157 |
C<$searchstring> is a space-separated list of search terms. Each term |
152 |
Special C<$filter> key '' is effectively expanded to search on surname firstname othernamescw |
| 158 |
must match the beginning a borrower's surname, first name, or other |
153 |
and cardnumber unless C<&search_on_fields> is defined |
| 159 |
name. |
|
|
| 160 |
|
154 |
|
| 161 |
C<$filter> is assumed to be a list of elements to filter results on |
155 |
Examples: |
| 162 |
|
156 |
|
| 163 |
C<$showallbranches> is used in IndependantBranches Context to display all branches results. |
157 |
$borrowers = Search('abcd', 'cardnumber'); |
| 164 |
|
158 |
|
| 165 |
C<&SearchMember> returns a two-element list. C<$borrowers> is a |
159 |
$borrowers = Search({''=>'abcd', category_type=>'I'}, 'surname'); |
| 166 |
reference-to-array; each element is a reference-to-hash, whose keys |
|
|
| 167 |
are the fields of the C<borrowers> table in the Koha database. |
| 168 |
C<$count> is the number of elements in C<$borrowers>. |
| 169 |
|
160 |
|
| 170 |
=cut |
161 |
=cut |
| 171 |
|
162 |
|
| 172 |
#' |
163 |
sub _express_member_find { |
| 173 |
#used by member enquiries from the intranet |
164 |
my ($filter) = @_; |
| 174 |
sub SearchMember { |
165 |
|
| 175 |
my ($searchstring, $orderby, $type,$category_type,$filter,$showallbranches ) = @_; |
|
|
| 176 |
my $dbh = C4::Context->dbh; |
| 177 |
my $query = ""; |
| 178 |
my $count; |
| 179 |
my @data; |
| 180 |
my @bind = (); |
| 181 |
|
| 182 |
# this is used by circulation everytime a new borrowers cardnumber is scanned |
166 |
# this is used by circulation everytime a new borrowers cardnumber is scanned |
| 183 |
# so we can check an exact match first, if that works return, otherwise do the rest |
167 |
# so we can check an exact match first, if that works return, otherwise do the rest |
| 184 |
$query = "SELECT * FROM borrowers |
168 |
my $dbh = C4::Context->dbh; |
| 185 |
LEFT JOIN categories ON borrowers.categorycode=categories.categorycode |
169 |
my $query = "SELECT borrowernumber FROM borrowers WHERE cardnumber = ?"; |
| 186 |
"; |
170 |
if ( my $borrowernumber = $dbh->selectrow_array($query, undef, $filter) ) { |
| 187 |
my $sth = $dbh->prepare("$query WHERE cardnumber = ?"); |
171 |
return( {"borrowernumber"=>$borrowernumber} ); |
| 188 |
$sth->execute($searchstring); |
|
|
| 189 |
my $data = $sth->fetchall_arrayref({}); |
| 190 |
if (@$data){ |
| 191 |
return ( scalar(@$data), $data ); |
| 192 |
} |
| 193 |
|
| 194 |
if ( $type eq "simple" ) # simple search for one letter only |
| 195 |
{ |
| 196 |
$query .= ($category_type ? " AND category_type = ".$dbh->quote($category_type) : ""); |
| 197 |
$query .= " WHERE (surname LIKE ? OR cardnumber like ?) "; |
| 198 |
if (C4::Context->preference("IndependantBranches") && !$showallbranches){ |
| 199 |
if (C4::Context->userenv && C4::Context->userenv->{flags} % 2 !=1 && C4::Context->userenv->{'branch'}){ |
| 200 |
$query.=" AND borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'}) unless (C4::Context->userenv->{'branch'} eq "insecure"); |
| 201 |
} |
| 202 |
} |
| 203 |
$query.=" ORDER BY $orderby"; |
| 204 |
@bind = ("$searchstring%","$searchstring"); |
| 205 |
} |
172 |
} |
| 206 |
else # advanced search looking in surname, firstname and othernames |
|
|
| 207 |
{ |
| 208 |
@data = split( ' ', $searchstring ); |
| 209 |
$count = @data; |
| 210 |
$query .= " WHERE "; |
| 211 |
if (C4::Context->preference("IndependantBranches") && !$showallbranches){ |
| 212 |
if (C4::Context->userenv && C4::Context->userenv->{flags} % 2 !=1 && C4::Context->userenv->{'branch'}){ |
| 213 |
$query.=" borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'})." AND " unless (C4::Context->userenv->{'branch'} eq "insecure"); |
| 214 |
} |
| 215 |
} |
| 216 |
$query.="((surname LIKE ? OR (surname LIKE ? AND surname REGEXP ?) |
| 217 |
OR firstname LIKE ? OR (firstname LIKE ? AND firstname REGEXP ?) |
| 218 |
OR othernames LIKE ? OR (othernames LIKE ? AND othernames REGEXP ?)) |
| 219 |
" . |
| 220 |
($category_type?" AND category_type = ".$dbh->quote($category_type):""); |
| 221 |
my $regex = '[[:punct:][:space:]]'.$data[0]; |
| 222 |
@bind = ( |
| 223 |
"$data[0]%", "%$data[0]%", $regex, |
| 224 |
"$data[0]%", "%$data[0]%", $regex, |
| 225 |
"$data[0]%", "%$data[0]%", $regex |
| 226 |
); |
| 227 |
for ( my $i = 1 ; $i < $count ; $i++ ) { |
| 228 |
$query = $query . " AND (" . " surname LIKE ? OR (surname LIKE ? AND surname REGEXP ?) |
| 229 |
OR firstname LIKE ? OR (firstname LIKE ? AND firstname REGEXP ?) |
| 230 |
OR othernames LIKE ? OR (othernames LIKE ? AND othernames REGEXP ?))"; |
| 231 |
$regex = '[[:punct:][:space:]]'.$data[$i]; |
| 232 |
push( @bind, |
| 233 |
"$data[$i]%", "%$data[$i]%", $regex, |
| 234 |
"$data[$i]%", "%$data[$i]%", $regex, |
| 235 |
"$data[$i]%", "%$data[$i]%", $regex |
| 236 |
); |
| 237 |
|
| 238 |
|
| 239 |
# FIXME - .= <<EOT; |
| 240 |
} |
| 241 |
$query = $query . ") OR cardnumber LIKE ? "; |
| 242 |
push( @bind, $searchstring ); |
| 243 |
$query .= "order by $orderby"; |
| 244 |
|
173 |
|
| 245 |
# FIXME - .= <<EOT; |
174 |
my ($search_on_fields, $searchtype); |
|
|
175 |
if ( length($filter) == 1 ) { |
| 176 |
$search_on_fields = [ qw(surname) ]; |
| 177 |
$searchtype = 'start_with'; |
| 178 |
} else { |
| 179 |
$search_on_fields = [ qw(surname firstname othernames cardnumber) ]; |
| 180 |
$searchtype = 'contain'; |
| 246 |
} |
181 |
} |
| 247 |
|
182 |
|
| 248 |
$sth = $dbh->prepare($query); |
183 |
return (undef, $search_on_fields, $searchtype); |
| 249 |
|
|
|
| 250 |
$debug and print STDERR "Q $orderby : $query\n"; |
| 251 |
$sth->execute(@bind); |
| 252 |
my @results; |
| 253 |
$data = $sth->fetchall_arrayref({}); |
| 254 |
|
| 255 |
return ( scalar(@$data), $data ); |
| 256 |
} |
184 |
} |
| 257 |
|
185 |
|
| 258 |
=head2 Search |
|
|
| 259 |
|
| 260 |
$borrowers_result_array_ref = &Search($filter,$orderby, $limit, |
| 261 |
$columns_out, $search_on_fields,$searchtype); |
| 262 |
|
| 263 |
Looks up patrons (borrowers) on filter. |
| 264 |
|
| 265 |
BUGFIX 499: C<$type> is now used to determine type of search. |
| 266 |
if $type is "simple", search is performed on the first letter of the |
| 267 |
surname only. |
| 268 |
|
| 269 |
$category_type is used to get a specified type of user. |
| 270 |
(mainly adults when creating a child.) |
| 271 |
|
| 272 |
C<$filter> can be |
| 273 |
- a space-separated list of search terms. Implicit AND is done on them |
| 274 |
- a hash ref containing fieldnames associated with queried value |
| 275 |
- an array ref combining the two previous elements Implicit OR is done between each array element |
| 276 |
|
| 277 |
|
| 278 |
C<$orderby> is an arrayref of hashref. Contains the name of the field and 0 or 1 depending if order is ascending or descending |
| 279 |
|
| 280 |
C<$limit> is there to allow limiting number of results returned |
| 281 |
|
| 282 |
C<&columns_out> is an array ref to the fieldnames you want to see in the result list |
| 283 |
|
| 284 |
C<&search_on_fields> is an array ref to the fieldnames you want to limit search on when you are using string search |
| 285 |
|
| 286 |
C<&searchtype> is a string telling the type of search you want todo : start_with, exact or contains are allowed |
| 287 |
|
| 288 |
=cut |
| 289 |
|
| 290 |
sub Search { |
186 |
sub Search { |
| 291 |
my ( $filter, $orderby, $limit, $columns_out, $search_on_fields, $searchtype ) = @_; |
187 |
my ( $filter, $orderby, $limit, $columns_out, $search_on_fields, $searchtype ) = @_; |
| 292 |
my @filters; |
188 |
|
| 293 |
my %filtersmatching_record; |
189 |
my $search_string; |
| 294 |
my @finalfilter; |
190 |
my $found_borrower; |
| 295 |
if ( ref($filter) eq "ARRAY" ) { |
191 |
|
| 296 |
push @filters, @$filter; |
192 |
if ( my $fr = ref $filter ) { |
| 297 |
} else { |
193 |
if ( $fr eq "HASH" ) { |
| 298 |
push @filters, $filter; |
194 |
if ( my $search_string = $filter->{''} ) { |
|
|
195 |
my ($member_filter, $member_search_on_fields, $member_searchtype) = _express_member_find($search_string); |
| 196 |
if ($member_filter) { |
| 197 |
$filter = $member_filter; |
| 198 |
$found_borrower = 1; |
| 199 |
} else { |
| 200 |
$search_on_fields ||= $member_search_on_fields; |
| 201 |
$searchtype ||= $member_searchtype; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
else { |
| 206 |
$search_string = $filter; |
| 207 |
} |
| 299 |
} |
208 |
} |
| 300 |
if ( C4::Context->preference('ExtendedPatronAttributes') ) { |
209 |
else { |
| 301 |
my $matching_records = C4::Members::Attributes::SearchIdMatchingAttribute($filter); |
210 |
$search_string = $filter; |
|
|
211 |
my ($member_filter, $member_search_on_fields, $member_searchtype) = _express_member_find($search_string); |
| 212 |
if ($member_filter) { |
| 213 |
$filter = $member_filter; |
| 214 |
$found_borrower = 1; |
| 215 |
} else { |
| 216 |
$search_on_fields ||= $member_search_on_fields; |
| 217 |
$searchtype ||= $member_searchtype; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if ( !$found_borrower && C4::Context->preference('ExtendedPatronAttributes' && $search_string) ) { |
| 222 |
my $matching_records = C4::Members::Attributes::SearchIdMatchingAttribute($search_string); |
| 302 |
if(scalar(@$matching_records)>0) { |
223 |
if(scalar(@$matching_records)>0) { |
|
|
224 |
my %filtersmatching_record; |
| 303 |
foreach my $matching_record (@$matching_records) { |
225 |
foreach my $matching_record (@$matching_records) { |
| 304 |
$filtersmatching_record{$$matching_record[0]}=1; |
226 |
$filtersmatching_record{$$matching_record[0]}=1; |
| 305 |
} |
227 |
} |
| 306 |
foreach my $k (keys(%filtersmatching_record)) { |
228 |
# push @filters, [ map {"borrowernumber"=>$_}, keys %filtersmatching_record]; |
| 307 |
push @filters, {"borrowernumber"=>$k}; |
229 |
$filter = [ map {"borrowernumber"=>$_}, keys %filtersmatching_record ]; |
| 308 |
} |
230 |
$found_borrower = 1; |
| 309 |
} |
231 |
} |
| 310 |
} |
232 |
} |
|
|
233 |
|
| 234 |
# $showallbranches was not used at the time SearchMember() was mainstreamed into Search(). |
| 235 |
# Mentioning for the reference |
| 236 |
|
| 237 |
if ( C4::Context->preference("IndependantBranches") ) { # && !$showallbranches){ |
| 238 |
if ( my $userenv = C4::Context->userenv ) { |
| 239 |
my $branch = $userenv->{'branch'}; |
| 240 |
if ( ($userenv->{flags} % 2 !=1) && |
| 241 |
$branch && $branch ne "insecure" ){ |
| 242 |
|
| 243 |
my $fr = ref $filter; |
| 244 |
$filter = { '' => $filter } unless $fr && $fr eq 'HASH'; |
| 245 |
|
| 246 |
$filter->{branchcode} = $branch; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if ($found_borrower) { |
| 252 |
$searchtype = "exact"; |
| 253 |
} |
| 311 |
$searchtype ||= "start_with"; |
254 |
$searchtype ||= "start_with"; |
| 312 |
push @finalfilter, \@filters; |
255 |
|
| 313 |
my $data = SearchInTable( "borrowers", \@finalfilter, $orderby, $limit, $columns_out, $search_on_fields, $searchtype ); |
256 |
return SearchInTable( "borrowers", $filter, $orderby, $limit, $columns_out, $search_on_fields, $searchtype ); |
| 314 |
return ($data); |
|
|
| 315 |
} |
257 |
} |
| 316 |
|
258 |
|
| 317 |
=head2 GetMemberDetails |
259 |
=head2 GetMemberDetails |