1: <?php
2: 3: 4: 5: 6:
7:
8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class QDateTimeTextBox extends QTextBox {
18:
19:
20:
21:
22:
23: protected $dttMinimum = null;
24: protected $dttMaximum = null;
25: protected $strDateTimeFormat = "MMM D, YYYY";
26: protected $dttDateTime = null;
27:
28: protected $strLabelForInvalid = 'For example, "Mar 20, 4:30pm" or "Mar 20"';
29: protected $calLinkedControl;
30:
31:
32:
33:
34:
35: public function ParsePostData() {
36:
37: if (array_key_exists($this->strControlId, $_POST)) {
38: parent::ParsePostData();
39: $this->dttDateTime = QDateTimeTextBox::ParseForDateTimeValue($this->strText);
40: }
41: }
42:
43: public static function ParseForDateTimeValue($strText) {
44:
45: $strText = strtolower(trim($strText));
46: while(strpos($strText, ' ') !== false)
47: $strText = str_replace(' ', ' ', $strText);
48: $strText = str_replace('.', '', $strText);
49: $strText = str_replace('@', ' ', $strText);
50:
51:
52: if ((strpos($strText, ':') === false) &&
53: (strpos($strText, 'am') === false) &&
54: (strpos($strText, 'pm') === false)) {
55:
56: $dttToReturn = new QDateTime($strText);
57: if ($dttToReturn->IsDateNull())
58: return null;
59: else
60: return $dttToReturn;
61: }
62:
63:
64: if ((strpos($strText, 'pm') !== false) &&
65: (strpos($strText, ':') === false)) {
66: $strText = str_replace(' pm', ':00 pm', $strText, $intCount);
67: if (!$intCount)
68: $strText = str_replace('pm', ':00 pm', $strText, $intCount);
69: } else if ((strpos($strText, 'am') !== false) &&
70: (strpos($strText, ':') === false)) {
71: $strText = str_replace(' am', ':00 am', $strText, $intCount);
72: if (!$intCount)
73: $strText = str_replace('am', ':00 am', $strText, $intCount);
74: }
75:
76: $dttToReturn = new QDateTime($strText);
77: if ($dttToReturn->IsDateNull())
78: return null;
79: else
80: return $dttToReturn;
81: }
82:
83: public function Validate() {
84: if (parent::Validate()) {
85: if ($this->strText != "") {
86: $dttTest = QDateTimeTextBox::ParseForDateTimeValue($this->strText);
87:
88: if (!$dttTest) {
89: $this->ValidationError = $this->strLabelForInvalid;
90: return false;
91: }
92:
93: if (!is_null($this->dttMinimum)) {
94: if ($this->dttMinimum == QDateTime::Now) {
95: $dttToCompare = new QDateTime(QDateTime::Now);
96: $strError = QApplication::Translate('in the past');
97: } else {
98: $dttToCompare = $this->dttMinimum;
99: $strError = QApplication::Translate('before ') . $dttToCompare->__toString();
100: }
101:
102: if ($dttTest->IsEarlierThan($dttToCompare)) {
103: $this->ValidationError = QApplication::Translate('Date cannot be ') . $strError;
104: return false;
105: }
106: }
107:
108: if (!is_null($this->dttMaximum)) {
109: if ($this->dttMaximum == QDateTime::Now) {
110: $dttToCompare = new QDateTime(QDateTime::Now);
111: $strError = QApplication::Translate('in the future');
112: } else {
113: $dttToCompare = $this->dttMaximum;
114: $strError = QApplication::Translate('after ') . $dttToCompare->__toString();
115: }
116:
117: if ($dttTest->IsLaterThan($dttToCompare)) {
118: $this->ValidationError = QApplication::Translate('Date cannot be ') . $strError;
119: return false;
120: }
121: }
122: }
123: } else
124: return false;
125:
126: return true;
127: }
128:
129:
130:
131:
132: public function __get($strName) {
133: switch ($strName) {
134:
135: case "Maximum": return $this->dttMaximum;
136: case "Minimum": return $this->dttMinimum;
137: case 'DateTimeFormat': return $this->strDateTimeFormat;
138: case 'DateTime': return $this->dttDateTime;
139: case 'LabelForInvalid': return $this->strLabelForInvalid;
140:
141: default:
142: try {
143: return parent::__get($strName);
144: } catch (QCallerException $objExc) {
145: $objExc->IncrementOffset();
146: throw $objExc;
147: }
148: }
149: }
150:
151:
152:
153:
154: public function __set($strName, $mixValue) {
155: $this->blnModified = true;
156:
157: switch ($strName) {
158:
159: case 'Maximum':
160: try {
161: if ($mixValue == QDateTime::Now)
162: $this->dttMaximum = QDateTime::Now;
163: else
164: $this->dttMaximum = QType::Cast($mixValue, QType::DateTime);
165: break;
166: } catch (QInvalidCastException $objExc) {
167: $objExc->IncrementOffset();
168: throw $objExc;
169: }
170:
171: case 'Minimum':
172: try {
173: if ($mixValue == QDateTime::Now)
174: $this->dttMinimum = QDateTime::Now;
175: else
176: $this->dttMinimum = QType::Cast($mixValue, QType::DateTime);
177: break;
178: } catch (QInvalidCastException $objExc) {
179: $objExc->IncrementOffset();
180: throw $objExc;
181: }
182:
183: case 'DateTimeFormat':
184: try {
185: $this->strDateTimeFormat = QType::Cast($mixValue, QType::String);
186:
187: $this->DateTime = $this->dttDateTime;
188: return $this->strDateTimeFormat;
189: } catch (QInvalidCastException $objExc) {
190: $objExc->IncrementOffset();
191: throw $objExc;
192: }
193:
194: case 'DateTime':
195: try {
196: $this->dttDateTime = QType::Cast($mixValue, QType::DateTime);
197: if (!$this->dttDateTime || !$this->strDateTimeFormat) {
198: parent::__set('Text', '');
199: } else {
200: parent::__set('Text', $this->dttDateTime->qFormat($this->strDateTimeFormat));
201: }
202: return $this->dttDateTime;
203: } catch (QInvalidCastException $objExc) {
204: $objExc->IncrementOffset();
205: throw $objExc;
206: }
207:
208: case 'Text':
209: $this->dttDateTime = QDateTimeTextBox::ParseForDateTimeValue($this->strText);
210: return parent::__set('Text', $mixValue);
211:
212: case 'LabelForInvalid':
213: try {
214: return ($this->strLabelForInvalid = QType::Cast($mixValue, QType::String));
215: } catch (QInvalidCastException $objExc) {
216: $objExc->IncrementOffset();
217: throw $objExc;
218: }
219:
220: default:
221: try {
222: parent::__set($strName, $mixValue);
223: } catch (QCallerException $objExc) {
224: $objExc->IncrementOffset();
225: throw $objExc;
226: }
227: break;
228: }
229: }
230:
231:
232:
233: public static function Codegen_VarName($strPropName) {
234: return 'cal' . $strPropName;
235: }
236:
237: }