* Add enclair_db option to UserDB.pm. Allows logging of enclair password
[interchange.git] / code / Widget / time.widget
1 # Copyright 2005-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 # $Id: time.widget,v 1.3 2007-03-30 23:40:58 pajamian Exp $
9
10 CodeDef time Widget 1
11 CodeDef time Description Time selector
12 CodeDef time Routine <<EOR
13 sub {
14         my($opt) = @_;
15
16         my $name = $opt->{name};
17         my $val  = $opt->{value};
18
19         my $now;
20         if($opt->{time} and $opt->{time_adjust} =~ /([-+]?)(\d+)/) {
21                 my $sign = $1 || '+';
22                 my $adjust = $2;
23                 $adjust *= 3600;
24                 $now = time;
25                 $now += $sign eq '+' ? $adjust : -$adjust;
26         }
27
28         my $sel_extra;
29         my $opt_extra;
30         for(qw/ class style extra /) {
31                 my $stag = "select_$_";
32                 my $otag = "option_$_";
33                 my $selapp;
34                 my $optapp;
35
36                 if($_ eq 'extra') {
37                         $selapp = " $opt->{$stag}";
38                         $optapp = " $opt->{$otag}";
39                 }
40                 else {
41                         $selapp = qq{ $_="$opt->{$stag}"};
42                         $optapp = qq{ $_="$opt->{$otag}"};
43                 }
44                 $sel_extra .= $opt->{$stag} ? $selapp : '';
45                 $opt_extra .= $opt->{$otag} ? $optapp : '';
46         }
47
48         my @t = localtime($now || time);
49         if (not $val) {
50                 $t[2]++ if $t[2] < 23;
51                 $val = POSIX::strftime("%H00", @t);
52         }
53
54         my $sel = 0;
55         my $out = qq{<SELECT NAME="$name"$sel_extra>};
56         my $o;
57         if ($opt->{blank}) {
58                 $out .= '<OPTION VALUE="0"$opt_extra>------</OPTION>';
59         }
60
61         $val = Vend::Form::round_to_fifteen($val);
62
63         if ($opt->{blank}) {
64                 $out .= '<OPTION VALUE="0"$opt_extra>--:--</OPTION>';
65         }
66         
67         my $ampm = defined $opt->{ampm} ? $opt->{ampm} : 1;
68         my $mod = '';
69         undef $sel;
70         my %special = qw/ 0 midnight 12 noon /;
71         
72         my @min;
73
74         $opt->{minutes} ||= '';
75
76         if($opt->{minutes} =~ /half/i) {
77                 @min = (0,30);
78         }
79         elsif($opt->{minutes} =~ /hourly/i) {
80                 @min = (0);
81         }
82         elsif($opt->{minutes} =~ /ten/i) {
83                 @min = (0,10,20,30,40,50);
84         }
85         elsif($opt->{minutes} =~ /[\0,]/) {
86                 @min = grep /^\d+$/ && $_ <= 59, split /[\0,\s]+/, $opt->{minutes};
87         }
88         else {
89                 @min = (0,15,30,45);
90         }
91
92         $opt->{start_hour} ||= 0;
93         for(qw/start_hour end_hour/) {
94                 $opt->{$_} = int(abs($opt->{$_}));
95                 if($opt->{$_} > 23) {
96                         $opt->{$_} = 0;
97                 }
98         }
99         $opt->{start_hour}      ||= 0;
100         $opt->{end_hour}        ||= 23;
101         
102         for my $hr ( $opt->{start_hour} .. $opt->{end_hour} ) {
103                 next if defined $opt->{start_hour} and $hr < $opt->{start_hour};
104                 next if defined $opt->{end_hour} and $hr > $opt->{end_hour};
105                 for my $min ( @min ) {
106                         my $disp_hour = $hr;
107                         if($opt->{ampm}) {
108                                 if( $hr < 12) {
109                                         $mod = 'am';
110                                 }
111                                 else {
112                                         $mod = 'pm';
113                                         $disp_hour = $hr - 12 unless $hr == 12;
114                                 }
115                                 $mod = errmsg($mod);
116                                 $mod = " $mod";
117                         }
118                         if($special{$hr} and $min == 0) {
119                                 $disp_hour = errmsg($special{$hr});
120                         }
121                         elsif($ampm) {
122                                 $disp_hour = sprintf("%2d:%02d%s", $disp_hour, $min, $mod);
123                         }
124                         else {
125                                 $disp_hour = sprintf("%02d:%02d", $hr, $min);
126                         }
127                         my $time = sprintf "%02d%02d", $hr, $min;
128                         $o = sprintf qq{<OPTION VALUE="%s"$opt_extra>%s}, $time, $disp_hour;
129                         ($out .= $o, next) unless ! $sel and $val;
130 #::logDebug("prospect=$time actual=$val");
131                         $o =~ s/>/ SELECTED>/ && $sel++
132                                 if $val eq $time;
133                         $out .= $o;
134                 }
135         }
136         $out .= "</SELECT>";
137         return $out;
138 }
139 EOR