1: <?php
2:
3: /**
4: * Class QDatepicker_SelectEvent2
5: * Use this class instead of the QDatepicker_SelectEvent. The QDatepicker_SelectEvent will cause the
6: * datepicker to not function correctly. The problem is related to how the datepicker is implemented on the
7: * JQueryUI end. They have been meaning to do a rewrite for quite some time, but have not gotten to that.
8: */
9: class QDatepicker_SelectEvent2 extends QEvent {
10: /** Event name for the event */
11: const EventName = 'QDatepicker_Select2';
12: }
13:
14: /**
15: * Datepicker Base File
16: *
17: * The QDatepickerBase class defined here provides an interface between the generated
18: * QDatepickerGen class, and QCubed. This file is part of the core and will be overwritten
19: * when you update QCubed. To override, make your changes to the QDatepicker.class.php file instead.
20: *
21: */
22:
23: /**
24: * Impelements a JQuery UI Datepicker
25: *
26: * A Datepicker is a field that is designed to just allow dates, and to popup a calendar for picking dates.
27: *
28: * @property string $DateFormat The format to use for displaying the date in the field
29: * @property string $DateTimeFormat Alias for DateFormat
30: * @property QDateTime $DateTime The date to set the field to
31: * @property mixed $Minimum Alias for MinDate
32: * @property mixed $Maximum Alias for MaxDate
33: * @property string $Text Textual date to set it to
34: *
35: * @link http://jqueryui.com/datepicker/
36: * @package Controls\Base
37: */
38: class QDatepickerBase extends QDatepickerGen {
39: /** @var string Default datetime format for the picker */
40: protected $strDateTimeFormat = "MM/DD/YYYY"; // same as default for JQuery UI control
41: /** @var QDateTime variable to hold the date time to be selected (or already selected) */
42: protected $dttDateTime; // default to no selection
43:
44: /**
45: * @param QControl|QControlBase|QForm $objParentObject
46: * @param null|string $strControlId
47: *
48: * @throws Exception|QCallerException|QInvalidCastException
49: */
50: public function __construct($objParentObject, $strControlId = null) {
51: parent::__construct ($objParentObject, $strControlId);
52:
53: parent::__set ('OnSelect', $this->OnSelectJs()); // setup a way to detect a selection
54: }
55:
56: /**
57: * Output JS that will record changes to the datepicker and fire our own select event.
58: */
59: protected function OnSelectJs () {
60: $strId = $this->getJqControlId();
61: $strJS = sprintf ('qcubed.recordControlModification("%s", "_Text", dateText); $j("#%s").trigger("QDatepicker_Select2")', $strId, $strId);
62: return $strJS;
63: }
64:
65: /////////////////////////
66: // Public Properties: GET
67: /////////////////////////
68: /**
69: * @param string $strName
70: *
71: * @return mixed|null|string
72: * @throws Exception
73: * @throws QCallerException
74: */
75: public function __get($strName) {
76: switch ($strName) {
77: // MISC
78: case "Maximum": return $this->MaxDate;
79: case "Minimum": return $this->MinDate;
80: case 'DateTimeFormat':
81: case 'DateFormat': return $this->strDateTimeFormat;
82: case 'DateTime': return $this->dttDateTime ? clone($this->dttDateTime) : null;
83:
84: default:
85: try {
86: return parent::__get($strName);
87: } catch (QCallerException $objExc) {
88: $objExc->IncrementOffset();
89: throw $objExc;
90: }
91: }
92: }
93: /////////////////////////
94: // Public Properties: SET
95: /////////////////////////
96: /**
97: * PHP magic method
98: *
99: * @param string $strName Property name
100: * @param string $mixValue Property value
101: *
102: * @return mixed|void
103: * @throws Exception|QCallerException|QInvalidCastException
104: */
105: public function __set($strName, $mixValue) {
106: switch ($strName) {
107: case 'MaxDate':
108: case 'Maximum':
109: if (is_string($mixValue)) {
110: if (preg_match('/[+-][0-9]+[dDwWmMyY]/', $mixValue)) {
111: parent::__set($strName, $mixValue);
112: break;
113: }
114: $mixValue = new QDateTime($mixValue);
115: }
116: parent::__set('MaxDate', QType::Cast($mixValue, QType::DateTime));
117: break;
118:
119: case 'MinDate':
120: case 'Minimum':
121: if (is_string($mixValue)) {
122: if (preg_match('/[+-][0-9]+[dDwWmMyY]/', $mixValue)) {
123: parent::__set($strName, $mixValue);
124: break;
125: }
126: $mixValue = new QDateTime($mixValue);
127: }
128: parent::__set('MinDate', QType::Cast($mixValue, QType::DateTime));
129: break;
130:
131: case 'DateTime':
132: try {
133: $this->dttDateTime = new QDateTime($mixValue, null, QDateTime::DateOnlyType);
134: parent::SetDate($this->dttDateTime);
135: break;
136: } catch (QInvalidCastException $objExc) {
137: $objExc->IncrementOffset();
138: throw $objExc;
139: }
140:
141: case 'JqDateFormat':
142: try {
143: parent::__set($strName, $mixValue);
144: $this->strDateTimeFormat = QCalendar::qcFrmt($this->JqDateFormat);
145: // trigger an update to reformat the text with the new format
146: $this->DateTime = $this->dttDateTime;
147: break;
148: } catch (QInvalidCastException $objExc) {
149: $objExc->IncrementOffset();
150: throw $objExc;
151: }
152:
153: case 'DateTimeFormat':
154: case 'DateFormat':
155: try {
156: $this->strDateTimeFormat = QType::Cast($mixValue, QType::String);
157: parent::__set('JqDateFormat', QCalendar::jqFrmt($this->strDateTimeFormat));
158: // trigger an update to reformat the text with the new format
159: $this->DateTime = $this->dttDateTime;
160: break;
161: } catch (QInvalidCastException $objExc) {
162: $objExc->IncrementOffset();
163: throw $objExc;
164: }
165:
166: case 'Text': // Set the selected date with a text value
167: $this->DateTime = $mixValue;
168: break;
169:
170: case '_Text': // Internal only. Do not use. Called by JS above to keep track of user selection.
171: $this->dttDateTime = new QDateTime($mixValue);
172: break;
173:
174: case 'OnSelect':
175: // Since we are using the OnSelect event already, and Datepicker doesn't allow binding, so there can be
176: // only one event, we will make sure our js is part of any new OnSelect js.
177: $mixValue = $this->OnSelectJs() . ';' . $mixValue;
178: parent::__set('OnSelect', $mixValue);
179: break;
180:
181: default:
182: try {
183: parent::__set($strName, $mixValue);
184: } catch (QCallerException $objExc) {
185: $objExc->IncrementOffset();
186: throw $objExc;
187: }
188: break;
189: }
190: }
191:
192: /* === Codegen Helpers, used during the Codegen process only. === */
193:
194: /**
195: * Returns the variable name for a control of this type during code generation process
196: *
197: * @param string $strPropName Property name for which the control to be generated is being generated
198: *
199: * @return string
200: */
201: public static function Codegen_VarName($strPropName) {
202: return 'cal' . $strPropName;
203: }
204:
205:
206: }