Merging from rel-1-2 to trunk
[koha.git] / opac / opac-reserve.pl
1 #!/usr/bin/perl
2 use strict;
3 require Exporter;
4 use CGI;
5
6 use C4::Search;
7 use C4::Output;       # gettemplate
8 use C4::Auth;         # checkauth, getborrowernumber.
9 use C4::Koha;
10 use C4::Circulation::Circ2;
11 use C4::Reserves2;
12
13 my $query = new CGI;
14
15
16 my ($loggedinuser, $cookie, $sessionID) = checkauth($query);
17
18 my $template = gettemplate("opac-reserve.tmpl", "opac");
19
20 # get borrower information ....
21 my $borrowernumber = getborrowernumber($loggedinuser);
22 my ($borr, $flags) = getpatroninformation(undef, $borrowernumber);
23 my @bordat;
24 $bordat[0] = $borr;
25 $template->param(BORROWER_INFO => \@bordat);
26
27 # get biblionumber.....
28 my $biblionumber = $query->param('bib');
29 $template->param(biblionumber => $biblionumber);
30
31 my $bibdata = bibdata($biblionumber);
32 $template->param($bibdata);
33
34 # get the rank number....
35 my ($rank,$reserves) = FindReserves($biblionumber);
36 foreach my $res (@$reserves) {
37     if ($res->{'found'} eq 'W') {
38         $rank--;
39     }
40 }
41 $rank++;
42 $template->param(rank => $rank);
43
44
45
46 # pass the pickup branch along....
47 my $branch = $query->param('branch');
48 $template->param(branch => $branch);
49
50 my $branches = getbranches();
51 $template->param(branchname => $branches->{$branch}->{'branchname'});
52
53
54 # make branch selection options...
55 my $branchoptions = '';
56 foreach my $br (keys %$branches) {
57     (next) unless $branches->{$br}->{'IS'};
58     my $selected = "";
59     if ($br eq $branch) {
60         $selected = "selected";
61     }
62     $branchoptions .= "<option value=$br $selected>$branches->{$br}->{'branchname'}</option>\n";
63 }
64 $template->param( branchoptions => $branchoptions);
65
66
67 #get the bibitem data....
68 my ($count,@data) = bibitems($biblionumber);
69
70 foreach my $bibitem (@data) {
71     my @barcodes = barcodes($bibitem->{'biblioitemnumber'});
72     my $barcodestext = "";
73     foreach my $num (@barcodes) {
74         my $message = $num->{'itemlost'} == 1 ? "(lost)" :
75             $num->{'itemlost'} == 2 ? "(long overdue)" : "($branches->{$num->{'holdingbranch'}}->{'branchname'})";
76         $barcodestext .= "$num->{'barcode'} $message <br>";
77     }
78     $barcodestext = substr($barcodestext, 0, -4);
79     $bibitem->{'copies'} = $barcodestext;
80 }
81
82
83
84 my @reqbibs = $query->param('reqbib');
85 if ($query->param('bibitemsselected')) {
86     $template->param(bibitemsselected => 1);
87     my @tempdata;
88     foreach my $bibitem (@data) {
89         foreach my $reqbib (@reqbibs){
90             push @tempdata, $bibitem if ($bibitem->{'biblioitemnumber'} == $reqbib) ;
91         }
92     }
93     @data = @tempdata;
94 } elsif ($query->param('placereserve')) {
95 # here we actually do the reserveration....
96     my $title = $bibdata->{'title'};
97     CreateReserve(undef,$branch,$borrowernumber,$biblionumber,'o',\@reqbibs,$rank,'',$title);
98     warn "reserve created\n";
99     print $query->redirect("/cgi-bin/koha/opac-user.pl");
100 } else {
101     $template->param(selectbibitems => 1);
102 }
103 # check that you can actually make the reserve.
104
105
106
107 $template->param(BIBLIOITEMS => \@data);
108
109 $template->param(loggedinuser => $loggedinuser);
110 print "Content-Type: text/html\n\n", $template->output;