View | Details | Raw Unified | Return to bug 21560
Collapse All | Expand All

(-)a/Koha/Util/OpenDocument.pm (+95 lines)
Line 0 Link Here
1
package Koha::Util::OpenDocument;
2
3
# Copyright 2019 Biblibre
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Encode qw( decode );
23
use File::Temp;
24
use File::Basename qw( dirname );
25
use OpenOffice::OODoc;
26
27
use parent qw( Exporter );
28
29
our @EXPORT = qw(
30
  generate_ods
31
);
32
33
=head1 NAME
34
35
Koha::Util::OpenDocument - utility class to manage filed in Open Document Format aka OpenDocument
36
37
=head1 METHODS
38
39
=head2 generate_ods
40
41
Generate an Open Document Sheet
42
43
Arguments are file path and content as an arrayref of lines containing arrayrefs of cells.
44
45
=cut
46
47
48
sub generate_ods {
49
    my ( $filepath, $content ) = @_;
50
51
    unless ( $filepath && $content ) {
52
        return;
53
    }
54
    my @input_rows = @$content;
55
    my $nb_rows    = scalar @input_rows;
56
    my $nb_cols;
57
    if ($nb_rows) {
58
        $nb_cols= scalar @{ $input_rows[0] };
59
    }
60
61
    # Create document
62
    my $wdir = dirname($filepath);
63
    odfWorkingDirectory($wdir);
64
    my $odf_doc = odfDocument( file => $filepath, create => 'spreadsheet' );
65
66
    if ($nb_rows) {
67
        # Prepare sheet
68
        my $odf_sheet = $odf_doc->expandTable( 0, $nb_rows + 1, $nb_cols );
69
        my @odf_rows = $odf_doc->getTableRows($odf_sheet);
70
71
        # Writing
72
        for ( my $i = 0 ; $i < $nb_rows ; $i++ ) {
73
            for ( my $j = 0 ; $j < $nb_cols ; $j++ ) {
74
                my $cellval = $input_rows[$i][$j];
75
                $odf_doc->cellValue( $odf_rows[$i], $j, $cellval );
76
            }
77
        }
78
    }
79
80
    # Done
81
    $odf_doc->save();
82
83
    return $odf_doc;
84
}
85
86
1;
87
__END__
88
89
=head1 AUTHOR
90
91
Koha Development Team <http://koha-community.org/>
92
93
Fridolin Somers <fridolin.somers@biblibre.com>
94
95
=cut
(-)a/misc/cronjobs/gather_print_notices.pl (-44 / +22 lines)
Lines 19-25 use Pod::Usage; Link Here
19
use Getopt::Long;
19
use Getopt::Long;
20
use C4::Log;
20
use C4::Log;
21
21
22
use File::Basename qw( dirname );
23
use Koha::DateUtils;
22
use Koha::DateUtils;
24
use MIME::Lite;
23
use MIME::Lite;
25
24
Lines 181-187 sub print_notices { Link Here
181
                filepath => $filepath,
180
                filepath => $filepath,
182
            });
181
            });
183
        } elsif ( $format eq 'ods' ) {
182
        } elsif ( $format eq 'ods' ) {
184
            generate_ods ({
183
            _generate_ods ({
185
                messages => $branch_messages,
184
                messages => $branch_messages,
186
                filepath => $filepath,
185
                filepath => $filepath,
187
            });
186
            });
Lines 249-305 sub generate_csv { Link Here
249
    }
248
    }
250
}
249
}
251
250
252
sub generate_ods {
251
sub _generate_ods {
253
    my ( $params ) = @_;
252
    my ( $params ) = @_;
254
    my $messages = $params->{messages};
253
    my $messages = $params->{messages};
255
    my $filepath = $params->{filepath};
254
    my $ods_filepath = $params->{filepath};
256
257
    # Create document
258
    use OpenOffice::OODoc;
259
    my $tmpdir = dirname $filepath;
260
    odfWorkingDirectory( $tmpdir );
261
    my $doc = odfDocument( file => $filepath, create => 'spreadsheet' );
262
255
263
    # Prepare sheet
256
    # Prepare sheet
264
    my @headers;
257
    my $ods_content;
265
    my @rows;
258
    my $has_headers;
266
    my $i = 0;
267
    foreach my $message ( @$messages ) {
259
    foreach my $message ( @$messages ) {
268
        my @lines = split /\n/, $message->{content};
260
        my @message_lines = split /\n/, $message->{content};
269
        chomp for @lines;
261
        chomp for @message_lines;
270
262
        # Get headers from first message
271
        # We don't have headers, get them
263
        if ($has_headers) {
272
        unless ( @headers ) {
264
            shift @message_lines;
273
            @headers = split $delimiter, $lines[0];
265
        } else {
274
            my $nb_cols = scalar @headers;
266
            $has_headers = 1;
275
            my $nb_rows = scalar @$messages;
276
            my $sheet = $doc->expandTable( 0, $nb_rows + 1, $nb_cols );
277
            @rows = $doc->getTableRows($sheet);
278
279
            # Write headers row
280
            my $row = $rows[0];
281
            my $j = 0;
282
            for my $header ( @headers ) {
283
                $doc->cellValue( $row, $j, Encode::encode( 'UTF8', $header ) );
284
                $j++;
285
            }
286
            $i = 1;
287
        }
267
        }
288
268
        foreach my $message_line ( @message_lines ) {
289
        # Write all rows
269
            my @content_row;
290
        shift @lines; # remove headers
270
            my @message_cells = split $delimiter, $message_line;
291
        for my $line ( @lines ) {
271
            foreach ( @message_cells ) {
292
            my @row_data = split $delimiter, $line;
272
                push @content_row, Encode::encode( 'UTF8', $_ );
293
            my $row = $rows[$i];
294
            # Note scalar(@$row_data) should be equal to $nb_cols
295
            for ( my $j = 0 ; $j < scalar(@row_data) ; $j++ ) {
296
                my $value = Encode::encode( 'UTF8', $row_data[$j] );
297
                $doc->cellValue( $row, $j, $value );
298
            }
273
            }
299
            $i++;
274
            push @$ods_content, \@content_row;
300
        }
275
        }
301
    }
276
    }
302
    $doc->save();
277
278
    # Process
279
    use Koha::Util::OpenDocument;
280
    generate_ods($ods_filepath, $ods_content);
303
}
281
}
304
282
305
sub send_files {
283
sub send_files {
(-)a/reports/guided_reports.pl (-36 / +18 lines)
Lines 23-29 use Text::CSV::Encoded; Link Here
23
use Encode qw( decode );
23
use Encode qw( decode );
24
use URI::Escape;
24
use URI::Escape;
25
use File::Temp;
25
use File::Temp;
26
use File::Basename qw( dirname );
27
use C4::Reports::Guided;
26
use C4::Reports::Guided;
28
use Koha::Reports;
27
use Koha::Reports;
29
use C4::Auth qw/:DEFAULT get_session/;
28
use C4::Auth qw/:DEFAULT get_session/;
Lines 922-964 elsif ($phase eq 'Export'){ Link Here
922
                $type = 'application/vnd.oasis.opendocument.spreadsheet';
921
                $type = 'application/vnd.oasis.opendocument.spreadsheet';
923
                my $ods_fh = File::Temp->new( UNLINK => 0 );
922
                my $ods_fh = File::Temp->new( UNLINK => 0 );
924
                my $ods_filepath = $ods_fh->filename;
923
                my $ods_filepath = $ods_fh->filename;
925
924
                my $ods_content;
926
                # Create document
925
927
                use OpenOffice::OODoc;
926
                # First line is headers
928
                my $tmpdir = dirname $ods_filepath;
927
                my @headers = header_cell_values($sth);
929
                odfWorkingDirectory( $tmpdir );
928
                push @$ods_content, \@headers;
930
                my $doc = odfDocument( file => $ods_filepath, create => 'spreadsheet' );
929
931
930
                # Other line in Unicode
932
                # Prepare sheet
931
                my $sql_rows = $sth->fetchall_arrayref();
933
                my @headers = header_cell_values( $sth );
932
                foreach my $sql_row ( @$sql_rows ) {
934
                my $rows = $sth->fetchall_arrayref();
933
                    my @content_row;
935
                my ( $nb_rows, $nb_cols ) = ( 0, 0 );
934
                    foreach my $sql_cell ( @$sql_row ) {
936
                $nb_rows = @$rows;
935
                        push @content_row, Encode::encode( 'UTF8', $sql_cell );
937
                $nb_cols = @headers;
938
                my $sheet = $doc->expandTable( 0, $nb_rows + 1, $nb_cols );
939
                my @rows = $doc->getTableRows($sheet);
940
941
                # Write headers row
942
                my $row = $rows[0];
943
                my $j = 0;
944
                for my $header ( @headers ) {
945
                    $doc->cellValue( $row, $j, $header );
946
                    $j++;
947
                }
948
949
                # Write all rows
950
                my $i = 1;
951
                for ( @$rows ) {
952
                    $row = $rows[$i];
953
                    for ( my $j = 0 ; $j < $nb_cols ; $j++ ) {
954
                        my $value = Encode::encode( 'UTF8', $rows->[$i - 1][$j] );
955
                        $doc->cellValue( $row, $j, $value );
956
                    }
936
                    }
957
                    $i++;
937
                    push @$ods_content, \@content_row;
958
                }
938
                }
959
939
960
                # Done
940
                # Process
961
                $doc->save();
941
                use Koha::Util::OpenDocument;
942
                generate_ods($ods_filepath, $ods_content);
943
944
                # Output
962
                binmode(STDOUT);
945
                binmode(STDOUT);
963
                open $ods_fh, '<', $ods_filepath;
946
                open $ods_fh, '<', $ods_filepath;
964
                $content .= $_ while <$ods_fh>;
947
                $content .= $_ while <$ods_fh>;
965
- 

Return to bug 21560