Jifty/Web/Form.pm

filter: Statementstmt 11.9% 15 / 126 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 21.0% 4 / 19 Podpod 100% 10 / 10 totaltotal 14.0% 29 / 207 CC 27 SCAR 37.0
CC
27
Cov
9%
start
132.0
submit
30.0
_preserve_continuations
20.0
CRAP 40.3
« Jifty/Test/WWW/Selenium.pm ↑ Summary Jifty/Plugin/REST/Dispatcher.pm »
1 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jifty::Web::Form;
5  
6 1 ▶use base qw/Jifty::Object Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 __PACKAGE__->mk_accessors(qw(actions printed_actions name call is_open disable_autocomplete target submit_to onsubmit class));
9  
10 # Alias id to name
11 1 *id = *name;
12  
13 1 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Jifty::Web::Form - Tools for rendering and dealing with HTML forms
18  
19 =head1 METHODS
20  
21 =head2 new ARGS
22  
23 Creates a new L<Jifty::Web::Form>. Arguments:
24  
25 =over
26  
27 =item id
28  
29 The HTML id attribute given to the form. This is aliased to L<name>. That is,
30 name and id are always equal and changing one changes the other.
31  
32 =item class
33  
34 The HTML class attribute given to the form.
35  
36 =item name
37  
38 The name given to the form. This is mostly for naming specific forms
39 for testing.
40  
41 =item call
42  
43 All buttons in this form will act as continuation calls for the given
44 continuation id.
45  
46 =item disable_autocomplete
47  
48 Disable B<browser> autocomplete for this form. Jifty autocomplete will still
49 work.
50  
51 =back
52  
53 =cut
54  
55 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
56 0 my $class = shift;
57 0 ▶ my $self = bless {}, ref $class ? ref $class : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 0 0
58  
59 0 my %args = (
60 name => undef,
61 call => undef,
62 submit_to => undef,
63 target => undef,
64 disable_autocomplete => undef,
65 @_,
66 );
67  
68 0 $self->_init(%args);
69 0 return $self;
70 }
71  
72 =head2 PRIVATE _init
73  
74 Reinitialize this form.
75  
76 =over
77  
78 =item name
79  
80 The form name
81  
82 =item call
83  
84 The continuation id to call
85  
86 =back
87  
88 =cut
89  
90  
91 0 ▶sub _init {
Subroutine _init: not called CC=1 SCAR=6.9
92 0 my $self = shift;
93 0 my %args = (name => undef,
94 call => undef,
95 target => undef,
96 submit_to => undef,
97 disable_autocomplete => undef,
98 @_);
99  
100 0 $self->actions( {} ) ;
101 0 $self->printed_actions( {} ) ;
102 0 $self->name($args{name});
103 0 $self->call($args{call});
104 0 $self->target($args{target});
105 0 $self->submit_to($args{'submit_to'});
106 0 $self->disable_autocomplete($args{disable_autocomplete});
107 }
108  
109  
110 =head2 actions
111  
112 Returns a reference to a hash of L<Jifty::Action> objects in this form keyed by moniker.
113  
114 If you want to add actions to this form, use L</add_action>
115  
116 =cut
117  
118 =head2 name [VALUE]
119  
120 Gets or sets the HTML name given to the form element.
121  
122 =cut
123  
124 =head2 call [CONTID]
125  
126 Gets or sets the continuation ID that will be called for this form.
127  
128 =cut
129  
130 =head2 is_open [BOOL]
131  
132 This accessor returns true if Jifty is currently in the middle of rendering a form
133 (if it's printed a <form> but not yet printed a </form> tag.) Use this in your
134 components to decide whether to open a form or not if you might be called from a
135 template that opened the form for you.
136  
137 =cut
138  
139 =head2 add_action PARAMHASH
140  
141 Calls L<Jifty::Web/new_action> with the paramhash given, and adds it to
142 the form.
143  
144 =cut
145  
146 0 ▶sub add_action {
Subroutine add_action: not called CC=1 SCAR=6.9
Pod documented
147 0 my $self = shift;
148 0 $self->register_action(Jifty->web->new_action(@_));
149 }
150  
151  
152  
153 =head2 register_action ACTION
154  
155 Adds C<ACTION> as an action for this form. Called so that actions'
156 form fields can register the action against the form they're being
157 used in.
158  
159 =cut
160  
161  
162 0 ▶sub register_action {
Subroutine register_action: not called CC=1 SCAR=6.9
Pod documented
163 0 my $self = shift;
164 0 my $action = shift;
165 0 $self->actions->{ $action->moniker } = $action;
166 0 weaken $self->actions->{ $action->moniker};
167 0 return $action;
168 }
169  
170  
171 =head2 has_action MONIKER
172  
173 If this form has an action whose moniker is C<MONIKER>, returns
174 it. Otherwise returns undef.
175  
176 =cut
177  
178 0 ▶sub has_action {
Subroutine has_action: not called CC=2 SCAR=17.9
Pod documented
179 0 my $self = shift;
180 0 my $moniker = shift;
181 0 ▶ if ( exists $self->actions->{$moniker} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->actions->{$moniker}) { } 0 0 0
182 0 return $self->actions->{$moniker};
183 }
184 0 else { return undef }
185  
186 }
187  
188  
189  
190 =head2 start
191  
192 Renders the opening form tag.
193  
194 =cut
195  
196 0 ▶sub start {
Subroutine start: not called CC=11 SCAR=48.8
Pod documented
197 0 my $self = shift;
198 0 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
199  
200 0 my %args = (@_);
201  
202 0 ▶ if ($self->is_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_open) 0 0 0
203 0 $self->log->warn("Trying to open a form when we already have one open");
204 }
205  
206 0 for (keys %args) {
207 0 ▶ if ( $self->can($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($_)) { } 0 0 0
208 0 $self->$_($args{$_});
209 } else {
210 0 my (undef, $template, $line) = caller;
211 0 $self->log->warn("Unknown parameter to Jifty->web->form->start: $_ in $template line $line");
212 }
213 }
214  
215 0 ▶ my $root = $self->submit_to
Condition0%0/3
$self->submit_to || 'URI'->new('Jifty'->web->request->top_request->request_uri)->path
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->submit_to || 'URI'->new('Jifty'->web->request->top_request->request_uri)->path
$self->submit_to 'URI'->new('Jifty'->web->request->top_request->request_uri)->path
Truth table0%0/3
A || B
A= $self->submit_toB= 'URI'->new('Jifty'->web->request->top_request->request_uri)->path
ABresult
1X1
011
000
216 || URI->new(Jifty->web->request->top_request->request_uri)->path;
217  
218 0 my $form_start = qq!<form method="post" action="! . Jifty->web->escape( $root ) . qq!"!;
219 0 ▶ $form_start .= qq! name="@{[ $self->name ]}"! if defined $self->name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->name 0 0 0
220 0 ▶ $form_start .= qq! id="@{[ $self->name ]}"! if defined $self->name; # always the same as name
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->name 0 0 0
221 0 ▶ $form_start .= qq! class="@{[ $self->class ]}"! if defined $self->class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->class 0 0 0
222 0 ▶ $form_start .= qq! target="@{[ $self->target ]}"! if defined $self->target;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->target 0 0 0
223 0 ▶ $form_start .= qq! autocomplete="off"! if defined $self->disable_autocomplete;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->disable_autocomplete 0 0 0
224 0 ▶ $form_start .= qq! onsubmit="! .Jifty->web->escape( $self->onsubmit ). qq!"! if defined $self->onsubmit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->onsubmit 0 0 0
225 0 $form_start .= qq! enctype="multipart/form-data" >\n!;
226 0 Jifty->web->out($form_start);
227  
228 # Write out state variables early, so that if a form gets
229 # submitted before the page finishes loading, the state vars don't
230 # get lost
231 0 $self->_preserve_state_variables();
232  
233 0 $self->is_open(1);
234 0 '';
235 }
236  
237 =head2 submit MESSAGE, [PARAMETERS]
238  
239 Renders a submit button with the text MESSAGE on it (which will be
240 HTML escaped). Returns the empty string (for ease of use in
241 interpolation). Any extra PARAMETERS are passed to
242 L<Jifty::Web::Form::Field::Clickable>'s constructor.
243  
244 =cut
245  
246 0 ▶sub submit {
Subroutine submit: not called CC=5 SCAR=34.0
Pod documented
247 0 my $self = shift;
248 0 my %args = (
249 submit => undef,
250 _form => $self,
251 as_button => 1,
252 @_,
253 );
254  
255 0 ▶ my @submit = ref($args{'submit'}) eq 'ARRAY' ? @{$args{'submit'}} : $args{'submit'};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'submit'} eq 'ARRAY' ? : 0 0 0
256 0 ▶ if ($self->actions->{'next_page'} && $submit[0] && ! grep {$_->moniker eq 'next_page' } @submit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->actions->{"next_page"} and $submit[0] and not grep({$_->moniker eq "next_page";} @submit)) 0 0 0
Condition0%0/6
$self->actions->{"next_page"} and $submit[0] and not grep({$_->moniker eq "next_page";} @submit)
!ll&&!rl&&r
000
$self->actions->{"next_page"} and $submit[0]
!ll&&!rl&&r
000
MC/DC0%0/3
$self->actions->{"next_page"} and $submit[0] and not grep({$_->moniker eq "next_page";} @submit)
$self->actions->{"next_page"} $submit[0] not grep({$_->moniker eq "next_page";} @submit)
Truth table0%0/4
A and B and not(C)
A= $self->actions->{"next_page"}B= $submit[0]C= not grep({$_->moniker eq "next_page";} @submit)
ABCresult
0XX0
10X0
1100
1111
257 0 push @submit, $self->actions->{'next_page'};
258 0 $args{'submit'} = \@submit;
259 }
260  
261  
262 0 my $button = Jifty::Web::Form::Clickable->new(%args)->generate;
263 0 Jifty->web->out(qq{<div class="submit_button">});
264 0 $button->render_widget;
265 0 Jifty->web->out(qq{</div>});
266  
267 0 return '';
268 }
269  
270 =head2 return MESSAGE, [PARAMETERS]
271  
272 Renders a return button with the text MESSAGE on it (which will be
273 HTML escaped). Returns the empty string (for ease of use in
274 interpolation). Any extra PARAMETERS are passed to
275 L<Jifty::Web::Form::Field::Button>'s constructor.
276  
277 =cut
278  
279 0 ▶sub return {
Subroutine return: not called CC=1 SCAR=6.9
Pod documented
280 0 my $self = shift;
281  
282 0 my $button = Jifty->web->return(as_button => 1, @_);
283 0 Jifty->web->out(qq{<div class="submit_button">});
284 0 $button->render_widget;
285 0 Jifty->web->out(qq{</div>});
286  
287 0 return '';
288 }
289  
290 =head2 end
291  
292 Renders the closing form tag (including rendering errors for and
293 registering all of the actions) After doing this, it resets its
294 internal state such that L</start> may be called again.
295  
296 =cut
297  
298 0 ▶sub end {
Subroutine end: not called CC=2 SCAR=17.9
Pod documented
299 0 my $self = shift;
300  
301 0 ▶ unless ($self->is_open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_open) 0 0 0
302 0 $self->log->warn("Trying to close a form when we don't have one open");
303 }
304 0 Jifty->web->out( qq!<div class="hidden">\n! );
305  
306 0 $self->_print_registered_actions();
307 0 $self->_preserve_continuations();
308  
309 0 Jifty->web->out( qq!</div>\n! );
310  
311 0 Jifty->web->out( qq!</form>\n! );
312 0 $self->is_open(0);
313 # Clear out all the registered actions and the name
314 0 $self->_init();
315  
316 0 '';
317 }
318  
319  
320 =head2 print_action_registration MONIKER
321  
322 Print out the action registration goo for this action _right now_, unless we've already done so.
323  
324 =cut
325  
326  
327 0 ▶sub print_action_registration {
Subroutine print_action_registration: not called CC=3 SCAR=24.8
Pod documented
328 0 my $self = shift;
329 0 my $moniker = shift;
330  
331  
332 0 my $action = $self->has_action($moniker);
333 0 ▶ return unless ($action);
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 0 0
334 0 ▶ return if exists $self->printed_actions->{$moniker};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->printed_actions->{$moniker} 0 0 0
335 0 $self->printed_actions->{$moniker} = 1;
336  
337 0 $action->register();
338  
339 }
340  
341  
342 # At the point this is called, it should only include actions we're
343 # registering that have no form fields and haven't been explicitly
344 # registered.
345 0 ▶sub _print_registered_actions {
Subroutine _print_registered_actions: not called CC=2 SCAR=17.9
346 0 my $self = shift;
347 0 for my $a ( keys %{ $self->actions } ) {
348 0 $self->print_action_registration($a);
349 }
350 }
351  
352 0 ▶sub _preserve_state_variables {
Subroutine _preserve_state_variables: not called CC=2 SCAR=17.9
353 0 my $self = shift;
354  
355 0 my %vars = Jifty->web->state_variables;
356 0 for (keys %vars) {
357 Jifty->web->out( qq{<input type="hidden" name="}
358 . 'J:V-' . $_
359 . qq{" value="}
360 0 . $vars{$_}
361 . qq{" />\n} );
362 }
363 }
364  
365 0 ▶sub _preserve_continuations {
Subroutine _preserve_continuations: not called CC=4 SCAR=30.0
366 0 my $self = shift;
367  
368 0 ▶ if ($self->call) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->call) { } 0 0 0
elsif ('Jifty'->web->request->continuation) { } 0 0 0
369 0 ▶ Jifty->web->out( qq{<input type="hidden" name="J:CALL" value="}
Branch: 1 branch
BranchTrueFalseTotal
ref $self->call ? : 0 0 0
370 . (ref $self->call ? $self->call->id : $self->call)
371 . qq{" />});
372 } elsif (Jifty->web->request->continuation) {
373 0 Jifty->web->out( qq{<input type="hidden" name="J:C" value="}
374 . Jifty->web->request->continuation->id
375 . qq{" />});
376 }
377  
378 }
379  
380 =head2 next_page PARAMHASH
381  
382 Set the page this form should go to on success. This simply creates a
383 L<Jifty::Action::Redirect> action; any parameters in the C<PARAMHASH>
384 are passed as arguments to the L<Jifty::Action::Redirect> action.
385  
386 Returns an empty string so it can be included in forms
387  
388 =cut
389  
390 0 ▶sub next_page {
Subroutine next_page: not called CC=2 SCAR=17.9
Pod documented
391 0 my $self = shift;
392 0 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
393 0 Carp::carp("next_page accepts a parameter hash. You probably want to specify url => ...");
394 }
395  
396 0 $self->add_action(class => "Jifty::Action::Redirect", moniker => "next_page", arguments => {@_});
397 0 return '';
398 }
399  
400 =head2 DESTROY
401  
402 Checks to ensure that forms that were opened were actually closed,
403 which is when actions are registered.
404  
405 =cut
406  
407 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
408 0 my $self = shift;
409 warn "Action $_ was never registered (form was never closed)"
410 0 for grep {not $self->printed_actions->{$_}} keys %{$self->actions};
411 }
412  
413 1 1;
« Jifty/Test/WWW/Selenium.pm ↑ Summary Jifty/Plugin/REST/Dispatcher.pm »