DatePicker.php 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\jui;

use yii\helpers\Html;
11
use yii\helpers\Json;
12 13 14 15 16 17 18

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


62 63 64 65 66 67 68 69 70 71 72
	/**
	 * @inheritdoc
	 */
	public function init()
	{
		parent::init();
		if ($this->inline && !isset($this->containerOptions['id'])) {
			$this->containerOptions['id'] = $this->options['id'] . '-container';
		}
	}

73 74 75 76 77
	/**
	 * Renders the widget.
	 */
	public function run()
	{
Alexander Kochetov committed
78
		echo $this->renderWidget() . "\n";
79
		$containerID = $this->inline ? $this->containerOptions['id'] : $this->options['id'];
80
		if ($this->language !== false) {
Qiang Xue committed
81 82
			$view = $this->getView();
			DatePickerRegionalAsset::register($view);
Qiang Xue committed
83 84

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

87
			$options = $this->clientOptions;
Qiang Xue committed
88
			$this->clientOptions = false;  // the datepicker js widget is already registered
89
			$this->registerWidget('datepicker', DatePickerAsset::className(), $containerID);
90 91
			$this->clientOptions = $options;
		} else {
92
			$this->registerWidget('datepicker', DatePickerAsset::className(), $containerID);
93 94 95 96
		}
	}

	/**
Alexander Kochetov committed
97
	 * Renders the DatePicker widget.
98 99
	 * @return string the rendering result.
	 */
Alexander Kochetov committed
100
	protected function renderWidget()
101
	{
Alexander Makarov committed
102
		$contents = [];
Alexander Kochetov committed
103

Alexander Kochetov committed
104
		if ($this->inline === false) {
Alexander Kochetov committed
105 106 107 108 109
			if ($this->hasModel()) {
				$contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options);
			} else {
				$contents[] = Html::textInput($this->name, $this->value, $this->options);
			}
110
		} else {
Alexander Kochetov committed
111 112 113 114 115 116 117 118
			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'];
119
			$contents[] = Html::tag('div', null, $this->containerOptions);
120
		}
Alexander Kochetov committed
121 122

		return implode("\n", $contents);
123 124
	}
}