You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					111 lines
				
				2.9 KiB
			
		
		
			
		
	
	
					111 lines
				
				2.9 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace yii\jui;
 | ||
|  | 
 | ||
|  | use Yii;
 | ||
|  | use yii\helpers\Html;
 | ||
| 
											12 years ago
										 | use yii\helpers\Json;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | /**
 | ||
|  |  * DatePicker renders an datepicker jQuery UI widget.
 | ||
|  |  *
 | ||
|  |  * For example:
 | ||
|  |  *
 | ||
|  |  * ```php
 | ||
| 
											12 years ago
										 |  * echo DatePicker::widget([
 | ||
| 
											13 years ago
										 |  *     'language' => 'ru',
 | ||
| 
											13 years ago
										 |  *     'model' => $model,
 | ||
|  |  *     'attribute' => 'country',
 | ||
| 
											12 years ago
										 |  *     'clientOptions' => [
 | ||
| 
											13 years ago
										 |  *         'dateFormat' => 'yy-mm-dd',
 | ||
| 
											12 years ago
										 |  *     ],
 | ||
|  |  * ]);
 | ||
| 
											13 years ago
										 |  * ```
 | ||
|  |  *
 | ||
|  |  * The following example will use the name property instead:
 | ||
|  |  *
 | ||
|  |  * ```php
 | ||
| 
											12 years ago
										 |  * echo DatePicker::widget([
 | ||
| 
											13 years ago
										 |  *     'language' => 'ru',
 | ||
| 
											13 years ago
										 |  *     'name'  => 'country',
 | ||
| 
											12 years ago
										 |  *     'clientOptions' => [
 | ||
| 
											13 years ago
										 |  *         'dateFormat' => 'yy-mm-dd',
 | ||
| 
											12 years ago
										 |  *     ],
 | ||
|  |  * ]);
 | ||
| 
											13 years ago
										 |  *```
 | ||
|  |  *
 | ||
|  |  * @see http://api.jqueryui.com/datepicker/
 | ||
|  |  * @author Alexander Kochetov <creocoder@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class DatePicker extends InputWidget
 | ||
| 
											13 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var string the locale ID (eg 'fr', 'de') for the language to be used by the date picker.
 | ||
|  | 	 * If this property set to false, I18N will not be involved. That is, the date picker will show in English.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public $language = false;
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean If true, shows the widget as an inline calendar and the input as a hidden field.
 | ||
| 
											13 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $inline = false;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Renders the widget.
 | ||
|  | 	 */
 | ||
|  | 	public function run()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		echo $this->renderWidget() . "\n";
 | ||
| 
											13 years ago
										 | 		if ($this->language !== false) {
 | ||
| 
											12 years ago
										 | 			$view = $this->getView();
 | ||
|  | 			DatePickerRegionalAsset::register($view);
 | ||
| 
											12 years ago
										 | 
 | ||
|  | 			$options = Json::encode($this->clientOptions);
 | ||
|  | 			$view->registerJs("$('#{$this->options['id']}').datepicker($.extend({}, $.datepicker.regional['{$this->language}'], $options));");
 | ||
|  | 
 | ||
| 
											12 years ago
										 | 			$options = $this->clientOptions;
 | ||
| 
											12 years ago
										 | 			$this->clientOptions = false;  // the datepicker js widget is already registered
 | ||
| 
											12 years ago
										 | 			$this->registerWidget('datepicker', DatePickerAsset::className());
 | ||
|  | 			$this->clientOptions = $options;
 | ||
|  | 		} else {
 | ||
|  | 			$this->registerWidget('datepicker', DatePickerAsset::className());
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Renders the DatePicker widget.
 | ||
| 
											13 years ago
										 | 	 * @return string the rendering result.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	protected function renderWidget()
 | ||
| 
											13 years ago
										 | 	{
 | ||
| 
											12 years ago
										 | 		$contents = [];
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 		if ($this->inline === false) {
 | ||
| 
											13 years ago
										 | 			if ($this->hasModel()) {
 | ||
|  | 				$contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options);
 | ||
|  | 			} else {
 | ||
|  | 				$contents[] = Html::textInput($this->name, $this->value, $this->options);
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			if ($this->hasModel()) {
 | ||
|  | 				$contents[] = Html::activeHiddenInput($this->model, $this->attribute, $this->options);
 | ||
|  | 				$this->clientOptions['defaultDate'] = $this->model->{$this->attribute};
 | ||
|  | 			} else {
 | ||
|  | 				$contents[] = Html::hiddenInput($this->name, $this->value, $this->options);
 | ||
|  | 				$this->clientOptions['defaultDate'] = $this->value;
 | ||
|  | 			}
 | ||
|  | 			$this->clientOptions['altField'] = '#' . $this->options['id'];
 | ||
|  | 			$this->options['id'] .= '-container';
 | ||
|  | 			$contents[] = Html::tag('div', null, $this->options);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 		return implode("\n", $contents);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | }
 |