| 1 | #!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # | ||
| 4 | # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org> | ||
| 5 | # Copyright (c) 2000-2002 Mitel Networks Corporation | ||
| 6 | # This software is distributed under the same licenses as Perl itself; | ||
| 7 | # see the file COPYING for details. | ||
| 8 | # | ||
| 9 | # NOTE TO DEVELOPERS: Use "XXX" to mark bugs or areas that need work | ||
| 10 | # use something like this to find how many things need work: | ||
| 11 | # find . | grep -v CVS | xargs grep XXX | wc -l | ||
| 12 | # | ||
| 13 | |||
| 14 | # | ||
| 15 | # $Id: HTML.pm,v 1.66 2003/02/05 17:18:35 anoncvs_gtkglext Exp $ | ||
| 16 | # | ||
| 17 | |||
| 18 | package CGI::FormMagick; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use Carp; | ||
| 22 | use Text::Iconv; | ||
| 23 | use CGI::FormMagick::L10N; | ||
| 24 | |||
| 25 | =pod | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | CGI::FormMagick::HTML - HTML output routines for FormMagick | ||
| 30 | |||
| 31 | =begin testing | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use_ok('CGI::FormMagick'); | ||
| 35 | use CGI; | ||
| 36 | use vars qw($fm); | ||
| 37 | |||
| 38 | $fm = new CGI::FormMagick(type => 'file', source => "t/simple.xml"); | ||
| 39 | $fm->{cgi} = new CGI(""); | ||
| 40 | isa_ok($fm, 'CGI::FormMagick'); | ||
| 41 | |||
| 42 | our $minimalist_fieldinfo_ref = { | ||
| 43 | validation => 'foo', | ||
| 44 | label => 'bar', | ||
| 45 | type => 'text', | ||
| 46 | id => 'baz' | ||
| 47 | }; | ||
| 48 | |||
| 49 | =end testing | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | These are internal-use-only routines for displaying HTML output, | ||
| 54 | probably only of interest to developers of FormMagick. | ||
| 55 | =head1 DEVELOPER ROUTINES | ||
| 56 | |||
| 57 | =head2 $self->print_page() | ||
| 58 | |||
| 59 | Prints out a page of the form, including the page header and footer, the | ||
| 60 | fields, and the buttons. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub print_page { | ||
| 65 | my ($self) = @_; | ||
| 66 | $self->print_page_header(); | ||
| 67 | $self->display_fields(); | ||
| 68 | $self->print_buttons(); | ||
| 69 | $self->print_page_footer(); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | =head2 print_buttons($fm) | ||
| 75 | |||
| 76 | print the table row containing the form's buttons | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub print_buttons { | ||
| 81 | my $fm = shift; | ||
| 82 | my $html_printed="0"; | ||
| 83 | print "\n"; | ||
| 84 | # print qq( <tr>\n <td>a</td>\n <td>); | ||
| 85 | my $label = $fm->localise("Previous"); | ||
| 86 | # print qq( <input type="submit" name="Previous" value="$label">\n) | ||
| 87 | # unless $fm->{page_number} == FIRST_PAGENUM() | ||
| 88 | # or $fm->{previousbutton} == 0; | ||
| 89 | |||
| 90 | unless ( $fm->{page_number} == FIRST_PAGENUM() or $fm->{previousbutton} == 0) { | ||
| 91 | print qq( <tr>\n <td></td>\n <td>); | ||
| 92 | $html_printed=1; | ||
| 93 | print qq( <input type="submit" name="Previous" value="$label">\n); | ||
| 94 | } | ||
| 95 | |||
| 96 | # check whether it's the last page yet | ||
| 97 | if ($fm->is_last_page()) { | ||
| 98 | if ($fm->{finishbutton}) { | ||
| 99 | print qq( <tr>\n <td></td>\n <td>) unless $html_printed; | ||
| 100 | $label = $fm->localise("Finish"); | ||
| 101 | print qq( <input type="submit" name="Finish" value="$label">\n); | ||
| 102 | } | ||
| 103 | } else { | ||
| 104 | if ($fm->{nextbutton}) { | ||
| 105 | print qq( <tr>\n <td></td>\n <td>) unless $html_printed; | ||
| 106 | $label = $fm->localise("Next"); | ||
| 107 | print qq(<input type="submit" name="Next" value="$label">\n); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | $label = $fm->localise("Clear this form"); | ||
| 111 | print qq(<input type="reset" value="$label">) | ||
| 112 | if $fm->{resetbutton}; | ||
| 113 | print qq(</td>\n </tr>\n) if $html_printed; | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | =pod | ||
| 118 | |||
| 119 | =head2 print_form_header($fm) | ||
| 120 | |||
| 121 | prints the header template and the form title (heading level 1) | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | sub print_form_header { | ||
| 126 | my $fm = shift; | ||
| 127 | my $title = $fm->form->{title}; | ||
| 128 | |||
| 129 | # print out the templated headers (based on what's specified in the | ||
| 130 | # HTML) then an h1 containing the form element's title attribute | ||
| 131 | |||
| 132 | print $fm->parse_template($fm->form->{header}); | ||
| 133 | print "<h1>", $fm->localise($title), "</h1>\n"; | ||
| 134 | } | ||
| 135 | |||
| 136 | =pod | ||
| 137 | |||
| 138 | =head2 print_form_footer($fm) | ||
| 139 | |||
| 140 | prints the stuff that goes at the bottom of every page of the form | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub print_form_footer { | ||
| 145 | my $fm = shift; | ||
| 146 | |||
| 147 | my $url = $fm->{cgi}->url(-relative => 1); | ||
| 148 | |||
| 149 | # here's how we clear our state IDs | ||
| 150 | print qq(<p><a href="$url">),$fm->localise('Start over again'), | ||
| 151 | qq(</a></p>) | ||
| 152 | if $fm->{startoverlink}; | ||
| 153 | |||
| 154 | # this is for debugging purposes | ||
| 155 | $fm->debug_msg(qq(<a href="$url?checkl10n=1">Check L10N</a>)); | ||
| 156 | |||
| 157 | # print the footer template | ||
| 158 | print $fm->parse_template($fm->form->{footer}); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | =pod | ||
| 163 | |||
| 164 | =head2 print_page_header($fm) | ||
| 165 | |||
| 166 | prints the page title (heading level 2) and description | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub print_page_header { | ||
| 171 | |||
| 172 | my $fm = shift; | ||
| 173 | my $title = $fm->page->{title}; | ||
| 174 | my $description = $fm->page->{description}; | ||
| 175 | my $url = $fm->{cgi}->url(-relative => 1); | ||
| 176 | my $enctype = $fm->get_page_enctype(); | ||
| 177 | |||
| 178 | # the level 2 heading is the page element's title heading | ||
| 179 | print "<h2>", $fm->localise($title), "</h2>\n" if $title; | ||
| 180 | print qq(<form method="POST" action="$url" enctype="$enctype">\n); | ||
| 181 | print qq( <input type="hidden" name="page" value="$fm->{page_number}">\n); | ||
| 182 | print qq( <input type="hidden" name="page_stack" value="$fm->{page_stack}">\n); | ||
| 183 | print " ",$fm->{cgi}->state_field(), "\n"; # hidden field with state ID | ||
| 184 | print " <table class=\"sme-noborders\">\n"; | ||
| 185 | #print " <col width=\"250\"><col width=\"*\">\n"; | ||
| 186 | if ($description) { | ||
| 187 | #print '<p class="pagedescription">', $fm->localise($description), "</p>\n"; | ||
| 188 | print " <tr><td colspan=2><p>", $fm->localise($description), "</p></td>\n </tr>\n"; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | =pod | ||
| 193 | |||
| 194 | =head2 print_page_footer($fm) | ||
| 195 | |||
| 196 | prints the stuff that goes at the bottom of a page, mostly just the | ||
| 197 | form and table close tags and stuff. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | sub print_page_footer { | ||
| 202 | my $fm = shift; | ||
| 203 | |||
| 204 | print " </table>\n</form>\n"; | ||
| 205 | } | ||
| 206 | |||
| 207 | =pod | ||
| 208 | |||
| 209 | =head2 print_field_description($description) | ||
| 210 | |||
| 211 | prints the description of a field | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub print_field_description { | ||
| 216 | my $fm = shift; | ||
| 217 | my $d = shift; | ||
| 218 | $d = $fm->localise($d); | ||
| 219 | print qq( <tr>\n <td colspan=2><p>$d</p></td>\n </tr>); | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | =pod | ||
| 224 | |||
| 225 | =head2 print_field_error($error) | ||
| 226 | |||
| 227 | prints any errors related to a field | ||
| 228 | |||
| 229 | =begin testing | ||
| 230 | |||
| 231 | my @errors = qw(FOO BAR BAZ); | ||
| 232 | |||
| 233 | $ENV{HTTP_ACCEPT_LANGUAGE} = "fr"; | ||
| 234 | $fm->parse_xml(); | ||
| 235 | is(ref $fm->{lexicon}, "HASH", "Lexicon is loaded"); | ||
| 236 | |||
| 237 | $fm->print_field_error(\@errors); | ||
| 238 | like($_STDOUT_, qr/le foo/, "Output of print_field_error should be localised"); | ||
| 239 | like($_STDOUT_, qr/le bar/, "Output of print_field_error list multiple fields"); | ||
| 240 | ok($_STDOUT_ !~ /le foo\./, "Output of print_field_error shouldn't have a dot on the end"); | ||
| 241 | |||
| 242 | =end testing | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub print_field_error { | ||
| 247 | my ($fm, $e) = @_; | ||
| 248 | my $errstr = join "<br>", map($fm->localise($_), @$e); | ||
| 249 | print " "; print qq(<tr><td colspan=2><div class="sme-error">$errstr</div></td></tr>); print "\n"; | ||
| 250 | } | ||
| 251 | |||
| 252 | =pod | ||
| 253 | |||
| 254 | =head2 display_fields($fm) | ||
| 255 | |||
| 256 | displays the fields for a page by looping through them | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub display_fields { | ||
| 261 | my ($fm) = @_; | ||
| 262 | |||
| 263 | my @definitions; | ||
| 264 | |||
| 265 | foreach my $field ( @{$fm->page->{fields}} ) { | ||
| 266 | my $info = $fm->gather_field_info($field); | ||
| 267 | if ($info->{type} eq "html") { | ||
| 268 | print qq(\n <tr><td cols="2">$info->{content}</td></tr>\n) if $info->{content}; | ||
| 269 | } elsif ($info->{type} eq "subroutine") { | ||
| 270 | my $output = $fm->do_external_routine($info->{src}) || ""; | ||
| 271 | print qq(\n <tr><td cols="2">$output</td></tr>\n) if $output; | ||
| 272 | } else { | ||
| 273 | $fm->print_field_description($info->{description}) if $info->{description}; | ||
| 274 | |||
| 275 | if (($info->{type} eq "select") || ($info->{type} eq "radio")) { | ||
| 276 | $fm->set_option_lv($info); | ||
| 277 | } | ||
| 278 | |||
| 279 | print qq( <tr>\n <td class="sme-noborders-label">) . $fm->localise($info->{label}). "\n" ; | ||
| 280 | my $inputfield = $fm->build_inputfield($info); | ||
| 281 | # this is to make the printed HTML look nice | ||
| 282 | $inputfield =~ s#^\s*\n##g; | ||
| 283 | print qq( <td class="sme-noborders-content">$inputfield</td>\n </tr>\n); | ||
| 284 | |||
| 285 | # display errors (if any) below the field label, taking up a whole row | ||
| 286 | my $error = $fm->{errors}->{$info->{label}}; | ||
| 287 | $fm->print_field_error($error) if $error; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | =pod | ||
| 293 | |||
| 294 | =head2 gather_field_info($field) | ||
| 295 | |||
| 296 | Gathers various information about a field and returns it as a hashref. | ||
| 297 | |||
| 298 | =begin testing | ||
| 299 | |||
| 300 | sub plain_sub { | ||
| 301 | return 'Vanilla'; | ||
| 302 | } | ||
| 303 | |||
| 304 | sub add_1 { | ||
| 305 | my (undef, $a) = @_; | ||
| 306 | return $a + 1; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub add_together { | ||
| 310 | my (undef, @a) = @_; | ||
| 311 | my $sum = 0; | ||
| 312 | $sum += $_ foreach @a; | ||
| 313 | return $sum; | ||
| 314 | } | ||
| 315 | |||
| 316 | { | ||
| 317 | foreach my $expectations ( | ||
| 318 | [ '', '' ], | ||
| 319 | [ 'plain', 'plain' ], | ||
| 320 | [ 'plain_sub()', 'Vanilla' ], | ||
| 321 | [ 'add_1(0)', '1' ], | ||
| 322 | [ 'add_1(1)', '2' ], | ||
| 323 | [ 'add_together(2, 3)', '5' ], | ||
| 324 | [ 'add_together(2, 3, 4)', '9' ], | ||
| 325 | ) { | ||
| 326 | my ($input, $expected) = @$expectations; | ||
| 327 | |||
| 328 | my %f = %$minimalist_fieldinfo_ref; | ||
| 329 | $f{value} = $input; | ||
| 330 | |||
| 331 | my $i = $fm->gather_field_info(\%f); | ||
| 332 | my $actual = $i->{value}; | ||
| 333 | is( | ||
| 334 | $actual, | ||
| 335 | $expected, | ||
| 336 | "gather_field_info('$input')" | ||
| 337 | ); | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | =end testing | ||
| 342 | |||
| 343 | =cut | ||
| 344 | |||
| 345 | sub gather_field_info { | ||
| 346 | my ($fm, $fieldinfo) = @_; | ||
| 347 | |||
| 348 | my %f = %$fieldinfo; | ||
| 349 | |||
| 350 | # value is set to what the user filled in, if they filled | ||
| 351 | # something in on a previous visit to this field | ||
| 352 | if ($fm->{cgi}->param($f{id})) { | ||
| 353 | $f{value} = $fm->{cgi}->param($f{id}); | ||
| 354 | |||
| 355 | # are we calling a subroutine to find the value? | ||
| 356 | } elsif ($fieldinfo->{value} and $fieldinfo->{value} =~ /\(.*\)/) { | ||
| 357 | $f{value} = $fm->do_external_routine($fieldinfo->{value}); | ||
| 358 | |||
| 359 | # otherwise, use value attribute or default to blank. | ||
| 360 | } else { | ||
| 361 | my $default = ($fieldinfo->{type} eq 'checkbox' ? 1 : ""); | ||
| 362 | if (defined $fieldinfo->{value}) { | ||
| 363 | my $converter = Text::Iconv->new("UTF-8", $fm->{charset} || "ISO-8859-1"); | ||
| 364 | $f{value} = $converter->convert($fieldinfo->{value}); | ||
| 365 | } else { | ||
| 366 | $f{value} = $default; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | |||
| 371 | if ($f{id}) { | ||
| 372 | $fm->debug_msg("Field name is $f{id}"); | ||
| 373 | } else { | ||
| 374 | $fm->debug_msg("Not a field, it's a $f{type}"); | ||
| 375 | } | ||
| 376 | |||
| 377 | return \%f; | ||
| 378 | } | ||
| 379 | |||
| 380 | =pod | ||
| 381 | |||
| 382 | =head2 build_inputfield ($fm, $forminfo) | ||
| 383 | |||
| 384 | Builds HTML for individual form fields. $forminfo is a hashref | ||
| 385 | containing information about the field. | ||
| 386 | |||
| 387 | =for testing | ||
| 388 | my $i = $fm->gather_field_info($minimalist_fieldinfo_ref); | ||
| 389 | ok(my $if = $fm->build_inputfield($i, CGI::FormMagick::TagMaker->new()), "build input field"); | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | sub build_inputfield { | ||
| 394 | my ($fm, $info) = @_; | ||
| 395 | |||
| 396 | my $inputfield; # HTML for a form input | ||
| 397 | my $tagmaker = new CGI::FormMagick::TagMaker->new(); | ||
| 398 | |||
| 399 | my $converter = Text::Iconv->new($fm->{charset} || "ISO-8859-1", "UTF-8"); | ||
| 400 | my $id = $converter->convert($info->{id}); | ||
| 401 | my $type = $converter->convert($info->{type}); | ||
| 402 | my $multiple = $converter->convert($info->{multiple}); | ||
| 403 | my $size = $converter->convert($info->{size}); | ||
| 404 | my $rows = $converter->convert($info->{rows}) if ($info->{rows}); | ||
| 405 | my $cols = $converter->convert($info->{cols}) if ($info->{cols}); | ||
| 406 | |||
| 407 | |||
| 408 | # value is already converted in gather_field_info IFF it came from the | ||
| 409 | # XML default (not user input or a subroutine, both of which | ||
| 410 | # should just have plain ol' Latin-1 | ||
| 411 | my $value = $info->{value}; | ||
| 412 | |||
| 413 | if ($info->{type} eq "select") { | ||
| 414 | |||
| 415 | my @labels = map { $fm->localise($_) } @{$info->{option_labels}}; | ||
| 416 | |||
| 417 | # don't specify a size if a size wasn't given in the XML | ||
| 418 | if ($info->{size} && $info->{size} ne "") { | ||
| 419 | $inputfield = $tagmaker->select_start( | ||
| 420 | type => $type, | ||
| 421 | name => $id, | ||
| 422 | multiple => $multiple, | ||
| 423 | size => $size, | ||
| 424 | ) | ||
| 425 | } else { | ||
| 426 | $inputfield = $tagmaker->select_start( | ||
| 427 | type => $type, | ||
| 428 | name => $id, | ||
| 429 | multiple => $multiple, | ||
| 430 | ) | ||
| 431 | } | ||
| 432 | |||
| 433 | $inputfield = $inputfield . $tagmaker->option_group( | ||
| 434 | value => $info->{option_values}, | ||
| 435 | text => \@labels, | ||
| 436 | ) . $tagmaker->select_end; | ||
| 437 | |||
| 438 | # nasty hack required here to select the desired value if it's preset | ||
| 439 | $inputfield =~ s/(<OPTION VALUE="$value")/$1 SELECTED/; | ||
| 440 | |||
| 441 | } elsif ($info->{type} eq "radio") { | ||
| 442 | my @labels = map { $fm->localise($_) } @{$info->{option_labels}}; | ||
| 443 | $inputfield = $tagmaker->input_group( | ||
| 444 | type => $type, | ||
| 445 | name => $id, | ||
| 446 | value => $info->{option_values} , | ||
| 447 | text => \@labels | ||
| 448 | ); | ||
| 449 | # nasty hack required here to select the desired value if it's preset | ||
| 450 | $inputfield =~ s/(VALUE="$info->{value}")/$1 CHECKED/; | ||
| 451 | |||
| 452 | } elsif ($info->{type} eq "checkbox") { | ||
| 453 | |||
| 454 | # figure out whether hte box should be checked or not | ||
| 455 | my $user_input = $fm->{cgi}->param($id); | ||
| 456 | my $c; | ||
| 457 | if (defined $user_input) { | ||
| 458 | $c = $user_input ? 1 : 0; | ||
| 459 | } else { | ||
| 460 | $c = $info->{checked}; # get from XML spec | ||
| 461 | } | ||
| 462 | |||
| 463 | $inputfield = $tagmaker->input( | ||
| 464 | type => $type, | ||
| 465 | name => $id, | ||
| 466 | value => $value, | ||
| 467 | checked => $c, | ||
| 468 | ); | ||
| 469 | } elsif ($info->{type} eq "literal") { | ||
| 470 | $inputfield = $value; | ||
| 471 | } elsif ($info->{type} eq "textarea") { | ||
| 472 | $inputfield = $tagmaker->textarea( | ||
| 473 | name => $id, | ||
| 474 | rows => $rows || 5, | ||
| 475 | cols => $cols || 60, | ||
| 476 | ); | ||
| 477 | } else { | ||
| 478 | # map HTML::TagMaker's functions to the type of this field. | ||
| 479 | my %translation_table = ( | ||
| 480 | # textarea => 'textarea', | ||
| 481 | checkbox => 'input_field', | ||
| 482 | text => 'input_field', | ||
| 483 | password => 'input_field', | ||
| 484 | file => 'input_field', | ||
| 485 | ); | ||
| 486 | my $function_name = $translation_table{$info->{type}}; | ||
| 487 | # make sure no size gets specified if the size isn't given in the XML | ||
| 488 | if ($info->{size} && $info->{size} ne "") { | ||
| 489 | $inputfield = $tagmaker->$function_name( | ||
| 490 | type => $type, | ||
| 491 | name => $id, | ||
| 492 | value => $value, | ||
| 493 | size => $size, | ||
| 494 | ); | ||
| 495 | } else { | ||
| 496 | $inputfield = $tagmaker->$function_name( | ||
| 497 | type => $type, | ||
| 498 | name => $id, | ||
| 499 | value => $value, | ||
| 500 | ); | ||
| 501 | } | ||
| 502 | } | ||
| 503 | return $inputfield; | ||
| 504 | } | ||
| 505 | |||
| 506 | =pod | ||
| 507 | |||
| 508 | =head2 set_option_lv($fm, $info) | ||
| 509 | |||
| 510 | Given $info (a hashref with info about a field) figures out the option | ||
| 511 | values/labels for select or radio fields and shoves them into | ||
| 512 | $info->{option_values} and $info->{option_labels} | ||
| 513 | |||
| 514 | =cut | ||
| 515 | |||
| 516 | sub set_option_lv { | ||
| 517 | my ($fm, $info) = @_; | ||
| 518 | |||
| 519 | # if this is a grouped input (one with options), we'll need to | ||
| 520 | # run the options function for it. | ||
| 521 | |||
| 522 | # DWIM whether the options are in a hash or an array. | ||
| 523 | my $lv_hashref = $fm->get_option_labels_and_values($info); | ||
| 524 | |||
| 525 | $info->{option_labels} = $lv_hashref->{labels}; | ||
| 526 | $info->{option_values} = $lv_hashref->{vals}; | ||
| 527 | |||
| 528 | } | ||
| 529 | |||
| 530 | return 1; |