Bug 15498: Let the user choose the CSV profile to export circ history
[koha.git] / tools / export.pl
index 03792c3..1ed7b1a 100755 (executable)
@@ -39,7 +39,7 @@ my $record_type       = $query->param("record_type");
 my $op                = $query->param("op") || '';
 my $output_format     = $query->param("format") || $query->param("output_format") || 'iso2709';
 my $backupdir         = C4::Context->config('backupdir');
-my $filename          = $query->param("filename") || 'koha.mrc';
+my $filename          = $query->param("filename") || ( $output_format eq 'csv' ? 'koha.csv' : 'koha.mrc' );
 $filename =~ s/(\r|\n)//;
 
 my $dbh = C4::Context->dbh;
@@ -69,6 +69,19 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
 
 my @branch = $query->multi_param("branch");
 
+my @messages;
+if ( $op eq 'export' ) {
+    my $filename = $query->param('id_list_file');
+    if ( $filename ) {
+        my $mimetype = $query->uploadInfo($filename)->{'Content-Type'};
+        my @valid_mimetypes = qw( application/octet-stream text/csv text/plain );
+        unless ( grep { /^$mimetype$/ } @valid_mimetypes ) {
+            push @messages, { type => 'alert', code => 'invalid_mimetype' };
+            $op = '';
+        }
+    }
+}
+
 if ( $op eq "export" ) {
 
     my $export_remove_fields = $query->param("export_remove_fields") || q||;
@@ -187,12 +200,6 @@ if ( $op eq "export" ) {
         );
 
         my $csv_profile_id = $query->param('csv_profile_id');
-        unless ( $csv_profile_id ) {
-            # FIXME export_format.profile should be a unique key
-            my $default_csv_profiles = Koha::CsvProfiles->search({ profile => C4::Context->preference('ExportWithCsvProfile') });
-            $csv_profile_id = $default_csv_profiles->count ? $default_csv_profiles->next->export_format_id : undef;
-        }
-
         Koha::Exporter::Record::export(
             {   record_type        => $record_type,
                 record_ids         => \@record_ids,
@@ -302,6 +309,7 @@ else {
         authority_types          => $authority_types,
         export_remove_fields     => C4::Context->preference("ExportRemoveFields"),
         csv_profiles             => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
+        messages                 => \@messages,
     );
 
     output_html_with_http_headers $query, $cookie, $template->output;