+my $op = $query->param('op') || '';
+
+if ( $op eq 'export' ) {
+ my @itemnumbers = $query->multi_param('itemnumber');
+ my $csv_profile_id = $query->param('csv_profile_id');
+ my @rows;
+ if ($csv_profile_id) {
+ # FIXME This following code has the same logic as GetBasketAsCSV
+ # We should refactor all the CSV export code
+ # Note: For MARC it is already done in Koha::Exporter::Record but not for SQL CSV profiles type
+ my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
+ die "There is no valid csv profile given" unless $csv_profile;
+
+ my $csv = Text::CSV_XS->new({'quote_char'=>'"','escape_char'=>'"','sep_char'=>$csv_profile->csv_separator,'binary'=>1});
+ my $csv_profile_content = $csv_profile->content;
+ my ( @headers, @fields );
+ while ( $csv_profile_content =~ /
+ ([^=\|]+) # header
+ =?
+ ([^\|]*) # fieldname (table.row or row)
+ \|? /gxms
+ ) {
+ my $header = $1;
+ my $field = ($2 eq '') ? $1 : $2;
+
+ $header =~ s/^\s+|\s+$//g; # Trim whitespaces
+ push @headers, $header;
+
+ $field =~ s/[^\.]*\.{1}//; # Remove the table name if exists.
+ $field =~ s/^\s+|\s+$//g; # Trim whitespaces
+ push @fields, $field;
+ }
+ my $items = Koha::Items->search({ itemnumber => { -in => \@itemnumbers } });
+ while ( my $item = $items->next ) {
+ my @row;
+ my $all_fields = $item->unblessed;
+ $all_fields = { %$all_fields, %{$item->biblio->unblessed}, %{$item->biblioitem->unblessed} };
+ for my $field (@fields) {
+ push @row, $all_fields->{$field};
+ }
+ push @rows, \@row;
+ }
+ my $content = join( $csv_profile->csv_separator, @headers ) . "\n";
+ for my $row ( @rows ) {
+ $csv->combine(@$row);
+ my $string = $csv->string;
+ $content .= $string . "\n";
+ }
+ print $query->header(
+ -type => 'text/csv',
+ -attachment => 'lost_items.csv',
+ );
+ print $content;
+ exit;
+ }
+} elsif ( $get_items ) {