Bug 11592: Add should_hide_marc method to filter
[srvgit] / Koha / Filter / MARC / ViewPolicy.pm
1 package Koha::Filter::MARC::ViewPolicy;
2
3 # Copyright 2015 Mark Tompsett
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 =head1 NAME
21
22 Koha::Filter::MARC::ViewPolicy - this filters a MARC record.
23
24 =head1 VERSION
25
26 version 1.0
27
28 =head1 SYNOPSIS
29
30 my $processor = Koha::RecordProcessor->new( { filters => ('ViewPolicy') } );
31
32 =head1 DESCRIPTION
33
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
37 menu.
38
39 =cut
40
41 use Modern::Perl;
42 use Carp;
43 use C4::Biblio;
44
45 use base qw(Koha::RecordProcessor::Base);
46 our $NAME    = 'MARC_ViewPolicy';
47 our $VERSION = '3.23';              # Master version I hope it gets in.
48
49 use constant FIRST_NONCONTROL_TAG => 10;    # tags < 10 are control tags.
50
51 =head1 SUBROUTINES/METHODS
52
53 =head2 filter
54
55     my $processor = Koha::RecordProcessor->new( { filters => ('ViewPolicy') } );
56 ...
57     my $newrecord = $processor->filter($record);
58     my $newrecords = $processor->filter(\@records);
59
60 This returns a filtered copy of the record based on the Advanced constraints
61 visibility settings.
62
63 =cut
64
65 sub filter {
66     my $self    = shift;
67     my $precord = shift;
68     my @records;
69
70     if ( !$precord ) {
71         return $precord;
72     }
73
74     if ( ref($precord) eq 'ARRAY' ) {
75         @records = @{$precord};
76     }
77     else {
78         push @records, $precord;
79     }
80
81     foreach my $current_record (@records) {
82         my $result        = $current_record;
83         my $interface     = $self->{options}->{interface} // 'opac';
84         my $frameworkcode = $self->{options}->{frameworkcode} // q{};
85         my $hide          = _should_hide_on_interface();
86
87         my $marcsubfieldstructure = GetMarcStructure( 0, $frameworkcode );
88
89         #if ($marcsubfieldstructure->{'000'}->{'@'}->{hidden}>0) {
90         # LDR field is excluded from $current_record->fields().
91         # if we hide it here, the MARCXML->MARC::Record->MARCXML
92         # transformation blows up.
93         #}
94         foreach my $field ( $result->fields() ) {
95             _filter_field(
96                 {
97                     field                 => $field,
98                     marcsubfieldstructure => $marcsubfieldstructure,
99                     hide                  => $hide,
100                     interface             => $interface,
101                     result                => $result
102                 }
103             );
104         }
105     }
106     return;
107 }
108
109 sub _filter_field {
110     my ($parameter) = @_;
111
112     my $field                 = $parameter->{field};
113     my $marcsubfieldstructure = $parameter->{marcsubfieldstructure};
114     my $hide                  = $parameter->{hide};
115     my $interface             = $parameter->{interface};
116     my $result                = $parameter->{result};
117
118     my $tag = $field->tag();
119     if ( $tag >= FIRST_NONCONTROL_TAG ) {
120         foreach my $subpairs ( $field->subfields() ) {
121             my ( $subtag, $value ) = @{$subpairs};
122
123             # visibility is a "level" (-9 to +9), default to 0
124             # -8 is flagged, and 9/-9 are not implemented.
125             my $visibility =
126               $marcsubfieldstructure->{$tag}->{$subtag}->{hidden};
127             $visibility //= 0;
128             if ( $hide->{$interface}->{$visibility} ) {
129
130                 # deleting last subfield doesn't delete field, so
131                 # this detects that case to delete the field.
132                 if ( scalar $field->subfields() <= 1 ) {
133                     $result->delete_fields($field);
134                 }
135                 else {
136                     $field->delete_subfield( code => $subtag );
137                 }
138             }
139         }
140     }
141
142     # control tags don't have subfields, use @ trick.
143     else {
144         # visibility is a "level" (-9 to +9), default to 0
145         # -8 is flagged, and 9/-9 are not implemented.
146         my $visibility = $marcsubfieldstructure->{$tag}->{q{@}}->{hidden};
147         $visibility //= 0;
148         if ( $hide->{$interface}->{$visibility} ) {
149             $result->delete_fields($field);
150         }
151
152     }
153     return;
154 }
155
156 sub initialize {
157     my $self  = shift;
158     my $param = shift;
159
160     my $options = $param->{options};
161     $self->{options} = $options;
162     $self->Koha::RecordProcessor::Base::initialize($param);
163     return;
164 }
165
166 # Copied and modified from 3.10.x help file
167 # marc_subfields_structure.hidden
168 # allows you to select from 19 possible visibility conditions, 17 of which are implemented. They are the following:
169 # -9 => Future use
170 # -8 => Flag
171 # -7 => OPAC !Intranet !Editor Collapsed
172 # -6 => OPAC Intranet !Editor !Collapsed
173 # -5 => OPAC Intranet !Editor Collapsed
174 # -4 => OPAC !Intranet !Editor !Collapsed
175 # -3 => OPAC !Intranet Editor Collapsed
176 # -2 => OPAC !Intranet Editor !Collapsed
177 # -1 => OPAC Intranet Editor Collapsed
178 # 0 => OPAC Intranet Editor !Collapsed
179 # 1 => !OPAC Intranet Editor Collapsed
180 # 2 => !OPAC !Intranet Editor !Collapsed
181 # 3 => !OPAC !Intranet Editor Collapsed
182 # 4 => !OPAC Intranet Editor !Collapsed
183 # 5 => !OPAC !Intranet !Editor Collapsed
184 # 6 => !OPAC Intranet !Editor !Collapsed
185 # 7 => !OPAC Intranet !Editor Collapsed
186 # 8 => !OPAC !Intranet !Editor !Collapsed
187 # 9 => Future use
188 # ( ! means 'not visible' or in the case of Collapsed 'not Collapsed')
189
190 sub _should_hide_on_interface {
191     my $hide = {
192         opac => {
193             '-8' => 1,
194             '1'  => 1,
195             '2'  => 1,
196             '3'  => 1,
197             '4'  => 1,
198             '5'  => 1,
199             '6'  => 1,
200             '7'  => 1,
201             '8'  => 1,
202         },
203         intranet => {
204             '-8' => 1,
205             '-7' => 1,
206             '-4' => 1,
207             '-3' => 1,
208             '-2' => 1,
209             '2'  => 1,
210             '3'  => 1,
211             '5'  => 1,
212             '8'  => 1,
213         },
214     };
215     return $hide;
216 }
217
218 =head2 should_hide_marc
219
220 Return a hash reference of whether a field, built from
221 kohafield and tag, is hidden (1) or not (0) for a given
222 interface
223
224   my $OpacHideMARC =
225     should_hide_marc( {
226                         frameworkcode => $frameworkcode,
227                         interface     => 'opac',
228                       } );
229
230   if ($OpacHideMARC->{'stocknumber'}==1) {
231        print "Hidden!\n";
232   }
233
234 C<$OpacHideMARC> is a ref to a hash which contains a series
235 of key value pairs indicating if that field (key) is
236 hidden (value == 1) or not (value == 0).
237
238 C<$frameworkcode> is the framework code.
239
240 C<$interface> is the interface. It defaults to 'opac' if
241 nothing is passed. Valid values include 'opac' or 'intranet'.
242
243 =cut
244
245 sub should_hide_marc {
246     my ( $self, $parms ) = @_;
247     my $frameworkcode = $parms->{frameworkcode} // q{};
248     my $interface     = $parms->{interface}     // 'opac';
249     my $hide          = _should_hide_on_interface();
250
251     my %shouldhidemarc;
252     my $marc_subfield_structure = GetMarcStructure( 0, $frameworkcode );
253     foreach my $tag ( keys %{$marc_subfield_structure} ) {
254         foreach my $subtag ( keys %{ $marc_subfield_structure->{$tag} } ) {
255             my $subfield_record = $marc_subfield_structure->{$tag}->{$subtag};
256             if ( ref $subfield_record eq 'HASH' ) {
257                 my $kohafield = $subfield_record->{'kohafield'};
258                 if ($kohafield) {
259                     my @tmpsplit   = split /[.]/xsm, $kohafield;
260                     my $field      = $tmpsplit[-1];
261                     my $hidden     = $subfield_record->{'hidden'};
262                     my $shouldhide = $hide->{$interface}->{$hidden};
263                     if ($shouldhide) {
264                         $shouldhidemarc{$field} = 1;
265                     }
266                     elsif ( !exists $shouldhidemarc{$field} ) {
267                         $shouldhidemarc{$field} = 0;
268                     }
269                 }
270             }
271         }
272     }
273
274     return \%shouldhidemarc;
275 }
276
277 =head1 DIAGNOSTICS
278
279  $ prove -v t/RecordProcessor.t
280  $ prove -v t/db_dependent/Filter_MARC_ViewPolicy.t
281
282 =head1 CONFIGURATION AND ENVIRONMENT
283
284 Install Koha. This filter will be used appropriately by the OPAC or Staff client.
285
286 =head1 INCOMPATIBILITIES
287
288 This is designed for MARC::Record filtering currently. It will not handle MARC::MARCXML.
289
290 =head1 DEPENDENCIES
291
292 The following Perl libraries are required: Modern::Perl and Carp.
293 The following Koha libraries are required: C4::Biblio, Koha::RecordProcessor, and Koha::RecordProcessor::Base.
294 These should all be installed if the koha-common package is installed or Koha is otherwise installed.
295
296 =head1 BUGS AND LIMITATIONS
297
298 This is the initial version. Please feel free to report bugs
299 at http://bugs.koha-community.org/.
300
301 =head1 AUTHOR
302
303 Mark Tompsett
304
305 =head1 LICENSE AND COPYRIGHT
306
307 Copyright 2015 Mark Tompsett
308
309 This file is part of Koha.
310
311 Koha is free software; you can redistribute it and/or modify it
312 under the terms of the GNU General Public License as published by
313 the Free Software Foundation; either version 3 of the License, or
314 (at your option) any later version.
315
316 Koha is distributed in the hope that it will be useful, but
317 WITHOUT ANY WARRANTY; without even the implied warranty of
318 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
319 GNU General Public License for more details.
320
321 You should have received a copy of the GNU General Public License
322 along with Koha; if not, see <http://www.gnu.org/licenses>.
323
324 =cut
325
326 1;