Bug 19768: Add "Title notes" tab to OpacSerialDefaultTab preference
[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::ILL::Request::Config;
31 use Koha::ILL::Requests;
32 use Koha::ILL::Request;
33 use Koha::Libraries;
34 use Koha::Patrons;
35 use Koha::ILL::Request::Workflow::Availability;
36 use Koha::ILL::Request::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::ILL::Request::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 my $op = Koha::ILL::Request->get_op_param_deprecation( 'opac', $params );
65
66 my ( $illrequest_id, $request );
67 if ( $illrequest_id = $params->{illrequest_id} ) {
68     $request = Koha::ILL::Requests->find($illrequest_id);
69     # Make sure the request belongs to the logged in user
70     if( !$request || $request->borrowernumber != $loggedinuser ) {
71         print $query->redirect("/cgi-bin/koha/errors/404.pl");
72         exit;
73     }
74 }
75
76 if ( ( $op eq 'cud-create' || $op eq 'cancreq' || $op eq 'cud-update' ) && !$patron->_result->categorycode->can_place_ill_in_opac ) {
77     print $query->redirect('/cgi-bin/koha/errors/403.pl');
78     exit;
79 }
80
81 if ( $op eq 'list' ) {
82
83     my $requests = Koha::ILL::Requests->search(
84         { borrowernumber => $loggedinuser }
85     );
86     $template->param(
87         requests => $requests,
88         backends => $backends
89     );
90
91 } elsif ( $op eq 'view') {
92     $template->param(
93         request => $request
94     );
95
96 } elsif ( $op eq 'cud-update') {
97     $request->notesopac($params->{notesopac})->store;
98     # Send a notice to staff alerting them of the update
99     $request->send_staff_notice('ILL_REQUEST_MODIFIED');
100     print $query->redirect(
101             '/cgi-bin/koha/opac-illrequests.pl?op=view&illrequest_id='
102           . $illrequest_id
103           . '&message=1' );
104     exit;
105 } elsif ( $op eq 'cancreq') {
106     $request->status('CANCREQ')->store;
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 'cud-create' ) {
113     if (!$params->{backend}) {
114         my $req = Koha::ILL::Request->new;
115         $template->param(
116             backends    => $req->available_backends
117         );
118     } else {
119         my $request = Koha::ILL::Request->new
120             ->load_backend($params->{backend});
121
122         # Before request creation operations - Preparation
123         my $availability =
124           Koha::ILL::Request::Workflow::Availability->new( $params, 'opac' );
125         my $type_disclaimer =
126           Koha::ILL::Request::Workflow::TypeDisclaimer->new( $params, 'opac' );
127
128         # ILLCheckAvailability operation
129         if ($availability->show_availability($request)) {
130             $op = 'availability';
131             $template->param(
132                 $availability->availability_template_params($params)
133             );
134             output_html_with_http_headers $query, $cookie,
135                 $template->output, undef,
136                 { force_no_caching => 1 };
137             exit;
138         # ILLModuleDisclaimerByType operation
139         } elsif ( $type_disclaimer->show_type_disclaimer($request)) {
140             $op = 'typedisclaimer';
141             $template->param(
142                 $type_disclaimer->type_disclaimer_template_params($params)
143             );
144             output_html_with_http_headers $query, $cookie,
145                 $template->output, undef,
146                 { force_no_caching => 1 };
147             exit;
148         }
149
150         my $patron = Koha::Patrons->find( { borrowernumber => $loggedinuser } );
151
152         $params->{cardnumber} = $patron->cardnumber;
153         $params->{branchcode} = $patron->branchcode;
154         $params->{opac}       = 1;
155         $params->{lang}       = C4::Languages::getlanguage($query);
156         my $backend_result = $request->backend_create($params);
157
158         if ($backend_result->{stage} eq 'copyrightclearance') {
159             $template->param(
160                 stage       => $backend_result->{stage},
161                 whole       => $backend_result
162             );
163         } else {
164             $template->param(
165                 types       => [ "Book", "Article", "Journal" ],
166                 branches    => Koha::Libraries->search->unblessed,
167                 whole       => $backend_result,
168                 request     => $request
169             );
170             if ($backend_result->{stage} eq 'commit') {
171                 # After creation actions
172                 if ( $params->{type_disclaimer_submitted} ) {
173                     $type_disclaimer->after_request_created( $params, $request );
174                 }
175                 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
176                 exit;
177             }
178         }
179
180     }
181 }
182
183 $template->param(
184     can_place_ill_in_opac => $patron->_result->categorycode->can_place_ill_in_opac,
185     message               => $params->{message},
186     illrequestsview       => 1,
187     op                    => $op
188 );
189
190 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };