1: <?php
2:
3: /**
4: * DatepickerBox Base File
5: *
6: * The QDatepickerBoxBase class defined here provides an interface between the generated
7: * QDatepickerBoxGen class, and QCubed. This file is part of the core and will be overwritten
8: * when you update QCubed. To override, make your changes to the QDatepickerBox.class.php file instead.
9: *
10: */
11:
12: /**
13: * Impelements a JQuery UI Datepicker in a box
14: * A Datepicker Box is similar to a Datepicker, but its not associated with a field. It
15: * just displays a calendar for picking a date.
16: *
17: * @property string $DateFormat The format to use for displaying the date
18: * @property string $DateTimeFormat Alias for DateFormat
19: * @property QDateTime $DateTime The date to set the field to
20: * @property mixed $Minimum Alias for MinDate
21: * @property mixed $Maximum Alias for MaxDate
22: * @property string $Text Textual date to set it to
23: * @property-write string $MinDateErrorMsg Message to display if we are before the minimum date
24: * @property-write string $MaxDateErrorMsg Message to display if we are after the maximum date
25: * @link http://jqueryui.com/datepicker/#inline
26: * @package Controls\Base
27: */
28: class QDatepickerBoxBase extends QDatepickerBoxGen {
29: /** @var string Format for the datetime to pick */
30: protected $strDateTimeFormat = "MM/DD/YYYY"; // matches default of JQuery UI control
31: /** @var QDateTime variable to store the picked value */
32: protected $dttDateTime;
33: /** @var string */
34: protected $strMinDateErrorMsg;
35: /** @var string */
36: protected $strMaxDateErrorMsg;
37:
38: public function ParsePostData() {
39: // Check to see if this Control's Value was passed in via the POST data
40: if (array_key_exists($this->strControlId, $_POST)) {
41: parent::ParsePostData();
42: $this->dttDateTime = new QDateTime($this->strText, null, QDateTime::DateOnlyType);
43: if ($this->dttDateTime->IsNull()) {
44: $this->dttDateTime = null;
45: }
46: }
47: }
48:
49: /**
50: * Validate the control.
51: * @return bool
52: */
53: public function Validate() {
54: if (!parent::Validate()) {
55: return false;
56: }
57:
58: if ($this->strText != '') {
59: $dttDateTime = new QDateTime($this->strText, null, QDateTime::DateOnlyType);
60: if ($dttDateTime->IsDateNull()) {
61: $this->ValidationError = QApplication::Translate("Invalid date");
62: return false;
63: }
64: if (!is_null($this->Minimum)) {
65: if ($dttDateTime->IsEarlierThan($this->Minimum)) {
66: if ($this->strMinDateErrorMsg) {
67: $this->ValidationError = $this->strMinDateErrorMsg;
68: } else {
69: $this->ValidationError = QApplication::Translate("Date is earlier than minimum allowed");
70: }
71: return false;
72: }
73: }
74:
75: if (!is_null($this->Maximum)) {
76: if ($dttDateTime->IsLaterThan($this->Maximum)) {
77: if ($this->strMaxDateErrorMsg) {
78: $this->ValidationError = $this->strMaxDateErrorMsg;
79: } else {
80: $this->ValidationError = QApplication::Translate("Date is later than maximum allowed");
81: }
82: return false;
83: }
84: }
85: }
86: return true;
87: }
88:
89: /////////////////////////
90: // Public Properties: GET
91: /////////////////////////
92: /**
93: * PHP magic method implementation
94: *
95: * @param string $strName
96: *
97: * @return mixed
98: * @throws Exception|QCallerException
99: */
100: public function __get($strName) {
101: switch ($strName) {
102: // MISC
103: case "Maximum":
104: return $this->MaxDate;
105: case "Minimum":
106: return $this->MinDate;
107: case 'DateTimeFormat':
108: case 'DateFormat':
109: return $this->strDateTimeFormat;
110: case 'DateTime':
111: return $this->dttDateTime ? clone($this->dttDateTime) : null;
112:
113: default:
114: try {
115: return parent::__get($strName);
116: } catch (QCallerException $objExc) {
117: $objExc->IncrementOffset();
118: throw $objExc;
119: }
120: }
121: }
122:
123: /////////////////////////
124: // Public Properties: SET
125: /////////////////////////
126: /**
127: * PHP magic method implementation
128: *
129: * @param string $strName Property name
130: * @param string $mixValue Property value to be set
131: *
132: * @throws Exception|QCallerException|QInvalidCastException
133: */
134: public function __set($strName, $mixValue) {
135: switch ($strName) {
136: case 'MaxDate':
137: case 'Maximum':
138: try{
139: if (is_string($mixValue)) {
140: if (preg_match('/[+-][0-9]+[dDwWmMyY]/', $mixValue)) {
141: parent::__set($strName, $mixValue);
142: break;
143: }
144: }
145: parent::__set('MaxDate', new QDateTime ($mixValue, null, QDateTime::DateOnlyType));
146: } catch (QInvalidCastException $objExc) {
147: $objExc->IncrementOffset();
148: throw $objExc;
149: }
150: break;
151:
152: case 'MinDate':
153: case 'Minimum':
154: try {
155: if (is_string($mixValue)) {
156: if (preg_match('/[+-][0-9]+[dDwWmMyY]/', $mixValue)) {
157: parent::__set($strName, $mixValue);
158: break;
159: }
160: }
161: parent::__set('MinDate', new QDateTime ($mixValue, null, QDateTime::DateOnlyType));
162: } catch (QInvalidCastException $objExc) {
163: $objExc->IncrementOffset();
164: throw $objExc;
165: }
166: break;
167:
168: case 'DateTime':
169: try {
170: $this->dttDateTime = new QDateTime($mixValue, null, QDateTime::DateOnlyType);
171: if ($this->dttDateTime && $this->dttDateTime->IsNull()) {
172: $this->dttDateTime = null;
173: $this->blnModified = true;
174: }
175: if (!$this->dttDateTime || !$this->strDateTimeFormat) {
176: parent::__set('Text', '');
177: } else {
178: parent::__set('Text', $this->dttDateTime->qFormat($this->strDateTimeFormat));
179: }
180: } catch (QInvalidCastException $objExc) {
181: $objExc->IncrementOffset();
182: throw $objExc;
183: }
184: break;
185:
186: case 'JqDateFormat':
187: try {
188: parent::__set($strName, $mixValue);
189: $this->strDateTimeFormat = QCalendar::qcFrmt($this->JqDateFormat);
190: // trigger an update to reformat the text with the new format
191: $this->DateTime = $this->dttDateTime;
192: } catch (QInvalidCastException $objExc) {
193: $objExc->IncrementOffset();
194: throw $objExc;
195: }
196: break;
197:
198: case 'DateTimeFormat':
199: case 'DateFormat':
200: try {
201: $this->strDateTimeFormat = QType::Cast($mixValue, QType::String);
202: parent::__set('JqDateFormat', QCalendar::jqFrmt($this->strDateTimeFormat));
203: // trigger an update to reformat the text with the new format
204: $this->DateTime = $this->dttDateTime;
205: } catch (QInvalidCastException $objExc) {
206: $objExc->IncrementOffset();
207: throw $objExc;
208: }
209: break;
210:
211: case 'Text':
212: parent::__set($strName, $mixValue);
213: $this->dttDateTime = new QDateTime($this->strText, null, QDateTime::DateOnlyType);
214: break;
215:
216: case 'MinDateErrorMsg':
217: $this->strMinDateErrorMsg = QType::Cast($mixValue, QType::String);
218: break;
219:
220: case 'MaxDateErrorMsg':
221: $this->strMaxDateErrorMsg = QType::Cast($mixValue, QType::String);
222: break;
223:
224: default:
225: try {
226: parent::__set($strName, $mixValue);
227: } catch (QCallerException $objExc) {
228: $objExc->IncrementOffset();
229: throw $objExc;
230: }
231: break;
232: }
233: }
234:
235: /**
236: * @return QModelConnectorParam[]
237: */
238: public static function GetModelConnectorParams() {
239: return array_merge(parent::GetModelConnectorParams(), array(
240: new QModelConnectorParam (get_called_class(), 'DateFormat', 'How to format the date. Default: MM/DD/YY', QType::String)
241: ));
242: }
243:
244: }