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
;
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 'illlist') {
272 # If we receive a pre-filter, make it available to the template
273 my $possible_filters = ['borrowernumber'];
274 my $active_filters = {};
275 foreach my $filter(@
{$possible_filters}) {
276 if ($params->{$filter}) {
277 # We shouldn't need to escape $filter here since we're using
278 # a whitelist, but just to be sure...
279 $active_filters->{uri_escape_utf8
($filter)} =
280 uri_escape_utf8
(scalar $params->{$filter});
284 if (keys %{$active_filters}) {
285 foreach my $key (keys %{$active_filters}) {
286 push @tpl_arr, $key . "=" . $active_filters->{$key};
290 prefilters
=> join("&", @tpl_arr)
292 } elsif ( $op eq "save_comment" ) {
293 die "Wrong CSRF token" unless Koha
::Token
->new->check_csrf({
294 session_id
=> scalar $cgi->cookie('CGISESSID'),
295 token
=> scalar $cgi->param('csrf_token'),
297 my $comment = Koha
::Illcomment
->new({
298 illrequest_id
=> scalar $params->{illrequest_id
},
299 borrowernumber
=> $patronnumber,
300 comment
=> scalar $params->{comment
},
303 # Redirect to view the whole request
304 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
305 scalar $params->{illrequest_id
}
310 my $request = Koha
::Illrequests
->find($params->{illrequest_id
});
311 my $backend_result = $request->custom_capability($op, $params);
313 whole
=> $backend_result,
317 # handle special commit rules & update type
318 handle_commit_maybe
($backend_result, $request);
323 backends
=> $backends,
324 types
=> [ "Book", "Article", "Journal" ],
326 branches
=> scalar Koha
::Libraries
->search,
329 output_html_with_http_headers
( $cgi, $cookie, $template->output );
331 sub handle_commit_maybe
{
332 my ( $backend_result, $request ) = @_;
334 # We need to special case 'commit'
335 if ( $backend_result->{stage
} eq 'commit' ) {
336 if ( $backend_result->{next} eq 'illview' ) {
338 # Redirect to a view of the newly created request
339 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
345 elsif ( $backend_result->{next} eq 'emigrate' ) {
347 # Redirect to a view of the newly created request
348 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
356 # Redirect to a requests list view
362 sub redirect_to_list
{
363 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');