| 1 | # $File: //member/autrijus/DBIx-ReportBuilder/lib/DBIx/ReportBuilder/Attribute.pm $ $Author: autrijus $ | ||
| 2 | # $Revision: #40 $ $Change: 8893 $ $DateTime: 2003/11/16 00:07:29 $ | ||
| 3 | |||
| 4 | package DBIx::ReportBuilder::Attribute; | ||
| 5 | use strict; | ||
| 6 | no warnings 'redefine'; | ||
| 7 | |||
| 8 | sub new { | ||
| 9 | my $class = shift; | ||
| 10 | return bless({ @_ }, $class); | ||
| 11 | } | ||
| 12 | |||
| 13 | sub Att { $_[0]{Att} } | ||
| 14 | sub Tag { $_[0]{Tag} } | ||
| 15 | sub Object { $_[0]{Object} } | ||
| 16 | sub TableObj { $_[0]{Object}->parent->parent } | ||
| 17 | sub Name { $_[0]->Object->ucase($_[0]{Att}) } | ||
| 18 | sub Type { $_[0]->Data->{$_[0]->{Att}}->{type} } | ||
| 19 | sub ReportBuilderObj { $_[0]->Object->twig } | ||
| 20 | |||
| 21 | sub Value { | ||
| 22 | my $self = shift; | ||
| 23 | my $obj = $self->Object or return; | ||
| 24 | my $att = $self->Att; | ||
| 25 | |||
| 26 | return $obj->att($att) unless $att eq 'text'; | ||
| 27 | |||
| 28 | # Text attribute needs $Var escaping | ||
| 29 | |||
| 30 | $obj = $obj->copy; | ||
| 31 | foreach my $var ($obj->children('var')) { | ||
| 32 | $var->set_text('${' . $self->Object->ucase($var->att('name')) . '}'); | ||
| 33 | } | ||
| 34 | my $text = $obj->text; | ||
| 35 | $text =~ s/\$\{(\w+)\}(?!\w)/\$$1/g; | ||
| 36 | return $text; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub Attributes { | ||
| 40 | my $self = shift; | ||
| 41 | my $array = $self->Tag2Attributes->{$self->Tag} or return; | ||
| 42 | return wantarray ? @$array : $array; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub Values { | ||
| 46 | my $rv = $_[0]->Data->{$_[0]->{Att}}->{values} or return; | ||
| 47 | return $rv->($_[0]) if UNIVERSAL::isa($rv => 'CODE'); | ||
| 48 | return wantarray ? @$rv : $rv; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub Default { | ||
| 52 | my $rv = $_[0]->Data->{$_[0]->{Att}}->{default}; | ||
| 53 | return unless defined($rv); | ||
| 54 | return $rv->($_[0]) if UNIVERSAL::isa($rv => 'CODE'); | ||
| 55 | return wantarray ? @$rv : $rv; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub Applicable { | ||
| 59 | return if !$_[0]->Attribute2Tags->{$_[0]->{Att}}{$_[0]->Tag || ''}; | ||
| 60 | my $rv = $_[0]->Data->{$_[0]->{Att}}->{applicable} or return 1; | ||
| 61 | return eval { $rv->($_[0]) }; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub Tag2Attributes () { { | ||
| 65 | p => [ qw( font size border align text ) ], | ||
| 66 | img => [ qw( width height src ) ], | ||
| 67 | include => [ qw( report ) ], | ||
| 68 | table => [ qw( table rows font size border caption ) ], | ||
| 69 | graph => [ qw( table shape style x_axis_field legend threed | ||
| 70 | threed_shading cumulate show_values values_vertical | ||
| 71 | rotate_chart caption ) ], | ||
| 72 | join => [ qw( table field table2 field2 ) ], # type | ||
| 73 | limit => [ qw( table field operator text ) ], # entryaggregator | ||
| 74 | orderby => [ qw( table field order ) ], | ||
| 75 | groupby => [ qw( table field ) ], | ||
| 76 | cell => [ qw( table field font size align function formula text ) ], | ||
| 77 | } }; | ||
| 78 | |||
| 79 | sub Data () { { | ||
| 80 | align => { | ||
| 81 | type => 'radio', | ||
| 82 | values => [qw( left center right )], | ||
| 83 | default => 'left', | ||
| 84 | }, | ||
| 85 | border => { | ||
| 86 | type => 'select', | ||
| 87 | values => [ 0 .. 4 ], | ||
| 88 | default => 0, | ||
| 89 | }, | ||
| 90 | caption => { | ||
| 91 | type => 'text', | ||
| 92 | }, | ||
| 93 | field => { | ||
| 94 | type => 'data_source', | ||
| 95 | values => \&_fields, | ||
| 96 | applicable => sub { | ||
| 97 | $_[0]->Object->att('table') or | ||
| 98 | $_[0]->TableObj->att('table') | ||
| 99 | }, | ||
| 100 | }, | ||
| 101 | field2 => { | ||
| 102 | type => 'data_source', | ||
| 103 | values => \&_fields2, | ||
| 104 | applicable => sub { $_[0]->Object->att('table2') }, | ||
| 105 | }, | ||
| 106 | font => { | ||
| 107 | type => 'select', | ||
| 108 | values => \&_fonts, | ||
| 109 | default => 'serif', | ||
| 110 | }, | ||
| 111 | formula => { | ||
| 112 | type => 'text', | ||
| 113 | }, | ||
| 114 | function => { | ||
| 115 | type => 'select', | ||
| 116 | values => ['', qw( COUNT SUM MAX MIN AVG DISTINCTCOUNT )], | ||
| 117 | applicable => sub { $_[0]->TableObj->first_child('groupbys')->children_count }, | ||
| 118 | }, | ||
| 119 | height => { | ||
| 120 | type => 'number', | ||
| 121 | }, | ||
| 122 | report => { | ||
| 123 | type => 'include', | ||
| 124 | }, | ||
| 125 | operator => { | ||
| 126 | type => 'select', | ||
| 127 | values => [ qw( = != < > ), 'LIKE', 'NOT LIKE', 'STARTSWITH', 'ENDSWITH' ], | ||
| 128 | default => '=', | ||
| 129 | }, | ||
| 130 | order => { | ||
| 131 | type => 'radio', | ||
| 132 | values => [ qw( ASC DESC ) ], | ||
| 133 | default => 'ASC', | ||
| 134 | }, | ||
| 135 | rows => { | ||
| 136 | type => 'number', | ||
| 137 | }, | ||
| 138 | size => { | ||
| 139 | type => 'select', | ||
| 140 | values => [ map { ($_*2) } 3..10 ], | ||
| 141 | default => 12, | ||
| 142 | }, | ||
| 143 | src => { | ||
| 144 | type => 'image', | ||
| 145 | }, | ||
| 146 | table => { | ||
| 147 | type => 'data_source', | ||
| 148 | values => \&_tables, | ||
| 149 | default => sub { $_[0]->TableObj->att('table') }, | ||
| 150 | }, | ||
| 151 | table2 => { | ||
| 152 | type => 'data_source', | ||
| 153 | values => \&_tables2, | ||
| 154 | }, | ||
| 155 | text => { | ||
| 156 | type => 'text', | ||
| 157 | }, | ||
| 158 | width => { | ||
| 159 | type => 'number', | ||
| 160 | }, | ||
| 161 | # ------------------------------------------ # | ||
| 162 | shape => { | ||
| 163 | type => 'select', | ||
| 164 | values => [qw(bars lines pie)], | ||
| 165 | default => 'bars', | ||
| 166 | }, | ||
| 167 | style => { | ||
| 168 | type => 'select', | ||
| 169 | values => sub { | ||
| 170 | ($_[0]->Object->att('shape') eq 'bars') | ||
| 171 | ? qw(bar cylinder) | ||
| 172 | : qw(line dots) | ||
| 173 | }, | ||
| 174 | applicable => \&_is_axis, | ||
| 175 | default => 'bar', | ||
| 176 | }, | ||
| 177 | legend => { | ||
| 178 | type => 'boolean', | ||
| 179 | applicable => \&_is_axis, | ||
| 180 | default => 0, | ||
| 181 | }, | ||
| 182 | (map { substr($_, 0, 1) . '_margin' => { | ||
| 183 | type => 'number', | ||
| 184 | default => 0, | ||
| 185 | } } qw(top bottom left right)), | ||
| 186 | (map { $_."_font" => { | ||
| 187 | type => 'select', | ||
| 188 | values => [qw(ming kai)], | ||
| 189 | default => 'ming'}, | ||
| 190 | $_."_fontsize" => { | ||
| 191 | type => 'number', | ||
| 192 | default => 12, | ||
| 193 | } } qw(title legend)), | ||
| 194 | (map { $_."_font" => { | ||
| 195 | type => 'select', | ||
| 196 | values => [qw(ming kai)], | ||
| 197 | applicable => \&_is_axis, | ||
| 198 | default => 'ming'}, | ||
| 199 | $_."_fontsize" => { | ||
| 200 | type => 'number', | ||
| 201 | applicable => \&_is_axis, | ||
| 202 | default => 12, | ||
| 203 | } } qw(x_label y_label x_axis y_axis values)), | ||
| 204 | (map { $_."_font" => { | ||
| 205 | type => 'select', | ||
| 206 | values => [qw(ming kai)], | ||
| 207 | applicable => \&_is_axis, | ||
| 208 | default => 'ming'}, | ||
| 209 | $_."_fontsize" => { | ||
| 210 | type => 'number', | ||
| 211 | applicable => \&_is_axis, | ||
| 212 | default => 12, | ||
| 213 | } } qw(label value)), | ||
| 214 | accentclr => { | ||
| 215 | type => 'select', | ||
| 216 | values => [qw( | ||
| 217 | white lgray gray dgray black lblue blue dblue | ||
| 218 | gold lyellow yellow dyellow lgreen green dgreen | ||
| 219 | lred red dred lpurple purple dpurple lorange | ||
| 220 | orange pink dpink marine cyan lbrown dbrown | ||
| 221 | )], | ||
| 222 | default => 'black', | ||
| 223 | }, | ||
| 224 | cumulate => { | ||
| 225 | type => 'boolean', | ||
| 226 | applicable => sub { $_[0]->Object->att('shape') eq 'bars' }, | ||
| 227 | default => 0, | ||
| 228 | }, | ||
| 229 | show_values => { | ||
| 230 | type => 'boolean', | ||
| 231 | applicable => sub { !$_[0]->Object->att('cumulate') }, | ||
| 232 | default => 0, | ||
| 233 | }, | ||
| 234 | values_vertical => { | ||
| 235 | type => 'boolean', | ||
| 236 | applicable => sub { $_[0]->Object->att('show_values') }, | ||
| 237 | default => 0, | ||
| 238 | }, | ||
| 239 | rotate_chart => { | ||
| 240 | type => 'boolean', | ||
| 241 | default => 0, | ||
| 242 | }, | ||
| 243 | x_axis_field => { | ||
| 244 | type => 'boolean', | ||
| 245 | default => 0, | ||
| 246 | }, | ||
| 247 | threed => { | ||
| 248 | type => 'boolean', | ||
| 249 | default => 1, | ||
| 250 | }, | ||
| 251 | threed_shading => { | ||
| 252 | type => 'boolean', | ||
| 253 | applicable => sub { $_[0]->Object->att('threed') }, | ||
| 254 | default => 1, | ||
| 255 | }, | ||
| 256 | } }; | ||
| 257 | |||
| 258 | my %a2t; | ||
| 259 | my $t2a = +Tag2Attributes; | ||
| 260 | while (my ($k, $v) = each %$t2a) { | ||
| 261 | $a2t{$_}{$k}++ for @$v; | ||
| 262 | } | ||
| 263 | $a2t{$_}{''}++ for keys %a2t; | ||
| 264 | sub Attribute2Tags () { \%a2t } | ||
| 265 | |||
| 266 | sub _fonts { 'serif', 'sans serif', 'monotype' } | ||
| 267 | |||
| 268 | sub _fields { | ||
| 269 | my $self = shift; | ||
| 270 | my $att = shift || 'table'; | ||
| 271 | my $obj = $self->Object; | ||
| 272 | my $table = $obj->att($att) || $self->TableObj->att($att) or return; | ||
| 273 | return $obj->twig->SearchObj->Fields($table); | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _fields2 { | ||
| 277 | my $self = shift; | ||
| 278 | return $self->_fields( 'table2' ); | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _tables { | ||
| 282 | my $self = shift; | ||
| 283 | my $tag = $self->Object->tag; | ||
| 284 | if ($self->Att eq 'table2' or $tag eq 'table' or $tag eq 'graph') { | ||
| 285 | return sort map { /(\w+)\W*$/ ? lc($1) : lc($_) } | ||
| 286 | $self->ReportBuilderObj->Handle->dbh->tables; | ||
| 287 | } | ||
| 288 | else { | ||
| 289 | # limit only to those accessible by us | ||
| 290 | my %tables; | ||
| 291 | my $obj = $self->TableObj or return; | ||
| 292 | $tables{ $obj->att('table') }++; | ||
| 293 | foreach my $clause ($obj->first_child('joins')->children) { | ||
| 294 | $tables{ $clause->att('table2') }++; | ||
| 295 | } | ||
| 296 | return sort map lc, grep defined, grep length, keys %tables; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | sub _tables2 { goto &_tables } | ||
| 300 | |||
| 301 | sub _is_axis { $_[0]->Object->att('shape') ne 'pie' } | ||
| 302 | |||
| 303 | 1; |