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