Bug 20362: Direct link to authority records missing in staff detail view (1xx , 7xx)
[koha-ffzg.git] / catalogue / export.pl
index efdf0ee..3785116 100755 (executable)
@@ -1,24 +1,21 @@
 #!/usr/bin/perl
-use strict;
-#use warnings; FIXME - Bug 2505
+use Modern::Perl;
 
 use C4::Record;
-use C4::Auth;
+use C4::Auth qw( get_template_and_user );
 use C4::Output;
-use C4::Biblio;
-use CGI;
-use C4::Ris;
+use C4::Biblio qw( GetMarcBiblio GetMarcControlnumber );
+use CGI qw ( -utf8 );
+use C4::Ris qw( marc2ris );
 
 
 
-my $query = new CGI;
+my $query = CGI->new;
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
     template_name   => "tools/export.tt",
     query           => $query,
     type            => "intranet",
-    authnotrequired => 0,
     flagsrequired   => { catalogue => 1 },
-    debug           => 1,
     });
 
 my $op=$query->param("op");
@@ -27,8 +24,21 @@ my $error = '';
 if ($op eq "export") {
     my $biblionumber = $query->param("bib");
         if ($biblionumber){
+            my $file_id = $biblionumber;
+            my $file_pre = "bib-";
 
-            my $marc = GetMarcBiblio($biblionumber, 1);
+            my $marc = GetMarcBiblio({
+                biblionumber => $biblionumber,
+                embed_items  => 1 });
+
+            if( C4::Context->preference('DefaultSaveRecordFileID') eq 'controlnumber' ){
+                my $marcflavour = C4::Context->preference('marcflavour'); #FIXME This option is required but does not change control num behaviour
+                my $control_num = GetMarcControlnumber( $marc, $marcflavour );
+                if( $control_num ){
+                    $file_id = $control_num;
+                    $file_pre = "record-";
+                }
+            }
 
             if ($format =~ /endnote/) {
                 $marc = marc2endnote($marc);
@@ -50,8 +60,8 @@ if ($op eq "export") {
                 $marc = marc2bibtex($marc);
                 $format = "bibtex";
             }
-            elsif ($format =~ /dc/) {
-                ($error,$marc) = marc2dcxml($marc,1);
+            elsif ($format =~ /dc$/) {
+                $marc = marc2dcxml(undef, undef, $biblionumber, $format);
                 $format = "dublin-core.xml";
             }
             elsif ($format =~ /marc8/) {
@@ -69,9 +79,12 @@ if ($op eq "export") {
                 ($error, $marc) = marc2marc($marc, 'marcstd', C4::Context->preference('marcflavour'));
                 $format = "marcstd";
             }
+            if ( $format =~ /utf8/ or $format =~ /marcstd/ ) {
+                binmode STDOUT, ':encoding(UTF-8)';
+            }
             print $query->header(
                 -type => 'application/octet-stream',
-                -attachment=>"bib-$biblionumber.$format");
+                -attachment=>"$file_pre$file_id.$format");
             print $marc;
         }
 }