Bug 35295: Move JS asset in returns.tt
[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 JSON qw( encode_json );
23
24 use CGI qw ( -utf8 );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Koha;
27 use C4::Output qw( output_html_with_http_headers );
28
29 use Koha::Illrequest::Config;
30 use Koha::Illrequests;
31 use Koha::Libraries;
32 use Koha::Patrons;
33 use Koha::Illrequest::Availability;
34
35 my $query = CGI->new;
36
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
40 our $params = $query->Vars();
41
42 # if illrequests is disabled, leave immediately
43 if ( ! C4::Context->preference('ILLModule') ) {
44     print $query->redirect("/cgi-bin/koha/errors/404.pl");
45     exit;
46 }
47
48 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
49     template_name   => "opac-illrequests.tt",
50     query           => $query,
51     type            => "opac",
52 });
53
54 # Are we able to actually work?
55 my $reduced  = C4::Context->preference('ILLOpacbackends');
56 my $backends = Koha::Illrequest::Config->new->available_backends($reduced);
57 my $backends_available = ( scalar @{$backends} > 0 );
58 $template->param( backends_available => $backends_available );
59
60 my $op = $params->{'method'} || 'list';
61
62 my ( $illrequest_id, $request );
63 if ( $illrequest_id = $params->{illrequest_id} ) {
64     $request = Koha::Illrequests->find($illrequest_id);
65     # Make sure the request belongs to the logged in user
66     if( !$request || $request->borrowernumber != $loggedinuser ) {
67         print $query->redirect("/cgi-bin/koha/errors/404.pl");
68         exit;
69     }
70 }
71
72 if ( $op eq 'list' ) {
73
74     my $requests = Koha::Illrequests->search(
75         { borrowernumber => $loggedinuser }
76     );
77     $template->param(
78         requests => $requests,
79         backends => $backends
80     );
81
82 } elsif ( $op eq 'view') {
83     $template->param(
84         request => $request
85     );
86
87 } elsif ( $op eq 'update') {
88     $request->notesopac($params->{notesopac})->store;
89     # Send a notice to staff alerting them of the update
90     $request->send_staff_notice('ILL_REQUEST_MODIFIED');
91     print $query->redirect(
92             '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id='
93           . $illrequest_id
94           . '&message=1' );
95     exit;
96 } elsif ( $op eq 'cancreq') {
97     $request->status('CANCREQ')->store;
98     print $query->redirect(
99             '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id='
100           . $illrequest_id
101           . '&message=1' );
102     exit;
103 } elsif ( $op eq 'create' ) {
104     if (!$params->{backend}) {
105         my $req = Koha::Illrequest->new;
106         $template->param(
107             backends    => $req->available_backends
108         );
109     } else {
110         my $request = Koha::Illrequest->new
111             ->load_backend($params->{backend});
112
113         # Does this backend enable us to insert an availability stage and should
114         # we? If not, proceed as normal.
115         if (
116             C4::Context->preference("ILLCheckAvailability") &&
117             $request->_backend_capability(
118                 'should_display_availability',
119                 $params
120             ) &&
121             # If the user has elected to continue with the request despite
122             # having viewed availability info, this flag will be set
123             !$params->{checked_availability}
124         ) {
125             # Establish which of the installed availability providers
126             # can service our metadata, if so, jump in
127             my $availability = Koha::Illrequest::Availability->new($params);
128             my $services = $availability->get_services({
129                 ui_context => 'opac'
130             });
131             if (scalar @{$services} > 0) {
132                 # Modify our method so we use the correct part of the
133                 # template
134                 $op = 'availability';
135                 # Prepare the metadata we're sending them
136                 my $metadata = $availability->prep_metadata($params);
137                 $template->param(
138                     metadata        => $metadata,
139                     services_json   => encode_json($services),
140                     services        => $services,
141                     illrequestsview => 1,
142                     message         => $params->{message},
143                     method          => $op,
144                     whole           => $params
145                 );
146                 output_html_with_http_headers $query, $cookie,
147                     $template->output, undef, { force_no_caching => 1 };
148                 exit;
149             }
150         }
151
152         $params->{cardnumber} = Koha::Patrons->find({
153             borrowernumber => $loggedinuser
154         })->cardnumber;
155         $params->{opac} = 1;
156         my $backend_result = $request->backend_create($params);
157         if ($backend_result->{stage} eq 'copyrightclearance') {
158             $template->param(
159                 stage       => $backend_result->{stage},
160                 whole       => $backend_result
161             );
162         } else {
163             $template->param(
164                 types       => [ "Book", "Article", "Journal" ],
165                 branches    => Koha::Libraries->search->unblessed,
166                 whole       => $backend_result,
167                 request     => $request
168             );
169             if ($backend_result->{stage} eq 'commit') {
170                 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
171                 exit;
172             }
173         }
174
175     }
176 }
177
178 $template->param(
179     message         => $params->{message},
180     illrequestsview => 1,
181     method          => $op
182 );
183
184 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };