Bug 19074: Fix category display in Batch patron modification.
[koha.git] / tools / export.pl
index a4c02da..72b18c4 100755 (executable)
@@ -21,16 +21,16 @@ use CGI qw ( -utf8 );
 use MARC::File::XML;
 use List::MoreUtils qw(uniq);
 use C4::Auth;
-use C4::Branch;             # GetBranches
-use C4::Csv;
-use C4::Koha;               # GetItemTypes
 use C4::Output;
 
 use Koha::Authority::Types;
 use Koha::Biblioitems;
+use Koha::CsvProfiles;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Exporter::Record;
+use Koha::ItemTypes;
+use Koha::Libraries;
 
 my $query = new CGI;
 
@@ -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;
@@ -48,7 +48,7 @@ my @record_ids;
 # biblionumbers is sent from circulation.pl only
 if ( $query->param("biblionumbers") ) {
     $record_type = 'bibs';
-    @record_ids = $query->param("biblionumbers");
+    @record_ids = $query->multi_param("biblionumbers");
 }
 
 # Default value for output_format is 'iso2709'
@@ -68,32 +68,39 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
 );
 
 my @branch = $query->multi_param("branch");
-my $only_my_branch;
-# Limit to local branch if IndependentBranches and not superlibrarian
-if (
-    (
-          C4::Context->preference('IndependentBranches')
-        && C4::Context->userenv
-        && !C4::Context->IsSuperLibrarian()
-        && C4::Context->userenv->{branch}
-    )
-    # Limit result to local branch strip_nonlocal_items
-    or $query->param('strip_nonlocal_items')
-) {
-    $only_my_branch = 1;
-    @branch = ( C4::Context->userenv->{'branch'} );
-}
 
-my %branchmap = map { $_ => 1 } @branch; # for quick lookups
+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 application/vnd.ms-excel );
+        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||;
     my @biblionumbers      = $query->multi_param("biblionumbers");
     my @itemnumbers        = $query->multi_param("itemnumbers");
+    my $strip_nonlocal_items =  $query->param('strip_nonlocal_items');
     my @sql_params;
     my $sql_query;
 
+    my $libraries = $strip_nonlocal_items
+        ? [ Koha::Libraries->find(C4::Context->userenv->{branch})->unblessed ]
+        : Koha::Libraries->search_filtered->unblessed;
+    my @branchcodes;
+    for my $branchcode ( @branch ) {
+        if ( grep { $_->{branchcode} eq $branchcode } @$libraries ) {
+            push @branchcodes, $branchcode;
+        }
+    }
+
     if ( $record_type eq 'bibs' or $record_type eq 'auths' ) {
         # No need to retrieve the record_ids if we already get them
         unless ( @record_ids ) {
@@ -105,11 +112,11 @@ if ( $op eq "export" ) {
                 my $end_callnumber       = $query->param("end_callnumber");
                 my $start_accession =
                   ( $query->param("start_accession") )
-                  ? dt_from_string( $query->param("start_accession") )
+                  ? dt_from_string( scalar $query->param("start_accession") )
                   : '';
                 my $end_accession =
                   ( $query->param("end_accession") )
-                  ? dt_from_string( $query->param("end_accession") )
+                  ? dt_from_string( scalar $query->param("end_accession") )
                   : '';
 
 
@@ -122,33 +129,35 @@ if ( $op eq "export" ) {
                             }
                         )
                         : (),
+
                     ( $start_callnumber or $end_callnumber )
                         ? (
-                            callnumber => {
+                            'items.itemcallnumber' => {
                                 ( $start_callnumber ? ( '>=' => $start_callnumber ) : () ),
                                 ( $end_callnumber   ? ( '<=' => $end_callnumber   ) : () ),
                             }
                         )
                         : (),
+
                     ( $start_accession or $end_accession )
                         ? (
-                            dateaccessioned => {
+                            'items.dateaccessioned' => {
                                 ( $start_accession ? ( '>=' => $start_accession ) : () ),
                                 ( $end_accession   ? ( '<=' => $end_accession   ) : () ),
                             }
                         )
                         : (),
-                    ( @branch ? ( 'items.homebranch' => { in => \@branch } ) : () ),
+                    ( @branchcodes ? ( 'items.homebranch' => { in => \@branchcodes } ) : () ),
                     ( $itemtype
                         ?
                           C4::Context->preference('item-level_itypes')
                             ? ( 'items.itype' => $itemtype )
-                            : ( 'biblioitems.itemtype' => $itemtype )
+                            : ( 'me.itemtype' => $itemtype )
                         : ()
                     ),
 
                 };
-                my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items' } );
+                my $biblioitems = Koha::Biblioitems->search( $conditions, { join => 'items', columns => 'biblionumber' } );
                 while ( my $biblioitem = $biblioitems->next ) {
                     push @record_ids, $biblioitem->biblionumber;
                 }
@@ -190,6 +199,7 @@ if ( $op eq "export" ) {
             -attachment => $filename,
         );
 
+        my $csv_profile_id = $query->param('csv_profile_id');
         Koha::Exporter::Record::export(
             {   record_type        => $record_type,
                 record_ids         => \@record_ids,
@@ -197,7 +207,7 @@ if ( $op eq "export" ) {
                 filename           => $filename,
                 itemnumbers        => \@itemnumbers,
                 dont_export_fields => $export_remove_fields,
-                csv_profile_id     => ( $query->param('csv_profile_id') || GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') ) || undef ),
+                csv_profile_id     => $csv_profile_id,
                 export_items       => (not $dont_export_items),
             }
         );
@@ -256,32 +266,15 @@ if ( $op eq "export" ) {
 
 else {
 
-    my $itemtypes = GetItemTypes;
-    my @itemtypesloop;
-    foreach my $thisitemtype ( sort keys %$itemtypes ) {
-        my %row = (
-            value       => $thisitemtype,
-            description => $itemtypes->{$thisitemtype}->{translated_description},
-        );
-        push @itemtypesloop, \%row;
-    }
-    my $branches = GetBranches($only_my_branch);
-    my @branchloop;
-    for my $thisbranch (
-        sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} }
-        keys %{$branches}
-      )
-    {
-        push @branchloop,
-          {
-            value      => $thisbranch,
-            selected   => %branchmap ? $branchmap{$thisbranch} : 1,
-            branchname => $branches->{$thisbranch}->{'branchname'},
-          };
-    }
+    my $itemtypes = Koha::ItemTypes->search_with_localization;
 
     my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
 
+    my $libraries = Koha::Libraries->search_filtered({}, { order_by => ['branchname'] })->unblessed;
+    for my $library ( @$libraries ) {
+        $library->{selected} = 1 if grep { $library->{branchcode} eq $_ } @branch;
+    }
+
     if (   $flags->{superlibrarian}
         && C4::Context->config('backup_db_via_tools')
         && $backupdir
@@ -303,11 +296,12 @@ else {
     }
 
     $template->param(
-        branchloop               => \@branchloop,
-        itemtypeloop             => \@itemtypesloop,
+        libraries                => $libraries,
+        itemtypes                => $itemtypes,
         authority_types          => $authority_types,
         export_remove_fields     => C4::Context->preference("ExportRemoveFields"),
-        csv_profiles             => C4::Csv::GetCsvProfiles('marc'),
+        csv_profiles             => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
+        messages                 => \@messages,
     );
 
     output_html_with_http_headers $query, $cookie, $template->output;