| 1 | package CGI::AppBuilder::Form; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Getopt::Std; | ||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Carp; | ||
| 9 | use CGI qw(:standard); | ||
| 10 | use CGI::AppBuilder; | ||
| 11 | use CGI::AppBuilder::Message qw(:echo_msg); | ||
| 12 | |||
| 13 | our $VERSION = 1.001; | ||
| 14 | require Exporter; | ||
| 15 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 16 | our @EXPORT = qw(); | ||
| 17 | our @EXPORT_OK = qw(disp_form replace_named_variables | ||
| 18 | explode_variable explode_html | ||
| 19 | ); | ||
| 20 | our %EXPORT_TAGS = ( | ||
| 21 | form => [qw(disp_form)], | ||
| 22 | all => [@EXPORT_OK] | ||
| 23 | ); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | CGI::AppBuilder::Form - Configuration initializer | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use CGI::AppBuilder::Form; | ||
| 32 | |||
| 33 | my $ab = CGI::AppBuilder::Form->new( | ||
| 34 | 'ifn', 'my_init.cfg', 'opt', 'vhS:a:'); | ||
| 35 | my ($q, $ar, $ar_log) = $ab->start_app($0, \%ARGV); | ||
| 36 | print $ab->disp_form($q, $ar); | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This class provides methods for reading and parsing configuration | ||
| 41 | files. | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | =head2 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 46 | |||
| 47 | This is a inherited method from CGI::AppBuilder. See the same method | ||
| 48 | in CGI::AppBuilder for more details. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my ($s, %args) = @_; | ||
| 54 | return $s->SUPER::new(%args); | ||
| 55 | } | ||
| 56 | |||
| 57 | =head2 disp_form ($q, $ar) | ||
| 58 | |||
| 59 | Input variables: | ||
| 60 | |||
| 61 | $q - CGI object | ||
| 62 | $ar - array ref for parameters | ||
| 63 | |||
| 64 | Variables used or routines called: | ||
| 65 | |||
| 66 | CGI::AppBuilder::Message | ||
| 67 | echo_msg - echo messages | ||
| 68 | set_param - get a parameter from hash array | ||
| 69 | CGI::AppBuilder::Config | ||
| 70 | eval_variables - replace value names with their values. | ||
| 71 | |||
| 72 | How to use: | ||
| 73 | |||
| 74 | my $ifn = 'myConfig.ini'; | ||
| 75 | my ($q,$ar) = $s->get_inputs($ifn); | ||
| 76 | $self->disp_form($q, $ar); | ||
| 77 | |||
| 78 | Return: none | ||
| 79 | |||
| 80 | This method expects the following varialbes in $ar: | ||
| 81 | |||
| 82 | gk - GUI key items | ||
| 83 | gi - GUI items | ||
| 84 | gc - GUI columns | ||
| 85 | gf - GUI form | ||
| 86 | db - database connection varialbes (optional) | ||
| 87 | vars_keep - variables separated by comma for hidden variables | ||
| 88 | hr_form - hash ref containing attributes for <FORM> such as | ||
| 89 | -target = "main" | ||
| 90 | |||
| 91 | This method performs the following tasks: | ||
| 92 | |||
| 93 | 1) checks whether GI, GC and GF variables being defined. | ||
| 94 | 2) replaces AR, DB, GI, and GC variables with their contents | ||
| 95 | 3) builds GF elements | ||
| 96 | 4) add hidden variables | ||
| 97 | 5) print the form | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub disp_form { | ||
| 102 | my $s = shift; | ||
| 103 | my ($q, $ar) = @_; | ||
| 104 | |||
| 105 | # check required GUI variables | ||
| 106 | foreach my $k (split /,/, 'gi,gc,gf') { | ||
| 107 | # $s->echo_msg(" checking $k...", 3); | ||
| 108 | next if exists $ar->{$k}; | ||
| 109 | print h1("GUI element - {$k} is not defined"); | ||
| 110 | return; | ||
| 111 | } | ||
| 112 | if ($ar->{gi} =~ /db->/ && ! exists $ar->{db}) { | ||
| 113 | print h1("GUI element - {db} is not defined"); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | my $mvs = {}; | ||
| 117 | $mvs = $s->eval_variables($ar->{gk}, $ar) if exists $ar->{gk}; | ||
| 118 | $mvs = $s->eval_variables($ar->{gi}, $ar) if exists $ar->{gi}; | ||
| 119 | $mvs = $s->eval_variables($ar->{gc}, $ar) if exists $ar->{gc}; | ||
| 120 | $mvs = $s->eval_variables($ar->{gf}, $ar) if exists $ar->{gf}; | ||
| 121 | |||
| 122 | my $db = (exists $ar->{db} && $ar->{db}) ? $ar->{db} : {}; | ||
| 123 | my $pr = {ar=>$ar, db=>$db}; | ||
| 124 | $s->replace_named_variables($ar, 'ar,db,gk,gi,gc','gk,gi,gc,gf'); | ||
| 125 | |||
| 126 | my $gk = $s->explode_variable($ar, 'gk', $pr); | ||
| 127 | $pr->{gk} = $gk; | ||
| 128 | my $gi = $s->explode_variable($ar, 'gi', $pr); | ||
| 129 | $pr->{gi} = $gi; | ||
| 130 | |||
| 131 | # my $gi = eval $ar->{gi}; | ||
| 132 | if (!$gi) { | ||
| 133 | $s->echo_msg("GI is empty." ,1); | ||
| 134 | } elsif ($gi !~ /HASH/) { | ||
| 135 | $s->echo_msg($ar->{gi},2); | ||
| 136 | $s->echo_msg("GI not properly defined." ,1); | ||
| 137 | } else { | ||
| 138 | $s->explode_html($gi, $pr); | ||
| 139 | } | ||
| 140 | # my $gc = eval $ar->{gc}; | ||
| 141 | my $gc = $s->explode_variable($ar, 'gc', $pr); | ||
| 142 | $pr->{gc} = $gc; | ||
| 143 | if ($gc !~ /HASH/ || ! exists $gc->{td}) { | ||
| 144 | $s->echo_msg($ar->{gc},2); | ||
| 145 | $s->echo_msg("GC not properly defined." ,1); | ||
| 146 | } else { | ||
| 147 | $s->echo_msg($ar->{gc},5); | ||
| 148 | $s->echo_msg($gc,5); | ||
| 149 | } | ||
| 150 | $s->echo_msg($ar->{gf},5); | ||
| 151 | # my $gf = eval $ar->{gf}; | ||
| 152 | my $gf = $s->explode_variable($ar, 'gf', $pr); | ||
| 153 | |||
| 154 | my $fmn = 'fm1'; | ||
| 155 | $fmn = $ar->{form_name} | ||
| 156 | if exists $ar->{form_name} && $ar->{form_name}; | ||
| 157 | print "<center>\n"; | ||
| 158 | my %fr = (-name => $fmn, -method=>uc $ar->{method}, | ||
| 159 | -action=>"$ar->{action}?", -enctype=>$ar->{encoding} ); | ||
| 160 | if (exists $ar->{hr_form} && $ar->{hr_form}) { | ||
| 161 | my $fr_hr = (ref($ar->{hr_form}) =~ /^HASH/) ? | ||
| 162 | $ar->{hr_form} : eval $ar->{hr_form}; | ||
| 163 | foreach my $k (keys %{$fr_hr}) { $fr{$k} = $fr_hr->{$k}; } | ||
| 164 | } | ||
| 165 | print start_form(%fr); | ||
| 166 | my $hvs = $s->set_param('vars_keep', $ar); | ||
| 167 | if ($hvs) { | ||
| 168 | foreach my $k (split /,/, $hvs) { | ||
| 169 | my $v = $s->set_param($k, $ar); | ||
| 170 | next if $v =~ /^\s*$/; | ||
| 171 | print hidden($k,$v); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | print "$gf\n"; | ||
| 175 | print end_form; | ||
| 176 | print "</center>\n"; | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | =head2 explode_html ($gi, $pr) | ||
| 181 | |||
| 182 | Input variables: | ||
| 183 | |||
| 184 | $gi - a hash ref | ||
| 185 | $pr - a parameter hash ref | ||
| 186 | |||
| 187 | Variables used or routines called: | ||
| 188 | |||
| 189 | CGI::AppBuilder::Message | ||
| 190 | echo_msg - echo messages | ||
| 191 | |||
| 192 | How to use: | ||
| 193 | |||
| 194 | my $ifn = 'myConfig.ini'; | ||
| 195 | my ($q,$ar) = $s->get_inputs($ifn); | ||
| 196 | $self->explote_html($ar->{gi}, $ar); | ||
| 197 | |||
| 198 | Return: none | ||
| 199 | |||
| 200 | This method enables a 'x' command in your GUI definition and | ||
| 201 | processes the complex elements in the hash ref. For | ||
| 202 | instance, you have a GUI hash: | ||
| 203 | |||
| 204 | gi = { # GUI Items | ||
| 205 | rts => ['rpt_src',['DataFax Generic','Study Specific'], | ||
| 206 | 'DataFax Generic'], | ||
| 207 | xtd_rts => 'radio_group', | ||
| 208 | act1 => td('Action'), | ||
| 209 | act2 => td({-colspan=>'2',-align=>'center'}, | ||
| 210 | gk->{opf} . submit('a','Go') . ' ' . submit('a','Update') . | ||
| 211 | ' ' . reset() ), | ||
| 212 | xcp_act => 'act1,act2', | ||
| 213 | act => submit('a','Go') . ' ' . reset(), | ||
| 214 | } | ||
| 215 | |||
| 216 | and the method will copy (xcp_act) the results (HTML text) | ||
| 217 | of $gi->{act1} and and $gi->{act2} into one. The $gi->{xcp_act} | ||
| 218 | will contains the combined string. | ||
| 219 | |||
| 220 | The 'xtd_' instructs the method to use $gi->{rts} as arguments for | ||
| 221 | the method name in $gi->{xtd_rts}. | ||
| 222 | |||
| 223 | =cut | ||
| 224 | |||
| 225 | sub explode_html { | ||
| 226 | my $s = shift; | ||
| 227 | my ($gi, $pr) = @_; | ||
| 228 | |||
| 229 | my $ar = (exists $pr->{ar}) ? $pr->{ar} : {}; | ||
| 230 | my $gk = (exists $pr->{gk}) ? $pr->{gk} : {}; | ||
| 231 | my $gc = (exists $pr->{gc}) ? $pr->{gc} : {}; | ||
| 232 | my $db = (exists $pr->{db}) ? $pr->{db} : {}; | ||
| 233 | foreach my $k (keys %$gi) { | ||
| 234 | next if ($k !~ /^x(td|cp)_(.+)/i); | ||
| 235 | my ($k1, $k2) = ($1, $2); | ||
| 236 | if ($k1 =~ /^td/i) { | ||
| 237 | my $tmp_ar = []; | ||
| 238 | if (ref($gi->{$k2}) =~ /ARRAY/) { | ||
| 239 | $tmp_ar = $gi->{$k2}; | ||
| 240 | } else { | ||
| 241 | $tmp_ar = eval $gi->{$k2}; | ||
| 242 | } | ||
| 243 | if ($gi->{$k} =~ /^radio_group/i) { | ||
| 244 | $gi->{$k} = radio_group(@$tmp_ar); | ||
| 245 | } elsif ($gi->{$k} =~ /^popup_menu/i) { | ||
| 246 | $gi->{$k} = popup_menu(@$tmp_ar); | ||
| 247 | } else { | ||
| 248 | $gi->{$k} = td(@$tmp_ar); | ||
| 249 | } | ||
| 250 | } else { | ||
| 251 | my $txt = ""; | ||
| 252 | foreach my $i (split /,/, $gi->{$k}) { | ||
| 253 | $txt .= $gi->{$i}; | ||
| 254 | } | ||
| 255 | $gi->{$k} = $txt; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | $s->echo_msg($gi,5); | ||
| 259 | return; | ||
| 260 | } | ||
| 261 | |||
| 262 | =head2 explode_variable ($xr, $i, $pr) | ||
| 263 | |||
| 264 | Input variables: | ||
| 265 | |||
| 266 | $xr - a hash ref such as the elements of gi,gk,gc,gf in | ||
| 267 | GUI hash array | ||
| 268 | $i - one of gi, gk, gc and gf | ||
| 269 | $pr - a parameter hash ref containing the values for $i to be | ||
| 270 | used in $xr | ||
| 271 | |||
| 272 | Variables used or routines called: | ||
| 273 | |||
| 274 | CGI::AppBuilder::Message | ||
| 275 | echo_msg - echo messages | ||
| 276 | |||
| 277 | How to use: | ||
| 278 | |||
| 279 | my $ifn = 'myConfig.ini'; | ||
| 280 | my ($q,$ar) = $s->get_inputs($ifn); | ||
| 281 | my $gi = $self->explode_variable($ar, 'gi', $ar); | ||
| 282 | my $gc = $self->explode_variable($ar, 'gc', $ar); | ||
| 283 | |||
| 284 | Return: hash or hash ref for $i. | ||
| 285 | |||
| 286 | This method replaces variable names with their values and HTML | ||
| 287 | commands with their results. | ||
| 288 | |||
| 289 | =cut | ||
| 290 | |||
| 291 | sub explode_variable { | ||
| 292 | my $s = shift; | ||
| 293 | my ($xr, $i, $pr) = @_; | ||
| 294 | my $hr = {}; | ||
| 295 | return wantarray ? %$hr : $hr if ! exists $xr->{$i}; | ||
| 296 | |||
| 297 | my $ar = (exists $pr->{ar}) ? $pr->{ar} : {}; | ||
| 298 | my $gi = (exists $pr->{gi}) ? $pr->{gi} : {}; | ||
| 299 | my $gk = (exists $pr->{gk}) ? $pr->{gk} : {}; | ||
| 300 | my $gc = (exists $pr->{gc}) ? $pr->{gc} : {}; | ||
| 301 | my $db = (exists $pr->{db}) ? $pr->{db} : {}; | ||
| 302 | if (ref($xr->{$i}) =~ /HASH/) { | ||
| 303 | foreach my $k (keys %{$xr->{$i}}) { | ||
| 304 | if (ref($xr->{$i}{$k}) =~ /^ARRAY/) { | ||
| 305 | for my $j (0..$#{$xr->{$i}{$k}}) { | ||
| 306 | $hr->{$k}[$j] = | ||
| 307 | (ref($xr->{$i}{$k}[$j]) =~ /^(ARRAY|HASH)/) ? | ||
| 308 | $xr->{$i}{$k}[$j] : eval $xr->{$i}{$k}[$j]; | ||
| 309 | } | ||
| 310 | } elsif (ref($xr->{$i}{$k}) =~ /^HASH/) { | ||
| 311 | foreach my $j (keys %{$xr->{$i}{$k}}) { | ||
| 312 | $hr->{$k}{$j} = | ||
| 313 | (ref($xr->{$i}{$k}{$j}) =~ /^(ARRAY|HASH)/) ? | ||
| 314 | $xr->{$i}{$k}{$j} : eval $xr->{$i}{$k}{$j}; | ||
| 315 | } | ||
| 316 | } else { | ||
| 317 | $hr->{$k} = ($k =~ /^x(td|cp)/i) ? $xr->{$i}{$k} : | ||
| 318 | eval $xr->{$i}{$k}; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | } else { | ||
| 322 | $hr = eval $xr->{$i}; | ||
| 323 | } | ||
| 324 | $s->echo_msg($hr,5); | ||
| 325 | return wantarray ? %$hr : $hr; | ||
| 326 | } | ||
| 327 | |||
| 328 | =head2 replace_named_variables ($ar, $vs, $ks) | ||
| 329 | |||
| 330 | Input variables: | ||
| 331 | |||
| 332 | $ar - a hash ref containing the elements of gi,gk,gc,gf in | ||
| 333 | GUI hash array | ||
| 334 | $vs - a list of variable names separated by comma such as | ||
| 335 | 'ar,db,gi,gk,gc' | ||
| 336 | $ks - a list of key elements separated by comma such as | ||
| 337 | 'gk,gi,gc,gf' | ||
| 338 | |||
| 339 | Variables used or routines called: | ||
| 340 | |||
| 341 | None | ||
| 342 | |||
| 343 | How to use: | ||
| 344 | |||
| 345 | my $ifn = 'myConfig.ini'; | ||
| 346 | my ($q,$ar) = $s->get_inputs($ifn); | ||
| 347 | $self->replace_named_variables($ar, 'ar,db,gk,gi,gc','gk,gi,gc,gf'); | ||
| 348 | |||
| 349 | Return: None. | ||
| 350 | |||
| 351 | This method replaces named variables with their values in $ar. | ||
| 352 | |||
| 353 | =cut | ||
| 354 | |||
| 355 | sub replace_named_variables { | ||
| 356 | my $s = shift; | ||
| 357 | my ($ar, $vs, $ks) = @_; | ||
| 358 | |||
| 359 | return if !$vs || !$ks; | ||
| 360 | $vs =~ s/\s+//g; $ks =~ s/\s+//g; # remove any blanks | ||
| 361 | |||
| 362 | foreach my $v (split /,/, $vs) { # variables: ar,db,gk,gi,gc | ||
| 363 | foreach my $k (split /,/, $ks) { # keys: gk,gi,gc,gf | ||
| 364 | next if ! exists $ar->{$k}; | ||
| 365 | $ar->{$k} =~ s/$v\->/\$$v->/g if ref($ar->{$k}) !~ /^HASH/; | ||
| 366 | next if ref($ar->{$k}) !~ /^HASH/; | ||
| 367 | foreach my $i (keys %{$ar->{$k}}) { | ||
| 368 | if (ref($ar->{$k}{$i}) !~ /^(ARRAY|HASH)/) { | ||
| 369 | $ar->{$k}{$i} =~ s/$v\->/\$$v->/g; | ||
| 370 | next; | ||
| 371 | } | ||
| 372 | if (ref($ar->{$k}{$i}) =~ /^ARRAY/) { | ||
| 373 | for my $j (0..$#{$ar->{$k}{$i}}) { | ||
| 374 | $ar->{$k}{$i}[$j] =~ s/$v\->/\$$v->/g; | ||
| 375 | } | ||
| 376 | } else { | ||
| 377 | for my $j (keys %{$ar->{$k}{$i}}) { | ||
| 378 | $ar->{$k}{$i}{$j} =~ s/$v\->/\$$v->/g; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | } | ||
| 382 | } | ||
| 383 | } | ||
| 384 | return; | ||
| 385 | } | ||
| 386 | |||
| 387 | 1; | ||
| 388 | |||
| 389 | =head1 HISTORY | ||
| 390 | |||
| 391 | =over 4 | ||
| 392 | |||
| 393 | =item * Version 0.10 | ||
| 394 | |||
| 395 | This version extracts the disp_form method from CGI::Getopt class, | ||
| 396 | inherits the new constructor from CGI::AppBuilder, and adds | ||
| 397 | new methods of replace_named_variables, explode_variable, and | ||
| 398 | explode_html. | ||
| 399 | |||
| 400 | =item * Version 0.20 | ||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | =head1 SEE ALSO (some of docs that I check often) | ||
| 405 | |||
| 406 | Oracle::Loader, Oracle::Trigger, CGI::Getopt, File::Xcopy, | ||
| 407 | CGI::AppBuilder, CGI::AppBuilder::Message, CGI::AppBuilder::Log, | ||
| 408 | CGI::AppBuilder::Config, etc. | ||
| 409 | |||
| 410 | =head1 AUTHOR | ||
| 411 | |||
| 412 | Copyright (c) 2005 Hanming Tu. All rights reserved. | ||
| 413 | |||
| 414 | This package is free software and is provided "as is" without express | ||
| 415 | or implied warranty. It may be used, redistributed and/or modified | ||
| 416 | under the terms of the Perl Artistic License (see | ||
| 417 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 418 | |||
| 419 | =cut | ||
| 420 |