Completed
Push — namespace2 ( 8a6673...791eac )
by Fabio
08:25
created

TJuiDatePicker   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 254
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 1
cbo 10

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getWidget() 0 4 1
A getWidgetID() 0 4 1
A getOptions() 0 9 2
A getValidOptions() 0 10 1
A getValidEvents() 0 4 1
A addAttributesToRender() 0 14 2
A getTextMode() 0 4 1
A setTextMode() 0 4 1
A getCulture() 0 4 1
A setCulture() 0 4 1
A getCurrentCulture() 0 6 3
A getDateFormat() 0 4 1
A setDateFormat() 0 4 1
A getData() 0 4 1
A setData() 0 4 1
A getDate() 0 4 1
A setDate() 0 4 1
A getTimeStamp() 0 7 2
A setTimeStamp() 0 11 4
A getTimeStampFromText() 0 7 1
A getValidationPropertyValue() 0 7 3
1
<?php
2
/**
3
 * TJuiDatePicker class file.
4
 *
5
 * @author LANDWEHR Computer und Software GmbH <[email protected]>
6
 * @link http://www.landwehr-software.de/
7
 * @copyright Copyright &copy; 2015 LANDWEHR Computer und Software GmbH
8
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9
 * @package \Prado\Web\UI\JuiControls
10
 */
11
12
namespace Prado\Web\UI\JuiControls;
13
14
use Prado\TPropertyValue;
15
use Prado\Exceptions\TNotSupportedException;
16
use Prado\Web\Javascripts\TJavaScript;
17
use Prado\Web\UI\ActiveControls\TActiveTextBox;
18
use Prado\Web\UI\INamingContainer;
19
20
/**
21
 * TJuiDatePicker class.
22
 *
23
 * TJuiDatePicker is a textbox that provides a date input. When the text box receives focus,
24
 * a calendar will pop up and users can pick up from it a date that will be automatically
25
 * entered into the text box. TJuiDatePicker is an extension to {@link TActivePanel} based on
26
 * jQuery-UI's {@link http://jqueryui.com/dialog/ Dialog} widget.
27
 *
28
 * <code>
29
 * <com:TJuiDatePicker ID="datepicker1" />
30
 * </code>
31
 *
32
 * @author LANDWEHR Computer und Software GmbH <[email protected]>
33
 * @package Prado\Web\UI\JuiControls
34
 * @since 3.3
35
 */
36
class TJuiDatePicker extends TActiveTextBox implements INamingContainer, IJuiOptions
37
{
38
39
	/**
40
	 * The static variable is used to determine if this is the first instance of TJuiDatePicker. If true,
41
	 * it will register an additional clientscript to set the language specific global default settings.
42
	 * @var boolean true, if this is the first instance of TJuiDatePicker, false otherwise
43
	 */
44
	private static $_first = true;
45
46
	/**
47
	 * Creates a new callback control, sets the adapter to
48
	 * TActiveControlAdapter. If you override this class, be sure to set the
49
	 * adapter appropriately by, for example, by calling this constructor.
50
	 */
51
	public function __construct()
52
	{
53
		parent::__construct();
54
		$this->setAdapter(new TJuiControlAdapter($this));
55
	}
56
57
	/**
58
	 * @return string the name of the jQueryUI widget method
59
	 */
60
	public function getWidget()
61
	{
62
		return 'datepicker';
63
	}
64
65
	/**
66
	 * @return string the clientid of the jQueryUI widget element
67
	 */
68
	public function getWidgetID()
69
	{
70
		return $this->getClientID();
71
	}
72
73
	/**
74
	 * Object containing defined javascript options
75
	 * @return TJuiControlOptions
76
	 */
77
	public function getOptions()
78
	{
79
		if (($options=$this->getViewState('JuiOptions'))===null)
80
		{
81
		  $options=new TJuiControlOptions($this);
82
		  $this->setViewState('JuiOptions', $options);
83
		}
84
		return $options;
85
	}
86
87
	/**
88
	 * Array containing valid javascript options
89
	 * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
90
	 */
91
	public function getValidOptions()
92
	{
93
		return array('altField', 'altFormat', 'appendText', 'autoSize', 'buttonImage', 'buttonImageOnly', 'buttonText', 'calculateWeek',
94
								 'changeMonth', 'changeYear', 'closeText', 'constrainInput', 'currentText', 'dateFormat', 'dayNames', 'dayNamesMin',
95
				         'dayNamesShort', 'defaultDate', 'duration', 'firstDay', 'gotoCurrent', 'hideIfNoPrevNext', 'isRTL', 'maxDate',
96
								 'minDate', 'monthNames', 'monthNamesShort', 'navigationAsDateFormat', 'nextText', 'numberOfMonths', 'prevText',
97
								 'selectOtherMonths', 'shortYearCutoff', 'showAnim', 'showButtonPanel', 'showCurrentAtPos', 'showMonthAfterYear',
98
				         'showOn', 'showOptions', 'showOtherMonths', 'showWeek', 'stepMonths', 'weekHeader', 'yearRange', 'yearSuffix',
99
								 'beforeShow', 'beforeShowDay', 'onChangeMonthYear', 'onClose', 'onSelect');
100
	}
101
102
	/**
103
	 * Array containing valid javascript events
104
	 * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
105
	 */
106
	public function getValidEvents()
107
	{
108
		return array();
109
	}
110
111
	/**
112
	 * Ensure that the ID attribute is rendered and registers the javascript code
113
	 * for initializing the active control. Also registers language specific global
114
	 * settings for the first used date picker.
115
	 */
116
	protected function addAttributesToRender($writer)
117
	{
118
		$cs=$this->getPage()->getClientScript();
119
		if(self::$_first)
120
		{
121
			$code="jQuery(document).ready(function(){jQuery.datepicker.setDefaults(jQuery.datepicker.regional['{$this->getCurrentCulture()}']);});";
122
			$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
123
			self::$_first=false;
124
		}
125
		parent::addAttributesToRender($writer);
126
		$options=TJavascript::encode($this->getOptions()->toArray());
127
		$code="jQuery('#".$this->getWidgetID()."').".$this->getWidget()."(".$options.");";
128
		$cs->registerEndScript(sprintf('%08X', crc32($code)), $code);
129
	}
130
131
	/**
132
	 * @return TTextBoxMode the behavior mode of the underlying {@link TTextBox} component.
133
	 * Fixed to TTextBoxMode::SingleLine for the TJuiDatePicker.
134
	 */
135
	public function getTextMode()
136
	{
137
		return TTextBoxMode::SingleLine;
138
	}
139
140
	/**
141
	 * Setting the behavior mode of the underlying TTextBox component is NOT supported.
142
	 * @param TTextBoxMode the text mode
143
	 * @throws TNotSupportedException not supported, fixed to TTextBoxMode::SingleLine.
144
	 */
145
	public function setTextMode($value)
146
	{
147
		throw new TNotSupportedException('juidatepicker_settextmode_unsupported');
148
	}
149
150
	/**
151
	 * Gets the current culture.
152
	 * @return string current culture, e.g. en_AU.
153
	 */
154
	public function getCulture()
155
	{
156
		return $this->getViewState('Culture', '');
157
	}
158
159
	/**
160
	 * Sets the culture/language for the date picker.
161
	 * @param string a culture string, e.g. en_AU.
162
	 */
163
	public function setCulture($value)
164
	{
165
		$this->setViewState('Culture', $value, '');
166
	}
167
168
	/**
169
	 * @return string the current culture, falls back to application if culture is not set.
170
	 */
171
	protected function getCurrentCulture()
172
	{
173
		$app = $this->getApplication()->getGlobalization(false);
174
		return $this->getCulture() == '' ?
175
				($app ? $app->getCulture() : 'en') : $this->getCulture();
176
	}
177
178
	/**
179
	 * @return string the format of the date string
180
	 */
181
	public function getDateFormat()
182
	{
183
		return $this->getViewState('DateFormat','dd-MM-yyyy');
184
	}
185
186
	/**
187
	 * Sets the format of the date string.
188
	 * @param string the format of the date string
189
	 */
190
	public function setDateFormat($value)
191
	{
192
		$this->setViewState('DateFormat',$value,'dd-MM-yyyy');
193
	}
194
195
	/**
196
	 * Returns the timestamp selected by the user.
197
	 * This method is required by {@link IDataRenderer}.
198
	 * It is the same as {@link getTimeStamp()}.
199
	 * @return integer the timestamp of the TDatePicker control.
200
	 * @see getTimeStamp
201
	 * @since 3.1.2
202
	 */
203
	public function getData()
204
	{
205
		return $this->getTimeStamp();
206
	}
207
208
	/**
209
	 * Sets the timestamp represented by this control.
210
	 * This method is required by {@link IDataRenderer}.
211
	 * It is the same as {@link setTimeStamp()}.
212
	 * @param integer the timestamp of the TDatePicker control.
213
	 * @see setTimeStamp
214
	 * @since 3.1.2
215
	 */
216
	public function setData($value)
217
	{
218
		$this->setTimeStamp($value);
219
	}
220
221
	/**
222
	 * @return string the date string.
223
	 */
224
	public function getDate()
225
	{
226
		return $this->getText();
227
	}
228
229
	/**
230
	 * @param string date string
231
	 */
232
	public function setDate($value)
233
	{
234
		$this->setText($value);
235
	}
236
237
	/**
238
	 * @return integer current selected date from the date picker as timestamp, NULL if timestamp is not set previously.
239
	 */
240
	public function getTimeStamp()
241
	{
242
		if(trim($this->getText())==='')
243
			return null;
244
		else
245
			return $this->getTimeStampFromText();
246
	}
247
248
	/**
249
	 * Sets the date for the date picker using timestamp.
250
	 * @param float time stamp for the date picker
251
	 */
252
	public function setTimeStamp($value)
253
	{
254
		if($value===null || (is_string($value) && trim($value)===''))
255
			$this->setText('');
256
		else
257
		{
258
			$date = TPropertyValue::ensureFloat($value);
259
			$formatter = Prado::createComponent('System.Util.TSimpleDateFormatter',$this->getDateFormat());
260
			$this->setText($formatter->format($date));
261
		}
262
	}
263
264
	/**
265
	 * Gets the date from the text input using TSimpleDateFormatter
266
	 * @return integer current selected date timestamp
267
	 */
268
	protected function getTimeStampFromText()
269
	{
270
		$pattern = $this->getDateFormat();
271
		$pattern = str_replace(array('MMMM', 'MMM'), array('MM','MM'), $pattern);
272
		$formatter = Prado::createComponent('System.Util.TSimpleDateFormatter',$pattern);
273
		return $formatter->parse($this->getText());
274
	}
275
276
	/**
277
	 * Returns the value to be validated.
278
	 * This methid is required by IValidatable interface.
279
	 * @return integer the interger timestamp if valid, otherwise the original text.
280
	 */
281
	public function getValidationPropertyValue()
282
	{
283
		if(($text = $this->getText()) === '')
284
			return '';
285
		$date = $this->getTimeStamp();
286
		return $date == null ? $text : $date;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $date of type null|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
287
	}
288
289
}