Bug 40481: (follow-up) Simplify item loop in OPAC MARC detail

Now that the item loop only processes item fields from the filtered
$items resultset, several checks and patterns become unnecessary.

Changes:
- Remove dead checks: tag < 10 and tab != 10 guards
- Resolve $tagslib->{$item_tag} once instead of per-subfield
- Destructure ($code, $value) instead of indexed $subf[$i][0/1]
- Replace per-subfield grep over date kohafields with a hash lookup
- Remove redundant second $tagslib lookup for $kohafield
- Collapse repeatable if/else into a ternary

No functional changes.

Test plan:
1. Apply patch
2. Run the same test plan as the previous patch
=> SUCCESS: Behavior is identical
3. Sign off :-D

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
(cherry picked from commit 573550aec7)
Signed-off-by: Jacob O'Mara <Jacob.omara@openfifth.co.uk>
This commit is contained in:
Tomás Cohen Arazi 2026-03-04 10:16:43 -03:00 committed by Jacob O'Mara
parent 7f175e2a0c
commit 7f16087f60
No known key found for this signature in database
1 changed files with 21 additions and 32 deletions

View File

@ -271,54 +271,43 @@ for ( my $tabloop = 0 ; $tabloop <= 9 ; $tabloop++ ) {
$template->param( "tab" . $tabloop . "XX" => \@loop_data );
}
# now, build item tab !
# the main difference is that datas are in lines and not in columns : thus, we build the <th> first, then the values...
# loop through each tag
# warning : we may have different number of columns in each row. Thus, we first build a hash, complete it if necessary
# then construct template.
# $record has already had all the item fields filtered above.
# Now, build item tab!
# Data is in rows (one per item) not columns: build headers (%witness) and row data (@item_loop).
$items->reset();
my @fields = map { $_->as_marc_field } $items->as_list;
my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
my @fields = map { $_->as_marc_field } $items->as_list;
my $item_tag = $fields[0] ? $fields[0]->tag() : undef;
my $item_tagslib = $item_tag ? $tagslib->{$item_tag} : {};
my %witness;
my @item_subfield_codes;
my @item_loop;
my %date_fields = map { $_ => 1 }
qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
foreach my $field (@fields) {
next if ( $field->tag() < 10 );
my @subf = $field->subfields;
my $item;
# loop through each subfield
for my $i ( 0 .. $#subf ) {
my $sf_def = $tagslib->{ $field->tag() }->{ $subf[$i][0] };
next if ( ( $sf_def->{tab} || 0 ) != 10 );
for my $subf ( $field->subfields ) {
my ( $code, $value ) = @$subf;
my $sf_def = $item_tagslib->{$code};
next if ( ( $sf_def->{hidden} || 0 ) > 0 );
push @item_subfield_codes, $subf[$i][0];
$witness{ $subf[$i][0] } = $sf_def->{lib};
push @item_subfield_codes, $code;
$witness{$code} = $sf_def->{lib};
# Allow repeatables (BZ 13574)
if ( $item->{ $subf[$i][0] } ) {
$item->{ $subf[$i][0] } .= ' | ';
} else {
$item->{ $subf[$i][0] } = q{};
}
$item->{$code} = ( $item->{$code} ) ? $item->{$code} . ' | ' : q{};
if ( $sf_def->{isurl} ) {
$item->{ $subf[$i][0] } .= "<a href=\"$subf[$i][1]\">$subf[$i][1]</a>";
$item->{$code} .= "<a href=\"$value\">$value</a>";
} elsif ( $sf_def->{kohafield} eq "biblioitems.isbn" ) {
$item->{ $subf[$i][0] } .= $subf[$i][1];
$item->{$code} .= $value;
} else {
$item->{ $subf[$i][0] } .= GetAuthorisedValueDesc(
$field->tag(), $subf[$i][0],
$subf[$i][1], '', $tagslib, '', 'opac'
) // q{};
$item->{$code} .= GetAuthorisedValueDesc( $item_tag, $code, $value, '', $tagslib, '', 'opac' ) // q{};
}
my $kohafield = $tagslib->{ $field->tag() }->{ $subf[$i][0] }->{kohafield};
$item->{ $subf[$i][0] } = output_pref( { str => $item->{ $subf[$i][0] }, dateonly => 1 } )
if grep { $kohafield eq $_ }
qw( items.dateaccessioned items.onloan items.datelastseen items.datelastborrowed items.replacementpricedate );
$item->{$code} = output_pref( { str => $item->{$code}, dateonly => 1 } )
if $date_fields{ $sf_def->{kohafield} // q{} };
}
push @item_loop, $item if $item;
}