AuthoritiesMarc.pm Improvements
[koha.git] / C4 / Biblio.pm
index bb86999..566365d 100755 (executable)
@@ -36,7 +36,7 @@ use C4::Charset;
 require C4::Heading;
 require C4::Serials;
 
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
 
 BEGIN {
        $VERSION = 1.00;
@@ -46,13 +46,16 @@ BEGIN {
 
        # to add biblios
 # EXPORTED FUNCTIONS.
+    push @EXPORT_OK, qw(
+        &GetRecordValue
+    );
+
        push @EXPORT, qw( 
                &AddBiblio
        );
 
        # to get something
        push @EXPORT, qw(
-           &Get
                &GetBiblio
                &GetBiblioData
                &GetBiblioItemData
@@ -60,9 +63,10 @@ BEGIN {
                &GetBiblioItemByBiblioNumber
                &GetBiblioFromItemNumber
                
-               GetFieldMapping
-               SetFieldMapping
-               DeleteFieldMapping
+               &GetRecordValue
+               &GetFieldMapping
+               &SetFieldMapping
+               &DeleteFieldMapping
                
                &GetISBDView
 
@@ -471,11 +475,11 @@ sub LinkBibHeadingsToAuthorities {
     return $num_headings_changed;
 }
 
-=head2 Get
+=head2 GetRecordValue
 
 =over 4
 
-my $values = Get($field, $record, $frameworkcode);
+my $values = GetRecordValue($field, $record, $frameworkcode);
 
 =back
 
@@ -483,7 +487,7 @@ Get MARC fields from a keyword defined in fieldmapping table.
 
 =cut
 
-sub Get {
+sub GetRecordValue {
     my ($field, $record, $frameworkcode) = @_;
     my $dbh = C4::Context->dbh;
     
@@ -505,7 +509,7 @@ sub Get {
         }
     }
     
-    return @result;
+    return \@result;
 }
 
 =head2 SetFieldMapping
@@ -1766,10 +1770,10 @@ sub TransformHtmlToXml {
                 if (   ( @$tags[$i] && @$tags[$i] > 10 )
                     && ( @$values[$i] ne "" ) )
                 {
-                    my $ind1 = substr( @$indicator[$j], 0, 1 );
+                    my $ind1 = _default_ind_to_space(substr( @$indicator[$j], 0, 1 ));
                     my $ind2;
                     if ( @$indicator[$j] ) {
-                        $ind2 = substr( @$indicator[$j], 1, 1 );
+                        $ind2 = _default_ind_to_space(substr( @$indicator[$j], 1, 1 ));
                     }
                     else {
                         warn "Indicator in @$tags[$i] is empty";
@@ -1798,10 +1802,8 @@ sub TransformHtmlToXml {
                         $first = 1;
                     }
                     else {
-                        my $ind1 = substr( @$indicator[$j], 0, 1 );
-                        my $ind2 = substr( @$indicator[$j], 1, 1 );
-                        $ind1 = " " if !defined($ind2) or $ind2 eq "";
-                        $ind2 = " " if !defined($ind2) or $ind2 eq "";
+                        my $ind1 = _default_ind_to_space( substr( @$indicator[$j], 0, 1 ) );
+                        my $ind2 = _default_ind_to_space( substr( @$indicator[$j], 1, 1 ) );
                         $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                         $xml .= "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
                         $first = 0;
@@ -1814,10 +1816,8 @@ sub TransformHtmlToXml {
             }
             else {
                 if ($first) {
-                    my $ind1 = substr( @$indicator[$j], 0, 1 );
-                    my $ind2 = substr( @$indicator[$j], 1, 1 );
-                    $ind1 = " " if !defined($ind2) or $ind2 eq "";
-                    $ind2 = " " if !defined($ind2) or $ind2 eq "";
+                    my $ind1 = _default_ind_to_space( substr( @$indicator[$j], 0, 1 ) );
+                    my $ind2 = _default_ind_to_space( substr( @$indicator[$j], 1, 1 ) );
                     $xml .= "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
                     $first = 0;
                 }
@@ -1844,6 +1844,21 @@ sub TransformHtmlToXml {
     return $xml;
 }
 
+=head2 _default_ind_to_space
+
+Passed what should be an indicator returns a space
+if its undefined or zero length
+
+=cut
+
+sub _default_ind_to_space {
+    my $s = shift;
+    if (!defined $s || $s eq q{}) {
+        return ' ';
+    }
+    return $s;
+}
+
 =head2 TransformHtmlToMarc
 
     L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
@@ -1916,8 +1931,8 @@ sub TransformHtmlToMarc {
         elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
             my $tag  = $1;
             
-            my $ind1 = substr($cgi->param($param),0,1);
-            my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
+            my $ind1 = _default_ind_to_space(substr($cgi->param($param),          0, 1));
+            my $ind2 = _default_ind_to_space(substr($cgi->param($params->[$i+1]), 0, 1));
             $newfield=0;
             my $j=$i+2;
             
@@ -1946,8 +1961,8 @@ sub TransformHtmlToMarc {
                         if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
                             $newfield = MARC::Field->new(
                                 $tag,
-                                ''.$ind1,
-                                ''.$ind2,
+                                $ind1,
+                                $ind2,
                                 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
                             );
                         }