From ef4fe34f9797c8e4467422a37113817e94c5c9ba Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Tue, 21 Jan 2014 15:57:25 -0500 Subject: [PATCH] Bug 11592 - opac scripts do not respect marc tag visibility Added two functions to C4/Biblio: GetFilteredOpacBiblio and GetOpacHideMARC. GetFilteredOpacBiblio returns a MARC::Record stripped of all the fields and subfields which are supposed to be hidden in OPAC. GetOpacHideMARC returns a hash of whether a particular key (eg. title, subtitle, etc.) is hidden. A value of 0 means no, a value of 1 means hidden. Made GetCOinSBiblio function handle hiding of 245$a more gracefully. Also, modified GetMarcSubjects to not generate an array entry of empty values. Tweaked C4/XSLT.pm to delete a field, if there weren't any subfields to actually update with. Properly hid 245$a, in the case that there is no visibility for OPAC, for opac-MARCdetail. opac-detail now filters the MARC::Record according to the hidden value in marc_subfield_structure properly. It also corrects a couple minor issues with a parameter not passed being used in a concatenation. How the subtitle values calculation works changed. It is now based on marc_subfield_structure and not fieldmapping. And, the GetBiblioData hash that generates template variables is now filtered by the results of a GetOpacHideMARC call. Lastly, opac-showmarc now converts the MARCXML record to a MARC::Record to easily filter using GetFilteredOpacBiblio, which is then turned back into MARCXML for processing. --- C4/Biblio.pm | 63 ++++++++++++++++++++++++++++++++++++++++++++++++- C4/XSLT.pm | 1 - opac/opac-detail.pl | 25 ++++---------------- opac/opac-showmarc.pl | 6 +++++ 4 files changed, 73 insertions(+), 22 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 97dcb07..157f26a 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -80,6 +80,7 @@ BEGIN { &GetMarcHosts GetMarcUrls &GetOpacHideMARC + &GetFilteredOpacBiblio &GetUsedMarcStructure &GetXmlBiblio &GetCOinSBiblio @@ -1230,7 +1231,7 @@ C<$frameworkcode> is the framework code. sub GetOpacHideMARC { my ( $frameworkcode ) = shift || ''; my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("select substring(kohafield,locate('.',kohafield)+1,40) as field,tagfield as tag,hidden from marc_subfield_structure where length(trim(kohafield))>0 and frameworkcode=? order by field,tagfield;"); + my $sth = $dbh->prepare("SELECT SUBSTRING(kohafield,LOCATE('.',kohafield)+1,40) AS field,tagfield AS tag,hidden FROM marc_subfield_structure WHERE LENGTH(TRIM(kohafield))>0 AND frameworkcode=? ORDER BY field,tagfield;"); my $rv = $sth->execute($frameworkcode); my %opachidemarc; my $data = $sth->fetchall_hashref( [ qw(field tag) ] ); @@ -1247,6 +1248,66 @@ sub GetOpacHideMARC { return \%opachidemarc; } +=head2 GetFilteredOpacBiblio + +Return a MARC::Record which has been filtered based +on the corresponding marc_subfield_structure records +for the given framework by excluding hidden>0. + + my $filtered_record = GetFilteredOpacBiblio($record,$frameworkcode); + +C<$record> is a MARC::Record. + +C<$frameworkcode> is the framework code. + +=cut + +sub GetFilteredOpacBiblio { + my ( $record ) = shift; + if (!$record) { return $record; } + + my ( $frameworkcode ) = shift || ''; + + my $marcsubfieldstructure = GetMarcStructure(0,$frameworkcode); + if ($marcsubfieldstructure->{'000'}->{'@'}->{hidden}>0) { + # LDR field is excluded from $record->fields(). + # if we hide it here, the MARCXML->MARC::Record->MARCXML transformation blows up. + print STDERR "Supposed to hide LEADER\n"; + } + foreach my $fields ($record->fields()) { + my $tag = $fields->tag(); + if ($tag>=10) { + foreach my $subpairs ($fields->subfields()) { + my ($subtag,$value) = @$subpairs; + my $hidden = $marcsubfieldstructure->{$tag}->{$subtag}->{hidden}; + $hidden //= 0; + $hidden = ($hidden<=0) ? 0 : 1; + if ($hidden) { + # deleting last subfield doesn't delete field, so + # this detects that case to delete the field. + if (scalar $fields->subfields() <= 1) { + $record->delete_fields($fields); + } + else { + $fields->delete_subfield( code => $subtag ); + } + } + } + } + # tags less than 10 don't have subfields, use @ trick. + else { + my $hidden = $marcsubfieldstructure->{$tag}->{'@'}->{hidden}; + $hidden //= 0; + $hidden = ($hidden<=0) ? 0 : 1; + if ($hidden) { + $record->delete_fields($fields); + } + } + } + + return $record; +} + =head2 GetMarcFromKohaField ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode); diff --git a/C4/XSLT.pm b/C4/XSLT.pm index 77f68a8..2a43a41 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -84,7 +84,6 @@ sub transformMARCXML4XSLT { if ($@) { warn "PROBLEM WITH RECORD"; next; } my $av = getAuthorisedValues4MARCSubfields($frameworkcode); foreach my $tag ( keys %$av ) { - print STDERR "TAG: $tag\n"; foreach my $field ( $record->field( $tag ) ) { if ( $av->{ $tag } ) { my @new_subfields = (); diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 2151140..6154646 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -51,7 +51,6 @@ use C4::Images; use Koha::DateUtils; use C4::HTML5Media; use C4::CourseReserves qw(GetItemCourseReservesInfo); -use Data::Dumper; BEGIN { if (C4::Context->preference('BakerTaylorEnabled')) { @@ -94,24 +93,7 @@ $template->param( biblionumber => $biblionumber ); # Filter out the things to hide in OPAC. my $frameworkcode = GetFrameworkCode($biblionumber); -my $marcsubfieldstructure = GetMarcStructure(0,$frameworkcode); -foreach my $fields ($record->fields()) { - my $tag = $fields->tag(); - foreach my $subpairs ($fields->subfields()) { - my ($subtag,$value) = @$subpairs; - my $hidden = $marcsubfieldstructure->{$tag}->{$subtag}->{hidden}; - $hidden //= 0; - $hidden = ($hidden<=0) ? 0 : 1; - if ($hidden) { - if (scalar $fields->subfields() == 1) { - $record->delete_fields($fields); - } - else { - $fields->delete_subfield( code => $subtag ); - } - } - } -} +#$record = GetFilteredOpacBiblio($record,$frameworkcode); # get biblionumbers stored in the cart my @cart_list; @@ -675,7 +657,10 @@ if ($st_tag && $st_subtag) { my @subtitles = $record->subfield($st_tag,$st_subtag); $subtitle = \@subtitles if scalar @subtitles; } -print STDERR Dumper($subtitle); +if (scalar @$subtitle) { + my @subtitles = map { { subfield => $_ } } @$subtitle; + $subtitle = \@subtitles; +} $template->param( MARCNOTES => $marcnotesarray, diff --git a/opac/opac-showmarc.pl b/opac/opac-showmarc.pl index 71fb804..380038b 100755 --- a/opac/opac-showmarc.pl +++ b/opac/opac-showmarc.pl @@ -57,6 +57,12 @@ else { if ($view eq 'card' || $view eq 'html') { my $xmlrecord= $importid? $record->as_xml(): GetXmlBiblio($biblionumber); + if (!$importid && $view eq 'html') { + my $filtered_record = MARC::Record->new_from_xml($xmlrecord); + my $frameworkcode = GetFrameworkCode($biblionumber); + $filtered_record = GetFilteredOpacBiblio($filtered_record,$frameworkcode); + $xmlrecord = $filtered_record->as_xml(); + } my $xslfile; my $xslfilename; my $htdocs = C4::Context->config('opachtdocs'); -- 1.7.9.5