1 package Koha::REST::V1::Biblios;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
23 use Koha::RecordProcessor;
24 use C4::Biblio qw(DelBiblio);
26 use List::MoreUtils qw(any);
27 use MARC::Record::MiJ;
37 Controller function that handles retrieving a single biblio object
42 my $c = shift->openapi->valid_input or return;
45 $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
46 unless $c->req->headers->accept =~ m/application\/json/;
48 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes );
54 error => "Object not found."
61 if ( $c->req->headers->accept =~ m/application\/json/ ) {
64 json => $biblio->to_api
68 my $record = $biblio->metadata->record;
74 text => $record->as_xml_record
79 text => $record->to_mij
84 text => $record->as_usmarc
88 format => 'text/plain',
89 text => $record->as_formatted
95 "application/marcxml+xml",
96 "application/marc-in-json",
105 $c->unhandled_exception($_);
111 Controller function that handles deleting a biblio object
116 my $c = shift->openapi->valid_input or return;
118 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
120 if ( not defined $biblio ) {
123 openapi => { error => "Object not found" }
128 my $error = DelBiblio( $biblio->id );
133 openapi => { error => $error }
137 return $c->render( status => 204, openapi => "" );
141 $c->unhandled_exception($_);
147 Controller function that handles retrieving a single biblio object
152 my $c = shift->openapi->valid_input or return;
154 my $biblio = Koha::Biblios->find(
155 { biblionumber => $c->validation->param('biblio_id') },
156 { prefetch => ['metadata'] } );
162 error => "Object not found."
169 my $record = $biblio->metadata->record;
171 my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
172 my $patron = $c->stash('koha.user');
174 # Check if the biblio should be hidden for unprivileged access
175 # unless there's a logged in user, and there's an exception for it's
177 unless ( $patron and $patron->category->override_hidden_items ) {
178 if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) )
183 error => "Object not found."
189 my $marcflavour = C4::Context->preference("marcflavour");
191 my $record_processor = Koha::RecordProcessor->new({
192 filters => 'ViewPolicy',
195 frameworkcode => $biblio->frameworkcode
198 # Apply framework's filtering to MARC::Record object
199 $record_processor->process($record);
205 text => $record->as_xml_record
210 text => $record->to_mij
215 text => $record->as_usmarc
219 format => 'text/plain',
220 text => $record->as_formatted
225 "application/marcxml+xml",
226 "application/marc-in-json",
234 $c->unhandled_exception($_);
240 Controller function that handles retrieving biblio's items
245 my $c = shift->openapi->valid_input or return;
247 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, { prefetch => ['items'] } );
253 error => "Object not found."
260 my $items_rs = $biblio->items;
261 my $items = $c->objects->search( $items_rs );
268 $c->unhandled_exception($_);
272 =head3 pickup_locations
274 Method that returns the possible pickup_locations for a given biblio
275 used for building the dropdown selector
279 sub pickup_locations {
280 my $c = shift->openapi->valid_input or return;
282 my $biblio_id = $c->validation->param('biblio_id');
283 my $biblio = Koha::Biblios->find( $biblio_id );
288 openapi => { error => "Biblio not found" }
292 my $patron_id = delete $c->validation->output->{patron_id};
293 my $patron = Koha::Patrons->find( $patron_id );
298 openapi => { error => "Patron not found" }
304 my $ps_set = $biblio->pickup_locations( { patron => $patron } );
306 my $pickup_locations = $c->objects->search( $ps_set );
309 if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
311 my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
312 my $libraries = $c->objects->search($libraries_rs);
316 $library->{needs_override} = (
317 any { $_->{library_id} eq $library->{library_id} }
327 openapi => \@response
331 @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
335 openapi => \@response
339 $c->unhandled_exception($_);