blib/lib/Embperl/Form/Control/datetime.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 92 MC/DCmcdc 0.0% 0 / 54 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 363 CC 30 SCAR 45.0
CC
30
Cov
0%
get_display_text
210.0
str2time
90.0
prepare_fdat
30.0
CRAP 90.1
1  
2 ###################################################################################
3 #
4 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
5 # Embperl - Copyright (c) 2008-2015 Gerald Richter
6 # Embperl - Copyright (c) 2015-2023 actevy.io
7 #
8 # You may distribute under the terms of either the GNU General Public
9 # License or the Artistic License, as specified in the Perl README file.
10 #
11 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
12 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 # $Id$
16 #
17 ###################################################################################
18  
19 package Embperl::Form::Control::datetime ;
20  
21 use strict ;
22 use base 'Embperl::Form::Control::number' ;
23  
24 use Embperl::Inline ;
25 use POSIX qw(strftime);
26 use Time::Local qw(timelocal_nocheck timegm_nocheck);
27 use Date::Calc qw{Delta_DHMS Add_Delta_Days} ;
28  
29 use vars qw{%fdat} ;
30  
31 our $tz_local = (timegm_nocheck(localtime())-time())/60;
32  
33  
34 # ---------------------------------------------------------------------------
35 #
36 # init - init the new control
37 #
38  
39 sub init
40  
41 {
42 my ($self) = @_ ;
43  
44 $self->{unit} ||= '' ;
45  
46
47 return $self ;
48 }
49
50 # ------------------------------------------------------------------------------------------
51 #
52 # get_display_text - returns the text that should be displayed
53 #
54  
55 sub get_display_text
56 {
57 my ($self, $req, $time) = @_ ;
58
59 $time = $self -> get_value ($req) if (!defined ($time)) ;
60 return $time if ($self -> {format} eq '-' || ($time =~ /\./)) ;
61 return if ($time eq '' && !exists $self -> {onempty}) ;
62  
63 if ($self -> {dynamic} && ($time =~ /^\s*((?:s|i|h|d|w|m|y|q)(?:\+|-)?(?:\d+)?)\s*/))
64 {
65 return $time ;#$1 ;
66 }
67
68  
69 my ($y, $m, $d, $h, $min, $s, $z) ;
70  
71 if ($self -> {onempty})
72 {
73 ($s,$min,$h,$d,$m,$y) = localtime ;
74 $m++ ;
75 $y += 1900 ;
76 if ($self -> {onempty} eq 'b')
77 {
78 $h = $min = $s = 0 ;
79 }
80 elsif ($self -> {onempty} eq 'e')
81 {
82 $h = 23 ;
83 $min = 59 ;
84 $s = 59 ;
85 }
86 }
87 else
88 {
89 ($y, $m, $d, $h, $min, $s, $z) = (($time . '00000000000000Z') =~ /^(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(.)/) ;
90 }
91  
92 my $date ;
93 if ($time =~ /^(\d\d\d\d)-(\d+)$/)
94 {
95 $date = $time ;
96 }
97 elsif ($d == 0 && $m == 0)
98 {
99 $date = $y ;
100 }
101 elsif ($d == 0)
102 {
103 $date = "$m.$y" ;
104 }
105 else
106 {
107 # Getting the local timezone
108  
109 $date = eval
110 {
111 my @time = gmtime(timegm_nocheck($s,$min,$h,$d,$m-1,$y-1900)+($tz_local*60));
112  
113 my $format = $self -> {notime} || ($s == 0 && $h == 0 && $min == 0)?'%d.%m.%Y':'%d.%m.%Y, %H:%M' ;
114 $format = '%d.%m.%Y, %H:%M:%S' if ($self -> {fulltime}) ;
115 strftime ($format, @time[0..5]) ;
116 } ;
117 }
118  
119 if ($time && !$date && ($time =~ /\d+\.\d+\.\d+/))
120 {
121 $date = $time ;
122 }
123  
124 return $date ;
125 }
126  
127  
128 # ------------------------------------------------------------------------------------------
129 #
130 # get_sort_value - returns the value that should be used to sort
131 #
132  
133 sub get_sort_value
134 {
135 my ($self, $req, $value) = @_ ;
136
137 $value = $self -> get_value ($req) if (!defined ($value)) ;
138 return $value ;
139 }
140
141 # ------------------------------------------------------------------------------------------
142 #
143 # init_data - daten aufteilen
144 #
145  
146 sub init_data
147 {
148 my ($self, $req, $parentctrl, $force) = @_ ;
149  
150 my $fdat = $req -> {docdata} || \%fdat ;
151 my $name = $self->{name} ;
152 my $time = $fdat->{$name} ;
153  
154 return if (($time eq '' && !exists $self -> {onempty}) || $self -> {format} eq '-' || ($req -> {"ef_datetime_init_done_$name"} && !$force)) ;
155  
156 $fdat->{$name} = $self -> get_display_text ($req, $time) ;
157 $req -> {"ef_datetime_init_done_$name"} = 1 ;
158 }
159  
160 # ---------------------------------------------------------------------------
161 #
162 # init_markup - add any dynamic markup to the form data
163 #
164  
165 sub init_markup
166  
167 {
168 my ($self, $req, $parentctl, $method) = @_ ;
169  
170 return if (!$self -> is_readonly($req) && (! $parentctl || ! $parentctl -> is_readonly($req))) ;
171
172 return $self -> init_data ($req, $parentctl) ;
173 }
174  
175  
176 # ---------------------------------------------------------------------------
177 #
178 # str2time
179 #
180  
181 sub str2time
182  
183 {
184 my ($date) = @_ ;
185  
186 my ($year, $mon, $day, $hour, $min, $sec) ;
187 if ($date eq '*' || $date eq '.')
188 {
189 my $offset ||= 0 ;
190 ($sec, $min, $hour, $day, $mon, $year) = gmtime (time + $offset) ;
191 $year += 1900 ;
192 $mon++ ;
193 }
194 else
195 {
196 $date =~ tr/,;/ / ;
197 my ($d, $t) = split (/\s+/, $date) ;
198 if ($d =~ /:/)
199 {
200 $t = $d ;
201 $d = '' ;
202 }
203 ($day, $mon, $year) = map { $_ + 0 } split (/\./, $d) ;
204 ($hour, $min, $sec) = map { $_ + 0 } split (/\:/, $t) ;
205  
206 if ($year == 0 || $mon == 0 || $day == 0)
207 {
208 my ($s, $min, $h, $md, $m, $y) = localtime ;
209  
210 $day ||= $md ;
211 $mon ||= $m + 1;
212 $year ||= $y + 1900 ;
213 }
214  
215 if ($year < 70)
216 {
217 $year += 2000 ;
218 }
219 elsif ($year >= 70 && $year < 100)
220 {
221 $year += 1900 ;
222 }
223 if ($year < 1907)
224 {
225 $year = $year % 100 + 2000 ;
226 }
227  
228 ($year,$mon,$day, $hour,$min,$sec) =
229 Date::Calc::Add_Delta_DHMS($year,$mon,$day, $hour,$min,$sec,
230 0, 0, -$tz_local, 0) if ($hour || $min || $sec) ;
231 }
232  
233 return $year?sprintf ('%04d%02d%02d%02d%02d%02dZ', $year, $mon, $day, $hour, $min, $sec):'' ;
234 }
235  
236  
237 # ------------------------------------------------------------------------------------------
238 #
239 # prepare_fdat - daten zusammenfuehren
240 #
241  
242 sub prepare_fdat
243 {
244 my ($self, $req) = @_ ;
245  
246 return if ($self -> is_readonly ($req) || $self -> {format} eq '-') ;
247
248 my $fdat = $req -> {form} || \%fdat ;
249 my $name = $self->{name} ;
250 return if (!exists $fdat->{$name}) ;
251 my $date = $fdat -> {$name} ;
252 return if ($date eq '') ;
253  
254 if ($self -> {dynamic} && ($date =~ /^\s*((?:s|i|h|d|w|m|y|q)\s*(?:\+|-)?\s*(?:\d+)?)\s*/))
255 {
256 $fdat->{$name} = $date ; #$1 ;
257 $fdat->{$name} =~ s/\s//g ;
258 return ;
259 }
260
261
262 $fdat -> {$name} = str2time ($date) ;
263 }
264  
265 # ---------------------------------------------------------------------------
266 #
267 # get_validate_auto_rules - get rules for validation, in case user did
268 # not specify any
269 #
270  
271 sub get_validate_auto_rules
272 {
273 my ($self, $req) = @_ ;
274
275 return [ $self -> {required}?(required => 1):(emptyok => 1), -type => 'DateTime' ] ;
276 }
277  
278 1 ;
279  
280 __EMBPERL__
281  
282  
283 [# ---------------------------------------------------------------------------
284 #
285 # show_control - output the control
286 #]
287  
288 [$ sub show_control ($self)
289  
290 $self -> {size} ||= 80 / ($self -> {width} || 2) ;
291 my $class = $self -> {class} ||= '' ;
292 my $fullid = $req -> {uuid} . '_' . $self ->{id} ;
293 $]
294  
295 <input type="text" name="[+ $self -> {force_name} || $self -> {name} +]" [+ do { local $escmode = 0 ; $self -> get_std_control_attr($req, $fullid) } +]
296 [$if $self -> {size} $]size="[+ $self->{size} +]"[$endif$]
297 [$if $self -> {maxlength} $]maxlength="[+ $self->{maxlength} +]"[$endif$]
298 _ef_attach="ef_datetime" _ef_dynamic="[+ $self -> {dynamic}?'true':'' +]"
299 >
300 [#
301 <script type="text/javascript">
302 $('#[+ $fullid +]').datepicker ({ showWeek: true,
303 [$if $self -> {dynamic} $]constrainInput: false, [$endif$]
304 showButtonPanel: true
305 }) ;
306 </script>
307 #]
308  
309 [$endsub$]
310  
311  
312 __END__