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
12 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;
|
||
11 years ago
|
use yii\helpers\Json;
|
||
12 years ago
|
|
||
|
/**
|
||
|
* DatePicker renders an datepicker jQuery UI widget.
|
||
|
*
|
||
|
* For example:
|
||
|
*
|
||
|
* ```php
|
||
11 years ago
|
* echo DatePicker::widget([
|
||
12 years ago
|
* 'language' => 'ru',
|
||
12 years ago
|
* 'model' => $model,
|
||
|
* 'attribute' => 'country',
|
||
11 years ago
|
* 'clientOptions' => [
|
||
12 years ago
|
* 'dateFormat' => 'yy-mm-dd',
|
||
11 years ago
|
* ],
|
||
|
* ]);
|
||
12 years ago
|
* ```
|
||
|
*
|
||
|
* The following example will use the name property instead:
|
||
|
*
|
||
|
* ```php
|
||
11 years ago
|
* echo DatePicker::widget([
|
||
12 years ago
|
* 'language' => 'ru',
|
||
12 years ago
|
* 'name' => 'country',
|
||
11 years ago
|
* 'clientOptions' => [
|
||
12 years ago
|
* 'dateFormat' => 'yy-mm-dd',
|
||
11 years ago
|
* ],
|
||
|
* ]);
|
||
12 years ago
|
*```
|
||
|
*
|
||
|
* @see http://api.jqueryui.com/datepicker/
|
||
|
* @author Alexander Kochetov <creocoder@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class DatePicker extends InputWidget
|
||
12 years ago
|
{
|
||
|
/**
|
||
12 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.
|
||
12 years ago
|
*/
|
||
|
public $language = false;
|
||
|
/**
|
||
12 years ago
|
* @var boolean If true, shows the widget as an inline calendar and the input as a hidden field.
|
||
12 years ago
|
*/
|
||
12 years ago
|
public $inline = false;
|
||
12 years ago
|
|
||
|
|
||
|
/**
|
||
|
* Renders the widget.
|
||
|
*/
|
||
|
public function run()
|
||
|
{
|
||
12 years ago
|
echo $this->renderWidget() . "\n";
|
||
12 years ago
|
if ($this->language !== false) {
|
||
12 years ago
|
$view = $this->getView();
|
||
|
DatePickerRegionalAsset::register($view);
|
||
11 years ago
|
|
||
|
$options = Json::encode($this->clientOptions);
|
||
|
$view->registerJs("$('#{$this->options['id']}').datepicker($.extend({}, $.datepicker.regional['{$this->language}'], $options));");
|
||
|
|
||
11 years ago
|
$options = $this->clientOptions;
|
||
11 years ago
|
$this->clientOptions = false; // the datepicker js widget is already registered
|
||
11 years ago
|
$this->registerWidget('datepicker', DatePickerAsset::className());
|
||
|
$this->clientOptions = $options;
|
||
|
} else {
|
||
|
$this->registerWidget('datepicker', DatePickerAsset::className());
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
12 years ago
|
* Renders the DatePicker widget.
|
||
12 years ago
|
* @return string the rendering result.
|
||
|
*/
|
||
12 years ago
|
protected function renderWidget()
|
||
12 years ago
|
{
|
||
11 years ago
|
$contents = [];
|
||
12 years ago
|
|
||
12 years ago
|
if ($this->inline === false) {
|
||
12 years ago
|
if ($this->hasModel()) {
|
||
|
$contents[] = Html::activeTextInput($this->model, $this->attribute, $this->options);
|
||
|
} else {
|
||
|
$contents[] = Html::textInput($this->name, $this->value, $this->options);
|
||
|
}
|
||
12 years ago
|
} else {
|
||
12 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);
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
return implode("\n", $contents);
|
||
12 years ago
|
}
|
||
|
}
|