1: <?php
2: 3: 4: 5: 6:
7:
8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class QDateTimePicker extends QControl {
28:
29:
30:
31:
32:
33: protected $dttDateTime = null;
34: protected $strDateTimePickerType = QDateTimePickerType::Date;
35: protected $strDateTimePickerFormat = QDateTimePickerFormat::MonthDayYear;
36:
37: protected $intMinimumYear = 1970;
38: protected $intMaximumYear = 2020;
39:
40: protected $intSelectedMonth = null;
41: protected $intSelectedDay = null;
42: protected $intSelectedYear = null;
43:
44:
45:
46: protected $blnAllowBlankTime = true;
47:
48: protected $blnAllowBlankDate = true;
49:
50: protected $strTimeSeparator = ':';
51:
52: protected $intSecondInterval = 1;
53:
54: protected $intMinuteInterval = 1;
55:
56: protected $intHourInterval = 1;
57:
58:
59:
60: protected $strJavaScripts = 'date_time_picker.js';
61: protected $strCssClass = 'datetimepicker';
62:
63:
64:
65:
66: public function ParsePostData() {
67:
68: $blnIsDateTimeSet = false;
69: if ($this->dttDateTime == null) {
70: $dttNewDateTime = QDateTime::Now();
71: } else {
72: $blnIsDateTimeSet = true;
73: $dttNewDateTime = $this->dttDateTime;
74: }
75:
76:
77: switch ($this->strDateTimePickerType) {
78: case QDateTimePickerType::Date:
79: case QDateTimePickerType::DateTime:
80: case QDateTimePickerType::DateTimeSeconds:
81: $strKey = $this->strControlId . '_lstMonth';
82: if (array_key_exists($strKey, $_POST)) {
83: $intMonth = $_POST[$strKey];
84: } else {
85: if ($blnIsDateTimeSet) {
86: $intMonth = $dttNewDateTime->Month;
87: } else {
88: $intMonth = null;
89: }
90: }
91:
92: $strKey = $this->strControlId . '_lstDay';
93: if (array_key_exists($strKey, $_POST)) {
94: $intDay = $_POST[$strKey];
95: } else {
96: if ($blnIsDateTimeSet) {
97: $intDay = $dttNewDateTime->Day;
98: } else {
99: $intDay = null;
100: }
101: }
102:
103: $strKey = $this->strControlId . '_lstYear';
104: if (array_key_exists($strKey, $_POST)) {
105: $intYear = $_POST[$strKey];
106: } else {
107: if ($blnIsDateTimeSet) {
108: $intYear = $dttNewDateTime->Year;
109: } else {
110: $intYear = null;
111: }
112: }
113:
114: $this->intSelectedMonth = $intMonth;
115: $this->intSelectedDay = $intDay;
116: $this->intSelectedYear = $intYear;
117:
118: if (strlen($intYear) && strlen($intMonth) && strlen($intDay)) {
119: $dttNewDateTime->setDate($intYear, $intMonth, $intDay);
120: } else {
121: $dttNewDateTime->Year = null;
122: }
123: break;
124: }
125:
126:
127: $blnIsTimeSet = false;
128: if(!$dttNewDateTime->IsTimeNull()){
129:
130: $blnIsTimeSet = true;
131: } else {
132:
133: $blnIsTimeSet = false;
134: }
135:
136: switch ($this->strDateTimePickerType) {
137: case QDateTimePickerType::Time:
138: case QDateTimePickerType::TimeSeconds:
139: case QDateTimePickerType::DateTime:
140: case QDateTimePickerType::DateTimeSeconds:
141: $strKey = $this->strControlId . '_lstHour';
142: if (array_key_exists($strKey, $_POST)) {
143: $intHour = $_POST[$strKey];
144: } else {
145: if($blnIsTimeSet){
146: $intHour = $dttNewDateTime->Hour;
147: } else {
148: $intHour = null;
149: }
150: }
151:
152: $strKey = $this->strControlId . '_lstMinute';
153: if (array_key_exists($strKey, $_POST)) {
154: $intMinute = $_POST[$strKey];
155: } else {
156: if($blnIsTimeSet){
157: $intMinute = $dttNewDateTime->Minute;
158: } else {
159: $intMinute = null;
160: }
161: }
162:
163: $intSecond = 0;
164:
165: if (($this->strDateTimePickerType == QDateTimePickerType::TimeSeconds) ||
166: ($this->strDateTimePickerType == QDateTimePickerType::DateTimeSeconds)
167: ) {
168: $strKey = $this->strControlId . '_lstSecond';
169: if (array_key_exists($strKey, $_POST)) {
170: $intSecond = $_POST[$strKey];
171: } else {
172: if ($blnIsTimeSet) {
173: $intSecond = $dttNewDateTime->Second;
174: } else {
175: $intSecond = 0;
176: }
177: }
178: }
179:
180: if (strlen($intHour) && strlen($intMinute) && strlen($intSecond)) {
181: $dttNewDateTime->setTime($intHour, $intMinute, $intSecond);
182: } else {
183: $dttNewDateTime->Hour = null;
184: }
185:
186: break;
187: }
188:
189: $this->dttDateTime = $dttNewDateTime;
190: }
191:
192: 193: 194: 195: 196:
197: protected function GetControlHtml() {
198:
199: $strCssClass = $this->strCssClass;
200: $this->strCssClass = '';
201: $strAttributes = $this->GetAttributes();
202: $this->strCssClass = $strCssClass;
203:
204: $strStyle = $this->GetStyleAttributes();
205: if ($strStyle)
206: $strAttributes .= sprintf(' style="%s"', $strStyle);
207:
208: $strCommand = sprintf(' onchange="Qcubed__DateTimePicker_Change(\'%s\', this);"', $this->strControlId);
209:
210: if ($this->dttDateTime) {
211: $dttDateTime = $this->dttDateTime;
212: } else {
213: $dttDateTime = new QDateTime();
214: }
215:
216: $strToReturn = '';
217:
218:
219: switch ($this->strDateTimePickerType) {
220: case QDateTimePickerType::Date:
221: case QDateTimePickerType::DateTime:
222: case QDateTimePickerType::DateTimeSeconds:
223:
224: $strMonthListbox = sprintf('<select name="%s_lstMonth" id="%s_lstMonth" class="month" %s%s>', $this->strControlId, $this->strControlId, $strAttributes, $strCommand);
225: if (!$this->blnRequired || $dttDateTime->IsDateNull())
226: if($this->blnAllowBlankDate)
227: $strMonthListbox .= '<option value="">--</option>';
228: for ($intMonth = 1; $intMonth <= 12; $intMonth++) {
229: if ((!$dttDateTime->IsDateNull() && ($dttDateTime->Month == $intMonth)) || ($this->intSelectedMonth == $intMonth))
230: $strSelected = ' selected="selected"';
231: else
232: $strSelected = '';
233: $strMonthListbox .= sprintf('<option value="%s"%s>%s</option>',
234: $intMonth,
235: $strSelected,
236: QApplication::Translate(strftime("%b", mktime(0, 0, 0, $intMonth, 1, 2000))));
237: }
238: $strMonthListbox .= '</select>';
239:
240:
241: $strDayListbox = sprintf('<select name="%s_lstDay" id="%s_lstDay" class="day" %s%s>', $this->strControlId, $this->strControlId, $strAttributes, $strCommand);
242: if (!$this->blnRequired || $dttDateTime->IsDateNull())
243: if($this->blnAllowBlankDate)
244: $strDayListbox .= '<option value="">--</option>';
245: if ($dttDateTime->IsDateNull()) {
246: if ($this->blnRequired) {
247:
248: for ($intDay = 1; $intDay <= 31; $intDay++) {
249: $strDayListbox .= sprintf('<option value="%s">%s</option>', $intDay, $intDay);
250: }
251: } else {
252:
253:
254:
255: if ($this->intSelectedMonth) {
256: $intSelectedYear = ($this->intSelectedYear) ? $this->intSelectedYear : 2000;
257: $intDaysInMonth = date('t', mktime(0, 0, 0, $this->intSelectedMonth, 1, $intSelectedYear));
258: for ($intDay = 1; $intDay <= $intDaysInMonth; $intDay++) {
259: if (($dttDateTime->Day == $intDay) || ($this->intSelectedDay == $intDay))
260: $strSelected = ' selected="selected"';
261: else
262: $strSelected = '';
263: $strDayListbox .= sprintf('<option value="%s"%s>%s</option>',
264: $intDay,
265: $strSelected,
266: $intDay);
267: }
268: } else {
269:
270: $strDayListbox .= '<option value="">--</option>';
271: }
272: }
273: } else {
274: $intDaysInMonth = $dttDateTime->PHPDate('t');
275: for ($intDay = 1; $intDay <= $intDaysInMonth; $intDay++) {
276: if (($dttDateTime->Day == $intDay) || ($this->intSelectedDay == $intDay))
277: $strSelected = ' selected="selected"';
278: else
279: $strSelected = '';
280: $strDayListbox .= sprintf('<option value="%s"%s>%s</option>',
281: $intDay,
282: $strSelected,
283: $intDay);
284: }
285: }
286: $strDayListbox .= '</select>';
287:
288:
289: $strYearListbox = sprintf('<select name="%s_lstYear" id="%s_lstYear" class="year" %s%s>', $this->strControlId, $this->strControlId, $strAttributes, $strCommand);
290: if (!$this->blnRequired || $dttDateTime->IsDateNull())
291: if($this->blnAllowBlankDate)
292: $strYearListbox .= '<option value="">--</option>';
293: for ($intYear = $this->intMinimumYear; $intYear <= $this->intMaximumYear; $intYear++) {
294: if ((($dttDateTime->Year == $intYear) || ($this->intSelectedYear == $intYear)))
295: $strSelected = ' selected="selected"';
296: else
297: $strSelected = '';
298: $strYearListbox .= sprintf('<option value="%s"%s>%s</option>', $intYear, $strSelected, $intYear);
299: }
300: $strYearListbox .= '</select>';
301:
302:
303: switch ($this->strDateTimePickerFormat) {
304: case QDateTimePickerFormat::MonthDayYear:
305: $strToReturn .= $strMonthListbox . $strDayListbox . $strYearListbox;
306: break;
307: case QDateTimePickerFormat::DayMonthYear:
308: $strToReturn .= $strDayListbox . $strMonthListbox . $strYearListbox;
309: break;
310: case QDateTimePickerFormat::YearMonthDay:
311: $strToReturn .= $strYearListbox . $strMonthListbox . $strDayListbox;
312: break;
313: }
314: }
315:
316: switch ($this->strDateTimePickerType) {
317: case QDateTimePickerType::DateTime:
318: case QDateTimePickerType::DateTimeSeconds:
319: $strToReturn .= '<span class="divider"></span>';
320: }
321:
322: switch ($this->strDateTimePickerType) {
323: case QDateTimePickerType::Time:
324: case QDateTimePickerType::TimeSeconds:
325: case QDateTimePickerType::DateTime:
326: case QDateTimePickerType::DateTimeSeconds:
327:
328: $strHourListBox = sprintf('<select name="%s_lstHour" id="%s_lstHour" class="hour" %s>', $this->strControlId, $this->strControlId, $strAttributes);
329: if (!$this->blnRequired || $dttDateTime->IsTimeNull())
330: if ($this->blnAllowBlankTime)
331: $strHourListBox .= '<option value="">--</option>';
332: for ($intHour = 0; $intHour <= 23; $intHour += $this->intHourInterval) {
333: if (!$dttDateTime->IsTimeNull() && ($dttDateTime->Hour == $intHour))
334: $strSelected = ' selected="selected"';
335: else
336: $strSelected = '';
337: $strHourListBox .= sprintf('<option value="%s"%s>%s</option>',
338: $intHour,
339: $strSelected,
340: date('g A', mktime($intHour, 0, 0, 1, 1, 2000)));
341: }
342: $strHourListBox .= '</select>';
343:
344:
345: $strMinuteListBox = sprintf('<select name="%s_lstMinute" id="%s_lstMinute" class="minute" %s>', $this->strControlId, $this->strControlId, $strAttributes);
346: if (!$this->blnRequired || $dttDateTime->IsTimeNull())
347: if ($this->blnAllowBlankTime)
348: $strMinuteListBox .= '<option value="">--</option>';
349: for ($intMinute = 0; $intMinute <= 59; $intMinute += $this->intMinuteInterval) {
350: if (!$dttDateTime->IsTimeNull() && ($dttDateTime->Minute == $intMinute))
351: $strSelected = ' selected="selected"';
352: else
353: $strSelected = '';
354: $strMinuteListBox .= sprintf('<option value="%s"%s>%02d</option>',
355: $intMinute,
356: $strSelected,
357: $intMinute);
358: }
359: $strMinuteListBox .= '</select>';
360:
361:
362:
363: $strSecondListBox = sprintf('<select name="%s_lstSecond" id="%s_lstSecond" class="second" %s>', $this->strControlId, $this->strControlId, $strAttributes);
364: if (!$this->blnRequired || $dttDateTime->IsTimeNull())
365: if ($this->blnAllowBlankTime)
366: $strSecondListBox .= '<option value="">--</option>';
367: for ($intSecond = 0; $intSecond <= 59; $intSecond += $this->intSecondInterval) {
368: if (!$dttDateTime->IsTimeNull() && ($dttDateTime->Second == $intSecond))
369: $strSelected = ' selected="selected"';
370: else
371: $strSelected = '';
372: $strSecondListBox .= sprintf('<option value="%s"%s>%02d</option>',
373: $intSecond,
374: $strSelected,
375: $intSecond);
376: }
377: $strSecondListBox .= '</select>';
378:
379:
380:
381: if (($this->strDateTimePickerType == QDateTimePickerType::DateTimeSeconds) ||
382: ($this->strDateTimePickerType == QDateTimePickerType::TimeSeconds))
383: $strToReturn .= $strHourListBox . $this->strTimeSeparator . $strMinuteListBox . $this->strTimeSeparator . $strSecondListBox;
384: else
385: $strToReturn .= $strHourListBox . $this->strTimeSeparator . $strMinuteListBox;
386: }
387:
388: if ($this->strCssClass)
389: $strCssClass = ' class="' . $this->strCssClass . '"';
390: else
391: $strCssClass = '';
392: return sprintf('<span id="%s"%s>%s</span>', $this->strControlId, $strCssClass, $strToReturn);
393: }
394:
395: public function Validate() {
396: if ($this->blnRequired) {
397: $blnIsNull = false;
398:
399: if (!$this->dttDateTime)
400: $blnIsNull = true;
401: else {
402: if ((($this->strDateTimePickerType == QDateTimePickerType::Date) ||
403: ($this->strDateTimePickerType == QDateTimePickerType::DateTime) ||
404: ($this->strDateTimePickerType == QDateTimePickerType::DateTimeSeconds )) &&
405: ($this->dttDateTime->IsDateNull()))
406: $blnIsNull = true;
407: else if ((($this->strDateTimePickerType == QDateTimePickerType::Time) ||
408: ($this->strDateTimePickerType == QDateTimePickerType::TimeSeconds)) &&
409: ($this->dttDateTime->IsTimeNull()))
410: $blnIsNull = true;
411: }
412:
413: if ($blnIsNull) {
414: if ($this->strName)
415: $this->ValidationError = sprintf(QApplication::Translate('%s is required'), $this->strName);
416: else
417: $this->ValidationError = QApplication::Translate('Required');
418: return false;
419: }
420: } else {
421: if ((($this->strDateTimePickerType == QDateTimePickerType::Date) ||
422: ($this->strDateTimePickerType == QDateTimePickerType::DateTime) ||
423: ($this->strDateTimePickerType == QDateTimePickerType::DateTimeSeconds )) &&
424: ($this->intSelectedDay || $this->intSelectedMonth || $this->intSelectedYear) &&
425: ($this->dttDateTime->IsDateNull())) {
426: $this->ValidationError = QApplication::Translate('Invalid Date');
427: return false;
428: }
429: }
430:
431: return true;
432: }
433:
434:
435:
436:
437: public function __get($strName) {
438: switch ($strName) {
439:
440: case "DateTime":
441: if (is_null($this->dttDateTime) || $this->dttDateTime->IsNull())
442: return null;
443: else {
444: $objToReturn = clone($this->dttDateTime);
445: return $objToReturn;
446: }
447:
448: case "DateTimePickerType": return $this->strDateTimePickerType;
449: case "DateTimePickerFormat": return $this->strDateTimePickerFormat;
450: case "MinimumYear": return $this->intMinimumYear;
451: case "MaximumYear": return $this->intMaximumYear;
452: case "AllowBlankTime": return $this->blnAllowBlankTime;
453: case "AllowBlankDate": return $this->blnAllowBlankDate;
454: case "TimeSeparator": return $this->strTimeSeparator;
455: case "SecondInterval": return $this->intSecondInterval;
456: case "MinuteInterval": return $this->intMinuteInterval;
457: case "HourInterval": return $this->intHourInterval;
458:
459: default:
460: try {
461: return parent::__get($strName);
462: } catch (QCallerException $objExc) {
463: $objExc->IncrementOffset();
464: throw $objExc;
465: }
466: }
467: }
468:
469:
470:
471:
472: public function __set($strName, $mixValue) {
473: $this->blnModified = true;
474:
475: switch ($strName) {
476:
477: case "DateTime":
478: try {
479: $dttDate = QType::Cast($mixValue, QType::DateTime);
480: } catch (QInvalidCastException $objExc) {
481: $objExc->IncrementOffset();
482: throw $objExc;
483: }
484:
485: $this->intSelectedMonth = null;
486: $this->intSelectedDay = null;
487: $this->intSelectedYear = null;
488:
489: if (is_null($dttDate) || $dttDate->IsNull())
490: $this->dttDateTime = null;
491: else
492: $this->dttDateTime = $dttDate;
493:
494: break;
495:
496: case "DateTimePickerType":
497: try {
498: $this->strDateTimePickerType = QType::Cast($mixValue, QType::String);
499: } catch (QInvalidCastException $objExc) {
500: $objExc->IncrementOffset();
501: throw $objExc;
502: }
503: break;
504:
505: case "DateTimePickerFormat":
506: try {
507: $this->strDateTimePickerFormat = QType::Cast($mixValue, QType::String);
508: } catch (QInvalidCastException $objExc) {
509: $objExc->IncrementOffset();
510: throw $objExc;
511: }
512: break;
513:
514: case "MinimumYear":
515: try {
516: $this->intMinimumYear = QType::Cast($mixValue, QType::String);
517: } catch (QInvalidCastException $objExc) {
518: $objExc->IncrementOffset();
519: throw $objExc;
520: }
521: break;
522:
523: case "MaximumYear":
524: try {
525: $this->intMaximumYear = QType::Cast($mixValue, QType::String);
526: } catch (QInvalidCastException $objExc) {
527: $objExc->IncrementOffset();
528: throw $objExc;
529: }
530: break;
531: case "AllowBlankTime":
532: try {
533: $this->blnAllowBlankTime = QType::Cast($mixValue, QType::Boolean);
534: } catch (QInvalidCastException $objExc) {
535: $objExc->IncrementOffset();
536: throw $objExc;
537: }
538: break;
539:
540: case "AllowBlankDate":
541: try {
542: $this->blnAllowBlankDate = QType::Cast($mixValue, QType::Boolean);
543: } catch (QInvalidCastException $objExc) {
544: $objExc->IncrementOffset();
545: throw $objExc;
546: }
547: break;
548:
549: case "TimeSeparator":
550: try {
551: $this->strTimeSeparator = QType::Cast($mixValue, QType::String);
552: } catch (QInvalidCastException $objExc) {
553: $objExc->IncrementOffset();
554: throw $objExc;
555: }
556: break;
557:
558: case "SecondInterval":
559: try {
560: $this->intSecondInterval = QType::Cast($mixValue, QType::Integer);
561: } catch (QInvalidCastException $objExc) {
562: $objExc->IncrementOffset();
563: throw $objExc;
564: }
565: break;
566:
567: case "MinuteInterval":
568: try {
569: $this->intMinuteInterval = QType::Cast($mixValue, QType::Integer);
570: } catch (QInvalidCastException $objExc) {
571: $objExc->IncrementOffset();
572: throw $objExc;
573: }
574: break;
575:
576: case "HourInterval":
577: try {
578: $this->intHourInterval = QType::Cast($mixValue, QType::Integer);
579: } catch (QInvalidCastException $objExc) {
580: $objExc->IncrementOffset();
581: throw $objExc;
582: }
583: break;
584:
585: default:
586: try {
587: parent::__set($strName, $mixValue);
588: break;
589: } catch (QCallerException $objExc) {
590: $objExc->IncrementOffset();
591: throw $objExc;
592: }
593: }
594: }
595:
596: }