1 # Copyright (C) 2005 Stanislav Sinyagin
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 # $Id: HTML.pm,v 1.2 2010-12-30 07:25:30 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 package Torrus::ReportOutput::HTML;
27 use Torrus::ReportOutput;
28 use Torrus::SiteConfig;
30 use base qw( Torrus::ReportOutput::Freeside Torrus::ReportOutput );
33 (January February March April May June
34 July August September October November December);
40 Torrus::SiteConfig::loadStyling();
42 my $htmldir = $self->{'outdir'} . '/html';
45 Verbose('Creating directory: ' . $htmldir);
46 if( not mkdir( $htmldir ) )
48 Error('Cannot create directory ' . $htmldir . ': ' . $!);
52 $self->{'htmldir'} = $htmldir;
55 new Template(INCLUDE_PATH => $Torrus::Global::templateDirs,
61 # Print the head page and years reference
65 my $allReports = shift;
67 return $self->render({
68 'filename' => $self->indexFilename(),
69 'template' => 'index',
70 'data' => $allReports });
74 # Print monthly report for a given service ID
75 # The fields argument is a hash of hashes:
76 # serviceid => reportname => month => fieldname => {value, units}
84 while( my( $serviceid, $ref ) = each %{$fields} )
87 'filename' => $self->srvIdFilename($year, $serviceid),
88 'template' => 'serviceid',
90 'serviceid' => $serviceid,
91 'year' => $year }) ? $ok:0;
97 # Print daily report -- NOT IMPLEMENTED YET
99 # day => reportname => serviceid => fieldname => {value, units}
111 # Print monthly report
113 # month => reportname => serviceid => fieldname => {value, units}
122 while( my( $month, $ref ) = each %{$fields} )
125 'filename' => $self->monthlyFilename($year, $month),
126 'template' => 'monthly',
129 'month' => $month }) )
131 push( @months, $month );
139 my @sorted = sort {$a <=>$b} @months;
140 $ok = $self->render({
141 'filename' => $self->yearlyFilename($year),
142 'template' => 'yearly',
143 'data' => {'months' => \@sorted},
144 'year' => $year }) ? $ok:0;
159 my $serviceid = shift;
161 return sprintf('%.4d_serviceid_%s.html', $year, $serviceid);
170 return sprintf('%.4d_monthly_%.2d.html', $year, $month);
178 return sprintf('%.4d_yearly.html', $year);
188 my $outfile = $self->{'htmldir'} . '/' . $opt->{'filename'};
189 my $tmplfile = $Torrus::ReportOutput::HTML::templates{$opt->{'template'}};
190 Debug('Rendering ' . $outfile . ' from ' . $tmplfile);
194 'plainURL' => $Torrus::Renderer::plainURL,
195 'style' => sub { return $self->style($_[0]); },
196 'treeName' => $self->{'options'}->{'Tree'},
197 'companyName'=> $Torrus::Renderer::companyName,
198 'companyURL' => $Torrus::Renderer::companyURL,
199 'siteInfo' => $Torrus::Renderer::siteInfo,
200 'version' => $Torrus::Global::version,
201 'xmlnorm' => \&xmlnormalize,
202 'data' => $opt->{'data'},
203 'year' => $opt->{'year'},
204 'month' => $opt->{'month'},
205 'serviceid' => $opt->{'serviceid'},
207 return $self->reportUrl($self->indexFilename());},
209 return $self->reportUrl($self->srvIdFilename($opt->{'year'},
211 'monthlyUrl' => sub {
212 return $self->reportUrl($self->monthlyFilename($opt->{'year'},
215 return $self->reportUrl($self->yearlyFilename($_[0]));},
216 'monthName' => sub {$self->monthName($_[0]);},
217 'formatValue' => sub {
220 return sprintf('%.2f %s', $_[0]->{'value'}, $_[0]->{'units'});
226 'timestamp' => sub { return time2str($Torrus::Renderer::timeFormat,
230 'freesideHeader' => sub { return $self->freesideHeader(@_); },
231 'freesideFooter' => sub { return $self->freesideFooter(); },
234 my $result = $self->{'tt'}->process( $tmplfile, $ttvars, $outfile );
238 Error("Error while rendering " . $outfile . ": " .
239 $self->{'tt'}->error());
251 my $ret = $Torrus::Renderer::styling{'report'}{$object};
252 if( not defined( $ret ) )
254 $ret = $Torrus::Renderer::styling{'default'}{$object};
265 return $monthNames[ $month - 1 ];
272 my $filename = shift;
274 return $Torrus::Renderer::rendererURL . '/' .
275 $self->{'options'}->{'Tree'} . '?htmlreport=' . $filename;
282 $txt =~ s/\&/\&\;/gm;
283 $txt =~ s/\</\<\;/gm;
284 $txt =~ s/\>/\>\;/gm;
285 $txt =~ s/\'/\&apos\;/gm;
286 $txt =~ s/\"/\"\;/gm;
298 # indent-tabs-mode: nil
299 # perl-indent-level: 4