From 9b5b2177b81f6c20239116d5ecb34d9a0247e97f Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Wed, 27 Feb 2019 17:01:35 +0200 Subject: [PATCH] Bug 20664: (follow-up) Switch to Koha objects for retrieving items Signed-off-by: Josef Moravec --- C4/Biblio.pm | 24 ++++++++---------------- C4/Items.pm | 35 ++++++++++++++++------------------- 2 files changed, 24 insertions(+), 35 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 78d0754a81..36e3678d12 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1075,22 +1075,14 @@ sub GetMarcSubfieldStructure { sub GetMarcFromKohaField { my ( $kohafield ) = @_; return unless $kohafield; - - my $cache = Koha::Caches->get_instance(); - my $cache_key = "MarcFromKohaField-$kohafield"; - my $cached = $cache->get_from_cache($cache_key); - if ( !defined $cached ) { - # The next call uses the Default framework since it is AUTHORITATIVE - # for all Koha to MARC mappings. - my $mss = GetMarcSubfieldStructure( '', { unsafe => 1 } ); # Do not change framework - my @retval; - foreach( @{ $mss->{$kohafield} } ) { - push @retval, $_->{tagfield}, $_->{tagsubfield}; - } - $cached = \@retval; - $cache->set_in_cache( $cache_key, $cached ); - } - return wantarray ? @$cached : ( @$cached ? $cached->[0] : undef ); + # The next call uses the Default framework since it is AUTHORITATIVE + # for all Koha to MARC mappings. + my $mss = GetMarcSubfieldStructure( '', { unsafe => 1 } ); # Do not change framework + my @retval; + foreach( @{ $mss->{$kohafield} } ) { + push @retval, $_->{tagfield}, $_->{tagsubfield}; + } + return wantarray ? @retval : ( @retval ? $retval[0] : undef ); } =head2 GetMarcSubfieldStructureFromKohaField diff --git a/C4/Items.pm b/C4/Items.pm index d136561f0e..643731cbc2 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -1354,25 +1354,29 @@ Returns an array of MARC::Record objects of the items for the biblio. sub GetMarcItemFields { my ( $biblionumber, $itemnumbers, $hidingrules ) = @_; - my $item_level_itype = C4::Context->preference('item-level_itypes'); - # This is so much faster than using Koha::Items->search that it makes sense even if it's ugly. - my $query = 'SELECT * FROM items WHERE biblionumber = ?'; + my $params = { + biblionumber => $biblionumber + }; if (@$itemnumbers) { - $query .= ' AND itemnumber IN (' . join(',', @$itemnumbers) . ')'; + my @itemnumberlist; + foreach my $itemnumber (@$itemnumbers) { + push @itemnumberlist, {itemnumber => $itemnumber}; + } + $params->{-or} = \@itemnumberlist; } - my $sth = C4::Context->dbh->prepare($query); - $sth->execute($biblionumber); - my $items = $sth->fetchall_arrayref({}); - $sth->finish(); + my $items = Koha::Items->search($params); my @item_fields; my ($itemtag, $itemsubfield) = GetMarcFromKohaField('items.itemnumber'); - ITEMLOOP: foreach my $item (@$items) { + ITEMLOOP: while ( my $item = $items->next() ) { + + my $item_unblessed = $item->unblessed; + $item_unblessed->{itype} = $item->effective_itemtype() unless $item_unblessed->{itype}; # Check hiding rules if (defined $hidingrules) { foreach my $field (keys %$hidingrules) { - my $val = $item->{$field}; + my $val = $item_unblessed->{$field}; $val = '' unless defined $val; # If the results matches the values in the hiding rules, skip the item @@ -1382,17 +1386,10 @@ sub GetMarcItemFields { } } - # Set correct item type - if (!$item_level_itype || !$item->{itype}) { - warn 'item-level_itypes set but no itemtype set for item (' . $item->{itemnumber} . ')' if (!$item->{itype}); - my $biblioitem = Koha::Biblioitems->find($item->{biblioitemnumber}); - $item->{itype} = $biblioitem->itemtype(); - } - my $mungeditem = { map { - defined($item->{$_}) && $item->{$_} ne '' ? ("items.$_" => $item->{$_}) : () - } keys %{ $item } + defined($item_unblessed->{$_}) && $item_unblessed->{$_} ne '' ? ("items.$_" => $item_unblessed->{$_}) : () + } keys %{ $item_unblessed } }; my $itemmarc = TransformKohaToMarc($mungeditem); -- 2.11.0