]> git.koha-community.org Git - koha.git/blob - opac/opac-illrequests.pl
Bug 34478: ILL INTRA preparation: Move function to ILLrequest.pm
[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 use POSIX qw( strftime );
29
30 use Koha::Illrequest::Config;
31 use Koha::Illrequests;
32 use Koha::Illrequest;
33 use Koha::Libraries;
34 use Koha::Patrons;
35 use Koha::Illrequest::Workflow::Availability;
36 use Koha::Illrequest::Workflow::TypeDisclaimer;
37
38 my $query = CGI->new;
39
40 # Grab all passed data
41 # 'our' since Plack changes the scoping
42 # of 'my'
43 our $params = $query->Vars();
44
45 # if illrequests is disabled, leave immediately
46 if ( ! C4::Context->preference('ILLModule') ) {
47     print $query->redirect("/cgi-bin/koha/errors/404.pl");
48     exit;
49 }
50
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
52     template_name   => "opac-illrequests.tt",
53     query           => $query,
54     type            => "opac",
55 });
56
57 # Are we able to actually work?
58 my $reduced  = C4::Context->preference('ILLOpacbackends');
59 my $backends = Koha::Illrequest::Config->new->available_backends($reduced);
60 my $backends_available = ( scalar @{$backends} > 0 );
61 $template->param( backends_available => $backends_available );
62 my $patron = Koha::Patrons->find($loggedinuser);
63
64 Koha::Illrequest->check_url_param_deprecation($params);
65
66 my $op = $params->{'op'} // $params->{'method'} // 'list';
67 $op = 'cud-create' if $op eq 'create' || $op eq 'add_form';
68
69 my ( $illrequest_id, $request );
70 if ( $illrequest_id = $params->{illrequest_id} ) {
71     $request = Koha::Illrequests->find($illrequest_id);
72     # Make sure the request belongs to the logged in user
73     if( !$request || $request->borrowernumber != $loggedinuser ) {
74         print $query->redirect("/cgi-bin/koha/errors/404.pl");
75         exit;
76     }
77 }
78
79 if ( ( $op eq 'cud-create' || $op eq 'cancreq' || $op eq 'cud-update' ) && !$patron->_result->categorycode->can_place_ill_in_opac ) {
80     print $query->redirect('/cgi-bin/koha/errors/403.pl');
81     exit;
82 }
83
84 if ( $op eq 'list' ) {
85
86     my $requests = Koha::Illrequests->search(
87         { borrowernumber => $loggedinuser }
88     );
89     $template->param(
90         requests => $requests,
91         backends => $backends
92     );
93
94 } elsif ( $op eq 'view') {
95     $template->param(
96         request => $request
97     );
98
99 } elsif ( $op eq 'cud-update') {
100     $request->notesopac($params->{notesopac})->store;
101     # Send a notice to staff alerting them of the update
102     $request->send_staff_notice('ILL_REQUEST_MODIFIED');
103     print $query->redirect(
104             '/cgi-bin/koha/opac-illrequests.pl?op=view&illrequest_id='
105           . $illrequest_id
106           . '&message=1' );
107     exit;
108 } elsif ( $op eq 'cancreq') {
109     $request->status('CANCREQ')->store;
110     print $query->redirect(
111             '/cgi-bin/koha/opac-illrequests.pl?op=view&illrequest_id='
112           . $illrequest_id
113           . '&message=1' );
114     exit;
115 } elsif ( $op eq 'cud-create' ) {
116     if (!$params->{backend}) {
117         my $req = Koha::Illrequest->new;
118         $template->param(
119             backends    => $req->available_backends
120         );
121     } else {
122         my $request = Koha::Illrequest->new
123             ->load_backend($params->{backend});
124
125         # Before request creation operations - Preparation
126         my $availability =
127           Koha::Illrequest::Workflow::Availability->new( $params, 'opac' );
128         my $type_disclaimer =
129           Koha::Illrequest::Workflow::TypeDisclaimer->new( $params, 'opac' );
130
131         # ILLCheckAvailability operation
132         if ($availability->show_availability($request)) {
133             $op = 'availability';
134             $template->param(
135                 $availability->availability_template_params($params)
136             );
137             output_html_with_http_headers $query, $cookie,
138                 $template->output, undef,
139                 { force_no_caching => 1 };
140             exit;
141         # ILLModuleDisclaimerByType operation
142         } elsif ( $type_disclaimer->show_type_disclaimer($request)) {
143             $op = 'typedisclaimer';
144             $template->param(
145                 $type_disclaimer->type_disclaimer_template_params($params)
146             );
147             output_html_with_http_headers $query, $cookie,
148                 $template->output, undef,
149                 { force_no_caching => 1 };
150             exit;
151         }
152
153         $params->{cardnumber} = $patron->cardnumber;
154         $params->{opac} = 1;
155         my $backend_result = $request->backend_create($params);
156
157         # After creation actions
158         if ( $params->{type_disclaimer_submitted} ) {
159             $type_disclaimer->after_request_created( $params, $request );
160             print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
161             exit;
162         }
163
164         if ($backend_result->{stage} eq 'copyrightclearance') {
165             $template->param(
166                 stage       => $backend_result->{stage},
167                 whole       => $backend_result
168             );
169         } else {
170             $template->param(
171                 types       => [ "Book", "Article", "Journal" ],
172                 branches    => Koha::Libraries->search->unblessed,
173                 whole       => $backend_result,
174                 request     => $request
175             );
176             if ($backend_result->{stage} eq 'commit') {
177                 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
178                 exit;
179             }
180         }
181
182     }
183 }
184
185 $template->param(
186     can_place_ill_in_opac => $patron->_result->categorycode->can_place_ill_in_opac,
187     message               => $params->{message},
188     illrequestsview       => 1,
189     op                    => $op
190 );
191
192 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };