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