3 # Copyright 2016 Catalyst IT
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use JSON
qw(encode_json);
22 use C4
::Auth
qw(checkauth);
25 use Koha
::ExternalContent
::RecordedBooks
;
28 my $logger = Koha
::Logger
->get({ interface
=> 'opac' });
29 my $page_url = $cgi->referer();
31 my ( $user, $cookie, $sessionID, $flags ) = checkauth
( $cgi, 1, {}, 'opac' );
33 my $action = $cgi->param('action') or response_bad_request
("No 'action' specified");
35 ($user && $sessionID) || $action eq 'search' or response_bad_request
("User not logged in");
38 my $rb = eval { Koha
::ExternalContent
::RecordedBooks
->new({ koha_session_id
=> $sessionID }) };
40 $logger->error($@
) if $@
;
43 is_identified
=> JSON
::false
,
47 my $is_identified = $rb->is_identified;
49 is_identified
=> $is_identified ? JSON
::true
: JSON
::false
,
51 response
(\
%data) unless $is_identified || $action eq 'search';
55 $action eq 'search' && do {
56 my $query = $cgi->param('q');
57 my $page = $cgi->param('page');
58 my $page_size = $cgi->param('page_size');
59 my $sort = $cgi->param('sort');
60 my $res = $rb->search({
63 page_size
=> $page_size,
66 $data{results
} = $res;
70 $action eq 'account' && do {
72 $data{account
} = $rb->patron;
73 $data{checkouts
} = $rb->checkouts;
74 $data{holds
} = $rb->holds;
76 response_bad_request
($rb->error_message($@
)) if $@
;
80 $action eq 'checkout' && do {
81 my $isbn = $cgi->param('isbn')
82 or response_bad_request
("No 'isbn' specified");
83 my $format = $cgi->param('format');
84 $data{action
} = eval { $rb->checkout($isbn, $format) };
85 response_bad_request
($rb->error_message($@
)) if $@
;
88 $data{checkouts
} = $rb->checkouts;
89 $data{holds
} = $rb->holds;
91 $data{error
} = $rb->error_message($@
) if $@
;
95 $action eq 'return' && do {
96 my $isbn = $cgi->param('isbn')
97 or response_bad_request
("No 'isbn' specified");
98 $data{action
} = eval { $rb->return($isbn) };
99 response_bad_request
($rb->error_message($@
)) if $@
;
101 $data{checkouts
} = eval { $rb->checkouts };
102 $data{error
} = $rb->error_message($@
) if $@
;
106 $action eq 'place_hold' && do {
107 my $isbn = $cgi->param('isbn')
108 or response_bad_request
("No 'isbn' specified");
109 $data{action
} = eval { $rb->place_hold($isbn) };
110 response_bad_request
($rb->error_message($@
)) if $@
;
112 $data{holds
} = eval { $rb->holds };
113 $data{error
} = $rb->error_message($@
) if $@
;
117 $action eq 'remove_hold' && do {
118 my $isbn = $cgi->param('isbn')
119 or response_bad_request
("No 'isbn' specified");
120 $data{action
} = eval { $rb->remove_hold($isbn) };
121 response_bad_request
($rb->error_message($@
)) if $@
;
123 $data{holds
} = eval { $rb->holds };
124 $data{error
} = $rb->error_message($@
) if $@
;
128 response_bad_request
("Invalid 'action': $action");
133 $data{error
} = $rb->error_message("$@");
139 sub response_bad_request
{
141 response
({error
=> $error}, "400 $error");
144 my ($data, $status_line) = @_;
145 $status_line ||= "200 OK";
146 output_with_http_headers
$cgi, undef, encode_json
($data), 'json', $status_line;