2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('form.TextField');
30 import('DateAndTime');
32 class DateField extends TextField {
33 var $mWeekStartDay = 0;
34 var $mDateFormat = "d/m/Y";
35 var $lToday = "Today";
38 var $lCalendarButtons = array('today'=>'Today', 'close'=>'Close');
40 function __construct($name) {
41 $this->class = 'DateField';
43 $this->mDateObj = new DateAndTime();
45 if (isset($GLOBALS["I18N"])) {
46 $this->localize($GLOBALS["I18N"]);
50 function localize($i18n) {
53 $this->mDateObj->setFormat($user->date_format);
55 $this->mMonthNames = $i18n->monthNames;
56 $this->mWeekDayShortNames = $i18n->weekdayShortNames;
57 $this->lToday = $i18n->getKey('label.today');
58 $this->lCalendarButtons['today'] = $i18n->getKey('label.today');
59 $this->lCalendarButtons['close'] = $i18n->getKey('button.close');
61 $this->mDateFormat = $user->date_format;
62 $this->mWeekStartDay = $user->week_start;
65 // set current value taken from session or database
66 function setValueSafe($value) {
67 if (isset($value) && (strlen($value) > 0)) {
68 $this->mDateObj->parseVal($value, DB_DATEFORMAT);
69 $this->value = $this->mDateObj->toString($this->mDateFormat); //?
72 // get value for storing in session or database
73 function getValueSafe() {
74 if (strlen($this->value)>0) {
75 $this->mDateObj->parseVal($this->value, $this->mDateFormat); //?
76 return $this->mDateObj->toString(DB_DATEFORMAT);
84 if (!$this->isEnabled()) {
85 $html = htmlspecialchars($this->getValue()).
86 "<input type=\"hidden\" name=\"$this->name\" value=\"".htmlspecialchars($this->getValue())."\">\n";
89 if ($this->id=="") $this->id = $this->name;
93 // http://www.nsftools.com/tips/JavaScriptTips.htm#datepicker
97 .dpTable {font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 12px; text-align: center; color: #505050; background-color: #ece9d8; border: 1px solid #AAAAAA;}
102 .dpTD {border: 1px solid #ece9d8;}
103 .dpDayHighlightTD {background-color: #CCCCCC;border: 1px solid #AAAAAA;}
104 .dpTDHover {background-color: #aca998;border: 1px solid #888888;cursor: pointer;color: red;}
108 .dpDayTD {background-color: #CCCCCC;border: 1px solid #AAAAAA;color: white;}
109 .dpTitleText {font-size: 12px;color: gray;font-weight: bold;}
110 .dpDayHighlight {color: 4060ff;font-weight: bold;}
111 .dpButton {font-family: Verdana, Tahoma, Arial, Helvetica, sans-serif;font-size: 10px;color: gray;background: #d8e8ff;font-weight: bold;padding: 0px;}
112 .dpTodayButton {font-family: Verdana, Tahoma, Arial, Helvetica, sans-serif;font-size: 10px;color: gray; background: #d8e8ff;font-weight: bold;}
115 var datePickerDivID = \"datepicker\";
116 var iFrameDivID = \"datepickeriframe\";
118 var dayArrayShort = new Array('".join("','",$this->mWeekDayShortNames)."');
119 var dayArrayMed = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
120 var dayArrayLong = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
121 var monthArrayShort = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
122 var monthArrayMed = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
123 var monthArrayLong = new Array('".join("','",$this->mMonthNames)."');
125 var defaultDateSeparator = \"".$this->mDateFormat[1]."\";
126 var defaultDateFormat = \"".$this->mDateFormat."\";
127 var dateSeparator = defaultDateSeparator;
128 var dateFormat = defaultDateFormat;
129 var startWeek = ".$this->mWeekStartDay.";
134 function getStartWeekDayNumber(date) {
135 var res = date.getDay() - startWeek;
142 function displayDatePicker(dateFieldName, displayBelowThisObject, dtFormat, dtSep) {
143 var targetDateField = document.getElementsByName(dateFieldName).item(0);
145 if (!displayBelowThisObject) displayBelowThisObject = targetDateField;
147 dateSeparator = dtSep;
149 dateSeparator = defaultDateSeparator;
152 dateFormat = dtFormat;
154 dateFormat = defaultDateFormat;
156 var x = displayBelowThisObject.offsetLeft;
157 var y = displayBelowThisObject.offsetTop + displayBelowThisObject.offsetHeight ;
159 var parent = displayBelowThisObject;
160 while (parent.offsetParent) {
161 parent = parent.offsetParent;
162 x += parent.offsetLeft;
163 y += parent.offsetTop ;
166 drawDatePicker(targetDateField, x, y);
168 function drawDatePicker(targetDateField, x, y) {
169 var dt = getFieldDate(targetDateField.value );
171 if (!document.getElementById(datePickerDivID)) {
172 var newNode = document.createElement(\"div\");
173 newNode.setAttribute(\"id\", datePickerDivID);
174 newNode.setAttribute(\"class\", \"dpDiv\");
175 newNode.setAttribute(\"style\", \"visibility: hidden;\");
176 document.body.appendChild(newNode);
179 var pickerDiv = document.getElementById(datePickerDivID);
180 pickerDiv.style.position = \"absolute\";
181 pickerDiv.style.left = x + \"px\";
182 pickerDiv.style.top = (y + 3) + \"px\";
183 pickerDiv.style.visibility = (pickerDiv.style.visibility == \"visible\" ? \"hidden\" : \"visible\");
184 pickerDiv.style.display = (pickerDiv.style.display == \"block\" ? \"none\" : \"block\");
185 pickerDiv.style.zIndex = 10000;
187 refreshDatePicker(targetDateField.name, dt.getFullYear(), dt.getMonth(), dt.getDate());
189 function refreshDatePicker(dateFieldName, year, month, day) {
190 var thisDay = new Date();
192 if ((month >= 0) && (year > 0)) {
193 thisDay = new Date(year, month, 1);
195 day = thisDay.getDate();
199 var crlf = \"\\r\\n\";
200 var TABLE = \"<table cols=7 class='dpTable'>\" + crlf;
201 var xTABLE = \"</table>\" + crlf;
202 var TR = \"<tr class='dpTR'>\";
203 var TR_title = \"<tr class='dpTitleTR' width='150' align='center'>\";
204 var TR_days = \"<tr class='dpDayTR'>\";
205 var TR_todaybutton = \"<tr class='dpTodayButtonTR'>\";
206 var xTR = \"</tr>\" + crlf;
207 var TD = \"<td class='dpTD' onMouseOut='this.className=\\\"dpTD\\\";' onMouseOver=' this.className=\\\"dpTDHover\\\";' \";
208 var TD_title = \"<td colspan=5 class='dpTitleTD'>\";
209 var TD_buttons = \"<td class='dpButtonTD' width='50'>\";
210 var TD_todaybutton = \"<td colspan=7 class='dpTodayButtonTD'>\";
211 var TD_days = \"<td class='dpDayTD'>\";
212 var TD_selected = \"<td class='dpDayHighlightTD' onMouseOut='this.className=\\\"dpDayHighlightTD\\\";' onMouseOver='this.className=\\\"dpTDHover\\\";' \";
213 var xTD = \"</td>\" + crlf;
214 var DIV_title = \"<div class='dpTitleText'>\";
215 var DIV_selected = \"<div class='dpDayHighlight'>\";
216 var xDIV = \"</div>\";
220 html += TR_title + '<td colspan=7>';
221 html += '<table width=\"250\">'+ TR_title;
222 html += TD_buttons + getButtonCodeYear(dateFieldName, thisDay, -1, \"<<\") + getButtonCode(dateFieldName, thisDay, -1, \"<\") + xTD;
223 html += TD_title + DIV_title + monthArrayLong[ thisDay.getMonth()] + \" \" + thisDay.getFullYear() + xDIV + xTD;
224 html += TD_buttons + getButtonCode(dateFieldName, thisDay, 1, \">\") + getButtonCodeYear(dateFieldName, thisDay, 1, \">>\") + xTD;
225 html += xTR + '</table>' + xTD;
229 for(i = 0; i < dayArrayShort.length; i++)
230 html += TD_days + dayArrayShort[(i + startWeek) % 7] + xTD;
235 //var startD = (thisDay.getDay()-startWeek<0?6:thisDay.getDay()-startWeek);
236 var startD = getStartWeekDayNumber(thisDay);
237 for (i = 0; i < startD; i++)
238 html += TD + \" \" + xTD;
241 dayNum = thisDay.getDate();
242 TD_onclick = \" onclick=\\\"updateDateField('\" + dateFieldName + \"', '\" + getDateString(thisDay) + \"');\\\">\";
245 html += TD_selected + TD_onclick + DIV_selected + dayNum + xDIV + xTD;
247 html += TD + TD_onclick + dayNum + xTD;
249 var startD = getStartWeekDayNumber(thisDay);
254 thisDay.setDate(thisDay.getDate() + 1);
255 } while (thisDay.getDate() > 1)
257 var startD = getStartWeekDayNumber(thisDay);
259 for (i = 6; i >= startD; i--) {
260 html += TD + \" \" + xTD;
265 var today = new Date();
266 var todayString = \"Today is \" + dayArrayMed[today.getDay()] + \", \" + monthArrayMed[ today.getMonth()] + \" \" + today.getDate();
267 html += TR_todaybutton + TD_todaybutton;
268 html += \"<button class='dpTodayButton' onClick=\\\"refreshDatePicker('\" + dateFieldName + \"'); updateDateFieldOnly('\" + dateFieldName + \"', '\" + getDateString(new Date()) + \"');\\\">".$this->lCalendarButtons['today']."</button> \";
269 html += \"<button class='dpTodayButton' onClick='updateDateField(\\\"\" + dateFieldName + \"\\\");'>".$this->lCalendarButtons['close']."</button>\";
274 document.getElementById(datePickerDivID).innerHTML = html;
279 function getButtonCode(dateFieldName, dateVal, adjust, label) {
280 var newMonth = (dateVal.getMonth () + adjust) % 12;
281 var newYear = dateVal.getFullYear() + parseInt((dateVal.getMonth() + adjust) / 12);
287 return \"<button class='dpButton' onClick='refreshDatePicker(\\\"\" + dateFieldName + \"\\\", \" + newYear + \", \" + newMonth + \");'>\" + label + \"</button>\";
290 function getButtonCodeYear(dateFieldName, dateVal, adjust, label) {
291 var newMonth = dateVal.getMonth();
292 var newYear = dateVal.getFullYear() + adjust;
294 return \"<button class='dpButton' onClick='refreshDatePicker(\\\"\" + dateFieldName + \"\\\", \" + newYear + \", \" + newMonth + \");'>\" + label + \"</button>\";
298 function getDateString(dateVal) {\n";
299 if (isset($GLOBALS['i18n'])) {
300 $html .= "dateVal.locale = \"".$GLOBALS['i18n']->lang."\";\n";
302 $html .= "return dateVal.strftime(dateFormat);
305 function getFieldDate(dateString) {
307 var dateVal = strptime(dateString, dateFormat);
309 dateVal = new Date();
311 if (dateVal == null) {
312 dateVal = new Date();
317 function splitDateString(dateString) {
319 if (dateString.indexOf(\"/\") >= 0)
320 dArray = dateString.split(\"/\");
321 else if (dateString.indexOf(\".\") >= 0)
322 dArray = dateString.split(\".\");
323 else if (dateString.indexOf(\"-\") >= 0)
324 dArray = dateString.split(\"-\");
325 else if (dateString.indexOf(\"\\\\\") >= 0)
326 dArray = dateString.split(\"\\\\\");
333 function updateDateField(dateFieldName, dateString) {
334 var targetDateField = document.getElementsByName(dateFieldName).item(0);
336 targetDateField.value = dateString;
338 var pickerDiv = document.getElementById(datePickerDivID);
339 pickerDiv.style.visibility = \"hidden\";
340 pickerDiv.style.display = \"none\";
343 targetDateField.focus();
345 if ((dateString) && (typeof(datePickerClosed) == \"function\"))
346 datePickerClosed(targetDateField);
349 function updateDateFieldOnly(dateFieldName, dateString) {
350 var targetDateField = document.getElementsByName(dateFieldName).item(0);
352 targetDateField.value = dateString;
355 function adjustiFrame(pickerDiv, iFrameDiv) {
356 var is_opera = (navigator.userAgent.toLowerCase().indexOf(\"opera\") != -1);
361 if (!document.getElementById(iFrameDivID)) {
362 var newNode = document.createElement(\"iFrame\");
363 newNode.setAttribute(\"id\", iFrameDivID);
364 newNode.setAttribute(\"src\", \"javascript:false;\");
365 newNode.setAttribute(\"scrolling\", \"no\");
366 newNode.setAttribute (\"frameborder\", \"0\");
367 document.body.appendChild(newNode);
371 pickerDiv = document.getElementById(datePickerDivID);
373 iFrameDiv = document.getElementById(iFrameDivID);
376 iFrameDiv.style.position = \"absolute\";
377 iFrameDiv.style.width = pickerDiv.offsetWidth;
378 iFrameDiv.style.height = pickerDiv.offsetHeight ;
379 iFrameDiv.style.top = pickerDiv.style.top;
380 iFrameDiv.style.left = pickerDiv.style.left;
381 iFrameDiv.style.zIndex = pickerDiv.style.zIndex - 1;
382 iFrameDiv.style.visibility = pickerDiv.style.visibility ;
383 iFrameDiv.style.display = pickerDiv.style.display;
390 $html .= "</script>\n";
392 $html .= "\n\t<input type=\"text\"";
393 $html .= " name=\"$this->name\" id=\"$this->id\"";
396 $html .= " size=\"$this->size\"";
398 if ($this->style!="")
399 $html .= " style=\"$this->style\"";
401 $html .= " maxlength=\"50\"";
403 if ($this->on_change!="")
404 $html .= " onchange=\"$this->on_change\"";
406 if ($this->on_click!="")
407 $html .= " onclick=\"$this->on_click\"";
409 $html .= " value=\"".htmlspecialchars($this->getValue())."\"";
413 $app_root = '/'.APP_NAME;
415 $html .= " <img src=\"".$app_root."/images/calendar.gif\" width=\"16\" height=\"16\" onclick=\"displayDatePicker('".$this->name."');\">\n";