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;
27 use Koha::Illrequests;
31 my $illRequests = Koha::Illrequests->new;
33 # Grab all passed data
34 # 'our' since Plack changes the scoping
36 our $params = $cgi->Vars();
38 # Leave immediately if ILLModule is disabled
39 unless ( C4::Context->preference('ILLModule') ) {
40 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
44 my $op = $params->{method} || 'illlist';
46 my ( $template, $patronnumber, $cookie ) = get_template_and_user( {
47 template_name => 'ill/ill-requests.tt',
50 flagsrequired => { ill => '*' },
53 # Are we able to actually work?
54 my $backends = Koha::Illrequest::Config->new->available_backends;
55 my $backends_available = ( scalar @{$backends} > 0 );
56 $template->param( backends_available => $backends_available );
58 if ( $backends_available ) {
59 if ( $op eq 'illview' ) {
60 # View the details of an ILL
61 my $request = Koha::Illrequests->find($params->{illrequest_id});
67 } elsif ( $op eq 'create' ) {
68 # We're in the process of creating a request
69 my $request = Koha::Illrequest->new->load_backend( $params->{backend} );
70 my $backend_result = $request->backend_create($params);
72 whole => $backend_result,
75 handle_commit_maybe($backend_result, $request);
77 } elsif ( $op eq 'confirm' ) {
78 # Backend 'confirm' method
79 # confirm requires a specific request, so first, find it.
80 my $request = Koha::Illrequests->find($params->{illrequest_id});
81 my $backend_result = $request->backend_confirm($params);
83 whole => $backend_result,
87 # handle special commit rules & update type
88 handle_commit_maybe($backend_result, $request);
90 } elsif ( $op eq 'cancel' ) {
91 # Backend 'cancel' method
92 # cancel requires a specific request, so first, find it.
93 my $request = Koha::Illrequests->find($params->{illrequest_id});
94 my $backend_result = $request->backend_cancel($params);
96 whole => $backend_result,
100 # handle special commit rules & update type
101 handle_commit_maybe($backend_result, $request);
103 } elsif ( $op eq 'edit_action' ) {
104 # Handle edits to the Illrequest object.
105 # (not the Illrequestattributes)
106 # We simulate the API for backend requests for uniformity.
108 my $request = Koha::Illrequests->find($params->{illrequest_id});
109 if ( !$params->{stage} ) {
110 my $backend_result = {
114 method => 'edit_action',
120 whole => $backend_result,
126 $request->borrowernumber($params->{borrowernumber});
127 $request->biblio_id($params->{biblio_id});
128 $request->branchcode($params->{branchcode});
129 $request->notesopac($params->{notesopac});
130 $request->notesstaff($params->{notesstaff});
132 my $backend_result = {
136 method => 'edit_action',
141 handle_commit_maybe($backend_result, $request);
144 } elsif ( $op eq 'moderate_action' ) {
145 # Moderate action is required for an ILL submodule / syspref.
146 # Currently still needs to be implemented.
149 } elsif ( $op eq 'delete_confirm') {
150 my $request = Koha::Illrequests->find($params->{illrequest_id});
156 } elsif ( $op eq 'delete' ) {
158 # Check if the request is confirmed, if not, redirect
159 # to the confirmation view
160 if ($params->{confirmed}) {
161 # We simply delete the request...
162 Koha::Illrequests->find( $params->{illrequest_id} )->delete;
163 # ... then return to list view.
166 print $cgi->redirect(
167 "/cgi-bin/koha/ill/ill-requests.pl?" .
168 "method=delete_confirm&illrequest_id=" .
169 $params->{illrequest_id});
173 } elsif ( $op eq 'mark_completed' ) {
174 my $request = Koha::Illrequests->find($params->{illrequest_id});
175 my $backend_result = $request->mark_completed($params);
177 whole => $backend_result,
181 # handle special commit rules & update type
182 handle_commit_maybe($backend_result, $request);
184 } elsif ( $op eq 'generic_confirm' ) {
185 my $request = Koha::Illrequests->find($params->{illrequest_id});
186 $params->{current_branchcode} = C4::Context->mybranch;
187 my $backend_result = $request->generic_confirm($params);
189 whole => $backend_result,
193 # handle special commit rules & update type
194 handle_commit_maybe($backend_result, $request);
196 } elsif ( $op eq 'illlist') {
197 # Display all current ILLs
198 my $requests = $illRequests->search();
201 requests => $requests
204 # If we receive a pre-filter, make it available to the template
205 my $possible_filters = ['borrowernumber'];
206 my $active_filters = [];
207 foreach my $filter(@{$possible_filters}) {
208 if ($params->{$filter}) {
209 push @{$active_filters},
210 { name => $filter, value => $params->{$filter}};
213 if (scalar @{$active_filters} > 0) {
215 prefilters => $active_filters
219 my $request = Koha::Illrequests->find($params->{illrequest_id});
220 my $backend_result = $request->custom_capability($op, $params);
222 whole => $backend_result,
226 # handle special commit rules & update type
227 handle_commit_maybe($backend_result, $request);
232 backends => $backends,
233 media => [ "Book", "Article", "Journal" ],
235 branches => Koha::Libraries->search,
236 here_link => "/cgi-bin/koha/ill/ill-requests.pl"
239 output_html_with_http_headers( $cgi, $cookie, $template->output );
241 sub handle_commit_maybe {
242 my ( $backend_result, $request ) = @_;
243 # We need to special case 'commit'
244 if ( $backend_result->{stage} eq 'commit' ) {
245 if ( $backend_result->{next} eq 'illview' ) {
246 # Redirect to a view of the newly created request
247 print $cgi->redirect(
248 '/cgi-bin/koha/ill/ill-requests.pl?method=illview&illrequest_id='.
252 # Redirect to a requests list view
258 sub redirect_to_list {
259 print $cgi->redirect('/cgi-bin/koha/ill/ill-requests.pl');