Make it works with RT >= 4.2.0
[manu/RT-Extension-SearchResults-ODS.git] / html / Search / Results.ods
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
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
16 %# from www.gnu.org.
17 %#
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.
22 %#
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.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
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.)
37 %#
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.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 <%ARGS>
49 $Format => undef
50 $Query => ''
51 $OrderBy => 'id'
52 $Order => 'ASC'
53 $PreserveNewLines => 0
54 </%ARGS>
55 <%ONCE>
56 my $no_html = HTML::Scrubber->new( deny => '*' );
57 </%ONCE>
58 <%INIT>
59 require HTML::Entities;
60
61 use OpenOffice::OODoc;
62 my $tmpdir;
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 );
67 } else {
68     $RT::Logger->crit("Neither the RT var directory ($RT::VarPath) nor the system tmpdir (@{[File::Spec->tmpdir]}) are writable!");
69 }
70 my $tmpfile = File::Spec->catfile( $tmpdir, 'Results.ods' );
71 odfWorkingDirectory($tmpdir);
72 my $workbook = odfDocument(file => $tmpfile, create => 'spreadsheet') or die $!;
73
74 $r->content_type('application/vnd.oasis.opendocument.spreadsheet');
75
76 my $DisplayFormat = $m->comp('/Elements/ScrubHTML', Content => $Format);
77
78 my @Format = $m->comp('/Elements/CollectionAsTable/ParseFormat', Format => $DisplayFormat);
79
80 my @columns;
81
82 my $should_loc = { map { $_ => 1 } qw(Status) };
83
84 my $col_entry = sub {
85     my $col = shift;
86     # in tsv output, "#" is often a comment character but we use it for "id"
87     delete $col->{title}
88         if $col->{title} and $col->{title} =~ /^\s*#\s*$/;
89     return {
90         header => Encode::encode_utf8(loc($col->{title} || $col->{attribute})),
91         map    => $m->comp(
92             "/Elements/ColumnMap",
93             Name  => $col->{attribute},
94             Attr  => 'value'
95         ),
96         should_loc => $should_loc->{$col->{attribute}},
97     }
98 };
99
100 if ($PreserveNewLines) {
101     my $col = [];
102     push @columns, $col;
103     for (@Format) {
104         if ($_->{title} eq 'NEWLINE') {
105             $col = [];
106             push @columns, $col;
107         }
108         else {
109             push @$col, $col_entry->($_);
110         }
111     }
112 }
113 else {
114     push @columns, [map { $_->{attribute}
115                           ? $col_entry->($_)
116                           : () } @Format];
117 }
118
119 my $Tickets = RT::Tickets->new( $session{'CurrentUser'} );
120 $Tickets->FromSQL( $Query );
121 if ( $OrderBy =~ /\|/ ) {
122     # Multiple Sorts
123     my @OrderBy = split /\|/, $OrderBy;
124     my @Order   = split /\|/, $Order;
125     $Tickets->OrderByCols(
126         map { { FIELD => $OrderBy[$_], ORDER => $Order[$_] } }
127         ( 0 .. $#OrderBy )
128     );
129 }
130 else {
131     $Tickets->OrderBy( FIELD => $OrderBy, ORDER => $Order );
132 }
133
134 my $rows_count = $Tickets->Count;
135 my $cols_count = 0;
136 foreach my $col (@columns) {
137     foreach my $sub_col (@$col) {
138         $cols_count++;
139     }
140 }
141
142 $workbook->expandTable(0, $rows_count, $cols_count );
143
144 sub int_to_alpha {
145     my $int = shift;
146     $int++;
147
148     my $dividend = $int;
149     my $alpha = '';
150     my $modulo;
151
152     while ($dividend > 0 ) {
153         $modulo = ($dividend - 1) % 26;
154         $alpha = chr(65 + $modulo).$alpha;
155         $dividend = int(($dividend - $modulo) / 26);
156     }
157
158     return $alpha;
159 }
160
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');
164
165 my $ws_col = 0;
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);
171         $ws_col++;
172     }
173 }
174
175 my $ws_row = 1;
176 my $ii = 0;
177 while (my $row = $Tickets->Next) {
178     $ws_col = 0;
179     for my $col (@columns) {
180         for (@$col) {
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);
190             $ws_col++;
191         }
192     }
193     $ws_row++;
194 }
195
196 $workbook->save();
197 open (ODT, $tmpfile);
198 while (<ODT>) {
199     $m->print($_);
200 }
201 close (ODT);
202 unlink ($tmpfile);
203 $m->abort();
204
205 $m->abort();
206
207 </%INIT>