DatePicker.php 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?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
use yii\helpers\Json;
13 14 15 16 17 18 19

/**
 * DatePicker renders an datepicker jQuery UI widget.
 *
 * For example:
 *
 * ```php
Alexander Makarov committed
20
 * echo DatePicker::widget([
21
 *     'language' => 'ru',
22 23
 *     'model' => $model,
 *     'attribute' => 'country',
Alexander Makarov committed
24
 *     'clientOptions' => [
25
 *         'dateFormat' => 'yy-mm-dd',
Alexander Makarov committed
26 27
 *     ],
 * ]);
28 29 30 31 32
 * ```
 *
 * The following example will use the name property instead:
 *
 * ```php
Alexander Makarov committed
33
 * echo DatePicker::widget([
34
 *     'language' => 'ru',
35
 *     'name'  => 'country',
Alexander Makarov committed
36
 *     'clientOptions' => [
37
 *         'dateFormat' => 'yy-mm-dd',
Alexander Makarov committed
38 39
 *     ],
 * ]);
40 41 42 43 44 45
 *```
 *
 * @see http://api.jqueryui.com/datepicker/
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
Alexander Kochetov committed
46
class DatePicker extends InputWidget
47 48
{
	/**
49 50
	 * @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.
51 52 53
	 */
	public $language = false;
	/**
Alexander Kochetov committed
54
	 * @var boolean If true, shows the widget as an inline calendar and the input as a hidden field.
55
	 */
Alexander Kochetov committed
56
	public $inline = false;
57 58 59 60 61 62 63


	/**
	 * Renders the widget.
	 */
	public function run()
	{
Alexander Kochetov committed
64
		echo $this->renderWidget() . "\n";
65
		if ($this->language !== false) {
Qiang Xue committed
66 67
			$view = $this->getView();
			DatePickerRegionalAsset::register($view);
Qiang Xue committed
68 69 70 71

			$options = Json::encode($this->clientOptions);
			$view->registerJs("$('#{$this->options['id']}').datepicker($.extend({}, $.datepicker.regional['{$this->language}'], $options));");

72
			$options = $this->clientOptions;
Qiang Xue committed
73
			$this->clientOptions = false;  // the datepicker js widget is already registered
74 75 76 77
			$this->registerWidget('datepicker', DatePickerAsset::className());
			$this->clientOptions = $options;
		} else {
			$this->registerWidget('datepicker', DatePickerAsset::className());
78 79 80 81
		}
	}

	/**
Alexander Kochetov committed
82
	 * Renders the DatePicker widget.
83 84
	 * @return string the rendering result.
	 */
Alexander Kochetov committed
85
	protected function renderWidget()
86
	{
Alexander Makarov committed
87
		$contents = [];
Alexander Kochetov committed
88

Alexander Kochetov committed
89
		if ($this->inline === false) {
Alexander Kochetov committed
90 91 92 93 94
			if ($this->hasModel()) {
				$contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options);
			} else {
				$contents[] = Html::textInput($this->name, $this->value, $this->options);
			}
95
		} else {
Alexander Kochetov committed
96 97 98 99 100 101 102 103 104 105
			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);
106
		}
Alexander Kochetov committed
107 108

		return implode("\n", $contents);
109 110
	}
}