Bug 28290: Don't send subfields to 'as_string' if none to send
[koha-ffzg.git] / C4 / Heading.pm
index 5dfd0fa..4d67cd5 100644 (file)
@@ -19,11 +19,9 @@ package C4::Heading;
 
 use Modern::Perl;
 
-use MARC::Record;
 use MARC::Field;
 use C4::Context;
-use Module::Load;
-use Carp;
+use Module::Load qw( load );
 
 
 =head1 NAME
@@ -33,7 +31,7 @@ C4::Heading
 =head1 SYNOPSIS
 
  use C4::Heading;
- my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode);
+ my $heading = C4::Heading->new_from_field($field, $frameworkcode);
  my $thesaurus = $heading->thesaurus();
  my $type = $heading->type();
  my $display_heading = $heading->display_form();
@@ -46,32 +44,30 @@ headings found in bibliographic and authority records.
 
 =head1 METHODS
 
-=head2 new_from_bib_field
+=head2 new_from_field
 
-  my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode, [, $marc_flavour]);
+  my $heading = C4::Heading->new_from_field($field, $frameworkcode, [, $auth]);
 
 Given a C<MARC::Field> object containing a heading from a 
 bib record, create a C<C4::Heading> object.
 
-The optional second parameter is the MARC flavour (i.e., MARC21
-or UNIMARC); if this parameter is not supplied, it is
-taken from the Koha application context.
+The optional third parameter is 'auth' - it is handled as boolean. If supplied we treat the field as an auth record field. Otherwise if it is a bib field. The fields checked are the same in a UNIMARC system and this parameter is ignored
 
 If the MARC field supplied is not a valid heading, undef
 is returned.
 
 =cut
 
-sub new_from_bib_field {
+sub new_from_field {
     my $class         = shift;
     my $field         = shift;
-    my $frameworkcode = shift;
-    my $marcflavour   = @_ ? shift : C4::Context->preference('marcflavour');
-
+    my $frameworkcode = shift; #FIXME this is not used?
+    my $auth          = shift;
+    my $marcflavour   = C4::Context->preference('marcflavour');
     my $marc_handler = _marc_format_handler($marcflavour);
 
     my $tag = $field->tag();
-    return unless $marc_handler->valid_bib_heading_tag( $tag, $frameworkcode );
+    return unless $marc_handler->valid_heading_tag( $tag, $frameworkcode, $auth );
     my $self = {};
 
     $self->{'field'} = $field;
@@ -79,7 +75,7 @@ sub new_from_bib_field {
         $self->{'auth_type'},   $self->{'thesaurus'},
         $self->{'search_form'}, $self->{'display_form'},
         $self->{'match_type'}
-    ) = $marc_handler->parse_heading($field);
+    ) = $marc_handler->parse_heading($field, $auth );
 
     bless $self, $class;
     return $self;
@@ -170,20 +166,22 @@ sub preferred_authorities {
     return $results;
 }
 
-=head2 valid_bib_heading_subfield
+=head2 valid_heading_subfield
+
+    if (C4::Heading::valid_heading_subfield('100', 'e', '')) ...
 
-    if (C4::Heading::valid_bib_heading_subfield('100', 'e', '')) ...
+Check if the given subfield is valid for the given field.
 
 =cut
 
-sub valid_bib_heading_subfield {
+sub valid_heading_subfield {
     my $tag           = shift;
     my $subfield      = shift;
-    my $marcflavour   = @_ ? shift : C4::Context->preference('marcflavour');
+    my $marcflavour   = C4::Context->preference('marcflavour');
+    my $auth          = shift;
 
     my $marc_handler = _marc_format_handler($marcflavour);
-
-    return $marc_handler->valid_bib_heading_subfield( $tag, $subfield );
+    return $marc_handler->valid_heading_subfield( $tag, $subfield, $auth );
 }
 
 =head1 INTERNAL METHODS
@@ -204,14 +202,14 @@ sub _search {
 
     if ($index) {
         push @marclist, $index;
-        push @and_or,   'and';
+        push @and_or,   'AND';
         push @operator, $self->{'match_type'};
         push @value,    $self->{'search_form'};
     }
 
     #    if ($self->{'thesaurus'}) {
     #        push @marclist, 'thesaurus';
-    #        push @and_or, 'and';
+    #        push @and_or, 'AND';
     #        push @excluding, '';
     #        push @operator, 'is';
     #        push @value, $self->{'thesaurus'};
@@ -247,7 +245,6 @@ depending on the selected MARC flavour.
 
 sub _marc_format_handler {
     my $marcflavour = uc shift;
-    $marcflavour = 'MARC21' if ( $marcflavour eq 'NORMARC' );
     my $pname = "C4::Heading::$marcflavour";
     load $pname;
     return $pname->new();