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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Controller';
23 use Koha::RecordProcessor;
24 use C4::Biblio qw(DelBiblio);
26 use MARC::Record::MiJ;
36 Controller function that handles retrieving a single biblio object
41 my $c = shift->openapi->valid_input or return;
44 $attributes = { prefetch => [ 'metadata' ] } # don't prefetch metadata if not needed
45 unless $c->req->headers->accept =~ m/application\/json/;
47 my $biblio = Koha::Biblios->find( { biblionumber => $c->validation->param('biblio_id') }, $attributes );
53 error => "Object not found."
60 if ( $c->req->headers->accept =~ m/application\/json/ ) {
63 json => $c->build_json_biblio( { biblio => $biblio } )
67 my $record = $biblio->metadata->record;
73 text => $record->as_xml_record
78 text => $record->to_mij
83 text => $record->as_usmarc
87 format => 'text/plain',
88 text => $record->as_formatted
94 "application/marcxml+xml",
95 "application/marc-in-json",
106 openapi => { error => "Something went wrong, check the logs ($_)" }
113 Controller function that handles deleting a biblio object
118 my $c = shift->openapi->valid_input or return;
120 my $biblio = Koha::Biblios->find( $c->validation->param('biblio_id') );
122 if ( not defined $biblio ) {
125 openapi => { error => "Object not found" }
130 my $error = DelBiblio( $biblio->id );
135 openapi => { error => $error }
139 return $c->render( status => 204, openapi => "" );
143 if ( $_->isa('DBIx::Class::Exception') ) {
146 openapi => { error => $_->{msg} }
152 openapi => { error => "Something went wrong, check the logs." }
160 Controller function that handles retrieving a single biblio object
165 my $c = shift->openapi->valid_input or return;
167 my $biblio = Koha::Biblios->find(
168 { biblionumber => $c->validation->param('biblio_id') },
169 { prefetch => ['metadata'] } );
175 error => "Object not found."
182 my $record = $biblio->metadata->record;
184 my $opachiddenitems_rules = C4::Context->yaml_preference('OpacHiddenItems');
185 my $patron = $c->stash('koha.user');
187 # Check if the biblio should be hidden for unprivileged access
188 # unless there's a logged in user, and there's an exception for it's
190 unless ( $patron and $patron->category->override_hidden_items ) {
191 if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) )
196 error => "Object not found."
202 my $marcflavour = C4::Context->preference("marcflavour");
204 my $record_processor = Koha::RecordProcessor->new({
205 filters => 'ViewPolicy',
208 frameworkcode => $biblio->frameworkcode
211 # Apply framework's filtering to MARC::Record object
212 $record_processor->process($record);
218 text => $record->as_xml_record
223 text => $record->to_mij
228 text => $record->as_usmarc
232 format => 'text/plain',
233 text => $record->as_formatted
238 "application/marcxml+xml",
239 "application/marc-in-json",
249 openapi => { error => "Something went wrong, check the logs ($_)" }
254 =head2 Internal methods
258 Helper function that maps unblessed Koha::Patron objects into REST api
267 foreach my $column ( keys %{$Koha::REST::V1::Biblios::to_api_mapping} ) {
268 my $mapped_column = $Koha::REST::V1::Biblios::to_api_mapping->{$column};
269 if ( exists $biblio->{$column}
270 && defined $mapped_column )
273 $biblio->{$mapped_column} = delete $biblio->{$column};
275 elsif ( exists $biblio->{$column}
276 && !defined $mapped_column )
279 delete $biblio->{$column};
287 =head3 build_json_biblio
289 Internal method that returns all the attributes from the biblio and biblioitems tables
293 sub build_json_biblio {
294 my ( $c, $args ) = @_;
296 my $biblio = $args->{biblio};
298 my $response = $biblio->TO_JSON;
299 my $biblioitem = $biblio->biblioitem->TO_JSON;
301 foreach my $key ( keys %{ $biblioitem } ) {
302 $response->{$key} = $biblioitem->{$key};
305 return _to_api($response);
308 =head2 Global variables
310 =head3 $to_api_mapping
314 our $to_api_mapping = {
315 agerestriction => 'age_restriction',
316 biblioitemnumber => undef, # meaningless
317 biblionumber => 'biblio_id',
318 collectionissn => 'collection_issn',
319 collectiontitle => 'collection_title',
320 collectionvolume => 'collection_volume',
321 copyrightdate => 'copyright_date',
322 datecreated => 'creation_date',
323 editionresponsibility => undef, # obsolete, not mapped
324 editionstatement => 'edition_statement',
325 frameworkcode => 'framework_id',
326 illus => 'illustrations',
327 itemtype => 'item_type',
328 lccn => 'lc_control_number',
329 place => 'publication_place',
330 publicationyear => 'publication_year',
331 publishercode => 'publisher',
332 seriestitle => 'series_title',
333 size => 'material_size',
334 totalissues => 'serial_total_issues',
335 unititle => 'uniform_title',
336 volumedate => 'volume_date',
337 volumedesc => 'volume_description',