1 package Koha::Filter::MARC::ViewPolicy;
3 # Copyright 2015 Mark Tompsett
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 Koha::Filter::MARC::ViewPolicy - this filters a MARC record.
30 my $processor = Koha::RecordProcessor->new( { filters => ('ViewPolicy') } );
34 Filter to remove fields based on the 'Advance constraints'
35 settings found when editing a particular subfield definition of
36 a MARC bibliographic framework found under the Koha administration
42 use C4::Biblio qw( GetMarcStructure );
44 use base qw(Koha::RecordProcessor::Base);
45 our $NAME = 'MARC_ViewPolicy';
46 our $VERSION = '3.23'; # Master version I hope it gets in.
48 use constant FIRST_NONCONTROL_TAG => 10; # tags < 10 are control tags.
50 =head1 SUBROUTINES/METHODS
54 my $processor = Koha::RecordProcessor->new( { filters => ('ViewPolicy') } );
56 my $newrecord = $processor->filter($record);
57 my $newrecords = $processor->filter(\@records);
59 This returns a filtered copy of the record based on the Advanced constraints
73 if ( ref($precord) eq 'ARRAY' ) {
74 @records = @{$precord};
77 push @records, $precord;
80 my $params = $self->params;
81 my $interface = $params->{options}->{interface} // 'opac';
82 my $frameworkcode = $params->{options}->{frameworkcode} // q{};
84 foreach my $current_record (@records) {
85 my $result = $current_record;
86 my $hide = _should_hide_on_interface();
88 my $marcsubfieldstructure = C4::Biblio::GetMarcStructure( 0, $frameworkcode, { unsafe => 1 } );
90 #if ($marcsubfieldstructure->{'000'}->{'@'}->{hidden}>0) {
91 # LDR field is excluded from $current_record->fields().
92 # if we hide it here, the MARCXML->MARC::Record->MARCXML
93 # transformation blows up.
95 foreach my $field ( $result->fields() ) {
99 marcsubfieldstructure => $marcsubfieldstructure,
101 interface => $interface,
111 my ($parameter) = @_;
113 my $field = $parameter->{field};
114 my $marcsubfieldstructure = $parameter->{marcsubfieldstructure};
115 my $hide = $parameter->{hide};
116 my $interface = $parameter->{interface};
117 my $result = $parameter->{result};
119 my $tag = $field->tag();
120 if ( $tag >= FIRST_NONCONTROL_TAG ) {
121 foreach my $subpairs ( $field->subfields() ) {
122 my ( $subtag, $value ) = @{$subpairs};
124 # visibility is a "level" (-9 to +9), default to 0
125 # -8 is flagged, and 9/-9 are not implemented.
127 $marcsubfieldstructure->{$tag}->{$subtag}->{hidden};
129 if ( $hide->{$interface}->{$visibility} ) {
131 # deleting last subfield doesn't delete field, so
132 # this detects that case to delete the field.
133 if ( scalar $field->subfields() <= 1 ) {
134 $result->delete_fields($field);
137 $field->delete_subfield( code => $subtag );
143 # control tags don't have subfields, use @ trick.
145 # visibility is a "level" (-9 to +9), default to 0
146 # -8 is flagged, and 9/-9 are not implemented.
147 my $visibility = $marcsubfieldstructure->{$tag}->{q{@}}->{hidden};
149 if ( $hide->{$interface}->{$visibility} ) {
150 $result->delete_fields($field);
157 # Copied and modified from 3.10.x help file
158 # marc_subfields_structure.hidden
159 # allows you to select from 19 possible visibility conditions, 17 of which are implemented. They are the following:
162 # -7 => OPAC !Intranet !Editor Collapsed
163 # -6 => OPAC Intranet !Editor !Collapsed
164 # -5 => OPAC Intranet !Editor Collapsed
165 # -4 => OPAC !Intranet !Editor !Collapsed
166 # -3 => OPAC !Intranet Editor Collapsed
167 # -2 => OPAC !Intranet Editor !Collapsed
168 # -1 => OPAC Intranet Editor Collapsed
169 # 0 => OPAC Intranet Editor !Collapsed
170 # 1 => !OPAC Intranet Editor Collapsed
171 # 2 => !OPAC !Intranet Editor !Collapsed
172 # 3 => !OPAC !Intranet Editor Collapsed
173 # 4 => !OPAC Intranet Editor !Collapsed
174 # 5 => !OPAC !Intranet !Editor Collapsed
175 # 6 => !OPAC Intranet !Editor !Collapsed
176 # 7 => !OPAC Intranet !Editor Collapsed
177 # 8 => !OPAC !Intranet !Editor !Collapsed
179 # ( ! means 'not visible' or in the case of Collapsed 'not Collapsed')
181 sub _should_hide_on_interface {
209 =head2 should_hide_marc
211 Return a hash reference of whether a field, built from
212 kohafield and tag, is hidden (1) or not (0) for a given
217 frameworkcode => $frameworkcode,
221 if ($OpacHideMARC->{'stocknumber'}==1) {
225 C<$OpacHideMARC> is a ref to a hash which contains a series
226 of key value pairs indicating if that field (key) is
227 hidden (value == 1) or not (value == 0).
229 C<$frameworkcode> is the framework code.
231 C<$interface> is the interface. It defaults to 'opac' if
232 nothing is passed. Valid values include 'opac' or 'intranet'.
236 sub should_hide_marc {
237 my ( $self, $parms ) = @_;
238 my $frameworkcode = $parms->{frameworkcode} // q{};
239 my $interface = $parms->{interface} // 'opac';
240 my $hide = _should_hide_on_interface();
243 my $marc_subfield_structure = GetMarcStructure( 0, $frameworkcode );
244 foreach my $tag ( keys %{$marc_subfield_structure} ) {
245 foreach my $subtag ( keys %{ $marc_subfield_structure->{$tag} } ) {
246 my $subfield_record = $marc_subfield_structure->{$tag}->{$subtag};
247 if ( ref $subfield_record eq 'HASH' ) {
248 my $kohafield = $subfield_record->{'kohafield'};
250 my @tmpsplit = split /[.]/xsm, $kohafield;
251 my $field = $tmpsplit[-1];
252 my $hidden = $subfield_record->{'hidden'};
253 my $shouldhide = $hide->{$interface}->{$hidden};
255 $shouldhidemarc{$field} = 1;
257 elsif ( !exists $shouldhidemarc{$field} ) {
258 $shouldhidemarc{$field} = 0;
265 return \%shouldhidemarc;
270 $ prove -v t/RecordProcessor.t
271 $ prove -v t/db_dependent/Filter_MARC_ViewPolicy.t
273 =head1 CONFIGURATION AND ENVIRONMENT
275 Install Koha. This filter will be used appropriately by the OPAC or staff interface.
277 =head1 INCOMPATIBILITIES
279 This is designed for MARC::Record filtering currently. It will not handle MARC::MARCXML.
283 The following Perl libraries are required: Modern::Perl and Carp.
284 The following Koha libraries are required: C4::Biblio, Koha::RecordProcessor, and Koha::RecordProcessor::Base.
285 These should all be installed if the koha-common package is installed or Koha is otherwise installed.
287 =head1 BUGS AND LIMITATIONS
289 This is the initial version. Please feel free to report bugs
290 at http://bugs.koha-community.org/.
296 =head1 LICENSE AND COPYRIGHT
298 Copyright 2015 Mark Tompsett
300 This file is part of Koha.
302 Koha is free software; you can redistribute it and/or modify it
303 under the terms of the GNU General Public License as published by
304 the Free Software Foundation; either version 3 of the License, or
305 (at your option) any later version.
307 Koha is distributed in the hope that it will be useful, but
308 WITHOUT ANY WARRANTY; without even the implied warranty of
309 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
310 GNU General Public License for more details.
312 You should have received a copy of the GNU General Public License
313 along with Koha; if not, see <http://www.gnu.org/licenses>.