Bug 15758: Koha::Libraries - Remove GetBranches
[srvgit] / tools / export.pl
index efcd544..c585d90 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::Libraries;
 
 my $query = new CGI;
 
@@ -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'
@@ -67,33 +67,27 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
     }
 );
 
-my @branch = $query->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 @branch = $query->multi_param("branch");
 
 if ( $op eq "export" ) {
 
     my $export_remove_fields = $query->param("export_remove_fields") || q||;
-    my @biblionumbers      = $query->param("biblionumbers");
-    my @itemnumbers        = $query->param("itemnumbers");
+    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 +99,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,23 +116,25 @@ 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')
@@ -148,7 +144,7 @@ if ( $op eq "export" ) {
                     ),
 
                 };
-                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 +186,13 @@ if ( $op eq "export" ) {
             -attachment => $filename,
         );
 
+        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,
@@ -197,7 +200,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),
             }
         );
@@ -265,23 +268,14 @@ else {
         );
         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 $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 +297,11 @@ else {
     }
 
     $template->param(
-        branchloop               => \@branchloop,
+        libraries                => $libraries,
         itemtypeloop             => \@itemtypesloop,
         authority_types          => $authority_types,
         export_remove_fields     => C4::Context->preference("ExportRemoveFields"),
-        csv_profiles             => C4::Csv::GetCsvProfiles('marc'),
+        csv_profiles             => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
     );
 
     output_html_with_http_headers $query, $cookie, $template->output;