blib/lib/Table.pm untested

filter: Statementstmt 0.0% 0 / 210 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 465 CC 62 SCAR 64.2
CC
62
Cov
0%
html_table
1122.0
html_tag
272.0
table_column
240.0
CRAP 616.4
1 package CGI::AppBuilder::Table;
2  
3 # Perl standard modules
4 use strict;
5 use warnings;
6 use Getopt::Std;
7 use POSIX qw(strftime);
8 use Carp;
9  
10 our $VERSION = 1.001;
11 require Exporter;
12 our @ISA = qw(Exporter CGI::AppBuilder);
13 our @EXPORT = qw();
14 our @EXPORT_OK = qw(html_table html_tag table_column
15 );
16 our %EXPORT_TAGS = (
17 table => [qw(html_table html_tag table_column)],
18 all => [@EXPORT_OK]
19 );
20  
21 use CGI::AppBuilder;
22 use CGI::AppBuilder::Message qw(:all);
23  
24 =head1 NAME
25  
26 CGI::AppBuilder::Table - Configuration initializer
27  
28 =head1 SYNOPSIS
29  
30 use CGI::AppBuilder::Table;
31  
32 my $ab = CGI::AppBuilder::Table->new(
33 'ifn', 'my_init.cfg', 'opt', 'vhS:a:');
34 my ($q, $ar, $ar_log) = $ab->start_app($0, \%ARGV);
35 print $ab->disp_form($q, $ar);
36  
37 =head1 DESCRIPTION
38  
39 This class provides methods for reading and parsing configuration
40 files.
41  
42 =cut
43  
44 =head2 new (ifn => 'file.cfg', opt => 'hvS:')
45  
46 This is a inherited method from CGI::AppBuilder. See the same method
47 in CGI::AppBuilder for more details.
48  
49 =cut
50  
51 sub new {
52 my ($s, %args) = @_;
53 return $s->SUPER::new(%args);
54 }
55  
56 =head2 html_table($arf, $cns, $br)
57  
58 Input variables:
59  
60 $arf - array ref containing the content of the table
61 $cns - column names separated by comma or
62 AUTO|AH|HASH - use $k in AH Array ${$arf}[$i]{$k}
63 $br - hash array ref for table format, it contains
64 css_table - CSS class name for <TABLE>
65 atr_table - attribute parameters for <TABLE>
66 css_tr - CSS class name for <TR>
67 atr_tr - attribute parameters for <TR>
68 atr_tr_odd - attribute parameters for ODD <TR>
69 atr_tr_even - attribute parameters for EVEN <TR>
70 css_tr_odd - CSS class name for ODD <TR>
71 css_tr_even - CSS class name for EVEN <TR>
72 css_select - CSS class name for <SELECT ...>
73 css_input - CSS class name for <INPUT type=input ...>
74 atr_sel - attributes for itemized <SELECT ...> for instance:
75 atr_sel = {
76 var1 => 'style="display:none"',
77 var2 => 'style="display:block"',
78 var3 => 'class="FormSel"',
79 }
80 atr_inp - attributes for itemized <INPUT type=input ...>
81 css_td - CSS class name for <TD>
82 atr_td - attribute parameters for <TD>
83 atr_cell - an array ref to attribute parameters for each cell
84 ${$br}{atr_cell}[$i][$j]
85 esc_vars - a list of escaped variables separated by comma.
86 fh_out - output file handler
87 cns_desc - hash ref containing column name description
88 tab_caption - table caption/header
89 tab_footer - table footer/notes
90 cel_title - a hash ref storing title attribute for defined cells.
91 You can define cells by the value stored in the cell
92 or 'row:col'.
93  
94 Variables used or methods called:
95  
96 CGI::AppBuilder::Message
97 set_param - get parameter from a hash
98  
99 How to use:
100  
101  
102 my @a = (['ColA','ColB'],[1,2],[5,6],[7,8]);
103 my $txt = $self->html_table(\@a);
104 my @b = ({A=>1,B=>2},{A=>5,B=>6},{A=>7,B=>8});
105 my $txt = $self->html_table(\@b,"",'A,B');
106 my $txt = $self->html_table(\@b,"",'A,B');
107  
108 Return: generates HTML Table codes.
109  
110 This method convert all the < and > into &lt; and &gt; for displaying,
111 except variables are specified in I<esc_vars>.
112  
113 =cut
114  
115 sub html_table {
116 my $s = shift;
117 my($ar,$cns,$br) = @_;
118 return "<b>No Input for html_table!</b>\n" if !$ar;
119 return "<b>Not a Array for html_table</b>\n" if $ar !~ /ARRAY/;
120 my ($cn_lst, $cr) = ("",[]);
121  
122 if (${$ar}[0] =~ /HASH/) {
123 if (!$cns || $cns =~ /^(auto|hash|AH)$/i) {
124 map { $cn_lst .= "$_," } (keys %{$ar->[0]});
125 $cn_lst =~ s/,$//; $cn_lst = lc $cn_lst;
126 } else { $cn_lst = $cns; }
127 $cn_lst =~ s/\s+//g;
128 $cr = $ar;
129 } else { # it is an array
130 if (!$cns || $cns =~ /^(auto|hash|AH)$/i) {
131 map { $cn_lst .= "$ar->[0][$_]," } 0..$#{$ar->[0]};
132 $cn_lst =~ s/,$//; $cn_lst = lc $cn_lst;
133 } else { $cn_lst = $cns; }
134 # $cn_lst =~ s/\s+//g;
135 $cn_lst =~ s/\s+/_/g;
136 my @a = split /,/, $cn_lst;
137 shift @$ar; # remove the first row
138 $cr = [map{my %tmp;@tmp{@a}=@$_;\%tmp}@$ar];
139 }
140 return "<b>No column names are provided or defined.</b>\n"
141 if !$cn_lst;
142 my $css_table = $s->set_param('css_table', $br);
143 my $atr_table = $s->set_param('atr_table', $br);
144 my $css_tr = $s->set_param('css_tr', $br);
145 my $atr_tr = $s->set_param('atr_tr', $br);
146 my $atr_tr_od = $s->set_param('atr_tr_odd', $br);
147 my $atr_tr_ev = $s->set_param('atr_tr_even', $br);
148 my $css_tr_od = $s->set_param('css_tr_odd', $br);
149 my $css_tr_ev = $s->set_param('css_tr_even', $br);
150 my $ar_sel = eval $s->set_param('atr_sel', $br);
151 my $ar_inp = eval $s->set_param('atr_inp', $br);
152 my $css_select= $s->set_param('css_select', $br);
153 my $css_input = $s->set_param('css_input', $br);
154 my $css_td = $s->set_param('css_td', $br);
155 my $atr_td = $s->set_param('atr_td', $br);
156 my $cel_title = $s->set_param('cel_title', $br); # Cell title
157 my $atr_cell = $s->set_param('atr_cell', $br);
158 $atr_cell = eval $atr_cell if $atr_cell;
159 $atr_cell = [] if ! $atr_cell;
160 my $pretty = $s->set_param('tab_pretty', $br);
161 my $cap = $s->set_param('tab_caption', $br);
162 my $ftr = $s->set_param('tab_footer', $br);
163 my $esc_vars = $s->set_param('esc_vars', $br);
164 my $esc = ($esc_vars) ? {
165 map { $_ => 1 } (split /,/,$esc_vars)
166 } : {};
167 # my $tr = $s->html_tag('TR',
168 # {class=>$css_tr,attr=>$atr_tr,pretty=>$pretty});
169 my $t = $s->html_tag('TABLE',
170 {class=>$css_table,attr=>$atr_table,pretty=>$pretty});
171 $t .= $s->html_tag('CAPTION',{pretty=>$pretty},$cap,1) if $cap;
172  
173 my ($i,$j,$k,$v);
174 # add column names
175 my $dsc = (exists $br->{cns_desc}) ? $br->{cns_desc} : {};
176 # map { $_ => {name=>ucfirst $_, desc=>ucfirst $_} }
177 # (split /,/, $cn_lst)
178 # add column header
179 $t .= $s->table_column($cn_lst,$br,$dsc);
180 # add data rows
181 my $p = {class=>$css_td,attr=>$atr_td,pretty=>$pretty,hr=>{}};
182 my $p_tr = {pretty=>$pretty,hr=>{}};
183 for $i (0 .. $#{$cr}) {
184 if ($i%2) { # odd
185 $p_tr->{class} = ($css_tr_od) ? $css_tr_od : $css_tr;
186 $p_tr->{attr} = ($atr_tr_od) ? $atr_tr_od : $atr_tr;
187 } else { # even
188 $p_tr->{class} = ($css_tr_ev) ? $css_tr_ev : $css_tr;
189 $p_tr->{attr} = ($atr_tr_ev) ? $atr_tr_ev : $atr_tr;
190 }
191 $t .= $s->html_tag('TR',$p_tr);
192 $j = -1;
193 foreach my $k (split /,/,$cn_lst) {
194 ++$j;
195 $v = $cr->[$i]{$k};
196 if ($v && $v =~ /type=["']?input["']?/si) {
197 $v =~ s/\<\s*input/\<INPUT class=$css_input /i
198 if $css_input;
199 $v =~ s/\<\s*input/\<INPUT $ar_inp->{$k} /i
200 if exists $ar_inp->{$k};
201 }
202 if ($v && $v =~ /\<\s*select/si) {
203 $v =~ s/\<\s*select/\<SELECT class=$css_select /i
204 if $css_select;
205 $v =~ s/\<\s*select/\<SELECT $ar_sel->{$k} /i
206 if exists $ar_sel->{$k};
207 }
208 if (! exists $esc->{$k}) {
209 $v =~ s/</\&lt;/g if $v;
210 $v =~ s/>/\&gt;/g if $v;
211 }
212 $v = "" if ! defined($v) || $v =~ /^\s*$/;
213 $p->{attr} = ($atr_cell->[$i][$j]) ?
214 "$atr_td $atr_cell->[$i][$j]" : $atr_td;
215 if ($cel_title && (exists $cel_title->{"$v"} ||
216 exists $cel_title->{"$i:$j"}) ) {
217 $p->{hr}{title}=(exists $cel_title->{"$v"}) ?
218 $cel_title->{"$v"}{desc}:$cel_title->{"$i:$j"}{desc};
219 # print "$v: ($i, $j)<br>\n";
220 }
221 $t .= $s->html_tag('TD',$p,"$v",1);
222 $p->{hr}{title} = ""; # reset title attribute
223 }
224 # $t .= "</TR>\n";
225 $t .= $s->html_tag('TR',{pretty=>$pretty},'',1);
226 }
227 $t .= "</TABLE>\n";
228 if ($ftr) {
229 $t .= $s->html_tag('CENTER',{},$s->html_tag('P',{},$ftr,1),1);
230 }
231 my $fh = ($br && exists $br->{fh_out})?$br->{fh_out}:"";
232 if ($fh) { print $fh $t; } else { return $t; }
233 }
234  
235 =head2 html_tag ($tag, $pr, $txt, $is_end)
236  
237 Input variables:
238  
239 $tag - HTML tag such as TR, TD, TABLE, SELECT, INPUT, etc.
240 $pr - tag attribute array ref. It contains three elements:
241 class - CSS class name
242 attr - attribute string such as 'width=5 onChange=js_func'
243 hr - hash ref with key and value pairs
244 pretty - whether to add line breaks
245 $txt - text to be incuded between the start and end tag such as
246 <TD>$txt</TD>
247 $is_end - whether to add an ending tag such as </TD>
248  
249 Variables used or methods called:
250  
251 None
252  
253 How to use:
254  
255 my $t1 = $self->html_tag('TD',{class=>'css_td'},'Text',1);
256 # $t1 contains:
257 # <TD class='css_td'>Text</TD>
258 my $t2 = $self->html_tag('TD',{class=>'td1',pretty=>1},'Text2',1);
259 # $t2 contains:
260 # <TD class='td1'>
261 # Text2
262 # </TD>
263 my $t3 = $self->html_tag('TD',{class=>'td1',pretty=>1,
264 attr=>'colspan=2 align=left',hr=>{onChange=>'js_func'}},
265 'Text2',1);
266 # $t3 contains:
267 # <TD class='td1' colspan=2 align=left onChange='js_func'>
268 # Text2
269 # </TD>
270  
271 Return: HTML codes.
272  
273 This method generates HTML codes based on the information provided.
274  
275 =cut
276  
277 sub html_tag {
278 my $s = shift;
279 my ($tag, $pr, $txt, $is_end) = @_;
280 $tag = uc $tag;
281 my $tg = lc $tag;
282 my $idt = {tr=>2,td=>4,li=>2,th=>4,caption=>2};
283 my $t = "";
284 if ($is_end && !$txt && $tg =~ /^(tr|table|caption)/) {
285 $t .= (exists $idt->{$tg}) ? ' 'x$idt->{$tg} : "";
286 $t .= "</$tag>\n" if $is_end;
287 return $t;
288 }
289 $t = (exists $idt->{$tg}) ? ' 'x$idt->{$tg} : "";
290 $t .= "<$tag";
291 $t .= " class=$pr->{class}"
292 if $pr && exists $pr->{class} && $pr->{class};
293 $t .= " $pr->{attr}"
294 if $pr && exists $pr->{attr} && $pr->{attr};
295 if ($pr && exists $pr->{hr} && ref($pr->{hr}) eq 'HASH') {
296 map { $t .= " $_='$pr->{hr}{$_}'" } (keys %{$pr->{hr}});
297 }
298 $t .= ">";
299 if ($pr && exists $pr->{pretty} && $pr->{pretty} &&
300 $tg !~ /^(td|li)/ ) {
301 $t .= "\n";
302 $t .= " $txt\n" if defined($txt) && $txt !~ /^\s*$/;
303 # $t .= (exists $idt->{$tg}) ? ' 'x$idt->{$tg} : "";
304 } else {
305 $t .= (defined($txt) && $txt !~ /^\s*$/) ? $txt :
306 (($tg =~ /^td/i) ? '&nbsp;' : "");
307 }
308 if ($is_end) {
309 if ($tg !~ /^(td|li|th)/) {
310 $t .= (exists $idt->{$tg}) ? ' 'x$idt->{$tg} : "";
311 }
312 $t .= "</$tag>\n" if $is_end;
313 }
314 return $t;
315 }
316  
317 =head2 table_column ($cn,$pr,$cr)
318  
319 Input variables:
320  
321 $cn - column names separated by comma, or
322 array ref containing column names , or
323 hash ref containing column names as keys
324 $pr - tag attribute array ref. It contains the following items:
325 css_tr - TR class name
326 atr_tr - TR attributes
327 css_td - TD class name
328 atr_td - TD attributes
329 pretty - whether to add line breaks
330 atr_cell - Cell attribute
331 $cr - column description hash ref $cr->{$k}{$itm}
332 where $k is column name and the items ($itm) are :
333 desc - column description
334 name - display name
335  
336 Variables used or methods called:
337  
338 html_tag - generate HTML tags
339  
340 How to use:
341  
342 my $cn = 'seq,fn,ln';
343 my $pr = {css_tr=>'tr_pretty',css_td=>'td_small',pretty=>1};
344 my $cr = {seq=>{name=>'Seq No',desc=>'Sequential number'},
345 fn =>{name=>'First Name',desc=>'First name'},
346 ln =>{name=>'Last Name',desc=>'Last name/family name'},
347 };
348 my $t = $self->table_column($cn,$pr,$cr);
349  
350 Return: HTML codes.
351  
352 This method generates HTML codes for table header row (TH)
353 based on the information provided.
354  
355 =cut
356  
357 sub table_column {
358 my $s = shift;
359 my ($cn,$pr,$cr) = @_;
360 carp "No column names are specified." if !$cn;
361 return if !$cn;
362  
363 my $cns = $cn;
364 if (ref($cn) =~ /ARRAY/) {
365 $cns = ""; map { $cns .= "$_," } @$cn; $cns =~ s/,$//;
366 } elsif (ref($cn) =~ /HASH/) {
367 $cns = ""; map { $cns .= "$_," } keys %$cn; $cns =~ s/,$//;
368 }
369 carp "No column names are specified." if !$cns;
370 return if !$cns;
371 my $css_tr = $s->set_param('css_tr', $pr);
372 my $atr_tr = $s->set_param('atr_tr', $pr);
373 my $css_td = $s->set_param('css_td', $pr);
374 my $atr_td = $s->set_param('atr_td', $pr);
375 my $pretty = $s->set_param('pretty', $pr);
376 my $atr_cell = $s->set_param('atr_cell', $pr);
377 $atr_cell = eval $atr_cell if $atr_cell;
378 $atr_cell = [] if ! $atr_cell;
379 my $esc_vars = $s->set_param('esc_vars',$pr);
380 my $esc = ($esc_vars) ? {
381 map { $_ => 1 } (split /,/,$esc_vars)
382 } : {};
383 my $t=$s->html_tag('TR',{class=>$css_tr,attr=>$atr_tr,pretty=>1});
384 my ($j,$x,$txt) = (-1,$atr_td,"");
385 my $p = {class=>$css_td,pretty=>$pretty,hr=>{}};
386 foreach my $k (split /,/,$cns) {
387 ++$j; $k = lc $k;
388 if ($cr && exists $cr->{$k}{name}) {
389 $txt = $cr->{$k}{name};
390 } else {
391 $txt = $k; $txt =~ s/_/ /g;
392 $txt = join ' ', (map {ucfirst $_} (split / /, $txt));
393 }
394 if (! exists $esc->{$k}) {
395 $txt =~ s/</\&lt;/g; $txt =~ s/>/\&gt;/g;
396 }
397 $x = ($x) ? "$x $atr_cell->[0][$j]" : $atr_cell->[0][$j]
398 if exists $atr_cell->[0][$j];
399 $p->{attr} = $x;
400 $p->{hr}{title}="$cr->{$k}{name} ($k): $cr->{$k}{desc}"
401 if $cr && exists $cr->{$k} &&
402 exists $cr->{$k}{name} && exists $cr->{$k}{desc};
403 $t .= $s->html_tag('TH',$p,$txt,1);
404 }
405 $t .= $s->html_tag('TR',{pretty=>$pretty},'',1);
406 # $t .= "</TR>\n";
407 return $t;
408 }
409  
410 1;
411  
412 =head1 HISTORY
413  
414 =over 4
415  
416 =item * Version 0.10
417  
418 This version extracts the disp_form method from CGI::Getopt class.
419  
420 0.11 Inherited the new constructor from CGI::AppBuilder.
421 0.12 Added html_tag and table_column functions
422 Modified html_table to use html_tag and table_column
423  
424 =item * Version 0.20
425  
426 =cut
427  
428 =head1 SEE ALSO (some of docs that I check often)
429  
430 Oracle::Loader, Oracle::Trigger, CGI::Getopt, File::Xcopy,
431 CGI::AppBuilder, CGI::AppBuilder::Message, CGI::AppBuilder::Log,
432 CGI::AppBuilder::Config, etc.
433  
434 =head1 AUTHOR
435  
436 Copyright (c) 2005 Hanming Tu. All rights reserved.
437  
438 This package is free software and is provided "as is" without express
439 or implied warranty. It may be used, redistributed and/or modified
440 under the terms of the Perl Artistic License (see
441 http://www.perl.com/perl/misc/Artistic.html)
442  
443 =cut
444