Bug 7317: Interlibrary loans framework for Koha.
[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::Illrequests;
28 use Koha::Libraries;
29
30 my $cgi = CGI->new;
31 my $illRequests = Koha::Illrequests->new;
32
33 # Grab all passed data
34 # 'our' since Plack changes the scoping
35 # of 'my'
36 our $params = $cgi->Vars();
37
38 my $op = $params->{method} || 'illlist';
39
40 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
41     template_name => 'ill/ill-requests.tt',
42     query         => $cgi,
43     type          => 'intranet',
44     flagsrequired => { ill => '*' },
45 } );
46
47 if ( $op eq 'illview' ) {
48     # View the details of an ILL
49     my $request = Koha::Illrequests->find($params->{illrequest_id});
50
51     $template->param(
52         request => $request
53     );
54
55 } elsif ( $op eq 'create' ) {
56     # We're in the process of creating a request
57     my $request = Koha::Illrequest->new
58         ->load_backend($params->{backend});
59     my $backend_result = $request->backend_create($params);
60     $template->param(
61         whole   => $backend_result,
62         request => $request
63     );
64     handle_commit_maybe($backend_result, $request);
65
66 } elsif ( $op eq 'confirm' ) {
67     # Backend 'confirm' method
68     # confirm requires a specific request, so first, find it.
69     my $request = Koha::Illrequests->find($params->{illrequest_id});
70     my $backend_result = $request->backend_confirm($params);
71     $template->param(
72         whole   => $backend_result,
73         request => $request,
74     );
75
76     # handle special commit rules & update type
77     handle_commit_maybe($backend_result, $request);
78
79 } elsif ( $op eq 'cancel' ) {
80     # Backend 'cancel' method
81     # cancel requires a specific request, so first, find it.
82     my $request = Koha::Illrequests->find($params->{illrequest_id});
83     my $backend_result = $request->backend_cancel($params);
84     $template->param(
85         whole   => $backend_result,
86         request => $request,
87     );
88
89     # handle special commit rules & update type
90     handle_commit_maybe($backend_result, $request);
91
92 } elsif ( $op eq 'edit_action' ) {
93     # Handle edits to the Illrequest object.
94     # (not the Illrequestattributes)
95     # We simulate the API for backend requests for uniformity.
96     # So, init:
97     my $request = Koha::Illrequests->find($params->{illrequest_id});
98     if ( !$params->{stage} ) {
99         my $backend_result = {
100             error   => 0,
101             status  => '',
102             message => '',
103             method  => 'edit_action',
104             stage   => 'init',
105             next    => '',
106             value   => {}
107         };
108         $template->param(
109             whole   => $backend_result,
110             request => $request
111         );
112     } else {
113         # Commit:
114         # Save the changes
115         $request->borrowernumber($params->{borrowernumber});
116         $request->biblio_id($params->{biblio_id});
117         $request->branchcode($params->{branchcode});
118         $request->notesopac($params->{notesopac});
119         $request->notesstaff($params->{notesstaff});
120         $request->store;
121         my $backend_result = {
122             error   => 0,
123             status  => '',
124             message => '',
125             method  => 'edit_action',
126             stage   => 'commit',
127             next    => 'illlist',
128             value   => {}
129         };
130         handle_commit_maybe($backend_result, $request);
131     }
132
133 } elsif ( $op eq 'moderate_action' ) {
134     # Moderate action is required for an ILL submodule / syspref.
135     # Currently still needs to be implemented.
136     redirect_to_list();
137
138 } elsif ( $op eq 'delete_confirm') {
139     my $request = Koha::Illrequests->find($params->{illrequest_id});
140
141     $template->param(
142         request => $request
143     );
144
145 } elsif ( $op eq 'delete' ) {
146
147     # Check if the request is confirmed, if not, redirect
148     # to the confirmation view
149     if ($params->{confirmed} == 1) {
150         # We simply delete the request...
151         my $request = Koha::Illrequests->find(
152             $params->{illrequest_id}
153         )->delete;
154         # ... then return to list view.
155         redirect_to_list();
156     } else {
157         print $cgi->redirect(
158             "/cgi-bin/koha/ill/ill-requests.pl?" .
159             "method=delete_confirm&illrequest_id=" .
160             $params->{illrequest_id});
161     }
162
163 } elsif ( $op eq 'mark_completed' ) {
164     my $request = Koha::Illrequests->find($params->{illrequest_id});
165     my $backend_result = $request->mark_completed($params);
166     $template->param(
167         whole => $backend_result,
168         request => $request,
169     );
170
171     # handle special commit rules & update type
172     handle_commit_maybe($backend_result, $request);
173
174 } elsif ( $op eq 'generic_confirm' ) {
175     my $request = Koha::Illrequests->find($params->{illrequest_id});
176     $params->{current_branchcode} = C4::Context->mybranch;
177     my $backend_result = $request->generic_confirm($params);
178     $template->param(
179         whole => $backend_result,
180         request => $request,
181     );
182
183     # handle special commit rules & update type
184     handle_commit_maybe($backend_result, $request);
185
186 } elsif ( $op eq 'illlist') {
187     # Display all current ILLs
188     my $requests = $illRequests->search();
189
190     $template->param(
191         requests => $requests
192     );
193
194     # If we receive a pre-filter, make it available to the template
195     my $possible_filters = ['borrowernumber'];
196     my $active_filters = [];
197     foreach my $filter(@{$possible_filters}) {
198         if ($params->{$filter}) {
199             push @{$active_filters},
200                 { name => $filter, value => $params->{$filter}};
201         }
202     }
203     if (scalar @{$active_filters} > 0) {
204         $template->param(
205             prefilters => $active_filters
206         );
207     }
208 } else {
209     my $request = Koha::Illrequests->find($params->{illrequest_id});
210     my $backend_result = $request->custom_capability($op, $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
220 # Get a list of backends
221 my $ir = Koha::Illrequest->new;
222
223 $template->param(
224     backends    => $ir->available_backends,
225     media       => [ "Book", "Article", "Journal" ],
226     query_type  => $op,
227     branches    => Koha::Libraries->search->unblessed,
228     here_link   => "/cgi-bin/koha/ill/ill-requests.pl"
229 );
230
231 output_html_with_http_headers( $cgi, $cookie, $template->output );
232
233 sub handle_commit_maybe {
234     my ( $backend_result, $request ) = @_;
235     # We need to special case 'commit'
236     if ( $backend_result->{stage} eq 'commit' ) {
237         if ( $backend_result->{next} eq 'illview' ) {
238             # Redirect to a view of the newly created request
239             print $cgi->redirect(
240                 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
241                 $request->id
242             );
243         } else {
244             # Redirect to a requests list view
245             redirect_to_list();
246         }
247     }
248 }
249
250 sub redirect_to_list {
251     print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');
252 }