File Coverage

blib/lib/CGI/Application/Plugin/FillInForm.pm
Criterion Covered Total %
statement 37 39 94.8
branch 15 20 75.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 57 64 89.0


line stmt bran cond sub pod time code
1             package CGI::Application::Plugin::FillInForm;
2 3     3   142097 use strict;
  3         7  
  3         176  
3             require Exporter;
4 3     3   21 use vars (qw/@ISA @EXPORT_OK $VERSION/);
  3         5  
  3         384  
5              
6             $VERSION = '1.12_01';
7             @ISA = qw(Exporter);
8             @EXPORT_OK = qw(fill_form);
9 3     3   20 use Carp;
  3         11  
  3         1687  
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     8 1 103553 my $self = shift;
63 8         18 my $html = shift;
64 8         17 my $data = shift;
65 8         19 my %extra_params = @_;
66              
67 8 50       41 die "html must be a scalarref!" unless (ref $html eq 'SCALAR');
68              
69 8         40 my %params = (
70             ignore_fields => [ $self->mode_param()],
71             );
72 8         139 my (@fdat, @fobject);
73              
74 8 100       40 if ($data) {
75              
76 6 100       31 $data = [$data] unless ref $data eq 'ARRAY';
77              
78 6         15 foreach my $source (@$data) {
79 16 100       48 if (ref $source eq 'HASH') {
    50          
    0          
80 8         17 push @fdat, $source;
81             }
82             elsif (ref $source) {
83 8 50       42 if ($source->can('param')) {
84 8         17 push @fobject, $source;
85             }
86             else {
87 0         0 croak "data source $source does not supply a param method";
88             }
89             }
90             elsif (defined $source) {
91 0         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 100       26 if (@fdat) {
101 4 100       14 if (@fdat > 1) {
102 2         5 my %merged;
103 2         6 foreach my $hash (@fdat) {
104 6         17 foreach my $key (keys %$hash) {
105 17         27 $merged{$key} = $hash->{$key};
106             }
107             }
108 2         7 $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         6 $params{'fdat'} = $fdat[0];
114             }
115             }
116              
117             # Multiple objects, however, are supported natively by
118             # HTML::FillInForm
119 6 100       23 $params{'fobject'} = \@fobject if @fobject;
120              
121             }
122             else {
123             # If no data sources are specified, then use
124             # $self->query
125 2         9 $params{'fobject'} = $self->query;
126             }
127              
128 8         791 require HTML::FillInForm;
129 8         4011 my $fif = new HTML::FillInForm;
130 8         8239 my $output = $fif->fill(scalarref => $html, %params, %extra_params);
131 8         4581 return \$output;
132             }
133              
134             =head1 AUTHORS
135              
136             Cees Hek published the first draft on the CGI::App wiki
137             Mark Stosberg, C<< >> polished it for release.
138              
139             =head1 BUGS
140              
141             Please report any bugs or feature requests to
142             C, or through the web interface at
143             L. 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             1; # End of CGI::Application::Plugin::FillInForm