| 1 | # | ||
| 2 | # $Id: Output.pm,v 1.4 2005/07/14 20:26:15 scottb Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | |||
| 6 | package DBIx::Knowledge::Output; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use DBIx::Knowledge; | ||
| 10 | use HTML::Entities; | ||
| 11 | use DBI; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | DBIx::Knowledge::Output - Output generator (HTML currently) | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | sub new | ||
| 20 | { | ||
| 21 | my ($caller,$header_repeat) = @_; | ||
| 22 | |||
| 23 | my $class = ref($caller) || $caller; | ||
| 24 | my $obj = {}; | ||
| 25 | |||
| 26 | $obj->{header_repeat} = $header_repeat; | ||
| 27 | |||
| 28 | return bless($obj,$class); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub generate_html | ||
| 32 | { | ||
| 33 | my ($self,$report_sth,$select_fields,$want_subtotal_fields) = @_; | ||
| 34 | $want_subtotal_fields ||= {}; | ||
| 35 | |||
| 36 | my $html = <<HTML; | ||
| 37 | <TABLE class="report_data" border="0" cellpadding="3" cellspacing="0"> | ||
| 38 | <TR> | ||
| 39 | HTML | ||
| 40 | |||
| 41 | $html .= header_row_html($select_fields); | ||
| 42 | |||
| 43 | my $last_was_group = 1; | ||
| 44 | my @recent_row = (); | ||
| 45 | my $grouping_id = 0; | ||
| 46 | my $want_sub = scalar(%$want_subtotal_fields); | ||
| 47 | my @sum_row = (); | ||
| 48 | while (my @row = $report_sth->fetchrow_array) | ||
| 49 | { | ||
| 50 | |||
| 51 | # The class will always add a psuedo-column to the result set at the end which is the GROUPING_ID() but only if subtotal fields were requested | ||
| 52 | $grouping_id = pop(@row) if $want_sub; | ||
| 53 | |||
| 54 | if ($want_sub && $grouping_id != 0) | ||
| 55 | { | ||
| 56 | my $field_text; | ||
| 57 | my $field_position; | ||
| 58 | my $html_class; | ||
| 59 | my $grouping_advice = $want_subtotal_fields->{$grouping_id}; | ||
| 60 | if ($grouping_advice->{FIELD_ID} eq $DBIx::Knowledge::REPORT_TOTAL_KEY) | ||
| 61 | { | ||
| 62 | $field_text = 'Report Summary'; | ||
| 63 | $field_position = 0; | ||
| 64 | $html_class = 'report_summary'; | ||
| 65 | @sum_row = @row; | ||
| 66 | $sum_row[0] = 'Report Summary'; | ||
| 67 | next; | ||
| 68 | } | ||
| 69 | else | ||
| 70 | { | ||
| 71 | $field_text = "Summary for " . $recent_row[$grouping_advice->{POSITION}]; | ||
| 72 | $field_text .= aux_info_text(\@row,$select_fields); | ||
| 73 | $field_position = $grouping_advice->{POSITION}; | ||
| 74 | $html_class = "data_subtotal$grouping_advice->{SUBTOTAL_RANK}"; | ||
| 75 | } | ||
| 76 | $row[$field_position] = $field_text; | ||
| 77 | $html .= data_row_html(\@row,$select_fields,$html_class,\@recent_row); | ||
| 78 | $html .= header_row_html($select_fields) if $self->{header_repeat}; | ||
| 79 | $last_was_group = 1; | ||
| 80 | } | ||
| 81 | else | ||
| 82 | { | ||
| 83 | if ($last_was_group) | ||
| 84 | { | ||
| 85 | @recent_row = @row; | ||
| 86 | } | ||
| 87 | my $recent = undef; | ||
| 88 | $recent = $last_was_group ? [] : \@recent_row if $want_sub; | ||
| 89 | ##at watch oblique dependency for html_class arg (should be undef) | ||
| 90 | $html .= data_row_html(\@row,$select_fields,undef,$recent); | ||
| 91 | $last_was_group = 0; | ||
| 92 | } | ||
| 93 | @recent_row = @row; | ||
| 94 | } | ||
| 95 | if (@sum_row) | ||
| 96 | { | ||
| 97 | $html .= data_row_html(\@sum_row,$select_fields,'report_summary'); | ||
| 98 | } | ||
| 99 | return undef if $report_sth->err; | ||
| 100 | |||
| 101 | $html .= qq[</TABLE>\n]; | ||
| 102 | |||
| 103 | return \$html; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub header_row_html | ||
| 107 | { | ||
| 108 | |||
| 109 | my $header_fields = shift; | ||
| 110 | |||
| 111 | my $html = "<TR>\n"; | ||
| 112 | foreach my $header_field (@$header_fields) | ||
| 113 | { | ||
| 114 | |||
| 115 | # Don't output and aux_info fields. These fields are only to | ||
| 116 | # be added to a summary row. | ||
| 117 | unless ($header_field->{aux_info}) { | ||
| 118 | $html .= "<TH>" . encode_entities($header_field->{header}) . "</TH>"; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | $html .= "</TR>\n"; | ||
| 122 | |||
| 123 | return $html; | ||
| 124 | |||
| 125 | } | ||
| 126 | |||
| 127 | sub data_row_html | ||
| 128 | { | ||
| 129 | my ($row,$select_fields,$html_class,$recent_row) = @_; | ||
| 130 | my $class = ''; | ||
| 131 | $class = qq[ class="$html_class" ] if defined($html_class); | ||
| 132 | my $html = "<TR$class>"; | ||
| 133 | my $i = 0; | ||
| 134 | |||
| 135 | # If this row only contains aux_info data suppress it - but only | ||
| 136 | # if this is not a summary row. We can tell if this is a summary | ||
| 137 | # row because $html_class is currently only defined for summary rows. | ||
| 138 | if (is_aux_info_row($row,$select_fields) && !defined $html_class) | ||
| 139 | { | ||
| 140 | return ''; | ||
| 141 | } | ||
| 142 | |||
| 143 | foreach my $val_orig (@$row) | ||
| 144 | { | ||
| 145 | |||
| 146 | # Do not output an aux info fields | ||
| 147 | if ($select_fields->[$i]{aux_info}) { | ||
| 148 | $i++; | ||
| 149 | next; | ||
| 150 | } | ||
| 151 | |||
| 152 | my $val = $val_orig; # Copy, don't modify ref | ||
| 153 | if (ref($recent_row)) | ||
| 154 | { | ||
| 155 | my $prev_val = $recent_row->[$i]; | ||
| 156 | $val = '' if $prev_val eq $val && $select_fields->[$i]{no_repeat} && ($i == 0 || $i == 1); | ||
| 157 | } | ||
| 158 | |||
| 159 | if (length($val)) | ||
| 160 | { | ||
| 161 | $val = encode_entities($val); | ||
| 162 | # After escaping turn any newlines into HTML <br/> tags | ||
| 163 | $val =~ s|\n|<br/>|gs; | ||
| 164 | } | ||
| 165 | else | ||
| 166 | { | ||
| 167 | $val = ' '; | ||
| 168 | } | ||
| 169 | |||
| 170 | my $classes = $select_fields->[$i]{html_class} || []; | ||
| 171 | if (($html_class =~ /^data_subtotal/ || $html_class eq 'report_summary') && ($val =~ /^Summary for / || $val eq 'Report Summary')) | ||
| 172 | { | ||
| 173 | push(@$classes,'subtotal_label'); | ||
| 174 | } | ||
| 175 | my $td_class = ''; | ||
| 176 | $td_class = qq[class="] . join(' ',@$classes) . qq["] if ref($classes); | ||
| 177 | $html .= qq[<TD $td_class ] . (ref($select_fields->[$i]{td_attr}) ? join(' ',@{$select_fields->[$i]{td_attr}}) : '') . qq[>$val</TD>]; | ||
| 178 | $i++; | ||
| 179 | } | ||
| 180 | $html .= "</TR>\n"; | ||
| 181 | return $html; | ||
| 182 | } | ||
| 183 | |||
| 184 | # Extract the aux info summary information and return a string that | ||
| 185 | # can be added to the report summary row. Search the query select | ||
| 186 | # fields looking for an aux_info marker. If found output the select | ||
| 187 | # header and corresponding value from the db result row. Arguments are | ||
| 188 | # a reference to an array of row values returned from the db and a | ||
| 189 | # reference to an array of the query select fields. | ||
| 190 | sub aux_info_text | ||
| 191 | { | ||
| 192 | my ($select_data, $select_fields) = @_; | ||
| 193 | my $aux_info_string = ''; | ||
| 194 | |||
| 195 | for (my $i=0; $i < scalar(@$select_fields); $i++) { | ||
| 196 | my $select_field = $select_fields->[$i]; | ||
| 197 | if ($select_field->{aux_info}) { | ||
| 198 | $aux_info_string .= sprintf("\n %s %s", | ||
| 199 | $select_field->{header}, | ||
| 200 | $select_data->[$i]); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | return $aux_info_string; | ||
| 205 | } | ||
| 206 | |||
| 207 | # Check a database row to see if it contains the data for an aux_info | ||
| 208 | # column. Normally these rows should not be displayed in the report | ||
| 209 | # body but the value in the aux_info column is appended to the summary | ||
| 210 | # text in the summary row. The test is not rigorous. First find if | ||
| 211 | # any aux_info_fields are defined, if so collect them. Then check | ||
| 212 | # each element of the db_row array corresponding to an an aux_info | ||
| 213 | # column and see if the element in that position is not numerically 0. | ||
| 214 | # If the value is non-zero, consider this db_row an aux_info row and | ||
| 215 | # return true. Arguments are an array ref to a database row and an | ||
| 216 | # array ref containing the query select fields. | ||
| 217 | sub is_aux_info_row | ||
| 218 | { | ||
| 219 | my ($db_row, $select_fields) = @_; | ||
| 220 | ##at this is a constant - should be populated once, not every time in this loop | ||
| 221 | my @aux_info_fields = (); | ||
| 222 | my $col; | ||
| 223 | for ($col=0; $col<scalar(@$select_fields); $col++) | ||
| 224 | { | ||
| 225 | push(@aux_info_fields, $col) if $select_fields->[$col]{aux_info}; | ||
| 226 | } | ||
| 227 | |||
| 228 | foreach $col (@aux_info_fields) | ||
| 229 | { | ||
| 230 | if ($db_row->[$col] != 0) | ||
| 231 | { | ||
| 232 | return 1; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | return 0; | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | 1; | ||
| 241 | |||
| 242 | __END__ |