lib/CGI/FormBuilder/Multi.pm

filter: Statementstmt 71.4% 60 / 84 Branchbran 41.6% 20 / 48 Conditioncond 30.0% 3 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.0% 9 / 10 Podpod 100% 5 / 5 totaltotal 59.5% 97 / 163 SCAR 46.0
CC
31
Cov
58%
navbar
156.0
new
17.9
form
6.1
CRAP 99.9
« lib/CGI/FormBuilder/Util.pm ↑ Summary lib/CGI/FormBuilder/Messages.pm »
1  
2 ###########################################################################
3 # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
4 # Please visit http://formbuilder.org for tutorials, support, and examples.
5 ###########################################################################
6  
7 package CGI::FormBuilder::Multi;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Multi - Create multi-page FormBuilder forms
12  
13 =head1 SYNOPSIS
14  
15 use CGI::FormBuilder::Multi;
16 use CGI::Session; # or something similar
17  
18 # Top-level "meta-form"
19 my $multi = CGI::FormBuilder::Multi->new(
20  
21 # form 1 options
22 { fields => [qw(name email daytime_phone evening_phone)],
23 title => 'Basic Info',
24 template => 'page1.tmpl',
25 validate => { name => 'NAME', email => 'EMAIL' },
26 required => [qw(name email daytime_phone)],
27 },
28  
29 # form 2 options
30 { fields => [qw(billing_name billing_card billing_exp
31 billing_address billing_city billing_state
32 billing_zip billing_phone)],
33 title => 'Billing',
34 template => 'page2.tmpl',
35 required => 'ALL',
36 },
37  
38 # form 3 options
39 { fields => [qw(same_as_billing shipping_address
40 shipping_city shipping_state shipping_zip)],
41 title => 'Shipping',
42 template => 'page3.tmpl',
43 required => 'ALL',
44 },
45  
46 # a couple options specific to this module
47 navbar => 1,
48  
49 # remaining options (not in hashrefs) apply to all forms
50 header => 1,
51 method => 'POST',
52 submit => 'Continue',
53 values => $dbi_hashref_query,
54 );
55  
56 # Get current page's form
57 my $form = $multi->form;
58  
59 if ($form->submitted && $form->validate) {
60  
61 # Retrieve session id
62 my $sid = $form->sessionid;
63  
64 # Initialize session
65 my $session = CGI::Session->new("driver:File", $sid, {Directory=>'/tmp'});
66  
67 # Automatically store updated data in session
68 $session->save_param($form);
69  
70 # last page?
71 if ($multi->page == $multi->pages) {
72 print $form->confirm;
73 exit;
74 }
75  
76 # Still here, goto next page
77 $multi->page++;
78  
79 # And re-get form (no "my" on $form!)
80 $form = $multi->form;
81  
82 # Make sure it has the right sessionid
83 $form->sessionid($session->id);
84  
85 # on page 3 we have special field handling
86 if ($multi->page == 3) {
87 $form->field(name => 'same_as_billing',
88 type => 'checkbox',
89 options => 'Yes',
90 jsclick => 'this.form.submit()');
91 }
92 }
93  
94 # Fall through and print next page's form
95 print $form->render;
96  
97 =cut
98  
99 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 1 ▶use CGI::FormBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 1 our $VERSION = '3.10';
107  
108 1 our %DEFAULT = (
109 pagename => '_page',
110 navbar => 0,
111 );
112  
113 sub new {
114 1 ▶ my $mod = shift;
Subroutine new: called CC=13 SCAR=28.9
Pod documented
115 1 ▶ my $class = ref($mod) || $mod;
Condition33%1/3
ref $mod || $mod
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $mod || $mod
ref $mod $mod
Truth table33%1/3
A || B
A= ref $modB= $mod
ABresult
1X1
011
000
116  
117 # Arg parsing is a little more complex than FormBuilder proper,
118 # since we keep going thru our options until we don't see hashrefs
119 1 my @forms = ();
120 1 ▶ while (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 3 1 4
121 3 push @forms, shift;
122 }
123  
124 # Remaining options are form opts
125 1 my %opt = arghash(@_);
126  
127 # If no forms, and specified number of pages, use that instead
128 1 ▶ if ($opt{pages}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"pages"}) 0 1 1
129 0 ▶ puke "Can't specify pages and form hashrefs" if @forms;
Branch: 1 branch
BranchTrueFalseTotal
if @forms 0 0 0
130 0 my $p = 0;
131 0 ▶ push @forms, {} while $p++ < $opt{pages};
Branch: 1 branch
BranchTrueFalseTotal
$p++ < $opt{"pages"} and do { push @forms, {} } 0 0 0
132 }
133 1 ▶ puke "Must specify at least one form or 'pages' option for ::Multi" unless @forms;
Branch: 1 branch
BranchTrueFalseTotal
unless @forms 0 1 1
134  
135 # Check for CGI params
136 # This is duplicated code straight out of FormBuilder.pm,
137 # but it's needed here as well so we can get our _page
138 1 ▶ unless ($opt{params} && ref $opt{params} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"params"} and ref $opt{"params"} ne "HASH") 1 0 1
Condition33%1/3
$opt{"params"} and ref $opt{"params"} ne "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
$opt{"params"} and ref $opt{"params"} ne "HASH"
$opt{"params"} ref $opt{"params"} ne "HASH"
Truth table33%1/3
A and B
A= $opt{"params"}B= ref $opt{"params"} ne "HASH"
ABresult
0X0
100
111
139 1 require CGI;
140 1 $CGI::USE_PARAM_SEMICOLONS = 0; # fuck ; in urls
141 1 $opt{params} = CGI->new($opt{params});
142 }
143  
144 # Options for me
145 1 my %me;
146 1 ▶ while (my($k,$v) = each %DEFAULT) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %CGI::FormBuilder::Multi::DEFAULT) 2 1 3
147 2 ▶ $me{$k} = exists $opt{$k} ? delete $opt{$k} : $v;
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{$k} ? : 1 1 2
148 }
149 1 $me{forms} = \@forms;
150  
151 # Plop in our defaults per-form unless it's an object
152 1 ▶ @forms = map { ref $_ eq 'HASH' ? { %opt, %$_ } : $_ } @forms;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 3 0 3
153  
154 # Top-level multi
155 1 my $self = bless \%me, $class;
156  
157 # Copy CGI object into self, and get page
158 1 $self->{params} = $opt{params};
159 1 $self->{keepextras} = $opt{keepextras};
160 1 ▶ $self->{page} = $self->{params}->param($self->{pagename}) || 1;
Condition50%1/2
$self->{'params'}->param($self->{'pagename'}) || 1
l!l
10
MC/DC0%0/1
$self->{'params'}->param($self->{'pagename'}) || 1
$self->{'params'}->param($self->{'pagename'})
Truth table50%1/2
A
A= $self->{'params'}->param($self->{'pagename'})
Aresult
11
00
161  
162 1 return $self;
163 }
164  
165 # return an lvalue to allow $multi->page++ and $multi->page--;
166 sub page : lvalue {
167 22 ▶ my $self = shift;
Subroutine page: called CC=2 SCAR=6.9
Pod documented
168 22 ▶ $self->{page} = shift if @_; # rvalue
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 20 22
169 22 $self->{page}; # lvalue
170 }
171  
172 1 *forms = \&pages;
173 sub pages {
174 4 ▶ my $self = shift;
Subroutine pages: called CC=2 SCAR=7.0
Pod documented
175 4 ▶ puke "No arguments allowed to \$multi->pages or \$multi->forms" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
176 4 return @{$self->{forms}};
177 }
178  
179 # return the form from this page, as a new object
180 sub form {
181 9 ▶ my $self = shift;
Subroutine form: called CC=6 SCAR=18.1
Pod documented
182 9 ▶ puke "No arguments allowed to \$multi->form" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9 9
183 9 my $page = $self->page;
184 9 my $idx = $page - 1;
185  
186 9 ▶ return $self->{_cache}{forms}[$idx] if $self->{_cache}{forms}[$idx];
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_cache"}{"forms"}[$idx] 5 4 9
187 puke "Invalid page $page, no form present"
188 4 ▶ unless my $form = $self->{forms}[$idx];
Branch: 1 branch
BranchTrueFalseTotal
unless my $form = $self->{"forms"}[$idx] 1 3 4
189  
190 3 ▶ if (ref $form eq 'CGI::FormBuilder') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $form eq 'CGI::FormBuilder') { } 0 3 3
191 # already constructed
192 } else {
193 3 $form = CGI::FormBuilder->new(%$form);
194 }
195  
196 # hooks
197 3 $form->page($self->page);
198 3 ▶ $form->text(scalar $self->navbar) if $self->{navbar}; # cheat
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"navbar"} 0 3 3
199  
200 # create new $form and cache for re-get
201 3 $self->{_cache}{forms}[$idx] = $form;
202 }
203  
204 # allow jumps between pages
205 sub navbar {
206 0 ▶ my $self = shift;
Subroutine navbar: not called CC=12 SCAR=50.5
Pod documented
207 0 ▶ $self->{navbar} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
208 0 my $base = basename;
209 0 my $pnam = $self->{pagename};
210 0 ▶ return '' unless $self->pages > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pages > 1 0 0 0
211  
212 # Look for extra params to keep
213 # Algorithm here is a bit different
214 0 my @keep;
215 0 ▶ if ($self->{keepextras}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"keepextras"}) 0 0 0
216 0 ▶ unless (ref $self->{keepextras}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"keepextras"}) 0 0 0
217 0 $self->{keepextras} = [ $self->{params}->param ];
218 }
219 0 for my $k (@{$self->{keepextras}}) {
220 0 ▶ next if $k eq $pnam;
Branch: 1 branch
BranchTrueFalseTotal
if $k eq $pnam 0 0 0
221 0 for my $v ($self->{params}->param($k)) {
222 0 push @keep, { name => $k, value => $v };
223 }
224 }
225 }
226  
227 0 my @html = ();
228 0 ▶ for (my $p=1; $p <= $self->pages; $p++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p <= $self->pages) 0 0 0
229 0 ▶ my $cl = $self->page == $p ? 'fb_multi_page' : 'fb_multi_link';
Branch: 1 branch
BranchTrueFalseTotal
$self->page == $p ? : 0 0 0
230
231 # this looks like gibberish
232 my $purl = basename . '?' . join '&',
233 0 map { "$_->{name}=$_->{value}" } @keep,
234 { name => $pnam, value => $p };
235
236 push @html, htmltag('a', href => $purl, class => $cl)
237 0 ▶ . ($self->{forms}[$p-1]{title} || "Page $p") . '</a>';
Condition0%0/2
$self->{'forms'}[$p - 1]{'title'} || "Page $p"
l!l
00
MC/DC0%0/1
$self->{'forms'}[$p - 1]{'title'} || "Page $p"
$self->{'forms'}[$p - 1]{'title'}
Truth table0%0/2
A
A= $self->{'forms'}[$p - 1]{'title'}
Aresult
11
00
238 }
239  
240 0 ▶ return wantarray ? @html : '<p>'. join(' | ', @html) . '<p>';
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
241 }
242  
243 1 1;
244 __END__
« lib/CGI/FormBuilder/Util.pm ↑ Summary lib/CGI/FormBuilder/Messages.pm »