summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/JuiControls/TJuiDatePicker.php
blob: 487714d235e6a1f659d3afd9a78b7ba786d98376 (plain)
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
 * TJuiDatePicker class file.
 *
 * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
 * @link http://www.landwehr-software.de/
 * @copyright Copyright &copy; 2015 LANDWEHR Computer und Software GmbH
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.ActiveControls
 */

/**
 * Load active text box.
 */
Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
Prado::using('System.Web.UI.ActiveControls.TCallbackEventParameter');
Prado::using('System.Web.UI.JuiControls.TJuiControlAdapter');
/**
 * TJuiDatePicker class.
 *
 * TJuiDatePicker is a textbox that provides a date input. When the text box receives focus,
 * a calendar will pop up and users can pick up from it a date that will be automatically
 * entered into the text box. TJuiDatePicker is an extension to {@link TActivePanel} based on
 * jQuery-UI's {@link http://jqueryui.com/dialog/ Dialog} widget.
 *
 * <code>
 * <com:TJuiDatePicker ID="datepicker1" />
 * </code>
 *
 * @author LANDWEHR Computer und Software GmbH <programmierung@landwehr-software.de>
 * @package System.Web.UI.JuiControls
 * @since 3.3
 */
class TJuiDatePicker extends TActiveTextBox implements INamingContainer, IJuiOptions
{

	/**
	 * The static variable is used to determine if this is the first instance of TJuiDatePicker. If true,
	 * it will register an additional clientscript to set the language specific global default settings.
	 * @var boolean true, if this is the first instance of TJuiDatePicker, false otherwise
	 */
	private static $_first = true;

	/**
	 * Creates a new callback control, sets the adapter to
	 * TActiveControlAdapter. If you override this class, be sure to set the
	 * adapter appropriately by, for example, by calling this constructor.
	 */
	public function __construct()
	{
		parent::__construct();
		$this->setAdapter(new TJuiControlAdapter($this));
	}

	/**
	 * @return string the name of the jQueryUI widget method
	 */
	public function getWidget()
	{
		return 'datepicker';
	}

	/**
	 * @return string the clientid of the jQueryUI widget element
	 */
	public function getWidgetID()
	{
		return $this->getClientID();
	}

	/**
	 * Object containing defined javascript options
	 * @return TJuiControlOptions
	 */
	public function getOptions()
	{
		if (($options=$this->getViewState('JuiOptions'))===null)
		{
		  $options=new TJuiControlOptions($this);
		  $this->setViewState('JuiOptions', $options);
		}
		return $options;
	}

	/**
	 * Array containing valid javascript options
	 * @return array()
	 */
	public function getValidOptions()
	{
		return array('altField', 'altFormat', 'appendText', 'autoSize', 'buttonImage', 'buttonImageOnly', 'buttonText', 'calculateWeek',
								 'changeMonth', 'changeYear', 'closeText', 'constrainInput', 'currentText', 'dateFormat', 'dayNames', 'dayNamesMin',
				         'dayNamesShort', 'defaultDate', 'duration', 'firstDay', 'gotoCurrent', 'hideIfNoPrevNext', 'isRTL', 'maxDate',
								 'minDate', 'monthNames', 'monthNamesShort', 'navigationAsDateFormat', 'nextText', 'numberOfMonths', 'prevText',
								 'selectOtherMonths', 'shortYearCutoff', 'showAnim', 'showButtonPanel', 'showCurrentAtPos', 'showMonthAfterYear',
				         'showOn', 'showOptions', 'showOtherMonths', 'showWeek', 'stepMonths', 'weekHeader', 'yearRange', 'yearSuffix',
								 'beforeShow', 'beforeShowDay', 'onChangeMonthYear', 'onClose', 'onSelect');
	}

	/**
	 * Array containing valid javascript events
	 * @return array()
	 */
	public function getValidEvents()
	{
		return array();
	}

	/**
	 * Ensure that the ID attribute is rendered and registers the javascript code
	 * for initializing the active control. Also registers language specific global
	 * settings for the first used date picker.
	 */
	protected function addAttributesToRender($writer)
	{
		$cs=$this->getPage()->getClientScript();
		if(self::$_first)
		{
			$code="jQuery(document).ready(function(){jQuery.datepicker.setDefaults(jQuery.datepicker.regional['{$this->getCurrentCulture()}']);});";
			$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
			self::$_first=false;
		}
		parent::addAttributesToRender($writer);
		$options=TJavascript::encode($this->getOptions()->toArray());
		$code="jQuery('#".$this->getWidgetID()."').".$this->getWidget()."(".$options.");";
		$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
	}

	/**
	 * @return TTextBoxMode the behavior mode of the underlying {@link TTextBox} component.
	 * Fixed to TTextBoxMode::SingleLine for the TJuiDatePicker.
	 */
	public function getTextMode()
	{
		return TTextBoxMode::SingleLine;
	}

	/**
	 * Setting the behavior mode of the underlying TTextBox component is NOT supported.
	 * @param TTextBoxMode the text mode
	 * @throws TNotSupportedException not supported, fixed to TTextBoxMode::SingleLine.
	 */
	public function setTextMode($value)
	{
		throw new TNotSupportedException('juidatepicker_settextmode_unsupported');
	}

	/**
	 * Gets the current culture.
	 * @return string current culture, e.g. en_AU.
	 */
	public function getCulture()
	{
		return $this->getViewState('Culture', '');
	}

	/**
	 * Sets the culture/language for the date picker.
	 * @param string a culture string, e.g. en_AU.
	 */
	public function setCulture($value)
	{
		$this->setViewState('Culture', $value, '');
	}

	/**
	 * @return string the current culture, falls back to application if culture is not set.
	 */
	protected function getCurrentCulture()
	{
		$app = $this->getApplication()->getGlobalization(false);
		return $this->getCulture() == '' ?
				($app ? $app->getCulture() : 'en') : $this->getCulture();
	}

	/**
	 * @return string the format of the date string
	 */
	public function getDateFormat()
	{
		return $this->getViewState('DateFormat','dd-MM-yyyy');
	}

	/**
	 * Sets the format of the date string.
	 * @param string the format of the date string
	 */
	public function setDateFormat($value)
	{
		$this->setViewState('DateFormat',$value,'dd-MM-yyyy');
	}

	/**
	 * Returns the timestamp selected by the user.
	 * This method is required by {@link IDataRenderer}.
	 * It is the same as {@link getTimeStamp()}.
	 * @return integer the timestamp of the TDatePicker control.
	 * @see getTimeStamp
	 * @since 3.1.2
	 */
	public function getData()
	{
		return $this->getTimeStamp();
	}

	/**
	 * Sets the timestamp represented by this control.
	 * This method is required by {@link IDataRenderer}.
	 * It is the same as {@link setTimeStamp()}.
	 * @param integer the timestamp of the TDatePicker control.
	 * @see setTimeStamp
	 * @since 3.1.2
	 */
	public function setData($value)
	{
		$this->setTimeStamp($value);
	}

	/**
	 * @return string the date string.
	 */
	public function getDate()
	{
		return $this->getText();
	}

	/**
	 * @param string date string
	 */
	public function setDate($value)
	{
		$this->setText($value);
	}

	/**
	 * @return integer current selected date from the date picker as timestamp, NULL if timestamp is not set previously.
	 */
	public function getTimeStamp()
	{
		if(trim($this->getText())==='')
			return null;
		else
			return $this->getTimeStampFromText();
	}

	/**
	 * Sets the date for the date picker using timestamp.
	 * @param float time stamp for the date picker
	 */
	public function setTimeStamp($value)
	{
		if($value===null || (is_string($value) && trim($value)===''))
			$this->setText('');
		else
		{
			$date = TPropertyValue::ensureFloat($value);
			$formatter = Prado::createComponent('System.Util.TSimpleDateFormatter',$this->getDateFormat());
			$this->setText($formatter->format($date));
		}
	}

	/**
	 * Gets the date from the text input using TSimpleDateFormatter
	 * @return integer current selected date timestamp
	 */
	protected function getTimeStampFromText()
	{
		$pattern = $this->getDateFormat();
		$pattern = str_replace(array('MMMM', 'MMM'), array('MM','MM'), $pattern);
		$formatter = Prado::createComponent('System.Util.TSimpleDateFormatter',$pattern);
		return $formatter->parse($this->getText());
	}

	/**
	 * Returns the value to be validated.
	 * This methid is required by IValidatable interface.
	 * @return integer the interger timestamp if valid, otherwise the original text.
	 */
	public function getValidationPropertyValue()
	{
		if(($text = $this->getText()) === '')
			return '';
		$date = $this->getTimeStamp();
		return $date == null ? $text : $date;
	}

}