1<?php
2
3require_once dirname(__FILE__).'/accesscheck.php';
4
5if (!defined('IN_WEBBLER') && !defined('WEBBLER')) {
6    class date
7    {
8        public $type = 'date';
9        public $name = '';
10        public $description = 'Date';
11        public $days = array();
12        public $months = array();
13        public $useTime = false;
14
15        public function __construct($name = '')
16        {
17            $this->days = array(
18                $GLOBALS['I18N']->get('Sunday'),
19                $GLOBALS['I18N']->get('Monday'),
20                $GLOBALS['I18N']->get('Tuesday'),
21                $GLOBALS['I18N']->get('Wednesday'),
22                $GLOBALS['I18N']->get('Thursday'),
23                $GLOBALS['I18N']->get('Friday'),
24                $GLOBALS['I18N']->get('Saturday'),
25            );
26            $this->months = array(
27                '01' => $GLOBALS['I18N']->get('January'),
28                '02' => $GLOBALS['I18N']->get('February'),
29                '03' => $GLOBALS['I18N']->get('March'),
30                '04' => $GLOBALS['I18N']->get('April'),
31                '05' => $GLOBALS['I18N']->get('May'),
32                '06' => $GLOBALS['I18N']->get('June'),
33                '07' => $GLOBALS['I18N']->get('July'),
34                '08' => $GLOBALS['I18N']->get('August'),
35                '09' => $GLOBALS['I18N']->get('September'),
36                '10' => $GLOBALS['I18N']->get('October'),
37                '11' => $GLOBALS['I18N']->get('November'),
38                '12' => $GLOBALS['I18N']->get('December'),
39            );
40            $this->name = $name;
41            $this->getDate();
42            $this->getTime();
43        }
44
45        public function setTime($time)
46        {
47            if (strpos($time, ':')) {
48                list($hr, $min, $sec) = explode(':', $time);
49            } else {
50                $hr = date('h');
51                $min = date('j');
52                $sec = date('s');
53            }
54            if (!isset($_REQUEST[$this->name]) || !is_array($_REQUEST[$this->name])) {
55                $_REQUEST[$this->name] = array();
56            }
57            $_REQUEST[$this->name]['hour'] = $hr;
58            $_REQUEST[$this->name]['minute'] = $min;
59        }
60
61        public function setDateTime($datetime)
62        {
63            //0000-00-00 00:00:00
64            list($date, $time) = explode(' ', $datetime);
65            $this->setDate($date);
66            $this->setTime($time);
67        }
68
69        public function setDate($date)
70        {
71            list($year, $month, $day) = explode('-', $date);
72            if (!isset($_REQUEST[$this->name]) || !is_array($_REQUEST[$this->name])) {
73                $_REQUEST[$this->name] = array();
74            }
75            $_REQUEST[$this->name]['year'] = $year;
76            $_REQUEST[$this->name]['month'] = $month;
77            $_REQUEST[$this->name]['day'] = $day;
78        }
79
80        public function getDate($value = '')
81        {
82            if (!$value) {
83                $value = $this->name;
84            }
85            if (!$value) {
86                $return = date('Y-m-d');
87            }
88            if (isset($_REQUEST[$value]['year']) && is_array($_REQUEST[$value]) && isset($_REQUEST[$value]['month']) && isset($_REQUEST[$value]['day'])) {
89                $return = sprintf('%04d-%02d-%02d', $_REQUEST[$value]['year'], $_REQUEST[$value]['month'],
90                    $_REQUEST[$value]['day']);
91            } else {
92                $return = date('Y-m-d');
93            }
94            // print "Date ".$value.' '.$return;
95            return $return;
96        }
97
98        public function getTime($value = '')
99        {
100            if (!$value) {
101                $value = $this->name;
102            }
103            if (isset($_REQUEST[$value]['hour']) && isset($_REQUEST[$value]['minute'])) {
104                return sprintf('%02d:%02d', $_REQUEST[$value]['hour'], $_REQUEST[$value]['minute']);
105            } else {
106                return date('H:i');
107            }
108        }
109
110        public function showInput($name, $fielddata, $value, $document_id = 0)
111        {
112            if (!$name) {
113                $name = $this->name;
114            }
115            //    dbg("$name $fielddata $value $document_id");
116            if (!is_array($value)) {
117                $year = substr($value, 0, 4);
118                $month = substr($value, 5, 2);
119                $day = substr($value, 8, 2);
120                $hour = substr($value, 11, 2);
121                $minute = substr($value, 14, 2);
122            } else {
123                $year = $value['year'];
124                $month = $value['month'];
125                $day = $value['day'];
126                $hour = $value['hour'];
127                $minute = $value['minute'];
128            }
129
130            if (!$day && !$month && !$year) {
131                $now = getdate(time());
132                $day = $now['mday'];
133                $month = $now['mon'];
134                $year = $now['year'];
135            }
136            $html = '<div class="date">';
137
138            $html .= "
139      <!-- $day / $month / $year -->".'
140     <select name="' .$name.'[day]">';
141            for ($i = 1; $i < 32; ++$i) {
142                $sel = '';
143                if ($i == $day) {
144                    $sel = 'selected="selected"';
145                }
146                $html .= sprintf('
147        <option value="%d" %s>%s</option>', $i, $sel, $i);
148            }
149            $html .= '
150      </select>
151      <select name="' .$name.'[month]">';
152            reset($this->months);
153            foreach ($this->months as $key => $val) {
154                $sel = '';
155                if ($key == $month) {
156                    $sel = 'selected="selected"';
157                }
158                $html .= sprintf('
159            <option value="%s" %s>%s</option>', $key, $sel, $val);
160            }
161            if ($year < 1800) {
162                $year = date('Y');
163            }
164            if (DATE_START_YEAR) {
165                $start = DATE_START_YEAR;
166            } else {
167                $start = $year - 3;
168            }
169            if (DATE_END_YEAR) {
170                $end = DATE_END_YEAR;
171            } else {
172                $end = $year + 10;
173            }
174
175            $html .= '
176      </select>
177      <select name="' .$name.'[year]">';
178            for ($i = $start; $i <= $end; ++$i) {
179                $html .= '
180          <option ';
181                if ($i == $year) {
182                    $html .= 'selected="selected"';
183                }
184                $html .= ">$i</option>";
185            }
186            $html .= '
187      </select>';
188            if ($this->useTime) {
189                $html .= '
190      <select name="' .$name.'[hour]">';
191                for ($i = 0; $i <= 23; ++$i) {
192                    $sel = '';
193                    if ($i == $hour) {
194                        $sel = 'selected="selected"';
195                    }
196                    $html .= sprintf('
197          <option value="%d" %s>%02d</option>', $i, $sel, $i);
198                }
199                $html .= '
200        </select>';
201                $html .= '
202        <select name="' .$name.'[minute]">';
203                for ($i = 0; $i <= 59; $i += 15) {
204                    $sel = '';
205                    if ($i == $minute) {
206                        $sel = 'selected="selected"';
207                    }
208                    $html .= sprintf('
209          <option value="%d" %s>%02d</option>', $i, $sel, $i);
210                }
211                $html .= '
212        </select>';
213            }
214
215            return $html.'</div>';
216        }
217
218        public function display($parent, $data, $leaf, $branch)
219        {
220            global $config;
221
222            return formatDate($data);
223        }
224
225        public function store($itemid, $fielddata, $value, $table)
226        {
227            Sql_query(sprintf('replace into %s values("%s",%d,"%s")', $table, $fielddata['name'], $itemid,
228                $this->getDate($value)));
229        }
230    }
231}
232