line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::FormFieldsFromJSON::Date; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
1698
|
use v5.10; |
|
3
|
|
|
|
|
9
|
|
4
|
3
|
|
|
3
|
|
14
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
18
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1407
|
use Time::Piece; |
|
3
|
|
|
|
|
11028
|
|
|
3
|
|
|
|
|
14
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
3
|
|
|
3
|
1
|
102
|
my ($self, $app) = @_; |
12
|
|
|
|
|
|
|
|
13
|
3
|
|
|
|
|
11
|
1; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub Mojolicious::Plugin::FormFieldsFromJSON::_date { |
17
|
4
|
|
|
4
|
|
87781
|
my ($self, $c, $field, %params) = @_; |
18
|
|
|
|
|
|
|
|
19
|
4
|
|
33
|
|
|
20
|
my $name = $field->{name} // $field->{label} // ''; |
|
|
|
0
|
|
|
|
|
20
|
4
|
|
33
|
|
|
22
|
my $id = $field->{id} // $name; |
21
|
4
|
100
|
|
|
|
8
|
my %local_params = %{ $params{$name} || {} }; |
|
4
|
|
|
|
|
25
|
|
22
|
|
|
|
|
|
|
|
23
|
4
|
100
|
|
|
|
25
|
my $requested = ( $c->param( $name . '-day' ) ) ? |
24
|
|
|
|
|
|
|
( sprintf "%04d-%02d-%02d", |
25
|
|
|
|
|
|
|
$c->param( $name . '-year' ), |
26
|
|
|
|
|
|
|
$c->param( $name . '-month' ), |
27
|
|
|
|
|
|
|
$c->param( $name . '-day' ), |
28
|
|
|
|
|
|
|
) : |
29
|
|
|
|
|
|
|
""; |
30
|
|
|
|
|
|
|
|
31
|
4
|
|
|
|
|
1213
|
my $now = localtime; |
32
|
4
|
|
66
|
|
|
326
|
my $default = $local_params{data} || $requested || $field->{data} || $now->ymd; |
33
|
4
|
|
|
|
|
85
|
my $selected = Time::Piece->strptime( $default, '%Y-%m-%d' ); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $day = _day_dropdown( |
36
|
|
|
|
|
|
|
$c, |
37
|
4
|
50
|
|
|
|
310
|
%{ $field->{attributes} || {} }, |
|
4
|
|
|
|
|
34
|
|
38
|
|
|
|
|
|
|
%local_params, |
39
|
|
|
|
|
|
|
day => $selected->mday, |
40
|
|
|
|
|
|
|
id => $id . '-day', |
41
|
|
|
|
|
|
|
name => $name . '-day', |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $month = _month_dropdown( |
45
|
|
|
|
|
|
|
$c, |
46
|
4
|
50
|
|
|
|
9414
|
%{ $field->{attributes} || {} }, |
|
4
|
|
|
|
|
35
|
|
47
|
|
|
|
|
|
|
%local_params, |
48
|
|
|
|
|
|
|
month => $selected->mon, |
49
|
|
|
|
|
|
|
id => $id . '-month', |
50
|
|
|
|
|
|
|
name => $name . '-month', |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $year = _year_dropdown( |
54
|
|
|
|
|
|
|
$c, |
55
|
|
|
|
|
|
|
startyear => $field->{startyear}, |
56
|
4
|
50
|
|
|
|
3416
|
%{ $field->{attributes} || {} }, |
|
4
|
|
|
|
|
31
|
|
57
|
|
|
|
|
|
|
%local_params, |
58
|
|
|
|
|
|
|
year => $selected->year, |
59
|
|
|
|
|
|
|
id => $id . '-year', |
60
|
|
|
|
|
|
|
name => $name . '-year', |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
4
|
|
50
|
|
|
2265
|
my $format = $params{format} || $field->{format} || '%Y-%m-%d'; |
64
|
4
|
|
|
|
|
16
|
$format =~ s/-//g; |
65
|
4
|
|
|
|
|
15
|
$format =~ s/\%Y/$year/g; |
66
|
4
|
|
|
|
|
45
|
$format =~ s/\%m/$month/g; |
67
|
4
|
|
|
|
|
50
|
$format =~ s/\%d/$day/g; |
68
|
|
|
|
|
|
|
|
69
|
4
|
|
|
|
|
59
|
return $format; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _day_dropdown { |
73
|
4
|
|
|
4
|
|
117
|
my ($c, %params) = @_; |
74
|
|
|
|
|
|
|
|
75
|
4
|
|
|
|
|
29
|
$c->param( $params{name}, '' ); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my @days = map{ |
78
|
4
|
100
|
|
|
|
67
|
$c->param( $params{name} => sprintf("%02d", $_) ) if $_ == $params{day}; |
|
124
|
|
|
|
|
230
|
|
79
|
124
|
|
|
|
|
366
|
[ $_ => sprintf("%02d", $_) ] |
80
|
|
|
|
|
|
|
}(1 .. 31); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $select = $c->select_field( |
83
|
|
|
|
|
|
|
$params{name}, |
84
|
|
|
|
|
|
|
\@days, |
85
|
|
|
|
|
|
|
id => $params{id}, |
86
|
4
|
50
|
|
|
|
12
|
%{ $params{attrs} || {} }, |
|
4
|
|
|
|
|
63
|
|
87
|
|
|
|
|
|
|
); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _month_dropdown { |
91
|
4
|
|
|
4
|
|
39
|
my ($c, %params) = @_; |
92
|
|
|
|
|
|
|
|
93
|
4
|
|
|
|
|
21
|
$c->param( $params{name}, '' ); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my @months = map{ |
96
|
4
|
100
|
|
|
|
59
|
$c->param( $params{name} => sprintf("%02d", $_) ) if $_ == $params{month}; |
|
48
|
|
|
|
|
101
|
|
97
|
48
|
|
|
|
|
185
|
[ $_ => sprintf("%02d", $_) ] |
98
|
|
|
|
|
|
|
}(1 .. 12); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $select = $c->select_field( |
101
|
|
|
|
|
|
|
$params{name}, |
102
|
|
|
|
|
|
|
\@months, |
103
|
|
|
|
|
|
|
id => $params{id}, |
104
|
4
|
50
|
|
|
|
11
|
%{ $params{attrs} || {} }, |
|
4
|
|
|
|
|
34
|
|
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub _year_dropdown { |
109
|
4
|
|
|
4
|
|
39
|
my ($c, %params) = @_; |
110
|
|
|
|
|
|
|
|
111
|
4
|
|
|
|
|
12
|
my $now = localtime; |
112
|
4
|
|
66
|
|
|
207
|
my $base = ( $params{startyear} // $now->year ); |
113
|
4
|
|
50
|
|
|
23
|
my $start = $base - ( $params{past} // 1 ); |
114
|
4
|
|
50
|
|
|
34
|
my $stop = $base + ( $params{future} // 5 ); |
115
|
|
|
|
|
|
|
|
116
|
4
|
|
|
|
|
21
|
$c->param( $params{name}, '' ); |
117
|
|
|
|
|
|
|
|
118
|
4
|
|
|
|
|
55
|
my $has_year; |
119
|
|
|
|
|
|
|
my @years = map{ |
120
|
4
|
100
|
|
|
|
33
|
$has_year++ if $_ == $params{year}; |
|
28
|
|
|
|
|
60
|
|
121
|
28
|
100
|
|
|
|
79
|
$c->param( $params{name} => $params{year} ) if $_ == $params{year}; |
122
|
28
|
|
|
|
|
126
|
[ $_ => $_ ] |
123
|
|
|
|
|
|
|
}($start .. $stop); |
124
|
|
|
|
|
|
|
|
125
|
4
|
50
|
|
|
|
13
|
if ( !$has_year ) { |
126
|
0
|
|
|
|
|
0
|
unshift @years, [ $params{year} => $params{year}, { selected => 'selected' } ]; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $select = $c->select_field( |
130
|
|
|
|
|
|
|
$params{name}, |
131
|
|
|
|
|
|
|
\@years, |
132
|
|
|
|
|
|
|
id => $params{id}, |
133
|
4
|
50
|
|
|
|
11
|
%{ $params{attrs} || {} }, |
|
4
|
|
|
|
|
38
|
|
134
|
|
|
|
|
|
|
); |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
__END__ |