X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=tools%2Fexport.pl;h=588f1b9c9379a2ab4d1a52379111a55b9beceb0f;hb=5faddae26f7dd2c877161652f396414f56337421;hp=a4c02dac234cdcaa4562c01322866303d87103bb;hpb=66aacace086f16d2eca860cc6c2aa164866d0488;p=srvgit diff --git a/tools/export.pl b/tools/export.pl index a4c02dac23..588f1b9c93 100755 --- a/tools/export.pl +++ b/tools/export.pl @@ -21,25 +21,30 @@ 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; +my $query = CGI->new; my $dont_export_items = $query->param("dont_export_item") || 0; 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; +if ( $record_type eq 'auths' ) { + $filename = $query->param("filename_auth") || ( $output_format eq 'xml' ? 'koha.xml' : 'koha.mrc' ); +} else { + $filename = $query->param("filename") || ( $output_format eq 'csv' ? 'koha.csv' : 'koha.mrc' ); +} $filename =~ s/(\r|\n)//; my $dbh = C4::Context->dbh; @@ -48,7 +53,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' @@ -61,38 +66,40 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( template_name => "tools/export.tt", query => $query, type => "intranet", - authnotrequired => 0, flagsrequired => { tools => 'export_catalog' }, - debug => 1, } ); 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 { $_ eq $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 @sql_params; - my $sql_query; + my $strip_items_not_from_libraries = $query->param('strip_items_not_from_libraries'); + + my $libraries = Koha::Libraries->search_filtered->unblessed; + my $only_export_items_for_branches = $strip_items_not_from_libraries ? \@branch : undef; + 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 @@ -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,8 +207,9 @@ 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), + only_export_items_for_branches => $only_export_items_for_branches, } ); } @@ -256,32 +267,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 +297,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;