From 7e5eab6b5ce01f841f8922c349f3ccf82751739e Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 7 Sep 2023 16:27:07 +0000 Subject: [PATCH] Bug 33960: Add api mapping and biblioitems to Old::Biblios to match Biblio Signed-off-by: Katrin Fischer Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- Koha/Old/Biblio.pm | 61 ++++++++++++++++++++++++++++++++++++++++++--- Koha/Old/Biblios.pm | 28 +++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/Koha/Old/Biblio.pm b/Koha/Old/Biblio.pm index 5c0e416619c..204f1456479 100644 --- a/Koha/Old/Biblio.pm +++ b/Koha/Old/Biblio.pm @@ -20,6 +20,7 @@ use Modern::Perl; use base qw(Koha::Object); use Koha::Old::Biblio::Metadatas; +use Koha::Old::Biblioitems; =head1 NAME @@ -40,7 +41,7 @@ Returns a Koha::Biblio::Metadata object =cut sub metadata { - my ( $self ) = @_; + my ($self) = @_; my $metadata = $self->_result->metadata; return Koha::Old::Biblio::Metadata->_new_from_dbic($metadata); @@ -55,7 +56,7 @@ Returns a Marc::Record object =cut sub record { - my ( $self ) = @_; + my ($self) = @_; return $self->metadata->record; } @@ -69,11 +70,65 @@ Returns the record schema (MARC21, USMARC or UNIMARC). =cut sub record_schema { - my ( $self ) = @_; + my ($self) = @_; return $self->metadata->schema // C4::Context->preference("marcflavour"); } +=head3 biblioitem + +my $field = $self->biblioitem + +Returns the related Koha::Old::Biblioitem object for this Biblio object + +=cut + +sub biblioitem { + my ($self) = @_; + return Koha::Old::Biblioitems->find( { biblionumber => $self->biblionumber } ); +} + +=head3 to_api + + my $json = $deleted_biblio->to_api; + +Overloaded method that returns a JSON representation of the Koha::Old::Biblio object, +suitable for API output. The related Koha::Old::Biblioitem object is merged as expected +on the API. + +=cut + +sub to_api { + my ( $self, $args ) = @_; + + my $response = $self->SUPER::to_api($args); + + $args = defined $args ? {%$args} : {}; + delete $args->{embed}; + + my $biblioitem = $self->biblioitem->to_api($args); + + return { %$response, %$biblioitem }; +} + +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Old::Biblio object +on the API. + +=cut + +sub to_api_mapping { + return { + biblionumber => 'biblio_id', + frameworkcode => 'framework_id', + unititle => 'uniform_title', + seriestitle => 'series_title', + copyrightdate => 'copyright_date', + datecreated => 'creation_date', + deleted_on => 'timestamp', + }; +} =head2 Internal methods diff --git a/Koha/Old/Biblios.pm b/Koha/Old/Biblios.pm index 990d067a180..ce7d12ad68e 100644 --- a/Koha/Old/Biblios.pm +++ b/Koha/Old/Biblios.pm @@ -33,6 +33,34 @@ Koha::Old::Biblios - Koha Old::Biblio Object set class =head2 Internal Methods +=head3 api_query_fixer + + $query_string = $biblios->api_query_fixer( $query_string, $context, $no_quotes ); + +Method that takes care of adjusting I<$query_string> as required. An optional I<$context> parameter +will be used to prefix the relevant query atoms if present. A I<$no_quotes> boolean parameter +can be passed to choose not to use quotes on matching. This is particularly useful in the context of I. + +=cut + +sub api_query_fixer { + my ( $self, $query, $context, $no_quotes ) = @_; + + my $quotes = $no_quotes ? '' : '"'; + + if ($context) { + $query =~ + s/${quotes}${context}\.(age_restriction|cn_class|cn_item|cn_sort|cn_source|cn_suffix|collection_issn|collection_title|collection_volume|ean|edition_statement|illustrations|isbn|issn|item_type|lc_control_number|notes|number|pages|publication_place|publication_year|publisher|material_size|serial_total_issues|url|volume|volume_date|volume_description)${quotes}/${quotes}${context}\.deletedbiblioitem\.$1${quotes}/g; + } else { + $query =~ + s/${quotes}(age_restriction|cn_class|cn_item|cn_sort|cn_source|cn_suffix|collection_issn|collection_title|collection_volume|ean|edition_statement|illustrations|isbn|issn|item_type|lc_control_number|notes|number|pages|publication_place|publication_year|publisher|material_size|serial_total_issues|url|volume|volume_date|volume_description)${quotes}/${quotes}deletedbiblioitem\.$1${quotes}/g; + $query =~ # handle ambiguous 'biblionumber' + s/${quotes}(biblio_id)${quotes}/${quotes}me\.$1${quotes}/g; + } + + return $query; +} + =head3 _type =cut -- 2.44.0