| 
      
            Line 0
          
      
      
        Link Here
      
     | 
  
            
               | 
               | 
              1 | 
              package Koha::Filter::MARC::OpacHiddenItems;  | 
            
            
              | 2 | 
               | 
            
            
              | 3 | 
              # Copyright 2016 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::OpacHiddenItems - 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 => ('OpacHiddenItems') } ); | 
            
            
              | 31 | 
               | 
            
            
              | 32 | 
              =head1 DESCRIPTION  | 
            
            
              | 33 | 
               | 
            
            
              | 34 | 
              Filter to remove fields based on the OPAC system preference OpacHiddenItems.  | 
            
            
              | 35 | 
               | 
            
            
              | 36 | 
              =cut  | 
            
            
              | 37 | 
               | 
            
            
              | 38 | 
              use C4::Biblio;  | 
            
            
              | 39 | 
              use Carp;  | 
            
            
              | 40 | 
              use Const::Fast;  | 
            
            
              | 41 | 
              use English qw( -no_match_vars );  | 
            
            
              | 42 | 
              use List::Util qw/any/;  | 
            
            
              | 43 | 
              use Modern::Perl;  | 
            
            
              | 44 | 
              use YAML;  | 
            
            
              | 45 | 
               | 
            
            
              | 46 | 
              use base qw(Koha::RecordProcessor::Base);  | 
            
            
              | 47 | 
               | 
            
            
              | 48 | 
              our $NAME    = 'MARC_OpacHiddenItems';  | 
            
            
              | 49 | 
              our $VERSION = '3.25';                   # Master version I hope it gets in.  | 
            
            
              | 50 | 
               | 
            
            
              | 51 | 
              const my $FIRST_NONCONTROL_TAG => 10;    # tags < 10 are control tags.  | 
            
            
              | 52 | 
               | 
            
            
              | 53 | 
              =head1 SUBROUTINES/METHODS  | 
            
            
              | 54 | 
               | 
            
            
              | 55 | 
              =head2 filter  | 
            
            
              | 56 | 
               | 
            
            
              | 57 | 
                  my $processor = Koha::RecordProcessor->new( { filters => ('OpacHiddenItems') } ); | 
            
            
              | 58 | 
              ...  | 
            
            
              | 59 | 
                  my $newrecord = $processor->filter($record);  | 
            
            
              | 60 | 
                  my $newrecords = $processor->filter(\@records);  | 
            
            
              | 61 | 
               | 
            
            
              | 62 | 
              This returns a filtered copy of the record based on the Advanced constraints  | 
            
            
              | 63 | 
              visibility settings.  | 
            
            
              | 64 | 
               | 
            
            
              | 65 | 
              =cut  | 
            
            
              | 66 | 
               | 
            
            
              | 67 | 
              sub filter { | 
            
            
              | 68 | 
                  my $self    = shift;  | 
            
            
              | 69 | 
                  my $precord = shift;  | 
            
            
              | 70 | 
                  my @records;  | 
            
            
              | 71 | 
               | 
            
            
              | 72 | 
                  if ( !$precord ) { | 
            
            
              | 73 | 
                      return $precord;  | 
            
            
              | 74 | 
                  }  | 
            
            
              | 75 | 
               | 
            
            
              | 76 | 
                  if ( ref($precord) eq 'ARRAY' ) { | 
            
            
              | 77 | 
                      @records = @{$precord}; | 
            
            
              | 78 | 
                  }  | 
            
            
              | 79 | 
                  else { | 
            
            
              | 80 | 
                      push @records, $precord;  | 
            
            
              | 81 | 
                  }  | 
            
            
              | 82 | 
               | 
            
            
              | 83 | 
                  my $yaml = C4::Context->preference('OpacHiddenItems'); | 
            
            
              | 84 | 
                  return () if ( !$yaml =~ /\S/xsm );  | 
            
            
              | 85 | 
                  $yaml = "$yaml\n\n";    # YAML is anal on ending \n. Surplus does not hurt  | 
            
            
              | 86 | 
                  my $hidingrules;  | 
            
            
              | 87 | 
                  my $return_value = eval { $hidingrules = YAML::Load($yaml); }; | 
            
            
              | 88 | 
                  if ( $EVAL_ERROR || !$return_value ) { | 
            
            
              | 89 | 
                      carp  | 
            
            
              | 90 | 
              "Unable to parse OpacHiddenItems syspref : $EVAL_ERROR ($return_value)";  | 
            
            
              | 91 | 
                      return;  | 
            
            
              | 92 | 
                  }  | 
            
            
              | 93 | 
                  my $dbh = C4::Context->dbh;  | 
            
            
              | 94 | 
               | 
            
            
              | 95 | 
                  foreach my $current_record (@records) { | 
            
            
              | 96 | 
                      my $biblionumber =  | 
            
            
              | 97 | 
                        C4::Biblio::get_koha_field_from_marc( 'biblio', 'biblionumber',  | 
            
            
              | 98 | 
                          $current_record, q{} ); | 
            
            
              | 99 | 
                      my $frameworkcode = q{}; | 
            
            
              | 100 | 
                      if ( defined $biblionumber ) { | 
            
            
              | 101 | 
                          my $biblio = GetBiblio($biblionumber);  | 
            
            
              | 102 | 
                          $frameworkcode = $biblio->{'frameworkcode'} // q{}; | 
            
            
              | 103 | 
                      }  | 
            
            
              | 104 | 
                      _filter_record( $hidingrules, $current_record, $frameworkcode );  | 
            
            
              | 105 | 
                  }  | 
            
            
              | 106 | 
                  return;  | 
            
            
              | 107 | 
              }  | 
            
            
              | 108 | 
               | 
            
            
              | 109 | 
              sub _filter_record { | 
            
            
              | 110 | 
                  my ( $hidingrules, $current_record, $frameworkcode ) = @_;  | 
            
            
              | 111 | 
               | 
            
            
              | 112 | 
                  my $dbh = C4::Context->dbh;  | 
            
            
              | 113 | 
               | 
            
            
              | 114 | 
                  # Run through the MARC record's subfields...  | 
            
            
              | 115 | 
                  my @fields = $current_record->fields();  | 
            
            
              | 116 | 
                  foreach my $field (@fields) { | 
            
            
              | 117 | 
               | 
            
            
              | 118 | 
                      my $tag = $field->tag();  | 
            
            
              | 119 | 
                      if ( int($tag) < $FIRST_NONCONTROL_TAG ) { | 
            
            
              | 120 | 
                          next;  | 
            
            
              | 121 | 
                      }  | 
            
            
              | 122 | 
               | 
            
            
              | 123 | 
                      # Purposely put into array variable, so as not  | 
            
            
              | 124 | 
                      # to potentially re-evaluate a mixed up array in the  | 
            
            
              | 125 | 
                      # midst of subfield deletes.  | 
            
            
              | 126 | 
                      my @subfields = $field->subfields();  | 
            
            
              | 127 | 
                      foreach my $subfield (@subfields) { | 
            
            
              | 128 | 
               | 
            
            
              | 129 | 
                          # determine it's tag, subtag, and value.  | 
            
            
              | 130 | 
                          my $subtag = $subfield->[0];  | 
            
            
              | 131 | 
                          my $value  = $subfield->[1];  | 
            
            
              | 132 | 
               | 
            
            
              | 133 | 
                          # find the matching kohafield, if it is in items  | 
            
            
              | 134 | 
                          my @params = ( $tag, $subtag );  | 
            
            
              | 135 | 
                          my $kohafield = $dbh->selectrow_array(  | 
            
            
              | 136 | 
                              'SELECT kohafield FROM marc_subfield_structure '  | 
            
            
              | 137 | 
                                . q{WHERE kohafield LIKE 'items.%' AND } | 
            
            
              | 138 | 
                                . 'tagfield=? and tagsubfield=?',  | 
            
            
              | 139 | 
                              undef, @params  | 
            
            
              | 140 | 
                          );  | 
            
            
              | 141 | 
               | 
            
            
              | 142 | 
                          # if there is a corresponding items kohafield...  | 
            
            
              | 143 | 
                          if ( defined $kohafield ) { | 
            
            
              | 144 | 
                              $kohafield =~ s/items.//xsmg;  | 
            
            
              | 145 | 
               | 
            
            
              | 146 | 
                              # Check it against what is supposed to be hidden.  | 
            
            
              | 147 | 
                              if ( any { $value eq $_ } @{ $hidingrules->{$kohafield} } ) { | 
            
            
              | 148 | 
               | 
            
            
              | 149 | 
                                  # When it's the last subfield.  | 
            
            
              | 150 | 
                                  if ( scalar $field->subfields() == 1 ) { | 
            
            
              | 151 | 
                                      $current_record->delete_field($field);  | 
            
            
              | 152 | 
                                  }  | 
            
            
              | 153 | 
                                  else { | 
            
            
              | 154 | 
                                      # Otherwise, just delete the subfield.  | 
            
            
              | 155 | 
                                      $field->delete_subfield( code => $subtag );  | 
            
            
              | 156 | 
                                  }  | 
            
            
              | 157 | 
                              }  | 
            
            
              | 158 | 
                          }  | 
            
            
              | 159 | 
                      }  | 
            
            
              | 160 | 
                  }  | 
            
            
              | 161 | 
                  return;  | 
            
            
              | 162 | 
              }  | 
            
            
              | 163 | 
               | 
            
            
              | 164 | 
              sub initialize { | 
            
            
              | 165 | 
                  my $self  = shift;  | 
            
            
              | 166 | 
                  my $param = shift;  | 
            
            
              | 167 | 
               | 
            
            
              | 168 | 
                  my $options = $param->{options}; | 
            
            
              | 169 | 
                  $self->{options} = $options; | 
            
            
              | 170 | 
                  $self->Koha::RecordProcessor::Base::initialize($param);  | 
            
            
              | 171 | 
                  return;  | 
            
            
              | 172 | 
              }  | 
            
            
              | 173 | 
               | 
            
            
              | 174 | 
              =head1 DIAGNOSTICS  | 
            
            
              | 175 | 
               | 
            
            
              | 176 | 
               $ prove -v t/RecordProcessor.t  | 
            
            
              | 177 | 
               $ prove -v t/db_dependent/Filter_MARC_OpacHiddenItems.t  | 
            
            
              | 178 | 
               | 
            
            
              | 179 | 
              =head1 CONFIGURATION AND ENVIRONMENT  | 
            
            
              | 180 | 
               | 
            
            
              | 181 | 
              Install Koha. This filter will be used appropriately by the OPAC or Staff client.  | 
            
            
              | 182 | 
               | 
            
            
              | 183 | 
              =head1 INCOMPATIBILITIES  | 
            
            
              | 184 | 
               | 
            
            
              | 185 | 
              This is designed for MARC::Record filtering currently. It will not handle MARC::MARCXML.  | 
            
            
              | 186 | 
               | 
            
            
              | 187 | 
              =head1 DEPENDENCIES  | 
            
            
              | 188 | 
               | 
            
            
              | 189 | 
              The following Perl libraries are required: Modern::Perl and Carp.  | 
            
            
              | 190 | 
              The following Koha libraries are required: C4::Biblio, Koha::RecordProcessor, and Koha::RecordProcessor::Base.  | 
            
            
              | 191 | 
              These should all be installed if the koha-common package is installed or Koha is otherwise installed.  | 
            
            
              | 192 | 
               | 
            
            
              | 193 | 
              =head1 BUGS AND LIMITATIONS  | 
            
            
              | 194 | 
               | 
            
            
              | 195 | 
              This is the initial version. Please feel free to report bugs  | 
            
            
              | 196 | 
              at http://bugs.koha-community.org/.  | 
            
            
              | 197 | 
               | 
            
            
              | 198 | 
              =head1 AUTHOR  | 
            
            
              | 199 | 
               | 
            
            
              | 200 | 
              Mark Tompsett  | 
            
            
              | 201 | 
               | 
            
            
              | 202 | 
              =head1 LICENSE AND COPYRIGHT  | 
            
            
              | 203 | 
               | 
            
            
              | 204 | 
              Copyright 2016 Mark Tompsett  | 
            
            
              | 205 | 
               | 
            
            
              | 206 | 
              This file is part of Koha.  | 
            
            
              | 207 | 
               | 
            
            
              | 208 | 
              Koha is free software; you can redistribute it and/or modify it  | 
            
            
              | 209 | 
              under the terms of the GNU General Public License as published by  | 
            
            
              | 210 | 
              the Free Software Foundation; either version 3 of the License, or  | 
            
            
              | 211 | 
              (at your option) any later version.  | 
            
            
              | 212 | 
               | 
            
            
              | 213 | 
              Koha is distributed in the hope that it will be useful, but  | 
            
            
              | 214 | 
              WITHOUT ANY WARRANTY; without even the implied warranty of  | 
            
            
              | 215 | 
              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  | 
            
            
              | 216 | 
              GNU General Public License for more details.  | 
            
            
              | 217 | 
               | 
            
            
              | 218 | 
              You should have received a copy of the GNU General Public License  | 
            
            
              | 219 | 
              along with Koha; if not, see <http://www.gnu.org/licenses>.  | 
            
            
              | 220 | 
               | 
            
            
              | 221 | 
              =cut  | 
            
            
              | 222 | 
               | 
            
            
              | 223 | 
              1;  |