Bug 23895: Move installer file into the mandatory directory
[koha.git] / Koha / Util / OpenDocument.pm
blob937c9bc60e22c97ca068bc3c39cf86551866852c
1 package Koha::Util::OpenDocument;
3 # Copyright 2019 Biblibre
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use Encode qw( decode );
23 use File::Temp;
24 use File::Basename qw( dirname );
25 use OpenOffice::OODoc;
27 use parent qw( Exporter );
29 our @EXPORT = qw(
30 generate_ods
33 =head1 NAME
35 Koha::Util::OpenDocument - utility class to manage filed in Open Document Format aka OpenDocument
37 =head1 METHODS
39 =head2 generate_ods
41 Generate an Open Document Sheet
43 Arguments are file path and content as an arrayref of lines containing arrayrefs of cells.
45 =cut
48 sub generate_ods {
49 my ( $filepath, $content ) = @_;
51 unless ( $filepath && $content ) {
52 return;
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] };
61 # Create document
62 my $wdir = dirname($filepath);
63 odfWorkingDirectory($wdir);
64 my $odf_doc = odfDocument( file => $filepath, create => 'spreadsheet' );
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);
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 );
80 # Done
81 $odf_doc->save();
83 return $odf_doc;
87 __END__
89 =head1 AUTHOR
91 Koha Development Team <http://koha-community.org/>
93 Fridolin Somers <fridolin.somers@biblibre.com>
95 =cut