Bug 21241: (follow-up) Syspref to control fallback to SMS when no email is defined
[koha.git] / C4 / Record.pm
index d24d631..cde523f 100644 (file)
@@ -29,14 +29,16 @@ use MARC::File::XML; # marc2marcxml, marcxml2marc, changeEncoding
 use Biblio::EndnoteStyle;
 use Unicode::Normalize; # _entity_encode
 use C4::Biblio; #marc2bibtex
-use C4::Csv; #marc2csv
 use C4::Koha; #marc2csv
 use C4::XSLT ();
 use YAML; #marcrecords2csv
 use Template;
 use Text::CSV::Encoded; #marc2csv
+use Koha::Items;
 use Koha::SimpleMARC qw(read_field);
 use Koha::XSLT_Handler;
+use Koha::CsvProfiles;
+use Koha::AuthorisedValues;
 use Carp;
 
 use vars qw(@ISA @EXPORT);
@@ -227,7 +229,7 @@ EXAMPLE
     my dcxml = marc2dcxml (undef, undef, 1, "oaidc");
 
 Convert MARC or MARCXML to Dublin Core metadata (XSLT Transformation),
-optionally can get an XML directly from database (biblioitems.marcxml)
+optionally can get an XML directly from biblio_metadata
 without item information. This method take into consideration the syspref
 'marcflavour' (UNIMARC, MARC21 and NORMARC).
 Return an XML file with the format defined in C<$format>
@@ -236,7 +238,7 @@ C<$marc> - an ISO-2709 scalar or MARC::Record object
 
 C<$xml> - a MARCXML file
 
-C<$biblionumber> - obtain the record directly from database (biblioitems.marcxml)
+C<$biblionumber> - biblionumber for database access
 
 C<$format> - accept three type of DC formats (oaidc, srwdc, and rdfdc )
 
@@ -257,7 +259,7 @@ sub marc2dcxml {
         # no need to catch errors or warnings marc2marcxml do it instead
         $marcxml = C4::Record::marc2marcxml( $marc );
     } elsif ( not defined $xml and defined $biblionumber ) {
-        # get MARCXML biblio directly from biblioitems.marcxml without item information
+        # get MARCXML biblio directly without item information
         $marcxml = C4::Biblio::GetXmlBiblio( $biblionumber );
     } else {
         $marcxml = $xml;
@@ -292,16 +294,15 @@ sub marc2dcxml {
         };
     } elsif ( $record =~ /^MARC::Record/ ) { # if OK makes xslt transformation
         my $xslt_engine = Koha::XSLT_Handler->new;
-        if ( $format =~ /oaidc|srwdc|rdfdc/ ) {
+        if ( $format =~ /^(dc|oaidc|srwdc|rdfdc)$/i ) {
             $output = $xslt_engine->transform( $marcxml, $xsl );
         } else {
             croak "The format argument ($format) not accepted.\n" .
                   "Please pass a valid format (oaidc, srwdc, or rdfdc)\n";
         }
-        my $err = $xslt_engine->err; # error number
-        my $errstr = $xslt_engine->errstr; # error message
+        my $err = $xslt_engine->err; # error code
         if ( $err ) {
-            croak "Error when processing $errstr Error number: $err\n";
+            croak "Error $err while processing\n";
         } else {
             return $output;
         }
@@ -401,7 +402,7 @@ Returns a CSV scalar
 
 C<$biblio> - a list of biblionumbers
 
-C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id and the GetCsvProfiles function in C4::Csv)
+C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
 
 C<$itemnumbers> - a list of itemnumbers to export
 
@@ -426,7 +427,8 @@ sub marc2csv {
     my $firstpass = 1;
     if ( @$itemnumbers ) {
         for my $itemnumber ( @$itemnumbers) {
-            my $biblionumber = GetBiblionumberFromItemnumber $itemnumber;
+            my $item = Koha::Items->find( $itemnumber );
+            my $biblionumber = $item->biblio->biblionumber;
             $output .= marcrecord2csv( $biblionumber, $id, $firstpass, $csv, $fieldprocessing, [$itemnumber] );
             $firstpass = 0;
         }
@@ -451,7 +453,7 @@ Returns a CSV scalar
 
 C<$biblio> - a biblionumber
 
-C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id and the GetCsvProfiles function in C4::Csv)
+C<$csvprofileid> - the id of the CSV profile to use for the export (see export_format.export_format_id)
 
 C<$header> - true if the headers are to be printed (typically at first pass)
 
@@ -468,21 +470,24 @@ sub marcrecord2csv {
     my $output;
 
     # Getting the record
-    my $record = GetMarcBiblio($biblio);
+    my $record = GetMarcBiblio({ biblionumber => $biblio });
     return unless $record;
-    C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblio, $itemnumbers );
+    C4::Biblio::EmbedItemsInMarcBiblio({
+        marc_record  => $record,
+        biblionumber => $biblio,
+        item_numbers => $itemnumbers });
     # Getting the framework
     my $frameworkcode = GetFrameworkCode($biblio);
 
     # Getting information about the csv profile
-    my $profile = GetCsvProfile($id);
+    my $profile = Koha::CsvProfiles->find($id);
 
     # Getting output encoding
-    my $encoding          = $profile->{encoding} || 'utf8';
+    my $encoding          = $profile->encoding || 'utf8';
     # Getting separators
-    my $csvseparator      = $profile->{csv_separator}      || ',';
-    my $fieldseparator    = $profile->{field_separator}    || '#';
-    my $subfieldseparator = $profile->{subfield_separator} || '|';
+    my $csvseparator      = $profile->csv_separator      || ',';
+    my $fieldseparator    = $profile->field_separator    || '#';
+    my $subfieldseparator = $profile->subfield_separator || '|';
 
     # TODO: Be more generic (in case we have to handle other protected chars or more separators)
     if ($csvseparator eq '\t') { $csvseparator = "\t" }
@@ -496,7 +501,7 @@ sub marcrecord2csv {
     $csv->sep_char($csvseparator);
 
     # Getting the marcfields
-    my $marcfieldslist = $profile->{content};
+    my $marcfieldslist = $profile->content;
 
     # Getting the marcfields as an array
     my @marcfieldsarray = split('\|', $marcfieldslist);
@@ -585,18 +590,22 @@ sub marcrecord2csv {
                 # If it is a subfield
                 my @loop_values;
                 if ( $tag->{subfieldtag} ) {
+                    my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, tagsubfield => $tag->{subfieldtag}, });
+                    $av = $av->count ? $av->unblessed : [];
+                    my $av_description_mapping = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
                     # For each field
                     foreach my $field (@fields) {
                         my @subfields = $field->subfield( $tag->{subfieldtag} );
                         foreach my $subfield (@subfields) {
-                            my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, $tag->{subfieldtag}, $frameworkcode, undef);
-                            push @loop_values, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield;
+                            push @loop_values, (defined $av_description_mapping->{$subfield}) ? $av_description_mapping->{$subfield} : $subfield;
                         }
                     }
 
                 # Or a field
                 } else {
-                    my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, undef, $frameworkcode, undef);
+                    my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, });
+                    $av = $av->count ? $av->unblessed : [];
+                    my $authvalues = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
 
                     foreach my $field ( @fields ) {
                         my $value;