blib/lib/CGI/FormBuilder/Field/datetime.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 152 SCAR 59.4
CC
19
Cov
0%
tag
306.0
script
12.0
BEGIN
2.0
CRAP 380.0
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 # The majority of this module's methods (including new) are
8 # inherited directly from ::base, since they involve things
9 # which are common, such as parameter parsing. The only methods
10 # that are individual to different fields are those that affect
11 # the rendering, such as script() and tag()
12  
13 package CGI::FormBuilder::Field::datetime;
14  
15 use strict;
16 use warnings;
17 no warnings 'uninitialized';
18  
19 use CGI::FormBuilder::Util;
20 use CGI::FormBuilder::Field;
21 use base 'CGI::FormBuilder::Field';
22  
23  
24 our $VERSION = '3.10';
25  
26 sub script {
27 my $self = shift;
28 my $name = $self->name;
29  
30 # The way script() works is slightly backwards: First the
31 # type-specific JS DOM code is generated, then this is
32 # passed as a string to Field->jsfield, which wraps this
33 # in the generic handling.
34  
35 # Holders for different parts of JS code
36 my $jsfunc = '';
37 my $jsfield = tovar($name);
38 my $close_brace = '';
39 my $in = indent(my $idt = 1); # indent
40  
41 my $alertstr = escapejs($self->jsmessage); # handle embedded '
42 $alertstr .= '\n';
43  
44 if ($self->growable) {
45 # special handling for growable, have to dynamically
46 # find out how many have been created
47 $jsfunc .= <<EOJS;
48 // $name: growable text or file box
49 var $jsfield = null;
50 var entered_$jsfield = 0;
51 var i = 0;
52 while (1) {
53 var growel = document.getElementById('$jsfield'+'_'+i);
54 if (growel == null) break; // last element
55 $jsfield = growel.value;
56 entered_$jsfield++;
57 i++;
58 EOJS
59  
60 $close_brace = <<EOJS;
61  
62 } // while $name
63 EOJS
64  
65 # required?
66 $close_brace .= <<EOJS if $self->required;
67 if (! entered_$jsfield) {
68 alertstr += '$alertstr';
69 invalid++;
70 }
71 EOJS
72 # indent the very last if/else tests so they're in the while loop
73 $in = indent($idt += 1);
74  
75 } else {
76  
77 # get value from text or other straight input
78 # at least this part makes some sense
79 $jsfunc .= <<EOJS;
80 // $name: standard text, hidden, password, or textarea box
81 var $jsfield = form.elements['$name'].value;
82 EOJS
83  
84 }
85  
86 return $self->jsfield($jsfunc, $close_brace, $in);
87 }
88  
89 *render = \&tag;
90 sub tag {
91 local $^W = 0; # -w sucks
92 my $self = shift;
93 my $attr = $self->attr;
94  
95 my $jspre = $self->{_form}->jsprefix;
96  
97 my $tag = '';
98 my @value = $self->tag_value; # sticky is different in <tag>
99 my @opt = $self->options;
100 debug 2, "my(@opt) = \$field->options";
101  
102 # Add in our "Other:" option if applicable
103 push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
104 if $self->other;
105  
106 debug 2, "$self->{name}: generating $attr->{type} input type";
107  
108 # We iterate over each value - this is the only reliable
109 # way to handle multiple form values of the same name
110 # (i.e., multiple <input> or <hidden> fields)
111 @value = (undef) unless @value; # this creates a single-element array
112  
113 # growable handling
114 my $count = 0; # for tracking the size of growable fields
115 my $limit; # for providing (optional) limits to growable fields
116 my $at_limit; # have we reached the limit of a growable field?
117 if ($self->growable && $self->growable ne 1) {
118 $limit = $self->growable;
119 }
120  
121 for my $value (@value) {
122 if ($limit && $count == $limit) {
123 belch "Number of supplied values (" . @value . ")"
124 . " for '$attr->{name}' exceeds growable limit $limit - discarding excess";
125 $at_limit = 1;
126 last;
127 }
128
129 # setup the value, convert standard if necessary
130 if ($value=~ m/([0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}) ([0-9]{2,2}[:-][0-9]{2,2}[:-][0-9]{2,2})/){
131 $value=$1 . 'T' . $2 . 'Z';
132 }
133 $attr->{value} = $value; # override
134 delete $attr->{value} unless defined $value;
135  
136 if ($self->growable && $self->javascript) {
137 # the inputs in growable fields need a unique id for fb_grow()
138 $attr->{id} = tovar("$attr->{name}_$count");
139 $count++;
140 }
141  
142 # render the tag
143 $tag .= htmltag('input', $attr);
144  
145 #
146 # If we have options, lookup the label instead of the true value.
147 # This code is reached if a field is marked 'static', since
148 # that is still rendered from ::text (here) but as a 'hidden'
149 # field. These options would be leftover from a former select
150 # or radio group that is now shown on a confirm() screen. Got it?
151 #
152 for (@opt) {
153 my($o,$n) = optval($_);
154 if ($o eq $value) {
155 $n ||= $attr->{labels}{$o} || ($self->nameopts ? toname($o) : $o);
156 $value = $n;
157 last;
158 }
159 }
160  
161 if ($self->growable && $self->javascript) {
162 # put linebreaks between the input tags in growable fields
163 # this puts the "Additonal [label]" button on the same line
164 # as the last input tag
165 $tag .= '<br />' unless $count == @value;
166 } else {
167 $tag .= '<br />' if $self->linebreaks;
168 }
169 }
170 # check to see if we just hit the limit
171 $at_limit = 1 if $limit && $count == $limit;
172  
173 # add the "Additional [label]" button
174 if ($self->growable && $self->javascript) {
175 $tag .= ' ' . htmltag('input',
176 id => $self->growname,
177 type => 'button',
178 onclick => "${jspre}grow('$attr->{name}')",
179 value => sprintf($self->{_form}{messages}->form_grow_default, $self->label),
180 ( $at_limit ? ( disabled => 'disabled') : () ),
181 );
182 }
183  
184 # add an additional tag for our _other field
185 $tag .= ' ' . $self->othertag if $self->other;
186  
187 debug 2, "$self->{name}: generated tag = $tag";
188 return $tag; # always return scalar tag
189 }
190  
191 1;
192  
193 __END__