3 # Copyright 2013 PTFS-Europe Ltd and Mark Gavillet
4 # Copyright 2014 PTFS-Europe Ltd
6 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use Koha::AuthorisedValues;
27 use Koha::Illrequests;
33 my $illRequests = Koha::Illrequests->new;
35 # Grab all passed data
36 # 'our' since Plack changes the scoping
38 our $params = $cgi->Vars();
40 # Leave immediately if ILLModule is disabled
41 unless ( C4::Context->preference('ILLModule') ) {
42 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
46 my $op = $params->{method} || 'illlist';
48 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
49 template_name => 'ill/ill-requests.tt',
52 flagsrequired => { ill => '*' },
55 # Are we able to actually work?
56 my $backends = Koha::Illrequest::Config->new->available_backends;
57 my $backends_available = ( scalar @{$backends} > 0 );
58 $template->param( backends_available => $backends_available );
60 if ( $backends_available ) {
61 if ( $op eq 'illview' ) {
62 # View the details of an ILL
63 my $request = Koha::Illrequests->find($params->{illrequest_id});
69 } elsif ( $op eq 'create' ) {
70 # We're in the process of creating a request
71 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
72 my $backend_result = $request->backend_create($params);
74 whole => $backend_result,
77 handle_commit_maybe($backend_result, $request);
79 } elsif ( $op eq 'confirm' ) {
80 # Backend 'confirm' method
81 # confirm requires a specific request, so first, find it.
82 my $request = Koha::Illrequests->find($params->{illrequest_id});
83 my $backend_result = $request->backend_confirm($params);
85 whole => $backend_result,
89 # handle special commit rules & update type
90 handle_commit_maybe($backend_result, $request);
92 } elsif ( $op eq 'cancel' ) {
93 # Backend 'cancel' method
94 # cancel requires a specific request, so first, find it.
95 my $request = Koha::Illrequests->find($params->{illrequest_id});
96 my $backend_result = $request->backend_cancel($params);
98 whole => $backend_result,
102 # handle special commit rules & update type
103 handle_commit_maybe($backend_result, $request);
105 } elsif ( $op eq 'edit_action' ) {
106 # Handle edits to the Illrequest object.
107 # (not the Illrequestattributes)
108 # We simulate the API for backend requests for uniformity.
110 my $request = Koha::Illrequests->find($params->{illrequest_id});
111 if ( !$params->{stage} ) {
112 my $backend_result = {
116 method => 'edit_action',
122 whole => $backend_result,
128 $request->borrowernumber($params->{borrowernumber});
129 $request->biblio_id($params->{biblio_id});
130 $request->branchcode($params->{branchcode});
131 $request->price_paid($params->{price_paid});
132 $request->notesopac($params->{notesopac});
133 $request->notesstaff($params->{notesstaff});
135 my $backend_result = {
139 method => 'edit_action',
144 handle_commit_maybe($backend_result, $request);
147 } elsif ( $op eq 'moderate_action' ) {
148 # Moderate action is required for an ILL submodule / syspref.
149 # Currently still needs to be implemented.
152 } elsif ( $op eq 'delete_confirm') {
153 my $request = Koha::Illrequests->find($params->{illrequest_id});
159 } elsif ( $op eq 'delete' ) {
161 # Check if the request is confirmed, if not, redirect
162 # to the confirmation view
163 if ($params->{confirmed}) {
164 # We simply delete the request...
165 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
166 # ... then return to list view.
169 print $cgi->redirect(
170 "/cgi-bin/koha/ill/ill-requests.pl?" .
171 "method=delete_confirm&illrequest_id=" .
172 $params->{illrequest_id});
176 } elsif ( $op eq 'mark_completed' ) {
177 my $request = Koha::Illrequests->find($params->{illrequest_id});
178 my $backend_result = $request->mark_completed($params);
180 whole => $backend_result,
184 # handle special commit rules & update type
185 handle_commit_maybe($backend_result, $request);
187 } elsif ( $op eq 'generic_confirm' ) {
191 $request = Koha::Illrequests->find($params->{illrequest_id});
192 $params->{current_branchcode} = C4::Context->mybranch;
193 $backend_result = $request->generic_confirm($params);
195 whole => $backend_result,
198 $template->param( error => $params->{error} )
203 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
204 $error = 'no_target_email';
206 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
207 $error = 'no_library_email';
210 $error = 'unknown_error';
212 print $cgi->redirect(
213 "/cgi-bin/koha/ill/ill-requests.pl?" .
214 "method=generic_confirm&illrequest_id=" .
215 $params->{illrequest_id} .
220 # handle special commit rules & update type
221 handle_commit_maybe($backend_result, $request);
222 } elsif ( $op eq 'illlist') {
224 # If we receive a pre-filter, make it available to the template
225 my $possible_filters = ['borrowernumber'];
226 my $active_filters = [];
227 foreach my $filter(@{$possible_filters}) {
228 if ($params->{$filter}) {
229 push @{$active_filters},
230 { name => $filter, value => $params->{$filter}};
233 if (scalar @{$active_filters} > 0) {
235 prefilters => $active_filters
239 my $request = Koha::Illrequests->find($params->{illrequest_id});
240 my $backend_result = $request->custom_capability($op, $params);
242 whole => $backend_result,
246 # handle special commit rules & update type
247 handle_commit_maybe($backend_result, $request);
252 backends => $backends,
253 media => [ "Book", "Article", "Journal" ],
255 branches => scalar Koha::Libraries->search,
258 output_html_with_http_headers( $cgi, $cookie, $template->output );
260 sub handle_commit_maybe {
261 my ( $backend_result, $request ) = @_;
262 # We need to special case 'commit'
263 if ( $backend_result->{stage} eq 'commit' ) {
264 if ( $backend_result->{next} eq 'illview' ) {
265 # Redirect to a view of the newly created request
266 print $cgi->redirect(
267 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
272 # Redirect to a requests list view
278 sub redirect_to_list {
279 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');