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

/**
 * DatePicker renders an datepicker jQuery UI widget.
 *
 * For example:
 *
 * ```php
 * echo DatePicker::widget(array(
20
 *     'language' => 'ru',
21 22 23 24 25 26 27 28 29 30 31 32
 *     'model' => $model,
 *     'attribute' => 'country',
 *     'clientOptions' => array(
 *         'dateFormat' => 'yy-mm-dd',
 *     ),
 * ));
 * ```
 *
 * The following example will use the name property instead:
 *
 * ```php
 * echo DatePicker::widget(array(
33
 *     'language' => 'ru',
34 35 36 37 38 39 40 41 42 43 44
 *     'name'  => 'country',
 *     'clientOptions' => array(
 *         'dateFormat' => 'yy-mm-dd',
 *     ),
 * ));
 *```
 *
 * @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 60 61 62


	/**
	 * Renders the widget.
	 */
	public function run()
	{
Alexander Kochetov committed
63
		echo $this->renderWidget() . "\n";
64 65
		$this->registerWidget('datepicker');
		if ($this->language !== false) {
66
			$this->getView()->registerAssetBundle("yii/jui/datepicker/i18n/$this->language");
67 68 69 70
		}
	}

	/**
Alexander Kochetov committed
71
	 * Renders the DatePicker widget.
72 73
	 * @return string the rendering result.
	 */
Alexander Kochetov committed
74
	protected function renderWidget()
75
	{
Alexander Kochetov committed
76 77
		$contents = array();

Alexander Kochetov committed
78
		if ($this->inline === false) {
Alexander Kochetov committed
79 80 81 82 83
			if ($this->hasModel()) {
				$contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options);
			} else {
				$contents[] = Html::textInput($this->name, $this->value, $this->options);
			}
84
		} else {
Alexander Kochetov committed
85 86 87 88 89 90 91 92 93 94
			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);
95
		}
Alexander Kochetov committed
96 97

		return implode("\n", $contents);
98 99
	}
}