Bug 20640: Add backend migration support to ILL
[koha.git] / ill / ill-requests.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 PTFS-Europe Ltd and Mark Gavillet
4 # Copyright 2014 PTFS-Europe Ltd
5 #
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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use CGI;
23
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;
31
32 use Try::Tiny;
33
34 our $cgi = CGI->new;
35 my $illRequests = Koha::Illrequests->new;
36
37 # Grab all passed data
38 # 'our' since Plack changes the scoping
39 # of 'my'
40 our $params = $cgi->Vars();
41
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;
46 }
47
48 my $op = $params->{method} || 'illlist';
49
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 } );
56
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
65 );
66
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});
71
72         $template->param(
73             request    => $request,
74             csrf_token => Koha::Token->new->generate_csrf({
75                 session_id => scalar $cgi->cookie('CGISESSID'),
76             }),
77         );
78
79     } elsif ( $op eq 'create' ) {
80         # We're in the process of creating a request
81         my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
82         my $backend_result = $request->backend_create($params);
83         $template->param(
84             whole   => $backend_result,
85             request => $request
86         );
87         handle_commit_maybe($backend_result, $request);
88
89     } elsif ( $op eq 'migrate' ) {
90         # We're in the process of migrating a request
91         my $request = Koha::Illrequests->find($params->{illrequest_id});
92         my $backend_result;
93         if ( $params->{backend} ) {
94             my $new_request = Koha::Illrequest->new->load_backend( $params->{backend} );
95             $backend_result = $new_request->backend_migrate($params);
96             $template->param(
97                 whole   => $backend_result,
98                 request => $new_request
99             );
100         }
101         else {
102             $request = Koha::Illrequests->find( $params->{illrequest_id} );
103             $backend_result = $request->backend_migrate($params);
104             $template->param(
105                 whole   => $backend_result,
106                 request => $request
107             );
108         }
109         handle_commit_maybe( $backend_result, $request );
110
111     } elsif ( $op eq 'confirm' ) {
112         # Backend 'confirm' method
113         # confirm requires a specific request, so first, find it.
114         my $request = Koha::Illrequests->find($params->{illrequest_id});
115         my $backend_result = $request->backend_confirm($params);
116         $template->param(
117             whole   => $backend_result,
118             request => $request,
119         );
120
121         # handle special commit rules & update type
122         handle_commit_maybe($backend_result, $request);
123
124     } elsif ( $op eq 'cancel' ) {
125         # Backend 'cancel' method
126         # cancel requires a specific request, so first, find it.
127         my $request = Koha::Illrequests->find($params->{illrequest_id});
128         my $backend_result = $request->backend_cancel($params);
129         $template->param(
130             whole   => $backend_result,
131             request => $request,
132         );
133
134         # handle special commit rules & update type
135         handle_commit_maybe($backend_result, $request);
136
137     } elsif ( $op eq 'edit_action' ) {
138         # Handle edits to the Illrequest object.
139         # (not the Illrequestattributes)
140         # We simulate the API for backend requests for uniformity.
141         # So, init:
142         my $request = Koha::Illrequests->find($params->{illrequest_id});
143         if ( !$params->{stage} ) {
144             my $backend_result = {
145                 error   => 0,
146                 status  => '',
147                 message => '',
148                 method  => 'edit_action',
149                 stage   => 'init',
150                 next    => '',
151                 value   => {}
152             };
153             $template->param(
154                 whole   => $backend_result,
155                 request => $request
156             );
157         } else {
158             # Commit:
159             # Save the changes
160             $request->borrowernumber($params->{borrowernumber});
161             $request->biblio_id($params->{biblio_id});
162             $request->branchcode($params->{branchcode});
163             $request->price_paid($params->{price_paid});
164             $request->notesopac($params->{notesopac});
165             $request->notesstaff($params->{notesstaff});
166             $request->store;
167             my $backend_result = {
168                 error   => 0,
169                 status  => '',
170                 message => '',
171                 method  => 'edit_action',
172                 stage   => 'commit',
173                 next    => 'illlist',
174                 value   => {}
175             };
176             handle_commit_maybe($backend_result, $request);
177         }
178
179     } elsif ( $op eq 'moderate_action' ) {
180         # Moderate action is required for an ILL submodule / syspref.
181         # Currently still needs to be implemented.
182         redirect_to_list();
183
184     } elsif ( $op eq 'delete_confirm') {
185         my $request = Koha::Illrequests->find($params->{illrequest_id});
186
187         $template->param(
188             request => $request
189         );
190
191     } elsif ( $op eq 'delete' ) {
192
193         # Check if the request is confirmed, if not, redirect
194         # to the confirmation view
195         if ($params->{confirmed}) {
196             # We simply delete the request...
197             Koha::Illrequests->find( $params->{illrequest_id} )->delete;
198             # ... then return to list view.
199             redirect_to_list();
200         } else {
201             print $cgi->redirect(
202                 "/cgi-bin/koha/ill/ill-requests.pl?" .
203                 "method=delete_confirm&illrequest_id=" .
204                 $params->{illrequest_id});
205             exit;
206         }
207
208     } elsif ( $op eq 'mark_completed' ) {
209         my $request = Koha::Illrequests->find($params->{illrequest_id});
210         my $backend_result = $request->mark_completed($params);
211         $template->param(
212             whole => $backend_result,
213             request => $request,
214         );
215
216         # handle special commit rules & update type
217         handle_commit_maybe($backend_result, $request);
218
219     } elsif ( $op eq 'generic_confirm' ) {
220         my $backend_result;
221         my $request;
222         try {
223             $request = Koha::Illrequests->find($params->{illrequest_id});
224             $params->{current_branchcode} = C4::Context->mybranch;
225             $backend_result = $request->generic_confirm($params);
226             $template->param(
227                 whole => $backend_result,
228                 request => $request,
229             );
230             $template->param( error => $params->{error} )
231                 if $params->{error};
232         }
233         catch {
234             my $error;
235             if ( $_->isa( 'Koha::Exceptions::Ill::NoTargetEmail' ) ) {
236                 $error = 'no_target_email';
237             }
238             elsif ( $_->isa( 'Koha::Exceptions::Ill::NoLibraryEmail' ) ) {
239                 $error = 'no_library_email';
240             }
241             else {
242                 $error = 'unknown_error';
243             }
244             print $cgi->redirect(
245                 "/cgi-bin/koha/ill/ill-requests.pl?" .
246                 "method=generic_confirm&illrequest_id=" .
247                 $params->{illrequest_id} .
248                 "&error=$error" );
249             exit;
250         };
251
252         # handle special commit rules & update type
253         handle_commit_maybe($backend_result, $request);
254     } elsif ( $op eq 'illlist') {
255
256         # If we receive a pre-filter, make it available to the template
257         my $possible_filters = ['borrowernumber'];
258         my $active_filters = [];
259         foreach my $filter(@{$possible_filters}) {
260             if ($params->{$filter}) {
261                 push @{$active_filters},
262                     { name => $filter, value => $params->{$filter}};
263             }
264         }
265         if (scalar @{$active_filters} > 0) {
266             $template->param(
267                 prefilters => $active_filters
268             );
269         }
270
271     } elsif ( $op eq "save_comment" ) {
272         die "Wrong CSRF token" unless Koha::Token->new->check_csrf({
273            session_id => scalar $cgi->cookie('CGISESSID'),
274            token      => scalar $cgi->param('csrf_token'),
275         });
276         my $comment = Koha::Illcomment->new({
277             illrequest_id  => scalar $params->{illrequest_id},
278             borrowernumber => $patronnumber,
279             comment        => scalar $params->{comment},
280         });
281         $comment->store();
282         # Redirect to view the whole request
283         print $cgi->redirect("/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id=".
284             scalar $params->{illrequest_id}
285         );
286         exit;
287
288     } else {
289         my $request = Koha::Illrequests->find($params->{illrequest_id});
290         my $backend_result = $request->custom_capability($op, $params);
291         $template->param(
292             whole => $backend_result,
293             request => $request,
294         );
295
296         # handle special commit rules & update type
297         handle_commit_maybe($backend_result, $request);
298     }
299 }
300
301 $template->param(
302     backends   => $backends,
303     types      => [ "Book", "Article", "Journal" ],
304     query_type => $op,
305     branches   => scalar Koha::Libraries->search,
306 );
307
308 output_html_with_http_headers( $cgi, $cookie, $template->output );
309
310 sub handle_commit_maybe {
311     my ( $backend_result, $request ) = @_;
312
313     # We need to special case 'commit'
314     if ( $backend_result->{stage} eq 'commit' ) {
315         if ( $backend_result->{next} eq 'illview' ) {
316
317             # Redirect to a view of the newly created request
318             print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
319                   . '?method=illview'
320                   . '&illrequest_id='
321                   . $request->id );
322             exit;
323         }
324         elsif ( $backend_result->{next} eq 'emigrate' ) {
325
326             # Redirect to a view of the newly created request
327             print $cgi->redirect( '/cgi-bin/koha/ill/ill-requests.pl'
328                   . '?method=migrate'
329                   . '&stage=emigrate'
330                   . '&illrequest_id='
331                   . $request->id );
332             exit;
333         }
334         else {
335             # Redirect to a requests list view
336             redirect_to_list();
337         }
338     }
339 }
340
341 sub redirect_to_list {
342     print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
343     exit;
344 }