| 1 | # $File: //member/autrijus/DBIx-ReportBuilder/lib/DBIx/ReportBuilder/Render.pm $ $Author: autrijus $ | ||
| 2 | # $Revision: #27 $ $Change: 8715 $ $DateTime: 2003/11/06 15:50:39 $ | ||
| 3 | |||
| 4 | package DBIx::ReportBuilder::Render; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use Safe; | ||
| 8 | use DBIx::ReportBuilder ':all'; | ||
| 9 | use base 'XML::Twig'; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($class, %args) = @_; | ||
| 13 | my $self = $class->SUPER::new( | ||
| 14 | twig_handlers => { | ||
| 15 | cells => \&cells, | ||
| 16 | join => \&join, | ||
| 17 | cell => \&cell, | ||
| 18 | limit => \&limit, | ||
| 19 | orderby => \&orderby, | ||
| 20 | groupby => \&groupby, | ||
| 21 | table => \&table, | ||
| 22 | graph => \&graph, | ||
| 23 | joins => \&joins, | ||
| 24 | limits => \&limits, | ||
| 25 | orderbys => \&orderbys, | ||
| 26 | groupbys => \&groupbys, | ||
| 27 | var => \&var, | ||
| 28 | meta => \&meta, | ||
| 29 | include => \&include, | ||
| 30 | %{$args{twig_handlers}||{}}, | ||
| 31 | }, | ||
| 32 | start_tag_handlers => { | ||
| 33 | table => \&search, | ||
| 34 | graph => \&search, | ||
| 35 | %{$args{start_tag_handlers}||{}}, | ||
| 36 | }, | ||
| 37 | end_tag_handlers => { | ||
| 38 | %{$args{end_tag_handlers}||{}}, | ||
| 39 | }, | ||
| 40 | twig_roots => { | ||
| 41 | %{$args{twig_roots}||{}}, | ||
| 42 | 'html' => 1 | ||
| 43 | }, | ||
| 44 | pretty_print => $args{pretty_print}, | ||
| 45 | ); | ||
| 46 | |||
| 47 | my $obj = $args{Object} or die "Must have object"; | ||
| 48 | $self->SetObject($obj); | ||
| 49 | $self->parse( $obj->sprint ); | ||
| 50 | return $self; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub Render { $_[0]->root->sprint } | ||
| 54 | sub Object { $_[0]->{object} } | ||
| 55 | sub SetObject { $_[0]->{object} = $_[1] } | ||
| 56 | |||
| 57 | sub NextPart { ++$_[0]->{next_part} } | ||
| 58 | |||
| 59 | sub loc { | ||
| 60 | my $self = shift; | ||
| 61 | $self->Object->loc(@_); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub p { | ||
| 65 | my %atts = %{$_->atts}; | ||
| 66 | $_->del_att( Atts($_) ); | ||
| 67 | $_->insert(font => { face => $atts{font} }) if $atts{font}; | ||
| 68 | $_->set_att(align => $atts{align}) if $atts{align}; | ||
| 69 | my $style; | ||
| 70 | $style .= "border: $atts{border}px black solid;" if $atts{border}; | ||
| 71 | $style .= "font-size: $atts{size};" if $atts{size}; | ||
| 72 | $_->set_att(style => $style) if $style; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub cells { | ||
| 76 | my $self = shift; | ||
| 77 | my $item = $_; | ||
| 78 | my @children = $item->cut_children; | ||
| 79 | my $tr = $item->insert('thead', 'tr'); | ||
| 80 | my $Headers = $item->parent->att('#Headers'); | ||
| 81 | |||
| 82 | foreach my $item (@children) { | ||
| 83 | my $th = $tr->insert_new_elt(last_child => 'th'); | ||
| 84 | $th->set_text($item->text); | ||
| 85 | $th->set_att( %{$_->atts} ); | ||
| 86 | $th->set_id($item->id); | ||
| 87 | $item->set_tag('td'); | ||
| 88 | push @$Headers, $item->text; | ||
| 89 | } | ||
| 90 | |||
| 91 | $item->insert_new_elt(last_child => 'tbody') | ||
| 92 | if $item->parent->tag eq 'table'; | ||
| 93 | |||
| 94 | $self->_do_search( | ||
| 95 | $item, | ||
| 96 | $item->parent->att('#Tables'), | ||
| 97 | $item->parent->att('#SearchBuilder'), | ||
| 98 | $item->parent->att('#Result'), | ||
| 99 | $item->last_child('tbody'), | ||
| 100 | \@children, | ||
| 101 | ); | ||
| 102 | |||
| 103 | $item->erase; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _do_search { | ||
| 107 | my ($self, $item, $tables, $SB, $result, $tbody, $children) = @_; | ||
| 108 | |||
| 109 | return unless $SB; | ||
| 110 | if (my $code = $self->Object->SearchHook) { | ||
| 111 | $code->($item, $tables, $SB); | ||
| 112 | } | ||
| 113 | $SB->RedoSearch; | ||
| 114 | |||
| 115 | my @fields = map { $_->att('field') } @$children; | ||
| 116 | my %vars = map { | ||
| 117 | $self->Object->lcase($_) => $self->Object->Var($_), | ||
| 118 | $self->Object->ucase($_) => $self->Object->Var($_), | ||
| 119 | } $self->Object->Vars; | ||
| 120 | |||
| 121 | $SB->DEBUG(1) if $::DEBUG; | ||
| 122 | |||
| 123 | my $tr_cnt = -1; | ||
| 124 | while (my $Record = $SB->Next) { | ||
| 125 | my $tr = $tbody->insert_new_elt(last_child => 'tr') if $tbody; | ||
| 126 | my $td_cnt = -1; ++$tr_cnt; | ||
| 127 | foreach my $item (@$children) { | ||
| 128 | my $td = $item->copy; | ||
| 129 | my $text = $Record->{$item->att('#Column')}; | ||
| 130 | my $formula = $td->att('formula'); | ||
| 131 | if (defined($formula) and length($formula)) { | ||
| 132 | my $safe = Safe->new; | ||
| 133 | $safe->permit(qw(:base_core :base_math)); | ||
| 134 | |||
| 135 | while (my ($k, $v) = each %vars) { | ||
| 136 | ${$safe->varglob($k)} = $v; | ||
| 137 | } | ||
| 138 | ${$safe->varglob('_')} = $text; | ||
| 139 | ${$safe->varglob($_)} = $Record->{$_} for @fields; | ||
| 140 | |||
| 141 | local $SIG{FPE} = sub {}; | ||
| 142 | local $SIG{__WARN__} = sub {}; | ||
| 143 | local $SIG{__DIE__} = sub {}; | ||
| 144 | $text = $safe->reval($formula); | ||
| 145 | } | ||
| 146 | |||
| 147 | if (!$tr) { | ||
| 148 | # this is a graph part | ||
| 149 | $result->[++$td_cnt][$tr_cnt] = $text; | ||
| 150 | next; | ||
| 151 | } | ||
| 152 | $td->set_text($text); | ||
| 153 | $td->del_att(qw( id field formula )); | ||
| 154 | $td->paste(last_child => $tr); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub table { | ||
| 160 | my $self = shift; | ||
| 161 | my $item = $_; | ||
| 162 | |||
| 163 | $item->set_att(width => '100%'); | ||
| 164 | $item->insert_new_elt('caption', { | ||
| 165 | map { $_ => $item->att($_) } grep { $item->att($_) } qw( font size ) | ||
| 166 | }, $item->att('caption') ); | ||
| 167 | $item->del_att(grep !/border/, Atts($item)); | ||
| 168 | } | ||
| 169 | |||
| 170 | sub graph { | ||
| 171 | my $self = shift; | ||
| 172 | my $item = $_; | ||
| 173 | $self->table($item); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub search { | ||
| 177 | my $self = shift; | ||
| 178 | my $SB = $self->Object->SearchObj or die "Cannot make Search"; | ||
| 179 | $SB->SetTable($_->att('table')); | ||
| 180 | $SB->UnLimit; | ||
| 181 | |||
| 182 | if (my $item = $_->att('rows')) { | ||
| 183 | $SB->RowsPerPage( $item ) | ||
| 184 | } | ||
| 185 | if (my $item = $_->att('firstrow')) { | ||
| 186 | $SB->FirstRow( $item ) | ||
| 187 | } | ||
| 188 | |||
| 189 | $_->set_att( | ||
| 190 | '#Tables' => { | ||
| 191 | ($SB->Table) => '' # key: table, val: alias | ||
| 192 | }, | ||
| 193 | '#OrderBy' => [], # passed to OrderByCols | ||
| 194 | '#GroupBy' => [], # passed to GroupByCols | ||
| 195 | '#SearchBuilder' => $SB, # SearchBuilder object | ||
| 196 | '#Result' => [], # result set | ||
| 197 | '#Headers' => [], # header set | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub _alias { | ||
| 202 | my $Tables = $_[0]->parent->parent->att('#Tables'); | ||
| 203 | my $rv = $Tables->{$_[0]->att('table')} or return; | ||
| 204 | return (ALIAS => $rv); | ||
| 205 | }; | ||
| 206 | |||
| 207 | sub join { | ||
| 208 | my $item = $_; | ||
| 209 | my $SB = $item->parent->parent->att('#SearchBuilder'); | ||
| 210 | my $Tables = $item->parent->parent->att('#Tables'); | ||
| 211 | $Tables->{$_->att('table2')} = $SB->Join( | ||
| 212 | TYPE => ($item->att('type') || 'left'), | ||
| 213 | ALIAS1 => ($Tables->{$item->att('table') || ''} || 'main'), | ||
| 214 | FIELD1 => $item->att('field'), | ||
| 215 | TABLE2 => $item->att('table2'), | ||
| 216 | FIELD2 => $item->att('field2'), | ||
| 217 | map { uc($_) => $item->att($_) } $item->att_names | ||
| 218 | ); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub limit { | ||
| 222 | my $item = $_; | ||
| 223 | my $SB = $item->parent->parent->att('#SearchBuilder'); | ||
| 224 | $SB->Limit( | ||
| 225 | CASESENSITIVE => 1, | ||
| 226 | VALUE => $item->text, | ||
| 227 | (map { uc($_) => $item->att($_) } $item->att_names), | ||
| 228 | _alias($item), | ||
| 229 | ); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub cell { | ||
| 233 | my $item = $_; | ||
| 234 | my $SB = $item->parent->parent->att('#SearchBuilder'); | ||
| 235 | $item->set_att('#Column', $SB->Column( | ||
| 236 | (map { uc($_) => $item->att($_) } $item->att_names), | ||
| 237 | _alias($item), | ||
| 238 | )); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub orderby { | ||
| 242 | my $item = $_; | ||
| 243 | my $OrderBy = $item->parent->parent->att('#OrderBy'); | ||
| 244 | push @$OrderBy, { | ||
| 245 | (map { uc($_) => $item->att($_) } $item->att_names), | ||
| 246 | _alias($item), | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | sub orderbys { | ||
| 251 | my $item = $_; | ||
| 252 | my $SB = $item->parent->att('#SearchBuilder'); | ||
| 253 | my $OrderBy = $item->parent->att('#OrderBy'); | ||
| 254 | $SB->OrderByCols( @$OrderBy ) if @$OrderBy; | ||
| 255 | $_->delete; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub groupby { | ||
| 259 | my $item = $_; | ||
| 260 | my $GroupBy = $item->parent->parent->att('#GroupBy'); | ||
| 261 | push @$GroupBy, { | ||
| 262 | (map { uc($_) => $item->att($_) } $item->att_names), | ||
| 263 | _alias($item), | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | sub groupbys { | ||
| 268 | my $item = $_; | ||
| 269 | my $SB = $item->parent->att('#SearchBuilder'); | ||
| 270 | my $GroupBy = $item->parent->att('#GroupBy'); | ||
| 271 | $SB->GroupByCols( @$GroupBy ) if @$GroupBy; | ||
| 272 | $_->delete; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub joins { $_->delete } | ||
| 276 | sub limits { $_->delete } | ||
| 277 | sub meta { $_->delete } | ||
| 278 | |||
| 279 | sub var { | ||
| 280 | my $self = shift; | ||
| 281 | my $item = $_; | ||
| 282 | $item->set_text( $self->Object->Var( $item->att('name')) ); | ||
| 283 | $item->set_tag('span'); | ||
| 284 | $item->del_att('name'); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub include { | ||
| 288 | XML::Twig::Elt->parse( | ||
| 289 | $_[0]->Object->render_report($_->att('report')) | ||
| 290 | )->replace($_); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub plotGraph { | ||
| 294 | my ($self, $item) = @_; | ||
| 295 | |||
| 296 | my $graph = $self->Object->GraphObj( | ||
| 297 | %{ $item->atts }, | ||
| 298 | width => 400, | ||
| 299 | height => 300, | ||
| 300 | ) or return; | ||
| 301 | |||
| 302 | my $png = $graph->Plot( | ||
| 303 | labels => $item->att('#Headers'), | ||
| 304 | data => $item->att('#Result'), | ||
| 305 | ) or return; | ||
| 306 | |||
| 307 | $item->insert_new_elt(div => { align => 'center' }) | ||
| 308 | ->insert_new_elt(img => { src => $self->Object->encode_src($png) }); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub HeadDimensions { | ||
| 312 | my ($self, $item) = @_; | ||
| 313 | my $size = $self->PageToDimensions->{$item->att('paper')}; | ||
| 314 | $size ||= $self->PageToDimensions->{'A4'}; | ||
| 315 | |||
| 316 | my $margins; | ||
| 317 | foreach my $key (map "margin_$_", qw/top right bottom left/) { | ||
| 318 | my $margin = $item->att($key); | ||
| 319 | $margin = '200' unless length($margin); | ||
| 320 | $margins .= "${margin}mm "; | ||
| 321 | } | ||
| 322 | |||
| 323 | if ($item->att('orientation') eq 'landscape') { | ||
| 324 | $size = join(' ', reverse split(/ /, $size)); | ||
| 325 | } | ||
| 326 | |||
| 327 | return " | ||
| 328 | \@page { size: $size; margin: $margins} | ||
| 329 | P { margin-bottom: 0.21cm } | ||
| 330 | TH P { margin-bottom: 0.21cm; font-style: italic } | ||
| 331 | TD P { margin-bottom: 0.21cm } | ||
| 332 | "; | ||
| 333 | } | ||
| 334 | |||
| 335 | use constant PageToDimensions => { | ||
| 336 | A1 => "59.4cm 84cm", | ||
| 337 | A2 => "42cm 59.4cm", | ||
| 338 | A3 => "29.7cm 42cm", | ||
| 339 | A4 => "21cm 29.7cm", | ||
| 340 | A5 => "14.85cm 21cm", | ||
| 341 | B1 => "70.7cm 100cm", | ||
| 342 | B2 => "50cm 70.7cm", | ||
| 343 | B3 => "35.3cm 50cm", | ||
| 344 | B4 => "25cm 35.3cm", | ||
| 345 | B5 => "17.7cm 25cm", | ||
| 346 | }; | ||
| 347 | |||
| 348 | 1; |