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->notesopac($params->{notesopac
});
132 $request->notesstaff($params->{notesstaff
});
134 my $backend_result = {
138 method
=> 'edit_action',
143 handle_commit_maybe
($backend_result, $request);
146 } elsif ( $op eq 'moderate_action' ) {
147 # Moderate action is required for an ILL submodule / syspref.
148 # Currently still needs to be implemented.
151 } elsif ( $op eq 'delete_confirm') {
152 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
158 } elsif ( $op eq 'delete' ) {
160 # Check if the request is confirmed, if not, redirect
161 # to the confirmation view
162 if ($params->{confirmed
}) {
163 # We simply delete the request...
164 Koha
::Illrequests
->find( $params->{illrequest_id
} )->delete;
165 # ... then return to list view.
168 print $cgi->redirect(
169 "/cgi-bin/koha/ill/ill-requests.pl?" .
170 "method=delete_confirm&illrequest_id=" .
171 $params->{illrequest_id
});
175 } elsif ( $op eq 'mark_completed' ) {
176 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
177 my $backend_result = $request->mark_completed($params);
179 whole
=> $backend_result,
183 # handle special commit rules & update type
184 handle_commit_maybe
($backend_result, $request);
186 } elsif ( $op eq 'generic_confirm' ) {
188 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
189 $params->{current_branchcode
} = C4
::Context
->mybranch;
190 my $backend_result = $request->generic_confirm($params);
192 whole
=> $backend_result,
195 $template->param( error
=> $params->{error
} )
198 # handle special commit rules & update type
199 handle_commit_maybe
($backend_result, $request);
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
} .
219 } elsif ( $op eq 'illlist') {
221 # If we receive a pre-filter, make it available to the template
222 my $possible_filters = ['borrowernumber'];
223 my $active_filters = [];
224 foreach my $filter(@
{$possible_filters}) {
225 if ($params->{$filter}) {
226 push @
{$active_filters},
227 { name
=> $filter, value
=> $params->{$filter}};
230 if (scalar @
{$active_filters} > 0) {
232 prefilters
=> $active_filters
236 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
237 my $backend_result = $request->custom_capability($op, $params);
239 whole
=> $backend_result,
243 # handle special commit rules & update type
244 handle_commit_maybe
($backend_result, $request);
249 backends
=> $backends,
250 media
=> [ "Book", "Article", "Journal" ],
252 branches
=> scalar Koha
::Libraries
->search,
255 output_html_with_http_headers
( $cgi, $cookie, $template->output );
257 sub handle_commit_maybe
{
258 my ( $backend_result, $request ) = @_;
259 # We need to special case 'commit'
260 if ( $backend_result->{stage
} eq 'commit' ) {
261 if ( $backend_result->{next} eq 'illview' ) {
262 # Redirect to a view of the newly created request
263 print $cgi->redirect(
264 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
268 # Redirect to a requests list view
274 sub redirect_to_list
{
275 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');