Bug 15774: Show additional field values in edit form
[koha.git] / ill / ill-requests.pl
blobd6be5c89b2625ff1ab02064ce81a01753c7e8773
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;
34 our $cgi = CGI->new;
35 my $illRequests = Koha::Illrequests->new;
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
40 our $params = $cgi->Vars();
42 # Leave immediately if ILLModule is disabled
43 unless ( C4::Context->preference('ILLModule') ) {
44 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
45 exit;
48 my $op = $params->{method} || 'illlist';
50 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
51 template_name => 'ill/ill-requests.tt',
52 query => $cgi,
53 type => 'intranet',
54 flagsrequired => { ill => '*' },
55 } );
57 # Are we able to actually work?
58 my $cfg = Koha::Illrequest::Config->new;
59 my $backends = $cfg->available_backends;
60 my $has_branch = $cfg->has_branch;
61 my $backends_available = ( scalar @{$backends} > 0 );
62 $template->param(
63 backends_available => $backends_available,
64 has_branch => $has_branch
67 if ( $backends_available ) {
68 if ( $op eq 'illview' ) {
69 # View the details of an ILL
70 my $request = Koha::Illrequests->find($params->{illrequest_id});
72 $template->param(
73 request => $request,
74 csrf_token => Koha::Token->new->generate_csrf({
75 session_id => scalar $cgi->cookie('CGISESSID'),
76 }),
77 ( $params->{error} ? ( error => $params->{error} ) : () ),
80 } elsif ( $op eq 'create' ) {
81 # We're in the process of creating a request
82 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
83 my $backend_result = $request->backend_create($params);
84 $template->param(
85 whole => $backend_result,
86 request => $request
88 handle_commit_maybe($backend_result, $request);
90 } elsif ( $op eq 'migrate' ) {
91 # We're in the process of migrating a request
92 my $request = Koha::Illrequests->find($params->{illrequest_id});
93 my $backend_result;
94 if ( $params->{backend} ) {
95 my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
96 $backend_result = $new_request->backend_migrate($params);
97 if ($backend_result) {
98 $template->param(
99 whole => $backend_result,
100 request => $new_request
102 $request = $new_request;
103 } else {
104 # Backend failure, redirect back to illview
105 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
106 . '?method=illview'
107 . '&illrequest_id='
108 . $request->id
109 . '&error=migrate_target' );
110 exit;
113 else {
114 $backend_result = $request->backend_migrate($params);
115 $template->param(
116 whole => $backend_result,
117 request => $request
120 handle_commit_maybe( $backend_result, $request );
122 } elsif ( $op eq 'confirm' ) {
123 # Backend 'confirm' method
124 # confirm requires a specific request, so first, find it.
125 my $request = Koha::Illrequests->find($params->{illrequest_id});
126 my $backend_result = $request->backend_confirm($params);
127 $template->param(
128 whole => $backend_result,
129 request => $request,
132 # handle special commit rules & update type
133 handle_commit_maybe($backend_result, $request);
135 } elsif ( $op eq 'cancel' ) {
136 # Backend 'cancel' method
137 # cancel requires a specific request, so first, find it.
138 my $request = Koha::Illrequests->find($params->{illrequest_id});
139 my $backend_result = $request->backend_cancel($params);
140 $template->param(
141 whole => $backend_result,
142 request => $request,
145 # handle special commit rules & update type
146 handle_commit_maybe($backend_result, $request);
148 } elsif ( $op eq 'edit_action' ) {
149 # Handle edits to the Illrequest object.
150 # (not the Illrequestattributes)
151 # We simulate the API for backend requests for uniformity.
152 # So, init:
153 my $request = Koha::Illrequests->find($params->{illrequest_id});
154 if ( !$params->{stage} ) {
155 my $backend_result = {
156 error => 0,
157 status => '',
158 message => '',
159 method => 'edit_action',
160 stage => 'init',
161 next => '',
162 value => {}
164 $template->param(
165 whole => $backend_result,
166 request => $request
168 } else {
169 # Commit:
170 # Save the changes
171 $request->borrowernumber($params->{borrowernumber});
172 $request->biblio_id($params->{biblio_id});
173 $request->branchcode($params->{branchcode});
174 $request->price_paid($params->{price_paid});
175 $request->notesopac($params->{notesopac});
176 $request->notesstaff($params->{notesstaff});
177 my $alias = (length $params->{status_alias} > 0) ?
178 $params->{status_alias} :
179 "-1";
180 $request->status_alias($alias);
181 $request->store;
182 my $backend_result = {
183 error => 0,
184 status => '',
185 message => '',
186 method => 'edit_action',
187 stage => 'commit',
188 next => 'illlist',
189 value => {}
191 handle_commit_maybe($backend_result, $request);
194 } elsif ( $op eq 'moderate_action' ) {
195 # Moderate action is required for an ILL submodule / syspref.
196 # Currently still needs to be implemented.
197 redirect_to_list();
199 } elsif ( $op eq 'delete_confirm') {
200 my $request = Koha::Illrequests->find($params->{illrequest_id});
202 $template->param(
203 request => $request
206 } elsif ( $op eq 'delete' ) {
208 # Check if the request is confirmed, if not, redirect
209 # to the confirmation view
210 if ($params->{confirmed}) {
211 # We simply delete the request...
212 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
213 # ... then return to list view.
214 redirect_to_list();
215 } else {
216 print $cgi->redirect(
217 "/cgi-bin/koha/ill/ill-requests.pl?" .
218 "method=delete_confirm&illrequest_id=" .
219 $params->{illrequest_id});
220 exit;
223 } elsif ( $op eq 'mark_completed' ) {
224 my $request = Koha::Illrequests->find($params->{illrequest_id});
225 my $backend_result = $request->mark_completed($params);
226 $template->param(
227 whole => $backend_result,
228 request => $request,
231 # handle special commit rules & update type
232 handle_commit_maybe($backend_result, $request);
234 } elsif ( $op eq 'generic_confirm' ) {
235 my $backend_result;
236 my $request;
237 try {
238 $request = Koha::Illrequests->find($params->{illrequest_id});
239 $params->{current_branchcode} = C4::Context->mybranch;
240 $backend_result = $request->generic_confirm($params);
241 $template->param(
242 whole => $backend_result,
243 request => $request,
245 $template->param( error => $params->{error} )
246 if $params->{error};
248 catch {
249 my $error;
250 if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
251 $error = 'no_target_email';
253 elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
254 $error = 'no_library_email';
256 else {
257 $error = 'unknown_error';
259 print $cgi->redirect(
260 "/cgi-bin/koha/ill/ill-requests.pl?" .
261 "method=generic_confirm&illrequest_id=" .
262 $params->{illrequest_id} .
263 "&error=$error" );
264 exit;
267 # handle special commit rules & update type
268 handle_commit_maybe($backend_result, $request);
269 } elsif ( $op eq 'illlist') {
271 # If we receive a pre-filter, make it available to the template
272 my $possible_filters = ['borrowernumber'];
273 my $active_filters = [];
274 foreach my $filter(@{$possible_filters}) {
275 if ($params->{$filter}) {
276 push @{$active_filters},
277 { name => $filter, value => $params->{$filter}};
280 if (scalar @{$active_filters} > 0) {
281 $template->param(
282 prefilters => $active_filters
286 } elsif ( $op eq "save_comment" ) {
287 die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
288 session_id => scalar $cgi->cookie('CGISESSID'),
289 token => scalar $cgi->param('csrf_token'),
291 my $comment = Koha::Illcomment->new({
292 illrequest_id => scalar $params->{illrequest_id},
293 borrowernumber => $patronnumber,
294 comment => scalar $params->{comment},
296 $comment->store();
297 # Redirect to view the whole request
298 print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
299 scalar $params->{illrequest_id}
301 exit;
303 } else {
304 my $request = Koha::Illrequests->find($params->{illrequest_id});
305 my $backend_result = $request->custom_capability($op, $params);
306 $template->param(
307 whole => $backend_result,
308 request => $request,
311 # handle special commit rules & update type
312 handle_commit_maybe($backend_result, $request);
316 $template->param(
317 backends => $backends,
318 types => [ "Book", "Article", "Journal" ],
319 query_type => $op,
320 branches => scalar Koha::Libraries->search,
323 output_html_with_http_headers( $cgi, $cookie, $template->output );
325 sub handle_commit_maybe {
326 my ( $backend_result, $request ) = @_;
328 # We need to special case 'commit'
329 if ( $backend_result->{stage} eq 'commit' ) {
330 if ( $backend_result->{next} eq 'illview' ) {
332 # Redirect to a view of the newly created request
333 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
334 . '?method=illview'
335 . '&illrequest_id='
336 . $request->id );
337 exit;
339 elsif ( $backend_result->{next} eq 'emigrate' ) {
341 # Redirect to a view of the newly created request
342 print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
343 . '?method=migrate'
344 . '&stage=emigrate'
345 . '&illrequest_id='
346 . $request->id );
347 exit;
349 else {
350 # Redirect to a requests list view
351 redirect_to_list();
356 sub redirect_to_list {
357 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
358 exit;