Commit ef21eb82 authored by knight's avatar knight

init

parents
# Changelog
## 1.0.5 - 2015-04-02
## Changed
- Added CHANGELOG.md file
## Fixed
- Fixed asset registration bug when using form
# Contributing
Contributions are **welcome** and will be fully **credited**.
We accept contributions via Pull Requests on [Github](https://github.com/2amigos/yii2-datepicker-widget).
## Pull Requests
- **[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).
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
- **Create feature branches** - Don't ask us to pull from your master branch.
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
- **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.
## Running Tests
``` bash
$ phpunit
```
**Happy coding**!
# The BSD License (BSD)
Copyright (c) 2013-2015, 2amigOS! Consulting Group LLC.
> Redistribution and use in source and binary forms, with or without modification,
> are permitted provided that the following conditions are met:
>
> Redistributions of source code must retain the above copyright notice, this
> list of conditions and the following disclaimer.
>
> Redistributions in binary form must reproduce the above copyright notice, this
> list of conditions and the following disclaimer in the documentation and/or
> other materials provided with the distribution.
>
> Neither the name of 2amigOS! Consulting Group, LLC. nor the names of its
> contributors may be used to endorse or promote products derived from
> this software without specific prior written permission.
>
>THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
>ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
>WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
>DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
>ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
>(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
>LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
>ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
>(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
>SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Bootstrap DatePicker Widget for Yii2
====================================
[![Latest Version](https://img.shields.io/github/tag/2amigos/yii2-date-picker-widget.svg?style=flat-square&label=release)](https://github.com/2amigos/yii2-date-picker-widget/tags)
[![Software License](https://img.shields.io/badge/license-BSD-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/2amigos/yii2-date-picker-widget/master.svg?style=flat-square)](https://travis-ci.org/2amigos/yii2-date-picker-widget)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/2amigos/yii2-date-picker-widget.svg?style=flat-square)](https://scrutinizer-ci.com/g/2amigos/yii2-date-picker-widget/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/2amigos/yii2-date-picker-widget.svg?style=flat-square)](https://scrutinizer-ci.com/g/2amigos/yii2-date-picker-widget)
[![Total Downloads](https://img.shields.io/packagist/dt/2amigos/yii2-date-picker-widget.svg?style=flat-square)](https://packagist.org/packages/2amigos/yii2-date-picker-widget)
Renders a [Bootstrap DatePicker plugin](http://bootstrapformhelpers.com/datepicker/).
Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```bash
$ composer require 2amigos/yii2-date-picker-widget:~1.0
```
or add
```json
"2amigos/yii2-date-picker-widget" : "~1.0"
```
to the require section of your application's `composer.json` file.
Usage
-----
The widget comes in two flavors:
- DatePicker
- DateRangePicker
**DatePicker**
This widget renders a Bootstrap DatePicker input control. Best suitable for model with date string attribute.
***Example of use with a form***
There are two ways of using it, with an `ActiveForm` instance or as a widget setting up its `model` and `attribute`.
```php
<?php
use dosamigos\datepicker\DatePicker;
// as a widget
?>
<?= DatePicker::widget([
'model' => $model,
'attribute' => 'date',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);?>
<?php
// with an ActiveForm instance
?>
<?= $form->field($model, 'date')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => true,
// modify template for custom rendering
'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);?>
```
***Example of use without a model***
```php
<?php
use dosamigos\datepicker\DatePicker;
?>
<?= DatePicker::widget([
'name' => 'Test',
'value' => '02-16-2012',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);?>
```
**DateRangePicker**
This widget renders a Bootstrap DateRangePicker Input control.
***Example of use with a form***
The following example works with a model that has two attributes named `date_from` and `date_to`.
```php
<?php
use dosamigos\datepicker\DateRangePicker;
?>
<?= $form->field($tour, 'date_from')->widget(DateRangePicker::className(), [
'attributeTo' => 'date_to',
'form' => $form, // best for correct client validation
'language' => 'es',
'size' => 'lg',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yyyy'
]
]);?>
```
***Example of use without a model***
```php
<?php
use dosamigos\datepicker\DateRangePicker;
?>
<?= DateRangePicker::widget([
'name' => 'date_from',
'value' => '02-16-2012',
'nameTo' => 'name_to',
'valueTo' => '02-20-2012'
]);?>
```
Testing
-------
To test the extension, is better to clone this repository on your computer. After, go to the extensions folder and do
the following (assuming you have `composer` installed on your computer):
```bash
$ composer install --no-interaction --prefer-source --dev
```
Once all required libraries are installed then do:
```bash
$ vendor/bin/phpunit
```
I would recommend to have `phpunit` globally installed together with `xdebug` so you can have code coverage analysis too.
Further Information
-------------------
Please, check the [Bootstrap DatePicker site](http://bootstrap-datepicker.readthedocs.io/en/latest/) documentation for further information about its configuration options.
Contributing
------------
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Credits
-------
- [Antonio Ramirez](https://github.com/tonydspaniard)
- [All Contributors](../../contributors)
License
-------
The BSD License (BSD). Please see [License File](LICENSE.md) for more information.
> [![2amigOS!](http://www.gravatar.com/avatar/55363394d72945ff7ed312556ec041e0.png)](http://www.2amigos.us)
<i>Web development has never been so fun!</i>
[www.2amigos.us](http://www.2amigos.us)
{
"name": "2amigos/yii2-date-picker-widget",
"description": "Bootstrap DatePicker widget for Yii2.",
"type": "yii2-extension",
"keywords": [
"2amigos",
"yii",
"yii2",
"yii 2",
"extension",
"widget",
"datepicker"
],
"homepage": "http://yiiwheels.com/extension/bootstrap-datepicker",
"license": "BSD-3-Clause",
"authors": [
{
"name": "2amigOS! Consulting Group",
"email": "hola@2amigos.us",
"homepage": "http://2amigos.us",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/2amigos/yii2-date-picker-widget/issues",
"source": "https://github.com/2amigos/yii2-date-picker-widget"
},
"require": {
"yiisoft/yii2": ">=2.0.9",
"yiisoft/yii2-bootstrap": "*",
"bower-asset/bootstrap-datepicker": "1.4.0"
},
"require-dev": {
"phpunit/phpunit": "4.*"
},
"autoload": {
"psr-4": {
"dosamigos\\datepicker\\": "src"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"asset-installer-paths": {
"bower-asset-library": "vendor/bower"
}
}
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\datepicker;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* DatePicker renders a DatePicker input.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\datepicker
*/
class DatePicker extends InputWidget
{
use DatePickerTrait;
/**
* @var string the addon markup if you wish to display the input as a component. If you don't wish to render as a
* component then set it to null or false.
*/
public $addon = '<i class="glyphicon glyphicon-calendar"></i>';
/**
* @var string the template to render the input.
*/
public $template = '{input}{addon}';
/**
* @var bool whether to render the input as an inline calendar
*/
public $inline = false;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->inline) {
$this->options['readonly'] = 'readonly';
Html::addCssClass($this->options, 'text-center');
}
if ($this->size) {
Html::addCssClass($this->options, 'input-' . $this->size);
Html::addCssClass($this->containerOptions, 'input-group-' . $this->size);
}
Html::addCssClass($this->options, 'form-control');
Html::addCssClass($this->containerOptions, 'input-group date');
}
/**
* @inheritdoc
*/
public function run()
{
$input = $this->hasModel()
? Html::activeTextInput($this->model, $this->attribute, $this->options)
: Html::textInput($this->name, $this->value, $this->options);
if ($this->inline) {
$input .= '<div></div>';
}
if ($this->addon && !$this->inline) {
$addon = Html::tag('span', $this->addon, ['class' => 'input-group-addon']);
$input = strtr($this->template, ['{input}' => $input, '{addon}' => $addon]);
$input = Html::tag('div', $input, $this->containerOptions);
}
if ($this->inline) {
$input = strtr($this->template, ['{input}' => $input, '{addon}' => '']);
}
echo $input;
$this->registerClientScript();
}
/**
* Registers required script for the plugin to work as DatePicker
*/
public function registerClientScript()
{
$js = [];
$view = $this->getView();
// @codeCoverageIgnoreStart
if ($this->language !== null) {
$this->clientOptions['language'] = $this->language;
DatePickerLanguageAsset::register($view)->js[] = 'bootstrap-datepicker.' . $this->language . '.min.js';
} else {
DatePickerAsset::register($view);
}
// @codeCoverageIgnoreEnd
$id = $this->options['id'];
$selector = ";jQuery('#$id')";
if ($this->addon || $this->inline) {
$selector .= ".parent()";
}
$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
if ($this->inline) {
$this->clientEvents['changeDate'] = "function (e){ jQuery('#$id').val(e.format());}";
}
$js[] = "$selector.datepicker($options);";
if (!empty($this->clientEvents)) {
foreach ($this->clientEvents as $event => $handler) {
$js[] = "$selector.on('$event', $handler);";
}
}
$view->registerJs(implode("\n", $js));
}
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\datepicker;
use yii\web\AssetBundle;
/**
* DatePickerAsset
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\datepicker
*/
class DatePickerAsset extends AssetBundle
{
public $sourcePath = '@bower/bootstrap-datepicker/dist';
public $css = [
'css/bootstrap-datepicker3.css',
];
public $js = [
'js/bootstrap-datepicker.js'
];
public $depends = [
'yii\bootstrap\BootstrapPluginAsset'
];
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\datepicker;
use yii\web\AssetBundle;
/**
* DatePickerLanguageAsset
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\datepicker
*/
class DatePickerLanguageAsset extends AssetBundle
{
public $sourcePath = '@bower/bootstrap-datepicker/dist/locales';
public $depends = [
'dosamigos\datepicker\DateRangePickerAsset'
];
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\datepicker;
/**
* DatePickerTrait holds common attributes
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\datepicker
*/
trait DatePickerTrait
{
/**
* @var string the language to use
*/
public $language;
/**
* @var array the options for the Bootstrap DatePicker plugin.
* Please refer to the Bootstrap DatePicker plugin Web page for possible options.
* @see http://bootstrap-datepicker.readthedocs.org/en/release/options.html
*/
public $clientOptions = [];
/**
* @var array the event handlers for the underlying Bootstrap DatePicker plugin.
* Please refer to the [DatePicker](http://bootstrap-datepicker.readthedocs.org/en/release/events.html) plugin
* Web page for possible events.
*/
public $clientEvents = [];
/**
* @var string the size of the input ('lg', 'md', 'sm', 'xs')
*/
public $size;
/**
* @var array HTML attributes to render on the container
*/
public $containerOptions = [];
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\datepicker;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* DateRangePicker renders a DatePicker range input.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\datepicker
*/
class DateRangePicker extends InputWidget
{
use DatePickerTrait;
/**
* @var string the attribute name for date range (to Date)
*/
public $attributeTo;
/**
* @var string the name for date range (to Date)
*/
public $nameTo;
/**
* @var string the value for date range (to Date value)
*/
public $valueTo;
/**
* @var array HTML attributes for the date to input
*/
public $optionsTo;
/**
* @var string the label to. Defaults to 'to'.
*/
public $labelTo = 'to';
/**
* @var \yii\widgets\ActiveForm useful for client validation of attributeTo
*/
public $form;
/**
* @var string the template to render. Used internally.
*/
protected $_template = '{inputFrom}<span class="input-group-addon">{labelTo}</span>{inputTo}';
/**
* @inheritdoc
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
if ((!$this->hasModel() && $this->nameTo === null) || ($this->hasModel() && $this->attributeTo === null)) {
// @codeCoverageIgnoreStart
throw new InvalidConfigException("Either 'nameTo', or 'model' and 'attributeTo' properties must be specified.");
// @codeCoverageIgnoreEnd
}
if ($this->size) {
Html::addCssClass($this->options, 'input-' . $this->size);
Html::addCssClass($this->optionsTo, 'input-' . $this->size);
Html::addCssClass($this->containerOptions, 'input-group-' . $this->size);
}
Html::addCssClass($this->containerOptions, 'input-group input-daterange');
Html::addCssClass($this->options, 'form-control');
Html::addCssClass($this->optionsTo, 'form-control');
}
/**
* @inheritdoc
*/
public function run()
{
if ($this->form) {
Html::addCssClass($this->options, 'datepicker-from');
Html::addCssClass($this->optionsTo, 'datepicker-to');
$inputFrom = $this->form->field(
$this->model,
$this->attribute,
[
'template' => '{input}{error}',
'options' => ['class' => 'input-group datepicker-range'],
]
)->textInput($this->options);
$inputTo = $this->form->field(
$this->model,
$this->attributeTo,
[
'template' => '{input}{error}',
'options' => ['class' => 'input-group datepicker-range'],
]
)->textInput($this->optionsTo);
} else {
$inputFrom = $this->hasModel()
? Html::activeTextInput($this->model, $this->attribute, $this->options)
: Html::textInput($this->name, $this->value, $this->options);
$inputTo = $this->hasModel()
? Html::activeTextInput($this->model, $this->attributeTo, $this->optionsTo)
: Html::textInput($this->nameTo, $this->valueTo, $this->optionsTo);
}
echo Html::tag(
'div',
strtr(
$this->_template,
['{inputFrom}' => $inputFrom, '{labelTo}' => $this->labelTo, '{inputTo}' => $inputTo]
), $this->containerOptions);
$this->registerClientScript();
}
/**
* Registers required script for the plugin to work as DateRangePicker
*/
public function registerClientScript()
{
$js = [];
$view = $this->getView();
// @codeCoverageIgnoreStart
if($this->language !== null) {
$this->clientOptions['language'] = $this->language;
DatePickerLanguageAsset::register($view)->js[] = 'bootstrap-datepicker.' . $this->language . '.min.js';
} else {
DateRangePickerAsset::register($view);
}
// @codeCoverageIgnoreEnd
$id = $this->options['id'];
$selector = ";jQuery('#$id').parent()";
if($this->form && $this->hasModel()) {
// @codeCoverageIgnoreStart
$selector .= '.parent()';
$class = "field-" . Html::getInputId($this->model, $this->attribute);
$js[] = "$selector.closest('.$class').removeClass('$class');";
// @codeCoverageIgnoreEnd
}
$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
$js[] = "$selector.datepicker($options);";
// @codeCoverageIgnoreStart
if (!empty($this->clientEvents)) {
foreach ($this->clientEvents as $event => $handler) {
$js[] = "$selector.on('$event', $handler);";
}
}
// @codeCoverageIgnoreEnd
$view->registerJs(implode("\n", $js));
}
}
<?php
/**
* @copyright Copyright (c) 2013-2016 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\datepicker;
use yii\web\AssetBundle;
/**
* DateRangePickerAsset
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\datepicker
*/
class DateRangePickerAsset extends AssetBundle
{
public $sourcePath = '@vendor/2amigos/yii2-date-picker-widget/src/assets';
public $css = [
'css/bootstrap-daterangepicker.css'
];
public $depends = [
'dosamigos\datepicker\DatePickerAsset'
];
}
/*!
* Custom styling. Removing padding when using DateRangePicker.
*/
.datepicker {
z-index: 1151 !important;
}
.datepicker-range {
padding: 0 !important;
border: 0 !important;
text-align: left !important;
}
.datepicker-from {
border-radius: 4px !important;
border-top-right-radius: 0 !important;
border-bottom-right-radius: 0 !important;
text-align: left !important;
}
.datepicker-to {
border-radius: 4px !important;
border-top-left-radius: 0 !important;
border-bottom-left-radius: 0 !important;
text-align: left !important;
}
div.datepicker-range {
width: 100%;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment