Bug 23248: Avoid opac-ISBDdetail.pl breaking on invalid biblionumber
[koha.git] / opac / opac-illrequests.pl
1 #!/usr/bin/perl
2
3 # Copyright 2017 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Output;
26
27 use Koha::Illrequest::Config;
28 use Koha::Illrequests;
29 use Koha::Libraries;
30 use Koha::Patrons;
31
32 my $query = new CGI;
33
34 # Grab all passed data
35 # 'our' since Plack changes the scoping
36 # of 'my'
37 our $params = $query->Vars();
38
39 # if illrequests is disabled, leave immediately
40 if ( ! C4::Context->preference('ILLModule') ) {
41     print $query->redirect("/cgi-bin/koha/errors/404.pl");
42     exit;
43 }
44
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
46     template_name   => "opac-illrequests.tt",
47     query           => $query,
48     type            => "opac",
49     authnotrequired => 0,
50 });
51
52 # Are we able to actually work?
53 my $reduced  = C4::Context->preference('ILLOpacbackends');
54 my $backends = Koha::Illrequest::Config->new->available_backends($reduced);
55 my $backends_available = ( scalar @{$backends} > 0 );
56 $template->param( backends_available => $backends_available );
57
58 my $op = $params->{'method'} || 'list';
59
60 if ( $op eq 'list' ) {
61
62     my $requests = Koha::Illrequests->search(
63         { borrowernumber => $loggedinuser }
64     );
65     my $req = Koha::Illrequest->new;
66     $template->param(
67         requests => $requests,
68         backends    => $backends
69     );
70
71 } elsif ( $op eq 'view') {
72     my $request = Koha::Illrequests->find({
73         borrowernumber => $loggedinuser,
74         illrequest_id  => $params->{illrequest_id}
75     });
76     $template->param(
77         request => $request
78     );
79
80 } elsif ( $op eq 'update') {
81     my $request = Koha::Illrequests->find({
82         borrowernumber => $loggedinuser,
83         illrequest_id  => $params->{illrequest_id}
84     });
85     $request->notesopac($params->{notesopac})->store;
86     print $query->redirect(
87         '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
88         $params->{illrequest_id} .
89         '&message=1'
90     );
91     exit;
92 } elsif ( $op eq 'cancreq') {
93     my $request = Koha::Illrequests->find({
94         borrowernumber => $loggedinuser,
95         illrequest_id  => $params->{illrequest_id}
96     });
97     $request->status('CANCREQ')->store;
98     print $query->redirect(
99         '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
100         $params->{illrequest_id} .
101         '&message=1'
102     );
103     exit;
104 } elsif ( $op eq 'create' ) {
105     if (!$params->{backend}) {
106         my $req = Koha::Illrequest->new;
107         $template->param(
108             backends    => $req->available_backends
109         );
110     } else {
111         my $request = Koha::Illrequest->new
112             ->load_backend($params->{backend});
113         $params->{cardnumber} = Koha::Patrons->find({
114             borrowernumber => $loggedinuser
115         })->cardnumber;
116         $params->{opac} = 1;
117         my $backend_result = $request->backend_create($params);
118         if ($backend_result->{stage} eq 'copyrightclearance') {
119             $template->param(
120                 stage       => $backend_result->{stage},
121                 whole       => $backend_result
122             );
123         } else {
124             $template->param(
125                 types       => [ "Book", "Article", "Journal" ],
126                 branches    => Koha::Libraries->search->unblessed,
127                 whole       => $backend_result,
128                 request     => $request
129             );
130             if ($backend_result->{stage} eq 'commit') {
131                 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
132                 exit;
133             }
134         }
135
136     }
137 }
138
139 $template->param(
140     message         => $params->{message},
141     illrequestsview => 1,
142     method          => $op
143 );
144
145 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };