├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── TimePicker.php └── TimePickerAsset.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All Notable changes to `yii2-timepicker` will be documented in this file. 4 | 5 | ## 1.1.0 - 2015-11-12 6 | 7 | ### Fixed 8 | - Added fallback for timepicker language files 9 | - When using translations, asset was registered without timestamp when asset manager `$appendTimestamp` was enabled 10 | - Default time removed 11 | - Travis + Scrutinizer 12 | 13 | ## 1.0.0 - 2015-06-26 14 | 15 | ### Fixed 16 | - Updated dependencies 17 | - Travis 18 | 19 | ## 0.1.0 - 2015-03-26 20 | 21 | - Initial release 22 | -------------------------------------------------------------------------------- /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/janisto/yii2-timepicker). 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 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Yii 2 Timepicker 2 | 3 | Yii 2 widget for [jQuery Timepicker Addon](https://github.com/trentrichardson/jQuery-Timepicker-Addon). 4 | 5 | [![Software License](https://img.shields.io/badge/license-Unlicense-blue.svg?style=flat-square)](LICENSE.md) 6 | [![Build Status](https://img.shields.io/travis/janisto/yii2-timepicker/master.svg?style=flat-square)](https://travis-ci.org/janisto/yii2-timepicker) 7 | [![Quality Score](https://img.shields.io/scrutinizer/g/janisto/yii2-timepicker.svg?style=flat-square)](https://scrutinizer-ci.com/g/janisto/yii2-timepicker) 8 | [![Packagist Version](https://img.shields.io/packagist/v/janisto/yii2-timepicker.svg?style=flat-square)](https://packagist.org/packages/janisto/yii2-timepicker) 9 | [![Total Downloads](https://img.shields.io/packagist/dt/janisto/yii2-timepicker.svg?style=flat-square)](https://packagist.org/packages/janisto/yii2-timepicker) 10 | 11 | ## Installation 12 | 13 | If you do not have [Composer](http://getcomposer.org/), you may install it by following the instructions 14 | at [getcomposer.org](http://getcomposer.org/doc/00-intro.md#installation-nix). 15 | 16 | You can then install this package using the following command: 17 | 18 | ```php 19 | php composer.phar require "janisto/yii2-timepicker" "*" 20 | ``` 21 | or add 22 | 23 | ```json 24 | "janisto/yii2-timepicker": "*" 25 | ``` 26 | 27 | to the require section of your application's `composer.json` file. 28 | 29 | ## Usage 30 | 31 | See [jQuery Timepicker](http://trentrichardson.com/examples/timepicker/#tp-options) options. 32 | 33 | For example to use the timepicker with a `yii\base\Model`: 34 | 35 | ```php 36 | echo TimePicker::widget([ 37 | //'language' => 'fi', 38 | 'model' => $model, 39 | 'attribute' => 'created_at', 40 | 'mode' => 'datetime', 41 | 'clientOptions' => [ 42 | 'dateFormat' => 'yy-mm-dd', 43 | 'timeFormat' => 'HH:mm:ss', 44 | 'showSecond' => true, 45 | ] 46 | ]); 47 | ``` 48 | 49 | ```php 50 | echo TimePicker::widget([ 51 | //'language' => 'fi', 52 | 'model' => $model, 53 | 'attribute' => 'created_at', 54 | 'mode' => 'datetime', 55 | 'inline' => true, 56 | 'clientOptions' => [ 57 | 'onClose' => new \yii\web\JsExpression('function(dateText, inst) { console.log("onClose: " + dateText); }'), 58 | 'onSelect' => new \yii\web\JsExpression('function(dateText, inst) { console.log("onSelect: " + dateText); }'), 59 | ] 60 | ]); 61 | ``` 62 | 63 | The following example will use the name property instead: 64 | 65 | ```php 66 | echo TimePicker::widget([ 67 | //'language' => 'fi', 68 | 'name' => 'from_time', 69 | 'value' => $value, 70 | 'mode' => 'time', 71 | 'clientOptions' => [ 72 | 'hour' => date('H'), 73 | 'minute' => date('i'), 74 | 'second' => date('s'), 75 | ] 76 | ]); 77 | ``` 78 | 79 | You can also use this widget in an `yii\widgets\ActiveForm` using the `yii\widgets\ActiveField::widget()` 80 | method, for example like this: 81 | 82 | ```php 83 | echo $form->field($model, 'field')->widget(\janisto\timepicker\TimePicker::className(), [ 84 | //'language' => 'fi', 85 | 'mode' => 'datetime', 86 | 'clientOptions' => [ 87 | 'dateFormat' => 'yy-mm-dd', 88 | 'timeFormat' => 'HH:mm:ss', 89 | 'showSecond' => true, 90 | ] 91 | ]); 92 | ``` 93 | 94 | ## Contributing 95 | 96 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 97 | 98 | ## Credits 99 | 100 | - [Jani Mikkonen](https://github.com/janisto) 101 | - [All Contributors](../../contributors) 102 | 103 | ## License 104 | 105 | Public domain. Please see [License File](LICENSE.md) for more information. 106 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "janisto/yii2-timepicker", 3 | "description": "Yii 2 widget for jQuery Timepicker Addon.", 4 | "keywords": ["yii2", "timepicker", "date", "time", "datetime", "widget", "extension"], 5 | "homepage": "https://github.com/janisto/yii2-timepicker", 6 | "type": "yii2-extension", 7 | "license": "public domain", 8 | "authors": [ 9 | { 10 | "name": "Jani Mikkonen", 11 | "email": "janisto@php.net" 12 | } 13 | ], 14 | "support": { 15 | "issues": "https://github.com/janisto/yii2-timepicker/issues?state=open", 16 | "source": "https://github.com/janisto/yii2-timepicker" 17 | }, 18 | "require": { 19 | "php": ">=5.4.0", 20 | "yiisoft/yii2": "*", 21 | "yiisoft/yii2-jui": ">=2.0.4", 22 | "bower-asset/jqueryui-timepicker-addon": ">=1.5.2" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "~4.5", 26 | "scrutinizer/ocular": "~1.1" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "janisto\\timepicker\\": "src" 31 | } 32 | }, 33 | "extra": { 34 | "asset-installer-paths": { 35 | "bower-asset-library": "vendor/bower" 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/TimePicker.php: -------------------------------------------------------------------------------- 1 | 'fi', 20 | * 'model' => $model, 21 | * 'attribute' => 'created_at', 22 | * 'mode' => 'datetime', 23 | * 'clientOptions' => [ 24 | * 'dateFormat' => 'yy-mm-dd', 25 | * 'timeFormat' => 'HH:mm:ss', 26 | * 'showSecond' => true, 27 | * ] 28 | * ]); 29 | * ``` 30 | * 31 | * ```php 32 | * echo TimePicker::widget([ 33 | * //'language' => 'fi', 34 | * 'model' => $model, 35 | * 'attribute' => 'created_at', 36 | * 'mode' => 'datetime', 37 | * 'inline' => true, 38 | * 'clientOptions' => [ 39 | * 'onClose' => new \yii\web\JsExpression('function(dateText, inst) { console.log("onClose: " + dateText); }'), 40 | * 'onSelect' => new \yii\web\JsExpression('function(dateText, inst) { console.log("onSelect: " + dateText); }'), 41 | * ] 42 | * ]); 43 | * ``` 44 | * 45 | * The following example will use the name property instead: 46 | * 47 | * ```php 48 | * echo TimePicker::widget([ 49 | * //'language' => 'fi', 50 | * 'name' => 'from_time', 51 | * 'value' => $value, 52 | * 'mode' => 'time', 53 | * 'clientOptions' => [ 54 | * 'hour' => date('H'), 55 | * 'minute' => date('i'), 56 | * 'second' => date('s'), 57 | * ] 58 | * ]); 59 | * ``` 60 | * 61 | * You can also use this widget in an `yii\widgets\ActiveForm` using the `yii\widgets\ActiveField::widget()` 62 | * method, for example like this: 63 | * 64 | * ```php 65 | * echo $form->field($model, 'field')->widget(\janisto\timepicker\TimePicker::className(), [ 66 | * //'language' => 'fi', 67 | * 'mode' => 'datetime', 68 | * 'clientOptions' => [ 69 | * 'dateFormat' => 'yy-mm-dd', 70 | * 'timeFormat' => 'HH:mm:ss', 71 | * 'showSecond' => true, 72 | * ] 73 | * ]); 74 | * ``` 75 | * 76 | * @author Jani Mikkonen 77 | * @license public domain (http://unlicense.org) 78 | * @link https://github.com/janisto/yii2-timepicker 79 | */ 80 | class TimePicker extends DatePicker 81 | { 82 | /** 83 | * @var string Widget mode (date, time or datetime). 84 | */ 85 | public $mode = 'datetime'; 86 | /** 87 | * @var string The addon markup if you wish to display the input as a component. If you don't wish to render as a 88 | * component then set it to null or false. 89 | */ 90 | public $addon = ''; 91 | /** 92 | * @var string The template to render the input. 93 | */ 94 | public $template = '{addon}{input}'; 95 | /** 96 | * @var string The size of the input (lg, md or sm) 97 | */ 98 | public $size; 99 | 100 | /** 101 | * @inheritdoc 102 | */ 103 | public function init() 104 | { 105 | parent::init(); 106 | if (!in_array($this->mode, ['date', 'time', 'datetime'])) { 107 | throw new InvalidConfigException('Unknown mode: "' . $this->mode . '". Use time, datetime or date!'); 108 | } 109 | if ($this->size) { 110 | Html::addCssClass($this->options, 'input-' . $this->size); 111 | Html::addCssClass($this->containerOptions, 'input-group-' . $this->size); 112 | } 113 | Html::addCssClass($this->options, 'form-control'); 114 | Html::addCssClass($this->containerOptions, 'input-group ' . $this->mode); 115 | } 116 | 117 | /** 118 | * @inheritdoc 119 | */ 120 | public function run() 121 | { 122 | if ($this->mode == 'date') { 123 | $this->clientOptions['showTime'] = false; 124 | } 125 | 126 | if ($this->inline === false) { 127 | if ($this->hasModel()) { 128 | $input = Html::activeTextInput($this->model, $this->attribute, $this->options); 129 | } else { 130 | $input = Html::textInput($this->name, $this->value, $this->options); 131 | } 132 | if ($this->addon) { 133 | $addon = Html::tag('span', $this->addon, ['class' => 'input-group-addon']); 134 | $input = strtr($this->template, ['{input}' => $input, '{addon}' => $addon]); 135 | $input = Html::tag('div', $input, $this->containerOptions); 136 | } 137 | } else { 138 | if ($this->hasModel()) { 139 | $input = Html::activeHiddenInput($this->model, $this->attribute, $this->options); 140 | $attribute = $this->attribute; 141 | $this->clientOptions['defaultDate'] = $this->model->$attribute; 142 | } else { 143 | $input = Html::hiddenInput($this->name, $this->value, $this->options); 144 | $this->clientOptions['defaultDate'] = $this->value; 145 | } 146 | $this->clientOptions['altField'] = '#' . $this->options['id']; 147 | $this->clientOptions['altFieldTimeOnly'] = false; 148 | $input .= Html::tag('div', null, $this->containerOptions); 149 | $input = strtr($this->template, ['{input}' => $input, '{addon}' => '']); 150 | } 151 | 152 | echo $input; 153 | $this->registerClientScript(); 154 | } 155 | 156 | /** 157 | * Register widget assets. 158 | */ 159 | public function registerClientScript() 160 | { 161 | $view = $this->getView(); 162 | $containerID = $this->inline ? $this->containerOptions['id'] : $this->options['id']; 163 | $language = $this->language ? $this->language : Yii::$app->language; 164 | $name = $this->mode . 'picker'; 165 | 166 | $timeAssetBundle = TimePickerAsset::register($view); 167 | if ($language !== 'en-US') { 168 | $timeAssetBundle->language = $language; 169 | $dateAssetBundle = DatePickerLanguageAsset::register($view); 170 | $dateAssetBundle->language = $language; 171 | } 172 | 173 | $this->registerClientOptions($name, $containerID); 174 | $this->registerClientEvents($name, $containerID); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/TimePickerAsset.php: -------------------------------------------------------------------------------- 1 | css[] = 'jquery-ui-timepicker-addon' . (YII_DEBUG ? '' : '.min') . '.css'; 31 | $this->js[] = 'jquery-ui-timepicker-addon' . (YII_DEBUG ? '' : '.min') . '.js'; 32 | 33 | $language = $this->language; 34 | 35 | if ($language !== null && $language !== 'en-US') { 36 | $fallbackLanguage = substr($language, 0, 2); 37 | if ($fallbackLanguage !== $language && !file_exists(Yii::getAlias($this->sourcePath . "/i18n/jquery-ui-timepicker-{$language}.js"))) { 38 | $language = $fallbackLanguage; 39 | } 40 | $this->js[] = "i18n/jquery-ui-timepicker-{$language}.js"; 41 | } 42 | 43 | parent::registerAssetFiles($view); 44 | } 45 | } 46 | --------------------------------------------------------------------------------