Bug 7317: Interlibrary loans framework for Koha.
[koha.git] / opac / opac-illrequests.pl
1 #!/usr/bin/perl
2
3 # Copyright 2017 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Koha;
25 use C4::Output;
26
27 use Koha::Illrequests;
28 use Koha::Libraries;
29 use Koha::Patrons;
30
31 my $query = new CGI;
32
33 # Grab all passed data
34 # 'our' since Plack changes the scoping
35 # of 'my'
36 our $params = $query->Vars();
37
38 # if illrequests is disabled, leave immediately
39 if ( ! C4::Context->preference('ILLModule') ) {
40     print $query->redirect("/cgi-bin/koha/errors/404.pl");
41     exit;
42 }
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
45     template_name   => "opac-illrequests.tt",
46     query           => $query,
47     type            => "opac",
48     authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
49 });
50
51 my $op = $params->{'method'} || 'list';
52
53 if ( $op eq 'list' ) {
54
55     my $requests = Koha::Illrequests->search(
56         { borrowernumber => $loggedinuser }
57     );
58     my $req = Koha::Illrequest->new;
59     $template->param(
60         requests => $requests,
61         backends    => $req->available_backends
62     );
63
64 } elsif ( $op eq 'view') {
65     my $request = Koha::Illrequests->find({
66         borrowernumber => $loggedinuser,
67         illrequest_id  => $params->{illrequest_id}
68     });
69     $template->param(
70         request => $request
71     );
72
73 } elsif ( $op eq 'update') {
74     my $request = Koha::Illrequests->find({
75         borrowernumber => $loggedinuser,
76         illrequest_id  => $params->{illrequest_id}
77     });
78     $request->notesopac($params->{notesopac})->store;
79     print $query->redirect(
80         '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
81         $params->{illrequest_id} .
82         '&message=1'
83     );
84 } elsif ( $op eq 'cancreq') {
85     my $request = Koha::Illrequests->find({
86         borrowernumber => $loggedinuser,
87         illrequest_id  => $params->{illrequest_id}
88     });
89     $request->status('CANCREQ')->store;
90     print $query->redirect(
91         '/cgi-bin/koha/opac-illrequests.pl?method=view&illrequest_id=' .
92         $params->{illrequest_id} .
93         '&message=1'
94     );
95
96 } elsif ( $op eq 'create' ) {
97     if (!$params->{backend}) {
98         my $req = Koha::Illrequest->new;
99         $template->param(
100             backends    => $req->available_backends
101         );
102     } else {
103         my $request = Koha::Illrequest->new
104             ->load_backend($params->{backend});
105         $params->{cardnumber} = Koha::Patrons->find({
106             borrowernumber => $loggedinuser
107         })->cardnumber;
108         my $backend_result = $request->backend_create($params);
109         $template->param(
110             media       => [ "Book", "Article", "Journal" ],
111             branches    => Koha::Libraries->search->unblessed,
112             whole       => $backend_result,
113             request     => $request
114         );
115         if ($backend_result->{stage} eq 'commit') {
116             print $query->redirect('/cgi-bin/koha/opac-illrequests.pl?message=2');
117         }
118     }
119
120
121 }
122
123 $template->param(
124     message         => $params->{message},
125     illrequestsview => 1,
126     method              => $op
127 );
128
129 output_html_with_http_headers $query, $cookie, $template->output;