Bug 30181: Make Koha::BackgroundJob->_derived_class return the right thing
[koha.git] / Koha / Util / OpenDocument.pm
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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use File::Basename qw( dirname );
23 use OpenOffice::OODoc qw( odfDocument odfWorkingDirectory );
24
25 use parent qw( Exporter );
26
27 our @EXPORT = qw(
28   generate_ods
29 );
30
31 =head1 NAME
32
33 Koha::Util::OpenDocument - utility class to manage filed in Open Document Format aka OpenDocument
34
35 =head1 METHODS
36
37 =head2 generate_ods
38
39 Generate an Open Document Sheet
40
41 Arguments are file path and content as an arrayref of lines containing arrayrefs of cells.
42
43 =cut
44
45
46 sub generate_ods {
47     my ( $filepath, $content ) = @_;
48
49     unless ( $filepath && $content ) {
50         return;
51     }
52     my @input_rows = @$content;
53     my $nb_rows    = scalar @input_rows;
54     my $nb_cols;
55     if ($nb_rows) {
56         $nb_cols= scalar @{ $input_rows[0] };
57     }
58
59     # Create document
60     my $wdir = dirname($filepath);
61     odfWorkingDirectory($wdir);
62     my $odf_doc = odfDocument( file => $filepath, create => 'spreadsheet' );
63
64     if ($nb_rows) {
65         # Prepare sheet
66         my $odf_sheet = $odf_doc->expandTable( 0, $nb_rows + 1, $nb_cols );
67         my @odf_rows = $odf_doc->getTableRows($odf_sheet);
68
69         # Writing
70         for ( my $i = 0 ; $i < $nb_rows ; $i++ ) {
71             for ( my $j = 0 ; $j < $nb_cols ; $j++ ) {
72                 my $cellval = $input_rows[$i][$j];
73                 $odf_doc->cellValue( $odf_rows[$i], $j, $cellval );
74             }
75         }
76     }
77
78     # Done
79     $odf_doc->save();
80
81     return $odf_doc;
82 }
83
84 1;
85 __END__
86
87 =head1 AUTHOR
88
89 Koha Development Team <http://koha-community.org/>
90
91 Fridolin Somers <fridolin.somers@biblibre.com>
92
93 =cut