| 1 | package CGI::Application::Plugin::FillInForm; | ||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 4 | require Exporter; | |||||||||||||||||
| 4 | 4 | ▶ | use vars (qw/@ISA @EXPORT_OK $VERSION/); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 4 | $VERSION = '1.12_01'; | |||||||||||||||||
| 7 | 4 | @ISA = qw(Exporter); | |||||||||||||||||
| 8 | 4 | @EXPORT_OK = qw(fill_form); | |||||||||||||||||
| 9 | 4 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | CGI::Application::Plugin::FillInForm - integrate with HTML::FillInForm | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | use CGI::Application::Plugin::FillInForm (qw/fill_form/); | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | my $t = $self->load_tmpl('farm.html'); | ||||||||||||||||||
| 20 | $t->param( organic => 'less_pesticides' ); | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | return $self->fill_form( \$t->output ); | ||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | This plugin provides a mix-in method to make using HTML::FillInForm more | ||||||||||||||||||
| 27 | convenient. | ||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | =head2 fill_form() | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | # fill an HTML form with data in a hashref or from an object with with a param() method | ||||||||||||||||||
| 32 | my $filled_html = $self->fill_form($html, $data); | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | # ...or fill from a list of data sources | ||||||||||||||||||
| 35 | # (each item in the list can be either a hashref or an object) | ||||||||||||||||||
| 36 | my $filled_html = $self->fill_form($html, [$user, $group, $query]); | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | # ...or default to getting data from $self->query() | ||||||||||||||||||
| 39 | my $filled_html = $self->fill_form($html); | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | # extra fields will be passed on through: | ||||||||||||||||||
| 42 | my $filled_html = $self->fill_form($html, undef, fill_passwords => 0 ); | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | This method provides an easier syntax for calling HTML::FillInForm, and an | ||||||||||||||||||
| 45 | intelligent default of using $self->query() as the default data source. | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | By default, the mode param (usually 'rm') of every data source will be | ||||||||||||||||||
| 48 | ignored. This prevents accidently clobbering your run mode for the next | ||||||||||||||||||
| 49 | page, which may be stored in a hidden field. | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | B<$html> must be a scalarref. | ||||||||||||||||||
| 52 | B<$filled_html> will be a reference to a string. | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | Because this method only loads HTML::FillInForm if it's needed, it should be | ||||||||||||||||||
| 55 | reasonable to load it in a base class and always have it available: | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | use CGI::Application::Plugin::FillInForm (qw/fill_form/); | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | =cut | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | sub fill_form { | ||||||||||||||||||
| 62 | 8 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
fill_form: called CC=14 SCAR=26.8
Pod
documented
| |||||||||||||||||||
| 63 | 8 | my $html = shift; | |||||||||||||||||
| 64 | 8 | my $data = shift; | |||||||||||||||||
| 65 | 8 | my %extra_params = @_; | |||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | 8 | ▶ | die "html must be a scalarref!" unless (ref $html eq 'SCALAR'); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | 8 | my %params = ( | |||||||||||||||||
| 70 | ignore_fields => [ $self->mode_param()], | ||||||||||||||||||
| 71 | ); | ||||||||||||||||||
| 72 | 8 | my (@fdat, @fobject); | |||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | 8 | ▶ | if ($data) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | 6 | ▶ | $data = [$data] unless ref $data eq 'ARRAY'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 6 | foreach my $source (@$data) { | |||||||||||||||||
| 79 | 16 | ▶ | if (ref $source eq 'HASH') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 80 | 8 | push @fdat, $source; | |||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | elsif (ref $source) { | ||||||||||||||||||
| 83 | 8 | ▶ | if ($source->can('param')) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | 8 | push @fobject, $source; | |||||||||||||||||
| 85 | } | ||||||||||||||||||
| 86 | else { | ||||||||||||||||||
| 87 | 0 | croak "data source $source does not supply a param method"; | |||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | elsif (defined $source) { | ||||||||||||||||||
| 91 | 0 | croak "data source $source is not a hash or object reference"; | |||||||||||||||||
| 92 | } | ||||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | # The docs to HTML::FillInForm suggest that you can pass an arrayref | ||||||||||||||||||
| 96 | # of %fdat hashes, but you can't. So if we receive more than one, | ||||||||||||||||||
| 97 | # we merge them. (This is no big deal, since this is what | ||||||||||||||||||
| 98 | # HTML::FillInForm would do anyway if it supported this feature.) | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | 6 | ▶ | if (@fdat) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | 4 | ▶ | if (@fdat > 1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 102 | 2 | my %merged; | |||||||||||||||||
| 103 | 2 | foreach my $hash (@fdat) { | |||||||||||||||||
| 104 | 6 | foreach my $key (keys %$hash) { | |||||||||||||||||
| 105 | 17 | $merged{$key} = $hash->{$key}; | |||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | } | ||||||||||||||||||
| 108 | 2 | $params{'fdat'} = \%merged; | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | else { | ||||||||||||||||||
| 111 | # If there's only one fdat hash anyway, then it's the | ||||||||||||||||||
| 112 | # first and only element in @fdat | ||||||||||||||||||
| 113 | 2 | $params{'fdat'} = $fdat[0]; | |||||||||||||||||
| 114 | } | ||||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | # Multiple objects, however, are supported natively by | ||||||||||||||||||
| 118 | # HTML::FillInForm | ||||||||||||||||||
| 119 | 6 | ▶ | $params{'fobject'} = \@fobject if @fobject; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | else { | ||||||||||||||||||
| 123 | # If no data sources are specified, then use | ||||||||||||||||||
| 124 | # $self->query | ||||||||||||||||||
| 125 | 2 | $params{'fobject'} = $self->query; | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 8 | require HTML::FillInForm; | |||||||||||||||||
| 129 | 8 | my $fif = new HTML::FillInForm; | |||||||||||||||||
| 130 | 8 | my $output = $fif->fill(scalarref => $html, %params, %extra_params); | |||||||||||||||||
| 131 | 8 | return \$output; | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | =head1 AUTHORS | ||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | Cees Hek published the first draft on the CGI::App wiki | ||||||||||||||||||
| 137 | Mark Stosberg, C<< <mark@summersault.com> >> polished it for release. | ||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | =head1 BUGS | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | Please report any bugs or feature requests to | ||||||||||||||||||
| 142 | C<bug-cgi-application-plugin-fillinform@rt.cpan.org>, or through the web interface at | ||||||||||||||||||
| 143 | L<http://rt.cpan.org>. I will be notified, and then you'll automatically | ||||||||||||||||||
| 144 | be notified of progress on your bug as I make changes. | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | =head1 CONTRIBUTING | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | Patches, questions and feedback are welcome. This project is managed using | ||||||||||||||||||
| 149 | the darcs source control system ( http://www.darcs.net/ ). My darcs archive is here: | ||||||||||||||||||
| 150 | http://mark.stosberg.com/darcs_hive/cap-fif/ | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | =head1 Copyright & License | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | Copyright 2005 Mark Stosberg, All Rights Reserved. | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||
| 158 | under the same terms as Perl itself. | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | =cut | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | 4 | 1; # End of CGI::Application::Plugin::FillInForm | |||||||||||||||||