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