├── CHANGELOG.md ├── src ├── DatePickerLanguageAsset.php ├── assets │ └── css │ │ └── bootstrap-daterangepicker.css ├── DateRangePickerAsset.php ├── DatePickerAsset.php ├── DatePickerTrait.php ├── DatePicker.php └── DateRangePicker.php ├── CONTRIBUTING.md ├── composer.json ├── LICENSE.md └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.0.5 - 2015-04-02 4 | 5 | ## Changed 6 | - Added CHANGELOG.md file 7 | 8 | ## Fixed 9 | - Fixed asset registration bug when using form 10 | -------------------------------------------------------------------------------- /src/DatePickerLanguageAsset.php: -------------------------------------------------------------------------------- 1 | 15 | * @link http://www.ramirezcobos.com/ 16 | * @link http://www.2amigos.us/ 17 | * @package dosamigos\datepicker 18 | */ 19 | class DatePickerLanguageAsset extends AssetBundle 20 | { 21 | public $sourcePath = '@bower/bootstrap-datepicker/dist/locales'; 22 | 23 | public $depends = [ 24 | 'dosamigos\datepicker\DateRangePickerAsset' 25 | ]; 26 | } 27 | -------------------------------------------------------------------------------- /src/assets/css/bootstrap-daterangepicker.css: -------------------------------------------------------------------------------- 1 | /*! 2 | * Custom styling. Removing padding when using DateRangePicker. 3 | */ 4 | .datepicker { 5 | z-index: 1151 !important; 6 | } 7 | 8 | .datepicker-range { 9 | padding: 0 !important; 10 | border: 0 !important; 11 | text-align: left !important; 12 | } 13 | 14 | .datepicker-from { 15 | border-radius: 4px !important; 16 | border-top-right-radius: 0 !important; 17 | border-bottom-right-radius: 0 !important; 18 | text-align: left !important; 19 | } 20 | 21 | .datepicker-to { 22 | border-radius: 4px !important; 23 | border-top-left-radius: 0 !important; 24 | border-bottom-left-radius: 0 !important; 25 | text-align: left !important; 26 | } 27 | 28 | div.datepicker-range { 29 | width: 100%; 30 | } 31 | -------------------------------------------------------------------------------- /src/DateRangePickerAsset.php: -------------------------------------------------------------------------------- 1 | 15 | * @link http://www.ramirezcobos.com/ 16 | * @link http://www.2amigos.us/ 17 | * @package dosamigos\datepicker 18 | */ 19 | class DateRangePickerAsset extends AssetBundle 20 | { 21 | public $sourcePath = '@vendor/2amigos/yii2-date-picker-widget/src/assets'; 22 | 23 | public $css = [ 24 | 'css/bootstrap-daterangepicker.css' 25 | ]; 26 | 27 | public $depends = [ 28 | 'dosamigos\datepicker\DatePickerAsset' 29 | ]; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/DatePickerAsset.php: -------------------------------------------------------------------------------- 1 | 15 | * @link http://www.ramirezcobos.com/ 16 | * @link http://www.2amigos.us/ 17 | * @package dosamigos\datepicker 18 | */ 19 | class DatePickerAsset extends AssetBundle 20 | { 21 | public $sourcePath = '@bower/bootstrap-datepicker/dist'; 22 | 23 | public $css = [ 24 | 'css/bootstrap-datepicker3.css', 25 | ]; 26 | 27 | public $js = [ 28 | 'js/bootstrap-datepicker.js' 29 | ]; 30 | 31 | public $depends = [ 32 | 'yii\bootstrap\BootstrapPluginAsset' 33 | ]; 34 | } 35 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/2amigos/yii2-datepicker-widget). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting. 23 | 24 | 25 | ## Running Tests 26 | 27 | ``` bash 28 | $ phpunit 29 | ``` 30 | 31 | 32 | **Happy coding**! 33 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2amigos/yii2-date-picker-widget", 3 | "description": "Bootstrap DatePicker widget for Yii2.", 4 | "type": "yii2-extension", 5 | "keywords": [ 6 | "2amigos", 7 | "yii", 8 | "yii2", 9 | "yii 2", 10 | "extension", 11 | "widget", 12 | "datepicker" 13 | ], 14 | "homepage": "http://yiiwheels.com/extension/bootstrap-datepicker", 15 | "license": "BSD-3-Clause", 16 | "authors": [ 17 | { 18 | "name": "2amigOS! Consulting Group", 19 | "email": "hola@2amigos.us", 20 | "homepage": "http://2amigos.us", 21 | "role": "Developer" 22 | } 23 | ], 24 | "support": { 25 | "issues": "https://github.com/2amigos/yii2-date-picker-widget/issues", 26 | "source": "https://github.com/2amigos/yii2-date-picker-widget" 27 | }, 28 | "require": { 29 | "yiisoft/yii2": ">=2.0.9", 30 | "yiisoft/yii2-bootstrap": "*", 31 | "bower-asset/bootstrap-datepicker": "1.7.0" 32 | }, 33 | "require-dev": { 34 | "phpunit/phpunit": "4.*" 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "dosamigos\\datepicker\\": "src" 39 | } 40 | }, 41 | "extra": { 42 | "branch-alias": { 43 | "dev-master": "1.0-dev" 44 | }, 45 | "asset-installer-paths": { 46 | "bower-asset-library": "vendor/bower" 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/DatePickerTrait.php: -------------------------------------------------------------------------------- 1 | 13 | * @link http://www.ramirezcobos.com/ 14 | * @link http://www.2amigos.us/ 15 | * @package dosamigos\datepicker 16 | */ 17 | trait DatePickerTrait 18 | { 19 | /** 20 | * @var string the language to use 21 | */ 22 | public $language; 23 | /** 24 | * @var array the options for the Bootstrap DatePicker plugin. 25 | * Please refer to the Bootstrap DatePicker plugin Web page for possible options. 26 | * @see http://bootstrap-datepicker.readthedocs.org/en/release/options.html 27 | */ 28 | public $clientOptions = []; 29 | /** 30 | * @var array the event handlers for the underlying Bootstrap DatePicker plugin. 31 | * Please refer to the [DatePicker](http://bootstrap-datepicker.readthedocs.org/en/release/events.html) plugin 32 | * Web page for possible events. 33 | */ 34 | public $clientEvents = []; 35 | /** 36 | * @var string the size of the input ('lg', 'md', 'sm', 'xs') 37 | */ 38 | public $size; 39 | /** 40 | * @var array HTML attributes to render on the container 41 | */ 42 | public $containerOptions = []; 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The BSD License (BSD) 2 | 3 | Copyright (c) 2013-2015, 2amigOS! Consulting Group LLC. 4 | 5 | > Redistribution and use in source and binary forms, with or without modification, 6 | > are permitted provided that the following conditions are met: 7 | > 8 | > Redistributions of source code must retain the above copyright notice, this 9 | > list of conditions and the following disclaimer. 10 | > 11 | > Redistributions in binary form must reproduce the above copyright notice, this 12 | > list of conditions and the following disclaimer in the documentation and/or 13 | > other materials provided with the distribution. 14 | > 15 | > Neither the name of 2amigOS! Consulting Group, LLC. nor the names of its 16 | > contributors may be used to endorse or promote products derived from 17 | > this software without specific prior written permission. 18 | > 19 | >THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 | >ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | >WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | >DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 | >ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 | >(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | >LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 | >ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | >(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | >SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /src/DatePicker.php: -------------------------------------------------------------------------------- 1 | 17 | * @link http://www.ramirezcobos.com/ 18 | * @link http://www.2amigos.us/ 19 | * @package dosamigos\datepicker 20 | */ 21 | class DatePicker extends InputWidget 22 | { 23 | use DatePickerTrait; 24 | 25 | /** 26 | * @var string the addon markup if you wish to display the input as a component. If you don't wish to render as a 27 | * component then set it to null or false. 28 | */ 29 | public $addon = ''; 30 | /** 31 | * @var string the template to render the input. 32 | */ 33 | public $template = '{input}{addon}'; 34 | /** 35 | * @var bool whether to render the input as an inline calendar 36 | */ 37 | public $inline = false; 38 | 39 | /** 40 | * @inheritdoc 41 | */ 42 | public function init() 43 | { 44 | parent::init(); 45 | 46 | if ($this->inline) { 47 | $this->options['readonly'] = 'readonly'; 48 | Html::addCssClass($this->options, 'text-center'); 49 | } 50 | if ($this->size) { 51 | Html::addCssClass($this->options, 'input-' . $this->size); 52 | Html::addCssClass($this->containerOptions, 'input-group-' . $this->size); 53 | } 54 | Html::addCssClass($this->options, 'form-control'); 55 | Html::addCssClass($this->containerOptions, 'input-group date'); 56 | } 57 | 58 | /** 59 | * @inheritdoc 60 | */ 61 | public function run() 62 | { 63 | 64 | $input = $this->hasModel() 65 | ? Html::activeTextInput($this->model, $this->attribute, $this->options) 66 | : Html::textInput($this->name, $this->value, $this->options); 67 | 68 | if ($this->inline) { 69 | $input .= '
'; 70 | } 71 | if ($this->addon && !$this->inline) { 72 | $addon = Html::tag('span', $this->addon, ['class' => 'input-group-addon']); 73 | $input = strtr($this->template, ['{input}' => $input, '{addon}' => $addon]); 74 | $input = Html::tag('div', $input, $this->containerOptions); 75 | } 76 | if ($this->inline) { 77 | $input = strtr($this->template, ['{input}' => $input, '{addon}' => '']); 78 | } 79 | echo $input; 80 | 81 | $this->registerClientScript(); 82 | } 83 | 84 | /** 85 | * Registers required script for the plugin to work as DatePicker 86 | */ 87 | public function registerClientScript() 88 | { 89 | $js = []; 90 | $view = $this->getView(); 91 | 92 | // @codeCoverageIgnoreStart 93 | if ($this->language !== null && $this->language !== 'en') { 94 | $this->clientOptions['language'] = $this->language; 95 | DatePickerLanguageAsset::register($view)->js[] = 'bootstrap-datepicker.' . $this->language . '.min.js'; 96 | } else { 97 | DatePickerAsset::register($view); 98 | } 99 | // @codeCoverageIgnoreEnd 100 | 101 | $id = $this->options['id']; 102 | $selector = ";jQuery('#$id')"; 103 | 104 | if ($this->addon || $this->inline) { 105 | $selector .= ".parent()"; 106 | } 107 | 108 | $options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : ''; 109 | 110 | if ($this->inline) { 111 | $this->clientEvents['changeDate'] = "function (e){ jQuery('#$id').val(e.format());}"; 112 | } 113 | 114 | $js[] = "$selector.datepicker($options);"; 115 | 116 | if (!empty($this->clientEvents)) { 117 | foreach ($this->clientEvents as $event => $handler) { 118 | $js[] = "$selector.on('$event', $handler);"; 119 | } 120 | } 121 | $view->registerJs(implode("\n", $js)); 122 | } 123 | 124 | } 125 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Bootstrap DatePicker Widget for Yii2 2 | ==================================== 3 | 4 | [](https://github.com/2amigos/yii2-date-picker-widget/tags) 5 | [](LICENSE.md) 6 | [](https://travis-ci.org/2amigos/yii2-date-picker-widget) 7 | [](https://scrutinizer-ci.com/g/2amigos/yii2-date-picker-widget/code-structure) 8 | [](https://scrutinizer-ci.com/g/2amigos/yii2-date-picker-widget) 9 | [](https://packagist.org/packages/2amigos/yii2-date-picker-widget) 10 | 11 | 12 | Renders a [Bootstrap DatePicker plugin](http://bootstrapformhelpers.com/datepicker/). 13 | 14 | Installation 15 | ------------ 16 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 17 | 18 | Either run 19 | 20 | ```bash 21 | $ composer require 2amigos/yii2-date-picker-widget:~1.0 22 | ``` 23 | or add 24 | 25 | ```json 26 | "2amigos/yii2-date-picker-widget" : "~1.0" 27 | ``` 28 | 29 | to the require section of your application's `composer.json` file. 30 | 31 | Usage 32 | ----- 33 | The widget comes in two flavors: 34 | 35 | - DatePicker 36 | - DateRangePicker 37 | 38 | **DatePicker** 39 | 40 | This widget renders a Bootstrap DatePicker input control. Best suitable for model with date string attribute. 41 | 42 | ***Example of use with a form*** 43 | There are two ways of using it, with an `ActiveForm` instance or as a widget setting up its `model` and `attribute`. 44 | 45 | ```php 46 | 51 | 52 | = DatePicker::widget([ 53 | 'model' => $model, 54 | 'attribute' => 'date', 55 | 'template' => '{addon}{input}', 56 | 'clientOptions' => [ 57 | 'autoclose' => true, 58 | 'format' => 'dd-M-yyyy' 59 | ] 60 | ]);?> 61 | 62 | 65 | = $form->field($model, 'date')->widget( 66 | DatePicker::className(), [ 67 | // inline too, not bad 68 | 'inline' => true, 69 | // modify template for custom rendering 70 | 'template' => '