1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 %# <sales@bestpractical.com>
8 %# (Except where explicitly superseded by other copyright notices)
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 %# General Public License for more details.
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 %# CONTRIBUTION SUBMISSION POLICY:
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
47 %# END BPS TAGGED BLOCK }}}
53 $PreserveNewLines => 0
56 my $no_html = HTML::Scrubber->new( deny => '*' );
59 require HTML::Entities;
61 use OpenOffice::OODoc;
63 if ( -w $RT::VarPath ) {
64 $tmpdir = File::Temp::tempdir( DIR => $RT::VarPath, CLEANUP => 1 );
65 } elsif (-w File::Spec->tmpdir) {
66 $tmpdir = File::Temp::tempdir( TMPDIR => 1, CLEANUP => 1 );
68 $RT::Logger->crit("Neither the RT var directory ($RT::VarPath) nor the system tmpdir (@{[File::Spec->tmpdir]}) are writable!");
70 my $tmpfile = File::Spec->catfile( $tmpdir, 'Results.ods' );
71 odfWorkingDirectory($tmpdir);
72 my $workbook = odfDocument(file => $tmpfile, create => 'spreadsheet') or die $!;
74 $r->content_type('application/vnd.oasis.opendocument.spreadsheet');
76 my $DisplayFormat = $m->comp('/Elements/ScrubHTML', Content => $Format);
78 my @Format = $m->comp('/Elements/CollectionAsTable/ParseFormat', Format => $DisplayFormat);
82 my $should_loc = { map { $_ => 1 } qw(Status) };
86 # in tsv output, "#" is often a comment character but we use it for "id"
88 if $col->{title} and $col->{title} =~ /^\s*#\s*$/;
90 header => Encode::encode_utf8(loc($col->{title} || $col->{attribute})),
92 "/Elements/ColumnMap",
93 Name => $col->{attribute},
96 should_loc => $should_loc->{$col->{attribute}},
100 if ($PreserveNewLines) {
104 if ($_->{title} eq 'NEWLINE') {
109 push @$col, $col_entry->($_);
114 push @columns, [map { $_->{attribute}
119 my $Tickets = RT::Tickets->new( $session{'CurrentUser'} );
120 $Tickets->FromSQL( $Query );
121 if ( $OrderBy =~ /\|/ ) {
123 my @OrderBy = split /\|/, $OrderBy;
124 my @Order = split /\|/, $Order;
125 $Tickets->OrderByCols(
126 map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } }
131 $Tickets->OrderBy( FIELD => $OrderBy, ORDER => $Order );
134 my $rows_count = $Tickets->Count;
136 foreach my $col (@columns) {
137 foreach my $sub_col (@$col) {
142 $workbook->expandTable(0, $rows_count, $cols_count );
152 while ($dividend > 0 ) {
153 $modulo = ($dividend - 1) % 26;
154 $alpha = chr(65 + $modulo).$alpha;
155 $dividend = int(($dividend - $modulo) / 26);
161 my $xml = '<table:database-ranges><table:database-range table:name="AllRTTickets" table:target-range-address="Sheet1.A1:Sheet1.'.int_to_alpha($cols_count - 1).$rows_count.'"/></table:database-ranges>';
162 my $xmlpos = $workbook->getElement('//table:table', 0);
163 $workbook->insertElement ( $xmlpos, $xml, position => 'after');
166 foreach my $col (@columns) {
167 foreach my $sub_col ( @$col ) {
168 my $label = $sub_col->{header};
169 Encode::_utf8_off($label);
170 $workbook->updateCell(0, 0, $ws_col, $label);
177 while (my $row = $Tickets->Next) {
179 for my $col (@columns) {
181 my $val = ProcessColumnMapValue($_->{map}, Arguments => [$row, $ii++], Escape => 0);
182 $val = loc($val) if $_->{should_loc};
183 # remove tabs from all field values, they screw up the tsv
184 $val = '' unless defined $val;
185 $val =~ s/(?:\n|\r)//g; $val =~ s{\t}{ }g;
186 $val = $no_html->scrub($val);
187 $val = HTML::Entities::decode_entities($val);
188 Encode::_utf8_off($val);
189 $workbook->updateCell(0, $ws_row, $ws_col, $val);
197 open (ODT, $tmpfile);