3 # Copyright 2017 PTFS-Europe Ltd
5 # This file is part of Koha.
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.
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.
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>.
22 use JSON qw( encode_json );
25 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use POSIX qw( strftime );
30 use Koha::Illrequest::Config;
31 use Koha::Illrequests;
34 use Koha::Illrequest::Workflow::Availability;
35 use Koha::Illrequest::Workflow::TypeDisclaimer;
39 # Grab all passed data
40 # 'our' since Plack changes the scoping
42 our $params = $query->Vars();
44 # if illrequests is disabled, leave immediately
45 if ( ! C4::Context->preference('ILLModule') ) {
46 print $query->redirect("/cgi-bin/koha/errors/404.pl");
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
51 template_name => "opac-illrequests.tt",
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 );
62 my $op = $params->{'method'} || 'list';
64 my ( $illrequest_id, $request );
65 if ( $illrequest_id = $params->{illrequest_id} ) {
66 $request = Koha::Illrequests->find($illrequest_id);
67 # Make sure the request belongs to the logged in user
68 if( !$request || $request->borrowernumber != $loggedinuser ) {
69 print $query->redirect("/cgi-bin/koha/errors/404.pl");
74 if ( $op eq 'list' ) {
76 my $requests = Koha::Illrequests->search(
77 { borrowernumber => $loggedinuser }
80 requests => $requests,
84 } elsif ( $op eq 'view') {
89 } elsif ( $op eq 'update') {
90 $request->notesopac($params->{notesopac})->store;
91 # Send a notice to staff alerting them of the update
92 $request->send_staff_notice('ILL_REQUEST_MODIFIED');
93 print $query->redirect(
94 '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id='
98 } elsif ( $op eq 'cancreq') {
99 $request->status('CANCREQ')->store;
100 print $query->redirect(
101 '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id='
105 } elsif ( $op eq 'create' ) {
106 if (!$params->{backend}) {
107 my $req = Koha::Illrequest->new;
109 backends => $req->available_backends
112 my $request = Koha::Illrequest->new
113 ->load_backend($params->{backend});
115 # Before request creation operations - Preparation
117 Koha::Illrequest::Workflow::Availability->new( $params, 'opac' );
118 my $type_disclaimer =
119 Koha::Illrequest::Workflow::TypeDisclaimer->new( $params, 'opac' );
121 # ILLCheckAvailability operation
122 if ($availability->show_availability($request)) {
123 $op = 'availability';
125 $availability->availability_template_params($params)
127 output_html_with_http_headers $query, $cookie,
128 $template->output, undef,
129 { force_no_caching => 1 };
131 # ILLModuleDisclaimerByType operation
132 } elsif ( $type_disclaimer->show_type_disclaimer($request)) {
133 $op = 'typedisclaimer';
135 $type_disclaimer->type_disclaimer_template_params($params)
137 output_html_with_http_headers $query, $cookie,
138 $template->output, undef,
139 { force_no_caching => 1 };
143 $params->{cardnumber} = Koha::Patrons->find({
144 borrowernumber => $loggedinuser
147 my $backend_result = $request->backend_create($params);
149 # After creation actions
150 if ( $params->{type_disclaimer_submitted} ) {
151 $type_disclaimer->after_request_created( $params, $request );
152 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
156 if ($backend_result->{stage} eq 'copyrightclearance') {
158 stage => $backend_result->{stage},
159 whole => $backend_result
163 types => [ "Book", "Article", "Journal" ],
164 branches => Koha::Libraries->search->unblessed,
165 whole => $backend_result,
168 if ($backend_result->{stage} eq 'commit') {
169 print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
178 message => $params->{message},
179 illrequestsview => 1,
183 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };