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
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>.
26 use Koha
::AuthorisedValues
;
28 use Koha
::Illrequests
;
36 my $illRequests = Koha
::Illrequests
->new;
38 # Grab all passed data
39 # 'our' since Plack changes the scoping
41 our $params = $cgi->Vars();
43 # Leave immediately if ILLModule is disabled
44 unless ( C4
::Context
->preference('ILLModule') ) {
45 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
49 my $op = $params->{method
} || 'illlist';
51 my ( $template, $patronnumber, $cookie ) = get_template_and_user
( {
52 template_name
=> 'ill/ill-requests.tt',
55 flagsrequired
=> { ill
=> '*' },
58 # Are we able to actually work?
59 my $cfg = Koha
::Illrequest
::Config
->new;
60 my $backends = $cfg->available_backends;
61 my $has_branch = $cfg->has_branch;
62 my $backends_available = ( scalar @
{$backends} > 0 );
64 backends_available
=> $backends_available,
65 has_branch
=> $has_branch
68 if ( $backends_available ) {
69 if ( $op eq 'illview' ) {
70 # View the details of an ILL
71 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
75 csrf_token
=> Koha
::Token
->new->generate_csrf({
76 session_id
=> scalar $cgi->cookie('CGISESSID'),
78 ( $params->{error
} ?
( error
=> $params->{error
} ) : () ),
81 } elsif ( $op eq 'create' ) {
82 # We're in the process of creating a request
83 my $request = Koha
::Illrequest
->new->load_backend( $params->{backend
} );
84 my $backend_result = $request->backend_create($params);
86 whole
=> $backend_result,
89 handle_commit_maybe
($backend_result, $request);
91 } elsif ( $op eq 'migrate' ) {
92 # We're in the process of migrating a request
93 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
95 if ( $params->{backend
} ) {
96 my $new_request = Koha
::Illrequest
->new->load_backend( $params->{backend
} );
97 $backend_result = $new_request->backend_migrate($params);
98 if ($backend_result) {
100 whole
=> $backend_result,
101 request
=> $new_request
103 $request = $new_request;
105 # Backend failure, redirect back to illview
106 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
110 . '&error=migrate_target' );
115 $backend_result = $request->backend_migrate($params);
117 whole
=> $backend_result,
121 handle_commit_maybe
( $backend_result, $request );
123 } elsif ( $op eq 'confirm' ) {
124 # Backend 'confirm' method
125 # confirm requires a specific request, so first, find it.
126 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
127 my $backend_result = $request->backend_confirm($params);
129 whole
=> $backend_result,
133 # handle special commit rules & update type
134 handle_commit_maybe
($backend_result, $request);
136 } elsif ( $op eq 'cancel' ) {
137 # Backend 'cancel' method
138 # cancel requires a specific request, so first, find it.
139 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
140 my $backend_result = $request->backend_cancel($params);
142 whole
=> $backend_result,
146 # handle special commit rules & update type
147 handle_commit_maybe
($backend_result, $request);
149 } elsif ( $op eq 'edit_action' ) {
150 # Handle edits to the Illrequest object.
151 # (not the Illrequestattributes)
152 # We simulate the API for backend requests for uniformity.
154 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
155 if ( !$params->{stage
} ) {
156 my $backend_result = {
160 method
=> 'edit_action',
166 whole
=> $backend_result,
172 $request->borrowernumber($params->{borrowernumber
});
173 $request->biblio_id($params->{biblio_id
});
174 $request->branchcode($params->{branchcode
});
175 $request->price_paid($params->{price_paid
});
176 $request->notesopac($params->{notesopac
});
177 $request->notesstaff($params->{notesstaff
});
178 my $alias = (length $params->{status_alias
} > 0) ?
179 $params->{status_alias
} :
181 $request->status_alias($alias);
183 my $backend_result = {
187 method
=> 'edit_action',
192 handle_commit_maybe
($backend_result, $request);
195 } elsif ( $op eq 'moderate_action' ) {
196 # Moderate action is required for an ILL submodule / syspref.
197 # Currently still needs to be implemented.
200 } elsif ( $op eq 'delete_confirm') {
201 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
207 } elsif ( $op eq 'delete' ) {
209 # Check if the request is confirmed, if not, redirect
210 # to the confirmation view
211 if ($params->{confirmed
}) {
212 # We simply delete the request...
213 Koha
::Illrequests
->find( $params->{illrequest_id
} )->delete;
214 # ... then return to list view.
217 print $cgi->redirect(
218 "/cgi-bin/koha/ill/ill-requests.pl?" .
219 "method=delete_confirm&illrequest_id=" .
220 $params->{illrequest_id
});
224 } elsif ( $op eq 'mark_completed' ) {
225 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
226 my $backend_result = $request->mark_completed($params);
228 whole
=> $backend_result,
232 # handle special commit rules & update type
233 handle_commit_maybe
($backend_result, $request);
235 } elsif ( $op eq 'generic_confirm' ) {
239 $request = Koha
::Illrequests
->find($params->{illrequest_id
});
240 $params->{current_branchcode
} = C4
::Context
->mybranch;
241 $backend_result = $request->generic_confirm($params);
243 whole
=> $backend_result,
246 $template->param( error
=> $params->{error
} )
251 if ( ref($_) eq 'Koha::Exceptions::Ill::NoTargetEmail' ) {
252 $error = 'no_target_email';
254 elsif ( ref($_) eq 'Koha::Exceptions::Ill::NoLibraryEmail' ) {
255 $error = 'no_library_email';
258 $error = 'unknown_error';
260 print $cgi->redirect(
261 "/cgi-bin/koha/ill/ill-requests.pl?" .
262 "method=generic_confirm&illrequest_id=" .
263 $params->{illrequest_id
} .
268 # handle special commit rules & update type
269 handle_commit_maybe
($backend_result, $request);
270 } elsif ( $op eq 'check_out') {
271 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
272 my $backend_result = $request->check_out($params);
275 whole
=> $backend_result,
278 } elsif ( $op eq 'illlist') {
280 # If we receive a pre-filter, make it available to the template
281 my $possible_filters = ['borrowernumber'];
282 my $active_filters = {};
283 foreach my $filter(@
{$possible_filters}) {
284 if ($params->{$filter}) {
285 # We shouldn't need to escape $filter here since we're using
286 # a whitelist, but just to be sure...
287 $active_filters->{uri_escape_utf8
($filter)} =
288 uri_escape_utf8
(scalar $params->{$filter});
292 if (keys %{$active_filters}) {
293 foreach my $key (keys %{$active_filters}) {
294 push @tpl_arr, $key . "=" . $active_filters->{$key};
298 prefilters
=> join("&", @tpl_arr)
300 } elsif ( $op eq "save_comment" ) {
301 die "Wrong CSRF token" unless Koha
::Token
->new->check_csrf({
302 session_id
=> scalar $cgi->cookie('CGISESSID'),
303 token
=> scalar $cgi->param('csrf_token'),
305 my $comment = Koha
::Illcomment
->new({
306 illrequest_id
=> scalar $params->{illrequest_id
},
307 borrowernumber
=> $patronnumber,
308 comment
=> scalar $params->{comment
},
311 # Redirect to view the whole request
312 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
313 scalar $params->{illrequest_id
}
318 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
319 my $backend_result = $request->custom_capability($op, $params);
321 whole
=> $backend_result,
325 # handle special commit rules & update type
326 handle_commit_maybe
($backend_result, $request);
331 backends
=> $backends,
332 types
=> [ "Book", "Article", "Journal" ],
334 branches
=> scalar Koha
::Libraries
->search,
337 output_html_with_http_headers
( $cgi, $cookie, $template->output );
339 sub handle_commit_maybe
{
340 my ( $backend_result, $request ) = @_;
342 # We need to special case 'commit'
343 if ( $backend_result->{stage
} eq 'commit' ) {
344 if ( $backend_result->{next} eq 'illview' ) {
346 # Redirect to a view of the newly created request
347 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
353 elsif ( $backend_result->{next} eq 'emigrate' ) {
355 # Redirect to a view of the newly created request
356 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
364 # Redirect to a requests list view
370 sub redirect_to_list
{
371 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');