Bug 20434: Update UNIMARC framework - auth (FAM)
[koha.git] / ill / ill-requests.pl
blob010c61e618d7f92ee4166436ed1957476d40f901
1 #!/usr/bin/perl
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
10 # version.
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.
20 use Modern::Perl;
22 use CGI;
24 use C4::Auth;
25 use C4::Output;
26 use Koha::AuthorisedValues;
27 use Koha::Illcomment;
28 use Koha::Illrequests;
29 use Koha::Libraries;
30 use Koha::Token;
32 use Try::Tiny;
33 use URI::Escape;
35 our $cgi = CGI->new;
36 my $illRequests = Koha::Illrequests->new;
38 # Grab all passed data
39 # 'our' since Plack changes the scoping
40 # of 'my'
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");
46 exit;
49 my $op = $params->{method} || 'illlist';
51 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
52 template_name => 'ill/ill-requests.tt',
53 query => $cgi,
54 type => 'intranet',
55 flagsrequired => { ill => '*' },
56 } );
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 );
63 $template->param(
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});
73 $template->param(
74 request => $request,
75 csrf_token => Koha::Token->new->generate_csrf({
76 session_id => scalar $cgi->cookie('CGISESSID'),
77 }),
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);
85 $template->param(
86 whole => $backend_result,
87 request => $request
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});
94 my $backend_result;
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) {
99 $template->param(
100 whole => $backend_result,
101 request => $new_request
103 $request = $new_request;
104 } else {
105 # Backend failure, redirect back to illview
106 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
107 . '?method=illview'
108 . '&illrequest_id='
109 . $request->id
110 . '&error=migrate_target' );
111 exit;
114 else {
115 $backend_result = $request->backend_migrate($params);
116 $template->param(
117 whole => $backend_result,
118 request => $request
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);
128 $template->param(
129 whole => $backend_result,
130 request => $request,
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);
141 $template->param(
142 whole => $backend_result,
143 request => $request,
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.
153 # So, init:
154 my $request = Koha::Illrequests->find($params->{illrequest_id});
155 if ( !$params->{stage} ) {
156 my $backend_result = {
157 error => 0,
158 status => '',
159 message => '',
160 method => 'edit_action',
161 stage => 'init',
162 next => '',
163 value => {}
165 $template->param(
166 whole => $backend_result,
167 request => $request
169 } else {
170 # Commit:
171 # Save the changes
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} :
180 "-1";
181 $request->status_alias($alias);
182 $request->store;
183 my $backend_result = {
184 error => 0,
185 status => '',
186 message => '',
187 method => 'edit_action',
188 stage => 'commit',
189 next => 'illlist',
190 value => {}
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.
198 redirect_to_list();
200 } elsif ( $op eq 'delete_confirm') {
201 my $request = Koha::Illrequests->find($params->{illrequest_id});
203 $template->param(
204 request => $request
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.
215 redirect_to_list();
216 } else {
217 print $cgi->redirect(
218 "/cgi-bin/koha/ill/ill-requests.pl?" .
219 "method=delete_confirm&illrequest_id=" .
220 $params->{illrequest_id});
221 exit;
224 } elsif ( $op eq 'mark_completed' ) {
225 my $request = Koha::Illrequests->find($params->{illrequest_id});
226 my $backend_result = $request->mark_completed($params);
227 $template->param(
228 whole => $backend_result,
229 request => $request,
232 # handle special commit rules & update type
233 handle_commit_maybe($backend_result, $request);
235 } elsif ( $op eq 'generic_confirm' ) {
236 my $backend_result;
237 my $request;
238 try {
239 $request = Koha::Illrequests->find($params->{illrequest_id});
240 $params->{current_branchcode} = C4::Context->mybranch;
241 $backend_result = $request->generic_confirm($params);
242 $template->param(
243 whole => $backend_result,
244 request => $request,
246 $template->param( error => $params->{error} )
247 if $params->{error};
249 catch {
250 my $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';
257 else {
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} .
264 "&error=$error" );
265 exit;
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});
283 my @tpl_arr = ();
284 if (keys %{$active_filters}) {
285 foreach my $key (keys %{$active_filters}) {
286 push @tpl_arr, $key . "=" . $active_filters->{$key};
289 $template->param(
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},
302 $comment->store();
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}
307 exit;
309 } else {
310 my $request = Koha::Illrequests->find($params->{illrequest_id});
311 my $backend_result = $request->custom_capability($op, $params);
312 $template->param(
313 whole => $backend_result,
314 request => $request,
317 # handle special commit rules & update type
318 handle_commit_maybe($backend_result, $request);
322 $template->param(
323 backends => $backends,
324 types => [ "Book", "Article", "Journal" ],
325 query_type => $op,
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'
340 . '?method=illview'
341 . '&illrequest_id='
342 . $request->id );
343 exit;
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'
349 . '?method=migrate'
350 . '&stage=emigrate'
351 . '&illrequest_id='
352 . $request->id );
353 exit;
355 else {
356 # Redirect to a requests list view
357 redirect_to_list();
362 sub redirect_to_list {
363 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
364 exit;