├── .gitignore ├── LICENSE ├── README.md ├── UPGRADE.md ├── composer.json └── src ├── DatePicker.php ├── DatePickerAsset.php ├── DateRangePicker.php ├── DateRangePickerAsset.php ├── DateTimePicker.php ├── DateTimePickerAsset.php ├── DateWidget.php └── MomentAsset.php /.gitignore: -------------------------------------------------------------------------------- 1 | # IDE & OS files 2 | .*.swp 3 | .DS_Store 4 | .buildpath 5 | .idea 6 | .project 7 | .settings 8 | Thumbs.db 9 | nbproject 10 | 11 | # vendor dirs 12 | vendor 13 | 14 | # composer lock files 15 | composer.lock 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, Roman Zhuravlev 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Datetime widgets for Yii2 2 | ========================= 3 | 4 | [![Latest Stable Version](https://poser.pugx.org/zhuravljov/yii2-datetime-widgets/version.svg)](https://packagist.org/packages/zhuravljov/yii2-datetime-widgets) 5 | [![Total Downloads](https://poser.pugx.org/zhuravljov/yii2-datetime-widgets/downloads.png)](https://packagist.org/packages/zhuravljov/yii2-datetime-widgets) 6 | 7 | Installation 8 | ------------ 9 | 10 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 11 | 12 | Add 13 | 14 | ``` 15 | "zhuravljov/yii2-datetime-widgets" : "~1.1.0" 16 | ``` 17 | 18 | to the require section of your application's `composer.json` file. 19 | 20 | DatePicker 21 | ---------- 22 | 23 | [Demo](https://uxsolutions.github.io/bootstrap-datepicker) from the bootstrap-datepicker plugin website. 24 | 25 | ### Usage 26 | 27 | ```php 28 | field($model, 'attribute')->widget(DatePicker::class, [ 29 | 'clientOptions' => [ 30 | 'format' => 'dd.mm.yyyy', 31 | 'language' => 'ru', 32 | 'autoclose' => true, 33 | 'todayHighlight' => true, 34 | ], 35 | 'clientEvents' => [], 36 | ]) ?> 37 | ``` 38 | 39 | DateTimePicker 40 | -------------- 41 | 42 | [Demo](http://www.malot.fr/bootstrap-datetimepicker/demo.php) from the bootstrap-datetimepicker plugin website. 43 | 44 | 45 | ### Usage 46 | 47 | ```php 48 | field($model, 'attribute')->widget(DateTimePicker::class, [ 49 | 'clientOptions' => [ 50 | 'format' => 'dd.mm.yyyy hh:ii', 51 | 'language' => 'ru', 52 | 'autoclose' => true, 53 | ], 54 | 'clientEvents' => [], 55 | ]) ?> 56 | ``` 57 | 58 | DateRangePicker 59 | --------------- 60 | 61 | [Demo](http://www.daterangepicker.com) from the bootstrap-daterangepicker plugin website. 62 | 63 | ### Usage 64 | 65 | ```php 66 | field($model, 'actual_time')->widget(DateRangePicker::class, [ 67 | 'clientOptions' => [ 68 | 'locale' => [ 69 | 'format' => 'YYYY-MM-DD', 70 | 'separator' => ' - ', 71 | ], 72 | 'ranges' => [ 73 | 'Today' => [ 74 | new JsExpression('moment()'), 75 | new JsExpression('moment()'), 76 | ], 77 | 'Yesterday' => [ 78 | new JsExpression('moment().subtract(1, "days")'), 79 | new JsExpression('moment().subtract(1, "days")'), 80 | ], 81 | ], 82 | ], 83 | 'clientEvents' => [], 84 | ]) ?> 85 | ``` 86 | -------------------------------------------------------------------------------- /UPGRADE.md: -------------------------------------------------------------------------------- 1 | Upgrading Instructions 2 | ====================== 3 | 4 | This file contains the upgrade notes. These notes highlight changes that could break your 5 | application when you upgrade the package from one version to another. 6 | 7 | Upgrade from 1.0.2 8 | ------------------ 9 | 10 | * Required PHP 5.5.0 and above. 11 | 12 | * Namespace `zhuravljov\widgets` changed to `zhuravljov\yii\widgets`. 13 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zhuravljov/yii2-datetime-widgets", 3 | "description": "Datetime widgets for Yii2.", 4 | "keywords": ["yii", "widget", "datepicker", "datetimepicker", "daterangepicker"], 5 | "type": "yii2-extension", 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Roman Zhuravlev", 10 | "email": "zhuravljov@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.5.0", 15 | "yiisoft/yii2": "~2.0.0", 16 | "yiisoft/yii2-bootstrap": "~2.0.0", 17 | "bower-asset/bootstrap-datepicker": "*", 18 | "bower-asset/smalot-bootstrap-datetimepicker": "*", 19 | "bower-asset/moment": "*", 20 | "bower-asset/bootstrap-daterangepicker": "*" 21 | }, 22 | "repositories": [ 23 | { 24 | "type": "composer", 25 | "url": "https://asset-packagist.org" 26 | } 27 | ], 28 | "autoload": { 29 | "psr-4": { 30 | "zhuravljov\\yii\\widgets\\": "src" 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/DatePicker.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | class DatePicker extends DateWidget 18 | { 19 | /** 20 | * @var array options for datepicker 21 | */ 22 | public $clientOptions = []; 23 | /** 24 | * @var array events for datepicker 25 | */ 26 | public $clientEvents = []; 27 | 28 | /** 29 | * @inheritdoc 30 | */ 31 | protected function registerPlugin() 32 | { 33 | $asset = DatePickerAsset::register($this->view); 34 | if (isset($this->clientOptions['language'])) { 35 | $lang = $this->clientOptions['language']; 36 | $this->view->registerJsFile($asset->baseUrl . "/locales/bootstrap-datepicker.$lang.min.js", [ 37 | 'depends' => DatePickerAsset::class, 38 | ]); 39 | } 40 | 41 | $id = $this->options['id']; 42 | $options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); 43 | $js = "jQuery('#$id').datepicker($options)"; 44 | foreach ($this->clientEvents as $event => $handler) { 45 | $js .= ".on('$event', $handler)"; 46 | } 47 | $this->view->registerJs($js . ';'); 48 | } 49 | } -------------------------------------------------------------------------------- /src/DatePickerAsset.php: -------------------------------------------------------------------------------- 1 | 18 | * 19 | * @see https://github.com/uxsolutions/bootstrap-datepicker/blob/master/bower.json 20 | */ 21 | class DatePickerAsset extends AssetBundle 22 | { 23 | public $sourcePath = '@bower/bootstrap-datepicker/dist'; 24 | public $js = [ 25 | 'js/bootstrap-datepicker.js', 26 | ]; 27 | public $css = [ 28 | 'css/bootstrap-datepicker3.css', 29 | ]; 30 | public $depends = [ 31 | JqueryAsset::class, 32 | BootstrapAsset::class, 33 | ]; 34 | } -------------------------------------------------------------------------------- /src/DateRangePicker.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | class DateRangePicker extends DateWidget 18 | { 19 | /** 20 | * @var array options for js plugin 21 | */ 22 | public $clientOptions = []; 23 | /** 24 | * @var array events for js plugin 25 | */ 26 | public $clientEvents = []; 27 | 28 | /** 29 | * @inheritdoc 30 | */ 31 | protected function registerPlugin() 32 | { 33 | DateRangePickerAsset::register($this->view); 34 | 35 | $id = $this->options['id']; 36 | $options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); 37 | $js = "jQuery('#$id').daterangepicker($options)"; 38 | foreach ($this->clientEvents as $event => $handler) { 39 | $js .= ".on('$event', $handler)"; 40 | } 41 | $this->view->registerJs($js . ';'); 42 | } 43 | } -------------------------------------------------------------------------------- /src/DateRangePickerAsset.php: -------------------------------------------------------------------------------- 1 | 18 | * 19 | * @see https://github.com/dangrossman/bootstrap-daterangepicker 20 | */ 21 | class DateRangePickerAsset extends AssetBundle 22 | { 23 | public $sourcePath = '@bower/bootstrap-daterangepicker'; 24 | public $js = [ 25 | 'daterangepicker.js', 26 | ]; 27 | public $css = [ 28 | 'daterangepicker.css', 29 | ]; 30 | public $depends = [ 31 | JqueryAsset::class, 32 | BootstrapAsset::class, 33 | MomentAsset::class, 34 | ]; 35 | } -------------------------------------------------------------------------------- /src/DateTimePicker.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | class DateTimePicker extends DateWidget 18 | { 19 | /** 20 | * @var array options for datetimepicker 21 | */ 22 | public $clientOptions = []; 23 | /** 24 | * @var array events for datetimepicker 25 | */ 26 | public $clientEvents = []; 27 | 28 | /** 29 | * @inheritdoc 30 | */ 31 | protected function registerPlugin() 32 | { 33 | $asset = DateTimePickerAsset::register($this->view); 34 | if (isset($this->clientOptions['language'])) { 35 | $lang = $this->clientOptions['language']; 36 | $this->view->registerJsFile($asset->baseUrl . "/js/locales/bootstrap-datetimepicker.$lang.js", [ 37 | 'depends' => DateTimePickerAsset::class, 38 | ]); 39 | } 40 | 41 | $id = $this->options['id']; 42 | $options = empty($this->clientOptions) ? '' : Json::encode($this->clientOptions); 43 | $js = "jQuery('#$id').datetimepicker($options)"; 44 | foreach ($this->clientEvents as $event => $handler) { 45 | $js .= ".on('$event', $handler)"; 46 | } 47 | $this->view->registerJs($js . ';'); 48 | } 49 | } -------------------------------------------------------------------------------- /src/DateTimePickerAsset.php: -------------------------------------------------------------------------------- 1 | 18 | * 19 | * @see https://github.com/smalot/bootstrap-datetimepicker/blob/master/bower.json 20 | */ 21 | class DateTimePickerAsset extends AssetBundle 22 | { 23 | public $sourcePath = '@bower/smalot-bootstrap-datetimepicker'; 24 | public $js = [ 25 | 'js/bootstrap-datetimepicker.js', 26 | ]; 27 | public $css = [ 28 | 'css/bootstrap-datetimepicker.css', 29 | ]; 30 | public $depends = [ 31 | JqueryAsset::class, 32 | BootstrapAsset::class, 33 | ]; 34 | } -------------------------------------------------------------------------------- /src/DateWidget.php: -------------------------------------------------------------------------------- 1 | 17 | */ 18 | abstract class DateWidget extends InputWidget 19 | { 20 | /** 21 | * @var string the template to render the input 22 | */ 23 | public $template = '{input}'; 24 | /** 25 | * @var array the HTML attributes for the input tag. 26 | */ 27 | public $options = [ 28 | 'class' => 'form-control', 29 | 'autocomplete' => 'off', 30 | ]; 31 | /** 32 | * @var string|null 33 | */ 34 | public $icon = 'calendar'; 35 | 36 | /** 37 | * Renders field 38 | */ 39 | public function run() 40 | { 41 | $this->registerPlugin(); 42 | 43 | if (is_string($this->icon)) { 44 | Html::addCssClass($this->field->options, 'has-feedback'); 45 | $this->template = strtr($this->template, [ 46 | '{input}' => '{input}' . Html::tag('span', '', [ 47 | 'class' => 'form-control-feedback glyphicon glyphicon-' . $this->icon, 48 | ]), 49 | ]); 50 | } 51 | 52 | return strtr($this->template, [ 53 | '{input}' => $this->hasModel() 54 | ? Html::activeTextInput($this->model, $this->attribute, $this->options) 55 | : Html::textInput($this->name, $this->value, $this->options), 56 | ]); 57 | } 58 | 59 | /** 60 | * Registers plugin 61 | */ 62 | abstract protected function registerPlugin(); 63 | } -------------------------------------------------------------------------------- /src/MomentAsset.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | class MomentAsset extends AssetBundle 18 | { 19 | public $sourcePath = '@bower/moment/min'; 20 | public $js = [ 21 | 'moment-with-locales.min.js', 22 | ]; 23 | } --------------------------------------------------------------------------------