Bug 13789 - facets with accented utf-8 characters generate double encoded links
[koha.git] / reserve / request.pl
blobce0e70f79ad16f2863e24443ec85728891d0152e
1 #!/usr/bin/perl
4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 # Parts Copyright 2011 Catalyst IT
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 =head1 request.pl
25 script to place reserves/requests
27 =cut
29 use strict;
30 use warnings;
31 use C4::Branch;
32 use CGI qw ( -utf8 );
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
35 use C4::Output;
36 use C4::Auth;
37 use C4::Reserves;
38 use C4::Biblio;
39 use C4::Items;
40 use C4::Koha;
41 use C4::Circulation;
42 use C4::Dates qw/format_date/;
43 use C4::Members;
44 use C4::Search; # enabled_staff_search_views
45 use Koha::DateUtils;
47 my $dbh = C4::Context->dbh;
48 my $sth;
49 my $input = new CGI;
50 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
52 template_name => "reserve/request.tt",
53 query => $input,
54 type => "intranet",
55 authnotrequired => 0,
56 flagsrequired => { reserveforothers => 'place_holds' },
60 my $multihold = $input->param('multi_hold');
61 $template->param(multi_hold => $multihold);
62 my $showallitems = $input->param('showallitems');
64 # get Branches and Itemtypes
65 my $branches = GetBranches();
66 my $itemtypes = GetItemTypes();
68 my $userbranch = '';
69 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
70 $userbranch = C4::Context->userenv->{'branch'};
74 # Select borrowers infos
75 my $findborrower = $input->param('findborrower');
76 $findborrower = '' unless defined $findborrower;
77 $findborrower =~ s|,| |g;
78 my $borrowernumber_hold = $input->param('borrowernumber') || '';
79 my $messageborrower;
80 my $maxreserves;
81 my $warnings;
82 my $messages;
84 my $date = C4::Dates->today('iso');
85 my $action = $input->param('action');
86 $action ||= q{};
88 if ( $action eq 'move' ) {
89 my $where = $input->param('where');
90 my $reserve_id = $input->param('reserve_id');
91 AlterPriority( $where, $reserve_id );
92 } elsif ( $action eq 'cancel' ) {
93 my $reserve_id = $input->param('reserve_id');
94 CancelReserve({ reserve_id => $reserve_id });
95 } elsif ( $action eq 'setLowestPriority' ) {
96 my $reserve_id = $input->param('reserve_id');
97 ToggleLowestPriority( $reserve_id );
98 } elsif ( $action eq 'toggleSuspend' ) {
99 my $reserve_id = $input->param('reserve_id');
100 my $suspend_until = $input->param('suspend_until');
101 ToggleSuspend( $reserve_id, $suspend_until );
104 if ($findborrower) {
105 my $borrowers = Search($findborrower, 'cardnumber');
107 if ($borrowers && @$borrowers) {
108 if ( @$borrowers == 1 ) {
109 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
111 else {
112 $template->param( borrower_list => sort_borrowerlist($borrowers));
114 } else {
115 $messageborrower = "'$findborrower'";
119 # If we have the borrowernumber because we've performed an action, then we
120 # don't want to try to place another reserve.
121 if ($borrowernumber_hold && !$action) {
122 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
123 my $diffbranch;
124 my @getreservloop;
125 my $count_reserv = 0;
127 # we check the reserves of the borrower, and if he can reserv a document
128 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
130 my $number_reserves =
131 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
133 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
134 $warnings = 1;
135 $maxreserves = 1;
138 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
139 my $expiry_date = $borrowerinfo->{dateexpiry};
140 my $expiry = 0; # flag set if patron account has expired
141 if ($expiry_date and $expiry_date ne '0000-00-00' and
142 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
143 $expiry = 1;
146 # check if the borrower make the reserv in a different branch
147 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
148 $diffbranch = 1;
151 $template->param(
152 borrowernumber => $borrowerinfo->{'borrowernumber'},
153 borrowersurname => $borrowerinfo->{'surname'},
154 borrowerfirstname => $borrowerinfo->{'firstname'},
155 borrowerstreetaddress => $borrowerinfo->{'address'},
156 borrowercity => $borrowerinfo->{'city'},
157 borrowerphone => $borrowerinfo->{'phone'},
158 borrowermobile => $borrowerinfo->{'mobile'},
159 borrowerfax => $borrowerinfo->{'fax'},
160 borrowerphonepro => $borrowerinfo->{'phonepro'},
161 borroweremail => $borrowerinfo->{'email'},
162 borroweremailpro => $borrowerinfo->{'emailpro'},
163 borrowercategory => $borrowerinfo->{'category'},
164 borrowerreservs => $count_reserv,
165 cardnumber => $borrowerinfo->{'cardnumber'},
166 expiry => $expiry,
167 diffbranch => $diffbranch,
168 messages => $messages,
169 warnings => $warnings
173 $template->param( messageborrower => $messageborrower );
175 # FIXME launch another time GetMember perhaps until
176 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
178 my @biblionumbers = ();
179 my $biblionumbers = $input->param('biblionumbers');
180 if ($multihold) {
181 @biblionumbers = split '/', $biblionumbers;
182 } else {
183 push @biblionumbers, $input->param('biblionumber');
186 my $itemdata_enumchron = 0;
187 my @biblioloop = ();
188 foreach my $biblionumber (@biblionumbers) {
190 my %biblioloopiter = ();
192 my $dat = GetBiblioData($biblionumber);
194 my $canReserve = CanBookBeReserved( $borrowerinfo->{borrowernumber}, $biblionumber );
195 if ( $canReserve eq 'OK' ) {
197 #All is OK and we can continue
199 elsif ( $canReserve eq 'tooManyReserves' ) {
200 $maxreserves = 1;
202 elsif ( $canReserve eq 'ageRestricted' ) {
203 $template->param( $canReserve => 1 );
204 $biblioloopiter{$canReserve} = 1;
206 else {
207 $biblioloopiter{$canReserve} = 1;
210 my $alreadypossession;
211 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
212 $alreadypossession = 1;
215 # get existing reserves .....
216 my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
217 my $count = scalar( @$reserves );
218 my $totalcount = $count;
219 my $holds_count = 0;
220 my $alreadyreserved = 0;
222 foreach my $res (@$reserves) {
223 if ( defined $res->{found} ) { # found can be 'W' or 'T'
224 $count--;
227 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
228 $holds_count++;
232 if ( $holds_count ) {
233 $alreadyreserved = 1;
234 $biblioloopiter{warn} = 1;
235 $biblioloopiter{alreadyres} = 1;
238 $template->param(
239 alreadyreserved => $alreadyreserved,
240 alreadypossession => $alreadypossession,
243 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
244 # make priorities options
246 my @optionloop;
247 for ( 1 .. $count + 1 ) {
248 push(
249 @optionloop,
251 num => $_,
252 selected => ( $_ == $count + 1 ),
256 # adding a fixed value for priority options
257 my $fixedRank = $count+1;
259 my @branchcodes;
260 my %itemnumbers_of_biblioitem;
261 my @itemnumbers;
263 ## $items is array of 'item' table numbers
264 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
265 @itemnumbers = @$items;
267 my @hostitems = get_hostitemnumbers_of($biblionumber);
268 if (@hostitems){
269 $template->param('hostitemsflag' => 1);
270 push(@itemnumbers, @hostitems);
273 if (!@itemnumbers) {
274 $template->param('noitems' => 1);
275 $biblioloopiter{noitems} = 1;
278 ## Hash of item number to 'item' table fields
279 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
281 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
282 ## when by definition all of the itemnumber have the same biblioitemnumber
283 foreach my $itemnumber (@itemnumbers) {
284 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
285 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
288 ## Should be same as biblionumber
289 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
291 my $notforloan_label_of = get_notforloan_label_of();
293 ## Hash of biblioitemnumber to 'biblioitem' table records
294 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
296 my @bibitemloop;
298 foreach my $biblioitemnumber (@biblioitemnumbers) {
299 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
300 my $num_available = 0;
301 my $num_override = 0;
302 my $hiddencount = 0;
304 $biblioitem->{description} =
305 $itemtypes->{ $biblioitem->{itemtype} }{description};
306 if($biblioitem->{biblioitemnumber} ne $biblionumber){
307 $biblioitem->{hostitemsflag}=1;
309 $biblioloopiter{description} = $biblioitem->{description};
310 $biblioloopiter{itypename} = $biblioitem->{description};
311 $biblioloopiter{imageurl} =
312 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
314 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
315 my $item = $iteminfos_of->{$itemnumber};
317 unless (C4::Context->preference('item-level_itypes')) {
318 $item->{itype} = $biblioitem->{itemtype};
321 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
322 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
323 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
325 # if the holdingbranch is different than the homebranch, we show the
326 # holdingbranch of the document too
327 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
328 $item->{holdingbranchname} =
329 $branches->{ $item->{holdingbranch} }{branchname};
332 if($item->{biblionumber} ne $biblionumber){
333 $item->{hostitemsflag}=1;
334 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
337 # add information
338 $item->{itemcallnumber} = $item->{itemcallnumber};
340 # if the item is currently on loan, we display its return date and
341 # change the background color
342 my $issues= GetItemIssue($itemnumber);
343 if ( $issues->{'date_due'} ) {
344 $item->{date_due} = format_sqldatetime($issues->{date_due});
345 $item->{backgroundcolor} = 'onloan';
348 # checking reserve
349 my ($reservedate,$reservedfor,$expectedAt,$reserve_id,$wait) = GetReservesFromItemnumber($itemnumber);
350 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
352 if ( defined $reservedate ) {
353 $item->{backgroundcolor} = 'reserved';
354 $item->{reservedate} = format_date($reservedate);
355 $item->{ReservedForBorrowernumber} = $reservedfor;
356 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
357 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
358 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
359 $item->{waitingdate} = $wait;
362 # Management of the notforloan document
363 if ( $item->{notforloan} ) {
364 $item->{backgroundcolor} = 'other';
365 $item->{notforloanvalue} =
366 $notforloan_label_of->{ $item->{notforloan} };
369 # Management of lost or long overdue items
370 if ( $item->{itemlost} ) {
372 # FIXME localized strings should never be in Perl code
373 $item->{message} =
374 $item->{itemlost} == 1 ? "(lost)"
375 : $item->{itemlost} == 2 ? "(long overdue)"
376 : "";
377 $item->{backgroundcolor} = 'other';
378 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
379 $item->{hide} = 1;
380 $hiddencount++;
384 # Check the transit status
385 my ( $transfertwhen, $transfertfrom, $transfertto ) =
386 GetTransfers($itemnumber);
388 if ( defined $transfertwhen && $transfertwhen ne '' ) {
389 $item->{transfertwhen} = format_date($transfertwhen);
390 $item->{transfertfrom} =
391 $branches->{$transfertfrom}{branchname};
392 $item->{transfertto} = $branches->{$transfertto}{branchname};
393 $item->{nocancel} = 1;
396 # If there is no loan, return and transfer, we show a checkbox.
397 $item->{notforloan} = $item->{notforloan} || 0;
399 # if independent branches is on we need to check if the person can reserve
400 # for branches they arent logged in to
401 if ( C4::Context->preference("IndependentBranches") ) {
402 if (! C4::Context->preference("canreservefromotherbranches")){
403 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
404 my $userenv = C4::Context->userenv;
405 unless ( C4::Context->IsSuperLibrarian ) {
406 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
411 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
413 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
414 my $policy_holdallowed = 1;
416 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
418 if ( $branchitemrule->{'holdallowed'} == 0 ||
419 ( $branchitemrule->{'holdallowed'} == 1 &&
420 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
421 $policy_holdallowed = 0;
424 if (
425 $policy_holdallowed
426 && !$item->{cantreserve}
427 && IsAvailableForItemLevelRequest($itemnumber)
428 && CanItemBeReserved(
429 $borrowerinfo->{borrowernumber}, $itemnumber
430 ) eq 'OK'
433 $item->{available} = 1;
434 $num_available++;
436 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
437 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
438 $item->{override} = 1;
439 $num_override++;
442 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
444 # Show serial enumeration when needed
445 if ($item->{enumchron}) {
446 $itemdata_enumchron = 1;
449 push @{ $biblioitem->{itemloop} }, $item;
452 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
453 $template->param( override_required => 1 );
454 } elsif ( $num_available == 0 ) {
455 $template->param( none_available => 1 );
456 $biblioloopiter{warn} = 1;
457 $biblioloopiter{none_avail} = 1;
459 $template->param( hiddencount => $hiddencount);
461 push @bibitemloop, $biblioitem;
464 # existingreserves building
465 my @reserveloop;
466 $reserves = GetReservesFromBiblionumber({ biblionumber => $biblionumber, all_dates => 1 });
467 foreach my $res ( sort {
468 my $a_found = $a->{found} || '';
469 my $b_found = $a->{found} || '';
470 $a_found cmp $b_found;
471 } @$reserves ) {
472 my %reserve;
473 my @optionloop;
474 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
475 push(
476 @optionloop,
478 num => $i,
479 selected => ( $i == $res->{priority} ),
484 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
485 my $item = $res->{'itemnumber'};
486 $item = GetBiblioFromItemNumber($item,undef);
487 $reserve{'wait'}= 1;
488 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
489 $reserve{'biblionumber'}=$item->{'biblionumber'};
490 $reserve{'barcodenumber'} = $item->{'barcode'};
491 $reserve{'wbrcode'} = $res->{'branchcode'};
492 $reserve{'itemnumber'} = $res->{'itemnumber'};
493 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
494 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
495 # Just because the holdingbranch matches the reserve branch doesn't mean the item
496 # has arrived at the destination, check for an open transfer for the item as well
497 my ( $transfertwhen, $transfertfrom, $transferto ) = C4::Circulation::GetTransfers( $res->{itemnumber} );
498 if ( not $transferto or $transferto ne $res->{branchcode} ) {
499 $reserve{'atdestination'} = 1;
502 # set found to 1 if reserve is waiting for patron pickup
503 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
504 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
505 } elsif ($res->{priority} > 0) {
506 if (defined($res->{itemnumber})) {
507 my $item = GetItem($res->{itemnumber});
508 $reserve{'itemnumber'} = $res->{'itemnumber'};
509 $reserve{'barcodenumber'} = $item->{'barcode'};
510 $reserve{'item_level_hold'} = 1;
514 # get borrowers reserve info
515 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
516 if (C4::Context->preference('HidePatronName')){
517 $reserve{'hidename'} = 1;
518 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
520 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
521 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
522 $reserve{'date'} = format_date( $res->{'reservedate'} );
523 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
524 $reserve{'biblionumber'} = $res->{'biblionumber'};
525 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
526 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
527 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
528 $reserve{'notes'} = $res->{'reservenotes'};
529 $reserve{'wait'} =
530 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
531 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
532 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
533 $reserve{'voldesc'} = $res->{'volumeddesc'};
534 $reserve{'ccode'} = $res->{'ccode'};
535 $reserve{'barcode'} = $res->{'barcode'};
536 $reserve{'priority'} = $res->{'priority'};
537 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
538 $reserve{'optionloop'} = \@optionloop;
539 $reserve{'suspend'} = $res->{'suspend'};
540 $reserve{'suspend_until'} = $res->{'suspend_until'};
541 $reserve{'reserve_id'} = $res->{'reserve_id'};
543 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
544 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
545 } else {
546 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
549 push( @reserveloop, \%reserve );
552 # get the time for the form name...
553 my $time = time();
555 $template->param(
556 branchloop => GetBranchesLoop($userbranch),
557 time => $time,
558 fixedRank => $fixedRank,
561 # display infos
562 $template->param(
563 optionloop => \@optionloop,
564 bibitemloop => \@bibitemloop,
565 itemdata_enumchron => $itemdata_enumchron,
566 date => $date,
567 biblionumber => $biblionumber,
568 findborrower => $findborrower,
569 title => $dat->{title},
570 author => $dat->{author},
571 holdsview => 1,
572 C4::Search::enabled_staff_search_views,
574 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
575 $template->param(
576 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
577 borrower_branchcode => $borrowerinfo->{'branchcode'},
581 $biblioloopiter{biblionumber} = $biblionumber;
582 $biblioloopiter{title} = $dat->{title};
583 $biblioloopiter{rank} = $fixedRank;
584 $biblioloopiter{reserveloop} = \@reserveloop;
586 if (@reserveloop) {
587 $template->param( reserveloop => \@reserveloop );
590 push @biblioloop, \%biblioloopiter;
593 $template->param( biblioloop => \@biblioloop );
594 $template->param( biblionumbers => $biblionumbers );
595 $template->param( maxreserves => $maxreserves );
597 if ($multihold) {
598 $template->param( multi_hold => 1 );
601 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
602 $template->param( reserve_in_future => 1 );
605 $template->param(
606 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
607 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
610 # printout the page
611 output_html_with_http_headers $input, $cookie, $template->output;
613 sub sort_borrowerlist {
614 my $borrowerslist = shift;
615 my $ref = [];
616 push @{$ref}, sort {
617 uc( $a->{surname} . $a->{firstname} ) cmp
618 uc( $b->{surname} . $b->{firstname} )
619 } @{$borrowerslist};
620 return $ref;