Bug 16048 [Follow-up] Making notices actions buttons
[koha.git] / C4 / Biblio.pm
index 191e9c7..fa1bb4c 100644 (file)
@@ -31,7 +31,6 @@ use POSIX qw(strftime);
 use Module::Load::Conditional qw(can_load);
 
 use C4::Koha;
-use C4::Dates qw/format_date/;
 use C4::Log;    # logaction
 use C4::Budgets;
 use C4::ClassSource;
@@ -40,6 +39,8 @@ use C4::Linker;
 use C4::OAI::Sets;
 
 use Koha::Cache;
+use Koha::Authority::Types;
+use Koha::Acquisition::Currencies;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -91,6 +92,7 @@ BEGIN {
 
       &GetAuthorisedValueDesc
       &GetMarcStructure
+      &IsMarcStructureInternal
       &GetMarcFromKohaField
       &GetMarcSubfieldStructureFromKohaField
       &GetFrameworkCode
@@ -573,8 +575,7 @@ sub LinkBibHeadingsToAuthorities {
                     $results{'linked'}->{ $heading->display_form() }++;
                 }
                 else {
-                    my $authtypedata =
-                      C4::AuthoritiesMarc::GetAuthType( $heading->auth_type() );
+                    my $authority_type = Koha::Authority::Types->find( $heading->auth_type() );
                     my $marcrecordauth = MARC::Record->new();
                     if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {
                         $marcrecordauth->leader('     nz  a22     o  4500');
@@ -583,7 +584,7 @@ sub LinkBibHeadingsToAuthorities {
                     $field->delete_subfield( code => '9' )
                       if defined $current_link;
                     my $authfield =
-                      MARC::Field->new( $authtypedata->{auth_tag_to_report},
+                      MARC::Field->new( $authority_type->auth_tag_to_report,
                         '', '', "a" => "" . $field->subfield('a') );
                     map {
                         $authfield->add_subfields( $_->[0] => $_->[1] )
@@ -1081,6 +1082,27 @@ sub GetBiblioItemInfosOf {
 
 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
 
+=head2 IsMarcStructureInternal
+
+    my $tagslib = C4::Biblio::GetMarcStructure();
+    for my $tag ( sort keys %$tagslib ) {
+        next unless $tag;
+        for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
+            next if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
+        }
+        # Process subfield
+    }
+
+GetMarcStructure creates keys (lib, tab, mandatory, repeatable) for a display purpose.
+These different values should not be processed as valid subfields.
+
+=cut
+
+sub IsMarcStructureInternal {
+    my ( $subfield ) = @_;
+    return ref $subfield ? 0 : 1;
+}
+
 =head2 GetMarcStructure
 
   $res = GetMarcStructure($forlibrarian,$frameworkcode);
@@ -1276,6 +1298,7 @@ OpacHiddenItems to be applied.
 sub GetMarcBiblio {
     my $biblionumber = shift;
     my $embeditems   = shift || 0;
+    my $opac         = shift || 0;
 
     if (not defined $biblionumber) {
         carp 'GetMarcBiblio called with undefined biblionumber';
@@ -1531,10 +1554,10 @@ sub MungeMarcPrice {
     my ( $price ) = @_;
     return unless ( $price =~ m/\d/ ); ## No digits means no price.
     # Look for the currency symbol and the normalized code of the active currency, if it's there,
-    my $active_currency = C4::Budgets->GetCurrency();
-    my $symbol = $active_currency->{'symbol'};
-    my $isocode = $active_currency->{'isocode'};
-    $isocode = $active_currency->{'currency'} unless defined $isocode;
+    my $active_currency = Koha::Acquisition::Currencies->get_active;
+    my $symbol = $active_currency->symbol;
+    my $isocode = $active_currency->isocode;
+    $isocode = $active_currency->currency unless defined $isocode;
     my $localprice;
     if ( $symbol ) {
         my @matches =($price=~ /
@@ -1658,7 +1681,7 @@ sub GetAuthorisedValueDesc {
 
         #---- itemtypes
         if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
-            return getitemtypeinfo($value)->{description};
+            return getitemtypeinfo($value)->{translated_description};
         }
 
         #---- "true" authorized value
@@ -1767,10 +1790,11 @@ sub GetMarcISSN {
 
 =head2 GetMarcNotes
 
-  $marcnotesarray = GetMarcNotes( $record, $marcflavour );
+    $marcnotesarray = GetMarcNotes( $record, $marcflavour );
 
-Get all notes from the MARC record and returns them in an array.
-The note are stored in different fields depending on MARC flavour
+    Get all notes from the MARC record and returns them in an array.
+    The notes are stored in different fields depending on MARC flavour.
+    MARC21 field 555 gets special attention for the $u subfields.
 
 =cut
 
@@ -1780,38 +1804,28 @@ sub GetMarcNotes {
         carp 'GetMarcNotes called on undefined record';
         return;
     }
-    my $scope;
-    if ( $marcflavour eq "UNIMARC" ) {
-        $scope = '3..';
-    } else {    # assume marc21 if not unimarc
-        $scope = '5..';
-    }
+
+    my $scope = $marcflavour eq "UNIMARC"? '3..': '5..';
     my @marcnotes;
-    my $note = "";
-    my $tag  = "";
-    my $marcnote;
-    my %blacklist = map { $_ => 1 } split(/,/,C4::Context->preference('NotesBlacklist'));
+    my %blacklist = map { $_ => 1 }
+        split( /,/, C4::Context->preference('NotesBlacklist'));
     foreach my $field ( $record->field($scope) ) {
         my $tag = $field->tag();
-        if (!$blacklist{$tag}) {
-            my $value = $field->as_string();
-            if ( $note ne "" ) {
-                $marcnote = { marcnote => $note, };
-                push @marcnotes, $marcnote;
-                $note = $value;
-            }
-            if ( $note ne $value ) {
-                $note = $note . " " . $value;
+        next if $blacklist{ $tag };
+        if( $marcflavour ne 'UNIMARC' && $tag =~ /555/ ) {
+            # Field 555$u contains URLs
+            # We first push the regular subfields and all $u's separately
+            # Leave further actions to the template
+            push @marcnotes, { marcnote => $field->as_string('abcd') };
+            foreach my $sub ( $field->subfield('u') ) {
+                push @marcnotes, { marcnote => $sub };
             }
+        } else {
+            push @marcnotes, { marcnote => $field->as_string() };
         }
     }
-
-    if ($note) {
-        $marcnote = { marcnote => $note };
-        push @marcnotes, $marcnote;    #load last tag into array
-    }
     return \@marcnotes;
-}    # end GetMarcNotes
+}
 
 =head2 GetMarcSubjects
 
@@ -2496,7 +2510,7 @@ sub _default_ind_to_space {
 =cut
 
 sub TransformHtmlToMarc {
-    my $cgi    = shift;
+    my ($cgi, $isbiblio) = @_;
 
     my @params = $cgi->param();
 
@@ -2515,16 +2529,17 @@ sub TransformHtmlToMarc {
 
     # creating a new record
     my $record = MARC::Record->new();
-    my $i      = 0;
     my @fields;
+    my ($biblionumbertagfield, $biblionumbertagsubfield) = (-1, -1);
+    ($biblionumbertagfield, $biblionumbertagsubfield) =
+        &GetMarcFromKohaField( "biblio.biblionumber", '' ) if $isbiblio;
 #FIXME This code assumes that the CGI params will be in the same order as the fields in the template; this is no absolute guarantee!
-    while ( $params[$i] ) {    # browse all CGI params
+    for (my $i = 0; $params[$i]; $i++ ) {    # browse all CGI params
         my $param    = $params[$i];
         my $newfield = 0;
 
         # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
         if ( $param eq 'biblionumber' ) {
-            my ( $biblionumbertagfield, $biblionumbertagsubfield ) = &GetMarcFromKohaField( "biblio.biblionumber", '' );
             if ( $biblionumbertagfield < 10 ) {
                 $newfield = MARC::Field->new( $biblionumbertagfield, $cgi->param($param), );
             } else {
@@ -2541,6 +2556,7 @@ sub TransformHtmlToMarc {
 
             if ( $tag < 10 ) {                              # no code for theses fields
                                                             # in MARC editor, 000 contains the leader.
+                next if $tag == $biblionumbertagfield;
                 if ( $tag eq '000' ) {
                     # Force a fake leader even if not provided to avoid crashing
                     # during decoding MARC record containing UTF-8 characters
@@ -2560,6 +2576,9 @@ sub TransformHtmlToMarc {
                 # browse subfields for this tag (reason for _code_ match)
                 while(defined $params[$j] && $params[$j] =~ /_code_/) {
                     last unless defined $params[$j+1];
+                    $j += 2 and next
+                        if $tag == $biblionumbertagfield and
+                           $cgi->param($params[$j]) eq $biblionumbertagsubfield;
                     #if next param ne subfield, then it was probably empty
                     #try next param by incrementing j
                     if($params[$j+1]!~/_subfield_/) {$j++; next; }
@@ -2577,7 +2596,6 @@ sub TransformHtmlToMarc {
             }
             push @fields, $newfield if ($newfield);
         }
-        $i++;
     }
 
     $record->append_fields(@fields);
@@ -3667,7 +3685,7 @@ sub prepare_host_field {
         if ( $field = $host->field('205') ) {
             my $s = $field->as_string();
             if ($s) {
-                $sfd{a} = $s;
+                $sfd{e} = $s;
             }
         }
         #URL