DatePicker.php 3.85 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
 *     'model' => $model,
23
 *     'attribute' => 'from_date',
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'  => 'from_date',
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 51 52 53 54 55 56 57 58 59 60 61
    /**
     * @var string the locale ID (eg 'fr', 'de') for the language to be used by the date picker.
     * If this property is empty, then the current application language will be used.
     */
    public $language;
    /**
     * @var boolean If true, shows the widget as an inline calendar and the input as a hidden field.
     */
    public $inline = false;
    /**
     * @var array the HTML attributes for the container tag. This is only used when [[inline]] is true.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $containerOptions = [];
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 79 80 81 82
    /**
     * Renders the widget.
     */
    public function run()
    {
        echo $this->renderWidget() . "\n";
        $containerID = $this->inline ? $this->containerOptions['id'] : $this->options['id'];
        $language = $this->language ? $this->language : Yii::$app->language;
83
        if ($language != 'en-US') {
84 85
            $view = $this->getView();
            DatePickerRegionalAsset::register($view);
86

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

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

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

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

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