File Coverage

blib/lib/CGI/Application/Plugin/FillInForm.pm
Criterion Covered Total %
statement 42 44 95.4
branch 17 22 77.2
condition 2 3 66.6
subroutine 5 5 100.0
pod 1 1 100.0
total 67 75 89.3


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