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

namespace yii\jui;

10
use Yii;
11
use yii\helpers\Html;
12
use yii\helpers\Json;
13 14

/**
15
 * DatePicker renders a datepicker jQuery UI widget.
16 17 18 19
 *
 * 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
	 * @var string the locale ID (eg 'fr', 'de') for the language to be used by the date picker.
50
	 * If this property is empty, then the current application language will be used.
51
	 */
52
	public $language;
53
	/**
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
	/**
	 * @var array the HTML attributes for the container tag. This is only used when [[inline]] is true.
	 */
	public $containerOptions = [];
61 62


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

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

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

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

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

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

		return implode("\n", $contents);
125 126
	}
}