View | Details | Raw Unified | Return to bug 11592
Collapse All | Expand All

(-)a/Koha/Filter/MARC/ViewPolicy.pm (+59 lines)
Lines 215-220 sub _should_hide_on_interface { Link Here
215
    return $hide;
215
    return $hide;
216
}
216
}
217
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
218
=head1 DIAGNOSTICS
277
=head1 DIAGNOSTICS
219
278
220
 $ prove -v t/RecordProcessor.t
279
 $ prove -v t/RecordProcessor.t
(-)a/t/db_dependent/Filter_MARC_ViewPolicy.t (-15 / +40 lines)
Lines 30-36 use MARC::Record; Link Here
30
use MARC::Field;
30
use MARC::Field;
31
use C4::Context;
31
use C4::Context;
32
use C4::Biblio;
32
use C4::Biblio;
33
use Koha::Cache qw/flush_all/;
33
use Koha::Cache qw/get_instance flush_all/;
34
use Koha::Database;
34
use Koha::Database;
35
35
36
BEGIN {
36
BEGIN {
Lines 60-72 sub run_hiding_tests { Link Here
60
60
61
    my ( $isbn_field, $isbn_subfield ) =
61
    my ( $isbn_field, $isbn_subfield ) =
62
      GetMarcFromKohaField( 'biblioitems.isbn', q{} );
62
      GetMarcFromKohaField( 'biblioitems.isbn', q{} );
63
    my $update_sql =
63
    my $update_sql = q{UPDATE marc_subfield_structure SET hidden=? };
64
        q{UPDATE marc_subfield_structure SET hidden=? }
64
    my $sth        = $dbh->prepare($update_sql);
65
      . q{WHERE tagfield='}
66
      . $isbn_field
67
      . q{' OR }
68
      . q{      tagfield='008';};
69
    my $sth = $dbh->prepare($update_sql);
70
    foreach my $hidden_value (@valid_hidden_values) {
65
    foreach my $hidden_value (@valid_hidden_values) {
71
66
72
        $sth->execute($hidden_value);
67
        $sth->execute($hidden_value);
Lines 99-108 sub run_hiding_tests { Link Here
99
        if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
94
        if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
100
95
101
            # Subfield and controlfield are set to be hidden
96
            # Subfield and controlfield are set to be hidden
102
            is( $filtered_record->field('020'),
97
            is( $filtered_record->field($isbn_field),
103
                undef,
98
                undef,
104
                "Data field has been deleted because of hidden=$hidden_value" );
99
                "Data field has been deleted because of hidden=$hidden_value" );
105
            isnt( $unfiltered_record->field('020'), undef,
100
            isnt( $unfiltered_record->field($isbn_field), undef,
106
"Data field has been deleted in the original record because of hidden=$hidden_value"
101
"Data field has been deleted in the original record because of hidden=$hidden_value"
107
            );
102
            );
108
103
Lines 118-127 sub run_hiding_tests { Link Here
118
113
119
        }
114
        }
120
        else {
115
        else {
121
            isnt( $filtered_record->field('020'), undef,
116
            isnt( $filtered_record->field($isbn_field), undef,
122
                "Data field hasn't been deleted because of hidden=$hidden_value"
117
                "Data field hasn't been deleted because of hidden=$hidden_value"
123
            );
118
            );
124
            isnt( $unfiltered_record->field('020'), undef,
119
            isnt( $unfiltered_record->field($isbn_field), undef,
125
"Data field hasn't been deleted in the original record because of hidden=$hidden_value"
120
"Data field hasn't been deleted in the original record because of hidden=$hidden_value"
126
            );
121
            );
127
122
Lines 133-143 sub run_hiding_tests { Link Here
133
"Control field hasn't been deleted in the original record because of hidden=$hidden_value"
128
"Control field hasn't been deleted in the original record because of hidden=$hidden_value"
134
            );
129
            );
135
130
131
            # force all the hidden values the same, so filtered and unfiltered
132
            # records should be identical.
136
            is_deeply( $filtered_record, $unfiltered_record,
133
            is_deeply( $filtered_record, $unfiltered_record,
137
                'Records are the same' );
134
                'Records are the same' );
138
        }
135
        }
139
136
140
    }
137
    }
138
139
    $sth->execute(-1); # -1 is visible in opac and intranet.
140
141
    my $cache = Koha::Cache->get_instance();
142
    $cache->flush_all();    # easy way to ensure DB is queried again.
143
144
    my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
145
        {
146
            frameworkcode => q{},
147
            interface     => $interface
148
        }
149
    );
150
    my @hiddenfields = grep { $shouldhidemarc->{$_}==1 } keys %{$shouldhidemarc};
151
152
    $sth->execute(8); # 8 is invisible in opac and intranet.
153
154
    $cache->flush_all();    # easy way to ensure DB is queried again.
155
156
    $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
157
        {
158
            frameworkcode => q{},
159
            interface     => $interface
160
        }
161
    );
162
    my @keyvalues = keys %{$shouldhidemarc};
163
    my @visiblefields = grep { $shouldhidemarc->{$_}==1 } @keyvalues;
164
165
    is(scalar @hiddenfields,0,'Should Hide MARC - Full Visibility');
166
    is_deeply(\@visiblefields,\@keyvalues,'Should Hide MARC - No Visibility');
141
    return;
167
    return;
142
}
168
}
143
169
Lines 164-170 sub create_marc_record { Link Here
164
190
165
subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
191
subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
166
192
167
    plan tests => 102;
193
    plan tests => 104;
168
194
169
    $schema->storage->txn_begin();
195
    $schema->storage->txn_begin();
170
    run_hiding_tests('opac');
196
    run_hiding_tests('opac');
Lines 173-179 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub { Link Here
173
199
174
subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
200
subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
175
201
176
    plan tests => 102;
202
    plan tests => 104;
177
203
178
    $schema->storage->txn_begin();
204
    $schema->storage->txn_begin();
179
    run_hiding_tests('intranet');
205
    run_hiding_tests('intranet');
180
- 

Return to bug 11592