blib/lib/Class/CGI/DateTime.pm

filter: Statementstmt 100% 45 / 45 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 66.6% 2 / 3 totaltotal 98.4% 64 / 65 CC 6 SCAR 9.2
CC
6
Cov
100%
handle
4.0
components
3.0
BEGIN
1.0
CRAP 2.5
↑ Summary
1 package Class::CGI::DateTime;
2  
3 3 ▶use base 'Class::CGI::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Class::CGI::DateTime - Fetch DateTime objects directly from your forms.
12  
13 =head1 VERSION
14  
15 Version 0.02
16  
17 =cut
18  
19 3 our $VERSION = '0.02';
20  
21 =head1 SYNOPSIS
22  
23 use Class::CGI
24 handlers => {
25 date => 'Class::CGI::DateTime',
26 order_date => 'Class::CGI::DateTime',
27 };
28 my $cgi = Class::CGI->new;
29 my $date = $cgi->param('date');
30 my $order_date = $cgi->param('order_date');
31  
32 if ( my %errors = $cgi->errors ) { ... }
33  
34 =head1 DESCRIPTION
35  
36 A common problem with Web programming is handling dates correctly. This
37 C<Class::CGI> handler attempts to do that for you in a standard way by
38 allowing you to have separate day, month and year dropdowns but request a
39 "date" parameter and get the correct object back, safely validated and
40 untainted.
41  
42 Multiple dates may be used in a single form and you can even specify hours,
43 minutes, timezones, and so on.
44  
45 =head2 Basic Usage
46  
47 If you have a form with C<< <select> >> boxes named C<day>, C<month> and
48 C<year>, you can fetch these into a C<DateTime> object with this:
49  
50 use Class::CGI
51 handlers => {
52 date => 'Class::CGI::DateTime',
53 };
54 my $cgi = Class::CGI->new;
55 my $date = $cgi->param('date');
56  
57 The value of each parameter should correspond to allowed values for that
58 parameter to the C<DateTime> constructor.
59  
60 =head2 Multiple dates in a form
61  
62 If you need more than one date object embedded in a form, additional date
63 objects may be specified by prefixing the form parameter names with a unique
64 identifier followed by a dot ("."). For example, to fetch a date and an order
65 date, you can do the following:
66  
67 use Class::CGI
68 handlers => {
69 date => 'Class::CGI::DateTime',
70 order_date => 'Class::CGI::DateTime',
71 };
72 my $cgi = Class::CGI->new;
73 my $date = $cgi->param('date');
74 my $order_date = $cgi->param('order_date');
75  
76 The C<date> parameter will be created from the C<day>, C<month> and C<year>
77 parameters in the C<Class::CGI> object. The C<order_date.date> parameter will
78 be created from the C<order_date.day>, C<order_date.month> and
79 C<order_date.year> parameters.
80  
81 =head2 Different parameters
82  
83 $cgi->args( date => { params => [qw/ day hour minute /] } );
84  
85 You cannot change the parameter names (e.g., no changing "day" to "jour" or
86 something like that), but you can specify additional parameters which the
87 C<DateTime> constructor expects. Each parameter's value must correspond to
88 the allowed values for the C<DateTime> object. You do this by setting the
89 requested parameters in C<Class::CGI>'s C<args()> method. The key is the date
90 parameter you wish to specify the arguments for and the value should be a
91 hashref with a key of "params" and a value being an array reference specifying
92 the names of the parameters desired.
93  
94 For example, if your HTML form has day, month, year and timezone, you might do
95 this:
96  
97 use Class::CGI
98 handlers => {
99 date => 'Class::CGI::DateTime',
100 };
101 my $cgi = Class::CGI->new;
102 $cgi->args(
103 date => { params => [qw/ day month year time_zone /] }
104 );
105 my $date = $cgi->param('date');
106  
107 The allowed parameter names, as specified from the C<DateTime> documentation,
108 are:
109  
110 =over 4
111  
112 =item * year
113  
114 =item * month
115  
116 =item * day
117  
118 =item * hour
119  
120 =item * minute
121  
122 =item * second
123  
124 =item * nanosecond
125  
126 =item * time_zone
127  
128 =back
129  
130 =head2 Error handling
131  
132 As usual, any errors reported by C<Class::CGI::DateTime> will be in the
133 C<Class::CGI> C<errors()> method. However, the validation errors reported by
134 the C<DateTime> object are generally aimed at programmers, not users of your
135 software. Thus, you'll want to provide more user friendly error messages.
136 For example:
137  
138 if ( my %error_for = $cgi->errors ) {
139 if ( exists $error_for{order_date} ) {
140 $error_for{order_date} = "You must enter a valid order date";
141 }
142 }
143  
144 If a date is required, it's easy to handle this:
145  
146 use Class::CGI
147 handlers => {
148 date => 'Class::CGI::DateTime',
149 order_date => 'Class::CGI::DateTime',
150 };
151 my $cgi = Class::CGI->new;
152 $cgi->required( qw/date order_date/ );
153 my $date = $cgi->param('date');
154 my $order_date = $cgi->param('order_date');
155  
156 C<Class::CGI::DateTime> will not create a date object if any of the required
157 components (day, month, year and so on) are missing. Further, a descriptive
158 error message will be set.
159  
160 Also note that at the present time, C<Class::CGI::DateTime> only ensures that
161 we can create a valid C<DateTime> object. Application-specific validation
162 (such as ensuring the date is in the future) belongs in your application and
163 should be handled there.
164  
165 =cut
166  
167 7 ▶sub handle {
Subroutine handle: called CC=4 SCAR=13.9
Pod documented
168 7 my $self = shift;
169 7 my $cgi = $self->cgi;
170 7 my $param = $self->param;
171  
172 7 my @params = $self->components;
173  
174 # original param name and param value
175 7 my %args = map { /([[:word:]]+)$/; $1, $cgi->raw_param($_) } @params;
176  
177 # untaint them puppies
178 7 ▶ while ( my ( $arg, $value ) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 36 7 43
179 36 ▶ if ( 'time_zone' eq $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('time_zone' eq $arg) { } 3 33 36
180 3 $value =~ /^(floating|local|\+[[:digit:]]+|[[:word:]]+\/[[:word:]]+)$/;
181 3 $args{$arg} = $1;
182 }
183 else {
184 33 $value =~ /^(\d+)$/;
185 33 $args{$arg} = $1;
186 }
187 }
188 7 my $datetime;
189
190 7 eval { $datetime = DateTime->new(%args) };
191  
192 7 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 6 7
193 1 $cgi->add_error( $param, 'You must supply a valid date' );
194 }
195 7 return $datetime;
196 }
197  
198 9 ▶sub components {
Subroutine components: called CC=3 SCAR=11.0
Pod undocumented
199 9 my $self = shift;
200 9 my $cgi = $self->cgi;
201 9 my $param = $self->param;
202  
203 9 my $requested_params = $cgi->args($param);
204 my @params = $requested_params
205 9 ▶ ? @{ $requested_params->{params} }
Branch: 1 branch
BranchTrueFalseTotal
$requested_params ? : 3 6 9
206 : qw(day month year);
207  
208 9 ▶ if ( 'date' ne $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("date" ne $param) 3 6 9
209 3 @params = map {"$param.$_"} @params;
210 }
211 9 return @params;
212 }
213  
214 2 ▶sub has_param {
Subroutine has_param: called CC=1 SCAR=0.0
Pod documented
215 2 my $self = shift;
216 2 return $self->has_virtual_param( $self->param, $self->components );
217 }
218  
219 =head1 AUTHOR
220  
221 Curtis "Ovid" Poe, C<< <ovid@cpan.org> >>
222  
223 =head1 BUGS
224  
225 Please report any bugs or feature requests to
226 C<bug-class-cgi-datetime@rt.cpan.org>, or through the web interface at
227 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-CGI-DateTime>.
228 I will be notified, and then you'll automatically be notified of progress on
229 your bug as I make changes.
230  
231 =head1 SEE ALSO
232  
233 =over 4
234  
235 =item *
236 DateTime - A date and time object
237  
238 =back
239  
240 =head1 ACKNOWLEDGEMENTS
241  
242 Adam Kennedy (Alias): for suggesting a better interface.
243  
244 =head1 COPYRIGHT & LICENSE
245  
246 Copyright 2006 Curtis "Ovid" Poe, all rights reserved.
247  
248 This program is free software; you can redistribute it and/or modify it
249 under the same terms as Perl itself.
250  
251 =cut
252  
253 3 1;
↑ Summary