FormMagick/Utils.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 165 CC 14 SCAR 20.0
CC
14
Cov
0%
parse_template
20.0
finished
12.0
debug_msg
6.0
CRAP 7.4
1 #!/usr/bin/perl -w
2 #
3 #
4 # FormMagick (c) 2000-2001 Kirrily Robert <skud@cpan.org>
5 # Copyright (c) 2000-2002 Mitel Networks Corporation
6 # This software is distributed under the same licenses as Perl itself;
7 # see the file COPYING for details.
8  
9 #
10 # $Id: Utils.pm,v 1.32 2003/02/05 17:18:37 anoncvs_gtkglext Exp $
11 #
12  
13 package CGI::FormMagick;
14  
15 use Text::Template;
16  
17 use strict;
18 use Carp;
19  
20 =pod
21  
22 =head1 NAME
23  
24 CGI::FormMagick::Utils - utility routines for FormMagick
25  
26 =head1 SYNOPSIS
27  
28 use CGI::FormMagick;
29  
30 =head1 DESCRIPTION
31  
32 =head2 debug_msg($msg)
33  
34 The debug method prints out a nicely formatted debug message. It can
35 be called from your script as C<$f->debug_msg($msg)>
36  
37 =begin testing
38  
39 BEGIN: {
40 use vars qw( $fm );
41 use lib "./lib";
42 use CGI::FormMagick;
43 }
44  
45 ok($fm = CGI::FormMagick->new(type => 'file', source => "t/simple.xml"), "create fm object");
46 $fm->parse_xml(); # suck in structure without display()ing
47  
48 =end testing
49  
50  
51 =cut
52  
53 sub debug_msg {
54 my $self = shift;
55 my $msg = shift;
56 my ($sub, $line) = (caller(1))[3,2];
57 print qq(<p class="debug">$sub $line: $msg</p>) if $self->{debug};
58 }
59  
60 =head2 $fm->get_page_by_name($name)
61  
62 get a page given the name attribute. Returns the numeric index of
63 the page, suitable for $wherenext.
64  
65 =for testing
66 is($fm->get_page_by_name('Personal'), 0, "get page by name");
67  
68 =cut
69  
70 sub get_page_by_name {
71 my ($self, $name) = @_;
72  
73 for (my $i = 0; $i < scalar(@{$self->{xml}->{pages}}); $i += 1) {
74 return $i if $self->{xml}->{pages}->[$i]->{name} eq $name;
75 }
76 return undef; # if you can't find that page
77 }
78  
79 =pod
80  
81 =head2 $fm->get_page_by_number($page_index)
82  
83 Given a page index, return a hashref containing the page's data.
84 This is just a convenience function.
85  
86 =for testing
87 is(ref($fm->get_page_by_number(0)), 'HASH', "get page by number");
88  
89 =cut
90  
91 sub get_page_by_number {
92 my ($self, $pagenum) = @_;
93 return $self->{xml}->{pages}->[$pagenum];
94 }
95  
96 =pod
97  
98 =head2 pop_page_stack($self)
99  
100 pops the last page off the stack of pages a user's visited... used
101 when the user clicks "Previous"
102  
103 removes the last element from the stack (modifying it in place in
104 $self->{page_stack}) and returns the element it removed. eg:
105  
106 # if the CGI "pagestack" parameter is "1,2,3,5"...
107 my $page = $self->pop_page_stack();
108 $self->{page_stack} will be 1,2,3
109 $page will be 5
110  
111 =begin testing
112  
113 local $fm->{page_stack} = "0,1,2,3";
114 my $p = $fm->pop_page_stack();
115 is($p, 3, "Pop page stack return value");
116 is($fm->{page_stack}, "0,1,2", "Pop page stack changes stack");
117  
118 local $fm->{page_stack} = "0";
119 $p = $fm->pop_page_stack();
120 is($p, 0, "Pop page stack return value");
121 is($fm->{page_stack}, "", "Pop page stack changes stack");
122  
123 =end testing
124  
125 =cut
126  
127 sub pop_page_stack {
128 my $self = shift;
129 my @pages = split(",", $self->{page_stack});
130 my $lastpage = pop(@pages);
131 $self->{page_stack} = join(",", @pages);
132 return $lastpage;
133 }
134  
135 =pod
136  
137 =head2 push_page_stack($newpage)
138  
139 push a new page onto the page stack that keeps track of where a user
140 has been.
141  
142 =begin testing
143  
144 local $fm->{page_stack} = "0,1,2,3";
145 $fm->push_page_stack(4);
146 is($fm->{page_stack}, "0,1,2,3,4", "Push page stack changes stack");
147  
148 local $fm->{page_stack} = "";
149 $fm->push_page_stack(0);
150 is($fm->{page_stack}, "0", "Push page stack changes empty stack");
151  
152 =end testing
153  
154 =cut
155  
156 sub push_page_stack {
157 my ($self, $newpage) = @_;
158 $self->{page_stack} = "$self->{page_stack},$newpage";
159 $self->{page_stack} =~ s/^,//;
160 }
161  
162  
163 =head2 $fm->parse_template($filename)
164  
165 parses a Text::Template file and returns the result. Will return undef
166 if the filename is invalid.
167  
168 =for testing
169 is($fm->parse_template(), undef, "Fail gracefully if no template");
170  
171 =cut
172  
173 sub parse_template {
174 my $self = shift;
175 my $filename = shift;
176 return undef unless $filename;
177  
178 if ($filename =~ /([^;]*)/) {
179 $filename = $1;
180 } else {
181 carp "Filename $filename is tainted, can't parse template";
182 return undef;
183 }
184 my $output = "";
185 if (-e $filename) {
186 my $template = new Text::Template (
187 type => 'file',
188 source => $filename,
189 UNTAINT => 1,
190 );
191 $output = $template->fill_in();
192 }
193 return $output;
194 }
195  
196 =head2 is_last_page()
197  
198 Figures out whether or not we're on the last page. Used by
199 print_buttons() in particular to tell whether to print a Finish button,
200 and to tell whether to do the form post-event.
201  
202 =for testing
203 is(@{$fm->form->{pages}}, 3, "We have three pages");
204 local $fm->{page_number} = 1;
205 ok(! $fm->is_last_page(), "It's not the last page");
206 local $fm->{page_number} = 3;
207 ok($fm->is_last_page(), "It is the last page");
208 local $fm->{page_number} = 99;
209 ok($fm->is_last_page(), "It's past the last page, but we cope OK");
210  
211 =cut
212  
213 sub is_last_page {
214 my $self = shift;
215 if ($self->{page_number} >= @{$self->form->{pages}} - 1) {
216 return 1;
217 } else {
218 return 0;
219 }
220 }
221  
222 =pod
223  
224 =head2 is_first_page()
225  
226 Figures out whether or not we're on the first page. Used mostly to
227 figure out whether we want to do the form pre-event.
228  
229 =for testing
230 is(@{$fm->form->{pages}}, 3, "We have three pages");
231 local $fm->{page_number} = 0;
232 ok($fm->is_first_page(), "Is page 0 the first page");
233 local $fm->{page_number} = 1;
234 ok(!$fm->is_first_page(), "Is page 1 the first page");
235  
236 =cut
237  
238 sub is_first_page {
239 my $self = shift;
240 if ($self->{page_number} == FIRST_PAGENUM()) {
241 return 1;
242 } else {
243 return 0;
244 }
245 }
246  
247 =head2 just_starting()
248  
249 Like is_first_page, but also checks for the absence of a "page"
250 parameter in the CGI, which would indicate that this is the very first
251 page we've looked at.
252  
253 =for testing
254 local $fm->{page_number} = 0;
255 local $fm->{cgi} = CGI->new("");
256 ok($fm->just_starting(), "Just starting");
257 local $fm->{page_number} = 0;
258 local $fm->{cgi} = CGI->new({ page => 1 });
259 ok(!$fm->just_starting(), "Not just starting");
260 local $fm->{page_number} = 0;
261 local $fm->{cgi} = CGI->new({ page => 0 });
262 ok(!$fm->just_starting(), "Not just starting even if page is 0");
263  
264 =cut
265  
266 sub just_starting {
267 my $self = shift;
268 if ($self->is_first_page() and not defined $self->{cgi}->param("page")) {
269 return 1;
270 } else {
271 return 0;
272 }
273 }
274  
275 =head2 finished
276  
277 Figures out whether the user's finished. This could be because they
278 clicked "Finish" or it could be because they were on the last page and
279 hit enter.
280  
281 This bears the same relationship to is_last_page as just_starting does to
282 is_first_page.
283  
284 =begin testing
285  
286 use CGI;
287  
288 $cgi = CGI->new( { Finish => 1 } );
289 local $fm->{cgi} = $cgi;
290 local $fm->{page_number} = 3;
291 ok($fm->finished(), "User is finished (clicked Finish on last page)");
292  
293 $cgi = CGI->new("");
294 local $fm->{cgi} = $cgi;
295 local $fm->{page_number} = 3;
296 ok($fm->finished(), "User is finished (last page, pressed enter)");
297  
298 $cgi = CGI->new("");
299 local $fm->{cgi} = $cgi;
300 local $fm->{page_number} = 1;
301 ok(!$fm->finished(), "User is NOT finished (not last page, pressed enter)");
302  
303 $cgi = CGI->new({ Previous => 1 });
304 local $fm->{cgi} = $cgi;
305 local $fm->{page_number} = 2;
306 ok(!$fm->finished(), "User is NOT finished (last page, didn't press enter)");
307  
308 =end testing
309  
310 =cut
311  
312 sub finished {
313 my $self = shift;
314 if ($self->{cgi}->param("Finish")
315 and $self->{page_number} >= @{$self->form->{pages}}) {
316 # note the difference between this test and the one in is_last_page()
317 # ... it's a fencepost thing. In the case of is_last_page we want
318 # to see if we're *on* the last page, but here we want to see if
319 # we're *past* it.
320  
321 return 1;
322 } elsif ($self->user_pressed_enter()
323 and $self->{page_number} >= @{$self->form->{pages}}) {
324 return 1;
325 } else {
326 return 0;
327 }
328 }
329  
330 =head2 user_pressed_enter()
331  
332 A weirdness in the HTML spec and/or
333 browser implementations thereof means that hitting "enter" on a
334 single-text-field form will submit the form without any value
335 being passed. Worse yet, at least one browser is reported to
336 automatically choose the first submit button on the form, in our
337 case "Previous", which is just WRONG but I can't see any way to
338 work around that.
339  
340 So this routine tells you if the user just hit enter. Returns 1 if they
341 did, or 0 otherwise.
342  
343 =begin testing
344  
345 use CGI;
346  
347 my $cgi = CGI->new("");
348 local $fm->{cgi} = $cgi;
349 ok($fm->user_pressed_enter(), "User pressed enter");
350  
351 $cgi = CGI->new({ Next => "foo" });
352 local $fm->{cgi} = $cgi;
353 ok(!$fm->user_pressed_enter(), "User clicked a button");
354  
355 =end testing
356  
357 =cut
358  
359 sub user_pressed_enter {
360 my $self = shift;
361 unless ( $self->{cgi}->param("Previous") or
362 $self->{cgi}->param("Next") or
363 $self->{cgi}->param("Finish") ) {
364 return 1;
365 } else {
366 return 0;
367 }
368 }
369  
370 =head2 $fm->form()
371  
372 Gets the form we're dealing with. With no args, returns an hashref to
373 the form data structure.
374  
375  
376 =for testing
377 my $form = $fm->form();
378 is(ref $form, "HASH", "form data structure is a hash");
379  
380 =cut
381  
382 sub form {
383 my ($fm) = @_;
384 return $fm->{xml};
385 }
386  
387 =pod
388  
389 =head2 $fm->page()
390  
391 Gets the current page we're dealing with, as a hashref.
392  
393 =for testing
394 local $fm->{page_number} = 0;
395 my $page = $fm->page();
396 is(ref $page, "HASH", "page data structure is a hash");
397  
398 =cut
399  
400 sub page {
401 my ($fm) = @_;
402 return $fm->form->{pages}->[$fm->{page_number}]
403 }
404  
405 =pod
406  
407 =head2 get_page_enctype
408  
409 Returns the appropriate encoding type for this page. A page that uses
410 FILE fields must also use the multipart/form-data encoding type. Any other
411 page may use either, but the default is to use the older and more compatible
412 application/x-www-urlencoded.
413  
414 =for testing
415 local $fm->{page_number} = 0;
416 local $fm->page->{fields}->[0]->{type} = 'text';
417 is($fm->get_page_enctype(), 'application/x-www-urlencoded',
418 'Detected standard enctype');
419 local $fm->page->{fields}->[0]->{type} = 'file';
420 is($fm->get_page_enctype(), 'multipart/form-data',
421 'Detected multipart enctype');
422  
423 =cut
424  
425 sub get_page_enctype
426 {
427 my $self = shift;
428 foreach my $field (@{$self->page->{fields}}) {
429 if ($field->{type} eq 'file') {
430 return 'multipart/form-data';
431 }
432 }
433 return 'application/x-www-urlencoded';
434 }
435  
436 =head2 $self->clear_navigation_params()
437  
438 Clear out the nagivation params Next, Previous, Finish and wherenext.
439 Otherwise they stick around in CGI::Persistent and cause havoc next time
440 you try to navigate.
441  
442 =begin testing
443  
444 local $fm->{cgi} = CGI->new({
445 Previous => 1,
446 Next => 1,
447 Finish => 1,
448 wherenext => 1,
449 });
450  
451 $fm->clear_navigation_params();
452 is($fm->{cgi}->param("Previous"), undef, "Clear Previous param");
453 is($fm->{cgi}->param("Next"), undef, "Clear Next param");
454 is($fm->{cgi}->param("Finish"), undef, "Clear Finish param");
455 is($fm->{cgi}->param("wherenext"), undef, "Clear wherenext param");
456  
457 =end testing
458  
459 =cut
460  
461 sub clear_navigation_params {
462 my $self = shift;
463  
464 foreach ( qw( Previous Next Finish wherenext ) ) {
465 $self->{cgi}->delete($_) if defined $self->{cgi}->param($_);
466 }
467 }
468  
469  
470  
471 return "FALSE"; # true value
472  
473 =pod
474  
475 =head1 SEE ALSO
476  
477 CGI::FormMagick;
478  
479 =cut