From 43e9d358dd217a0338d1b88f4813bb5637b8c1d5 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 11 Oct 2019 14:43:09 -0300 Subject: [PATCH] Bug 23807: Add Koha::Item->as_marc_field --- Koha/Item.pm | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Koha/Item.pm b/Koha/Item.pm index 552e977e97..2b2eb5fffc 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -26,6 +26,7 @@ use Koha::Database; use Koha::DateUtils qw( dt_from_string ); use C4::Context; + use Koha::Checkouts; use Koha::IssuingRules; use Koha::Item::Transfer::Limits; @@ -385,6 +386,49 @@ sub has_pending_hold { return !C4::Context->preference('AllowItemsOnHoldCheckout') && $pending_hold->count ? 1: 0; } +=head3 as_marc_field + + my $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } ); + my $field = $item->as_marc_field({ [ mss => $mss ] }); + +This method returns a MARC::Field object representing the Koha::Item object +with the current mappings configuration. + +=cut + +sub as_marc_field { + my ( $self, $params ) = @_; + + my $mss = $params->{mss} // C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } ); + my $item_tag = $mss->{'items.itemnumber'}[0]->{tagfield}; + + my @subfields; + + my @columns = $self->_result->result_source->columns; + + foreach my $item_field ( @columns ) { + my $mapping = $mss->{ "items.$item_field"}[0]; + my $tagfield = $mapping->{tagfield}; + my $tagsubfield = $mapping->{tagsubfield}; + next if !$tagfield; # TODO: Should we raise an exception instead? + # Feels like safe fallback is better + + push @subfields, $tagsubfield => $self->$item_field; + } + + my $unlinked_item_subfields = C4::Items::_parse_unlinked_item_subfields_from_xml($self->more_subfields_xml); + push( @subfields, @{$unlinked_item_subfields} ) + if defined $unlinked_item_subfields and $#$unlinked_item_subfields > -1; + + my $field; + + $field = MARC::Field->new( + "$item_tag", ' ', ' ', @subfields + ) if @subfields; + + return $field; +} + =head2 Internal methods =head3 _type -- 2.23.0