From 908e8175f3f8c091d425bcaba6e18871d63291a0 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Tue, 8 Aug 2017 09:56:22 +0530 Subject: [PATCH] Updates to release v1.6.8 --- CHANGE.md | 97 ++++++------- DateRangeBehavior.php | 29 ++-- DateRangePicker.php | 289 ++++++++++++++++++++------------------- DateRangePickerAsset.php | 3 + LanguageAsset.php | 6 + MomentAsset.php | 6 + 6 files changed, 229 insertions(+), 201 deletions(-) diff --git a/CHANGE.md b/CHANGE.md index c97365d..55244c6 100644 --- a/CHANGE.md +++ b/CHANGE.md @@ -3,93 +3,98 @@ Change Log: `yii2-date-range` ## Version 1.6.8 -**Date:** 22-Mar-2017 +**Date:** 08-Aug-2017 -- (enh #85, #86): Add Greek Translations. -- (enh #88): Enhance preset dropdown to default today's date when date is empty. -- (enh #90): Add Date Range Behavior for easier application with model range attributes. -- (enh #91): Update moment library to latest release. -- Update daterangepicker library to latest release. -- Add github contribution and issue/PR logging templates. -- (enh #92): Correct example for Date Range Behavior in README.md. +- Chronological arrangement of issues in CHANGE.log. +- (enh #101): Update Turkish Translations. +- (enh #98): Update Danish Translations. +- (enh #97): Update Hebrew Translations. +- (enh #95): Update Chinese Traditional Translations. - (enh #93): Various styling enhancements and preset plugin defaults for preset dropdown. +- (enh #92): Correct example for Date Range Behavior in README.md. +- Add github contribution and issue/PR logging templates. +- Update daterangepicker library to latest release. +- (enh #91): Update moment library to latest release. +- (enh #90): Add Date Range Behavior for easier application with model range attributes. +- (enh #88): Enhance preset dropdown to default today's date when date is empty. +- (enh #85, #86): Add Greek Translations. ## Version 1.6.7 **Date:** 12-Jul-2016 -- (enh #58): Add support for separate start and end attributes and inputs. -- (enh #59): Add Hungarian Translations. -- (enh #61): Add Dutch Translations. -- (enh #62): Add Romanian Translations. -- Add branch alias for dev-master latest release. -- (bug #65): Correct moment `weekdaysStart` to `weekdaysMin`. -- (enh #67): Parse input change correctly when range input value is cleared. -- (enh #68): Update to latest release of bootstrap-daterangepicker plugin and moment library. -- (bug #70): More better attribute and input options parsing. -- (enh #71): Add Thai translations. -- (bug #73): Correct dependency for `DateRangePickerAsset` and `LanguageAsset`. -- (bug #74): Correct asset bundle dependency. -- (bug #75): Correct code for PHP 5.5. - (bug #76, #77, #78, #79): Correct dependency for `DateRangePickerAsset`. +- (bug #75): Correct code for PHP 5.5. +- (bug #74): Correct asset bundle dependency. +- (bug #73): Correct dependency for `DateRangePickerAsset` and `LanguageAsset`. +- (enh #71): Add Thai translations. +- (bug #70): More better attribute and input options parsing. +- (enh #68): Update to latest release of bootstrap-daterangepicker plugin and moment library. +- (enh #67): Parse input change correctly when range input value is cleared. +- (bug #65): Correct moment `weekdaysStart` to `weekdaysMin`. +- Add branch alias for dev-master latest release. +- (enh #62): Add Romanian Translations. +- (enh #61): Add Dutch Translations. +- (enh #59): Add Hungarian Translations. +- (enh #58): Add support for separate start and end attributes and inputs. ## Version 1.6.6 **Date:** 11-Jan-2016 -- (enh #55): Enhancements for PJAX based reinitialization. Complements enhancements in kartik-v/yii2-krajee-base#52 and kartik-v/yii2-krajee-base#53. - (enh #56): Update to latest version of bootstrap-daterangepicker. +- (enh #55): Enhancements for PJAX based reinitialization. Complements enhancements in kartik-v/yii2-krajee-base#52 and kartik-v/yii2-krajee-base#53. ## Version 1.6.5 **Date:** 22-Oct-2015 -- (enh #52): New property `autoUpdateOnInit` to prevent plugin triggering change due to `pluginOptions['autoUpdateInput']` default setting. - (enh #53): Added correct German translations. +- (enh #52): New property `autoUpdateOnInit` to prevent plugin triggering change due to `pluginOptions['autoUpdateInput']` default setting. ## Version 1.6.4 **Date:** 19-Oct-2015 -- (enh #41): Add Simplified Chinese translations. -- (enh #43): Add Slovak translations. - (enh #51): Update to latest release of bootstrap-datarangepicker plugin. +- (enh #43): Add Slovak translations. +- (enh #41): Add Simplified Chinese translations. ## Version 1.6.3 **Date:** 22-May-2015 -- (enh #31): Add Ukranian translations. -- (enh #32): Add Portugese translations. -- (enh #36): Add Polish translations. -- (enh #38): Update to latest release of bootstrap-datarangepicker plugin. - (enh #40): Update moment library and locales. +- (enh #38): Update to latest release of bootstrap-datarangepicker plugin. +- (enh #36): Add Polish translations. +- (enh #32): Add Portugese translations. +- (enh #31): Add Ukranian translations. ## Version 1.6.2 **Date:** 02-Mar-2015 -- (enh #27): Correct initial value initialization for all cases. +- (enh #29): Improve validation to retrieve the right translation messages folder. - (enh #28): Upgrade to latest release of bootstrap-daterangepicker plugin. +- (enh #27): Correct initial value initialization for all cases. - Set copyright year to current. -- (enh #29): Improve validation to retrieve the right translation messages folder. ## Version 1.6.1 **Date:** 16-Feb-2015 -- (enh #27): Correct initial value initialization for all cases. -- (enh #28): Upgrade to latest release of bootstrap-daterangepicker plugin. - Set copyright year to current. +- (enh #28): Upgrade to latest release of bootstrap-daterangepicker plugin. +- (enh #27): Correct initial value initialization for all cases. ## Version 1.6.0 **Date:** 12-Jan-2015 -- (enh #22): Estonian translation for kvdrp.php -- (enh #23): Russian translations updated. -- Code formatting updates as per Yii2 standards. - Revamp to use new Krajee base InputWidget and TranslationTrait. +- Code formatting updates as per Yii2 standards. +- (enh #23): Russian translations updated. +- (enh #22): Estonian translation for kvdrp.php ## Version 1.5.0 @@ -105,35 +110,35 @@ Change Log: `yii2-date-range` **Date:** 25-Nov-2014 -- (enh #17): Updated Russian translations -- (bug #18): Plugin data attributes not set because of input rendering sequence. - (enh #19): Enhance widget to use updated plugin registration from Krajee base +- (bug #18): Plugin data attributes not set because of input rendering sequence. +- (enh #17): Updated Russian translations ## Version 1.3.0 **Date:** 21-Nov-2014 -- (enh #7): Added Russian Translations -- (enh #12): Added Spanish Translations -- (enh #13): Update moment.js related range initializations. -- (enh #14): Update moment library to latest release. -- (enh #15): Revamp widget to remove dependency on custom locale JS files enhancement - (enh #16): Update Lithunian translations and create German translations. +- (enh #15): Revamp widget to remove dependency on custom locale JS files enhancement +- (enh #14): Update moment library to latest release. +- (enh #13): Update moment.js related range initializations. +- (enh #12): Added Spanish Translations +- (enh #7): Added Russian Translations ## Version 1.2.0 **Date:** 20-Nov-2014 -- (bug #11): Fix bug in daterangepicker.js for duplicate dates in Dec 2013. - Upgrade to latest plugin release 1.3.16 dated 12-Nov-2014. +- (bug #11): Fix bug in daterangepicker.js for duplicate dates in Dec 2013. ## Version 1.1.0 **Date:** 10-Nov-2014 -- PSR4 alias change -- Set dependency on Krajee base components - Set release to stable +- Set dependency on Krajee base components +- PSR4 alias change ## Version 1.0.0 diff --git a/DateRangeBehavior.php b/DateRangeBehavior.php index b96dd39..9448e63 100644 --- a/DateRangeBehavior.php +++ b/DateRangeBehavior.php @@ -5,6 +5,7 @@ * @package yii2-date-range * @version 1.6.8 */ + namespace kartik\daterange; use yii\base\Model; @@ -81,6 +82,18 @@ class DateRangeBehavior extends Behavior */ public $separator; + /** + * Parses the given date into a Unix timestamp. + * + * @param string $date a date string + * + * @return integer|false a Unix timestamp. False on failure. + */ + protected static function dateToTime($date) + { + return strtotime($date); + } + /** * @inheritdoc */ @@ -94,7 +107,9 @@ public function init() } } else { if (!isset($this->dateStartAttribute) || !isset($this->dateEndAttribute)) { - throw new InvalidConfigException('The "dateStartAttribute" and "dateEndAttribute" properties must be specified.'); + throw new InvalidConfigException( + 'The "dateStartAttribute" and "dateEndAttribute" properties must be specified.' + ); } } } @@ -111,6 +126,7 @@ public function events() /** * Handles owner 'afterValidate' event. + * * @param \yii\base\Event $event event instance. */ public function afterValidate($event) @@ -137,6 +153,7 @@ public function afterValidate($event) /** * Evaluates the attribute value and assigns it to the given attribute. + * * @param string $attribute the owner attribute name * @param string $dateFormat the PHP date format string * @param string $date a date string @@ -154,14 +171,4 @@ protected function setOwnerAttribute($attribute, $dateFormat, $date) } } } - - /** - * Parses the given date into a Unix timestamp. - * @param string $date a date string - * @return integer|false a Unix timestamp. False on failure. - */ - protected static function dateToTime($date) - { - return strtotime($date); - } } diff --git a/DateRangePicker.php b/DateRangePicker.php index 871fca6..5dd576b 100644 --- a/DateRangePicker.php +++ b/DateRangePicker.php @@ -120,8 +120,8 @@ class DateRangePicker extends InputWidget HTML; /** - * HTML attributes for span to display default value for preset dropdown. By default for a preset dropdown, - * if the value is empty, it will default to "Today". The following special options are supported: + * HTML attributes for the `span` element that displays the default value for a preset dropdown. By default for a + * preset dropdown, if the value is empty, it will default to "Today". The following special options are supported: * - `tag`: the HTML tag under which the default value markup will be displayed when empty. Defaults to `em`. */ public $defaultPresetValueOptions = ['class' => 'text-muted']; @@ -161,94 +161,6 @@ class DateRangePicker extends InputWidget */ protected $_endInput = ''; - /** - * @inheritdoc - */ - public function run() - { - $this->initSettings(); - echo $this->renderInput(); - } - - /** - * Initializes widget settings - * - * @throws InvalidConfigException - */ - protected function initSettings() - { - $this->_msgCat = 'kvdrp'; - $this->initI18N(__DIR__); - $this->initLocale(); - if ($this->convertFormat && isset($this->pluginOptions['locale']['format'])) { - $this->pluginOptions['locale']['format'] = static::convertDateFormat($this->pluginOptions['locale']['format']); - } - $locale = ArrayHelper::getValue($this->pluginOptions, 'locale', []); - $this->_format = ArrayHelper::getValue($locale, 'format', 'YYYY-MM-DD'); - $this->_separator = ArrayHelper::getValue($locale, 'separator', ' - '); - if (!empty($this->value)) { - $dates = explode($this->_separator, $this->value); - if (count($dates) > 1) { - $this->pluginOptions['startDate'] = $dates[0]; - $this->pluginOptions['endDate'] = $dates[1]; - $this->initRangeValue('start', $dates[0]); - $this->initRangeValue('end', $dates[1]); - } - } elseif ($this->startAttribute && $this->endAttribute) { - $start = $this->getRangeValue('start'); - $end = $this->getRangeValue('end'); - $this->value = $start . $this->_separator . $end; - if ($this->hasModel()) { - $attr = $this->attribute; - $this->model->$attr = $this->value; - } - $this->pluginOptions['startDate'] = $start; - $this->pluginOptions['endDate'] = $end; - } - $value = empty($this->value) ? '' : $this->value; - $this->containerTemplate = str_replace('{value}', $value, $this->containerTemplate); - - // Set `autoUpdateInput` to false for certain settings - if (!$this->autoUpdateOnInit || $this->hideInput || $this->useWithAddon) { - $this->pluginOptions['autoUpdateInput'] = false; - } - $this->_startInput = $this->getRangeInput('start'); - $this->_endInput = $this->getRangeInput('end'); - if (empty($this->containerOptions['id'])) { - $this->containerOptions['id'] = $this->options['id'] . '-container'; - } - if (empty($this->containerOptions['class'])) { - $css = $this->presetDropdown ? '' : ' input-group'; - $this->containerOptions['class'] = 'kv-drp-container' . $css; - } - $this->initRange(); - $this->registerAssets(); - } - - /** - * Initialize locale settings - */ - protected function initLocale() - { - $this->setLanguage(''); - if (empty($this->_langFile)) { - return; - } - $localeSettings = ArrayHelper::getValue($this->pluginOptions, 'locale', []); - $localeSettings += [ - 'applyLabel' => Yii::t('kvdrp', 'Apply'), - 'cancelLabel' => Yii::t('kvdrp', 'Cancel'), - 'fromLabel' => Yii::t('kvdrp', 'From'), - 'toLabel' => Yii::t('kvdrp', 'To'), - 'weekLabel' => Yii::t('kvdrp', 'W'), - 'customRangeLabel' => Yii::t('kvdrp', 'Custom Range'), - 'daysOfWeek' => new JsExpression('moment.weekdaysMin()'), - 'monthNames' => new JsExpression('moment.monthsShort()'), - 'firstDay' => new JsExpression('moment.localeData()._week.dow') - ]; - $this->pluginOptions['locale'] = $localeSettings; - } - /** * Automatically convert the date format from PHP DateTime to Moment.js DateTime format as required by the * `bootstrap-daterangepicker` plugin. @@ -262,7 +174,7 @@ protected function initLocale() */ protected static function convertDateFormat($format) { - return strtr($format, [ + $conversions = [ // meridian lowercase remains same // 'a' => 'a', // meridian uppercase remains same @@ -305,57 +217,8 @@ protected static function convertDateFormat($format) 'Y' => 'YYYY', // unix timestamp 'U' => 'X', - ]); - } - - /** - * Initializes the pluginOptions range list - */ - protected function initRange() - { - if (isset($dummyValidation)) { - /** @noinspection PhpUnusedLocalVariableInspection */ - $msg = Yii::t('kvdrp', 'Select Date Range'); - } - if ($this->presetDropdown) { - $this->initRangeExpr = $this->hideInput = true; - $this->pluginOptions['opens'] = 'left'; - $this->pluginOptions['ranges'] = [ - Yii::t('kvdrp', "Today") => ["moment().startOf('day')", "moment()"], - Yii::t('kvdrp', "Yesterday") => [ - "moment().startOf('day').subtract(1,'days')", - "moment().endOf('day').subtract(1,'days')" - ], - Yii::t('kvdrp', "Last {n} Days", ['n' => 7]) => [ - "moment().startOf('day').subtract(6, 'days')", - "moment()" - ], - Yii::t('kvdrp', "Last {n} Days", ['n' => 30]) => [ - "moment().startOf('day').subtract(29, 'days')", - "moment()" - ], - Yii::t('kvdrp', "This Month") => ["moment().startOf('month')", "moment().endOf('month')"], - Yii::t('kvdrp', "Last Month") => [ - "moment().subtract(1, 'month').startOf('month')", - "moment().subtract(1, 'month').endOf('month')" - ], - ]; - if (empty($this->value)) { - $this->pluginOptions['startDate'] = new JsExpression("moment().startOf('day')"); - $this->pluginOptions['endDate'] = new JsExpression("moment()"); - } - } - if (!$this->initRangeExpr || empty($this->pluginOptions['ranges']) || !is_array($this->pluginOptions['ranges'])) { - return; - } - $range = []; - foreach ($this->pluginOptions['ranges'] as $key => $value) { - if (!is_array($value) || empty($value[0]) || empty($value[1])) { - throw new InvalidConfigException("Invalid settings for pluginOptions['ranges']. Each range value must be a two element array."); - } - $range[$key] = [static::parseJsExpr($value[0]), static::parseJsExpr($value[1])]; - } - $this->pluginOptions['ranges'] = $range; + ]; + return strtr($format, $conversions); } /** @@ -370,6 +233,15 @@ protected static function parseJsExpr($value) return $value instanceof JsExpression ? $value : new JsExpression($value); } + /** + * @inheritdoc + */ + public function run() + { + $this->initSettings(); + echo $this->renderInput(); + } + /** * Registers the needed client assets */ @@ -396,7 +268,7 @@ public function registerAssets() $change = $rangeJs . "{$input}.val(val).trigger('change');"; if ($this->presetDropdown) { $id = "{$id}.find('.kv-drp-dropdown')"; - } + } if ($this->hideInput) { $script = "var val={$val};{$id}.find('.range-value').html(val);{$change}"; } elseif ($this->useWithAddon) { @@ -436,6 +308,135 @@ public function registerAssets() $this->registerPlugin($this->pluginName, $id, null, $this->callback); } + /** + * Initializes widget settings + * + * @throws InvalidConfigException + */ + protected function initSettings() + { + $this->_msgCat = 'kvdrp'; + $this->initI18N(__DIR__); + $this->initLocale(); + if ($this->convertFormat && isset($this->pluginOptions['locale']['format'])) { + $this->pluginOptions['locale']['format'] = static::convertDateFormat( + $this->pluginOptions['locale']['format'] + ); + } + $locale = ArrayHelper::getValue($this->pluginOptions, 'locale', []); + $this->_format = ArrayHelper::getValue($locale, 'format', 'YYYY-MM-DD'); + $this->_separator = ArrayHelper::getValue($locale, 'separator', ' - '); + if (!empty($this->value)) { + $dates = explode($this->_separator, $this->value); + if (count($dates) > 1) { + $this->pluginOptions['startDate'] = $dates[0]; + $this->pluginOptions['endDate'] = $dates[1]; + $this->initRangeValue('start', $dates[0]); + $this->initRangeValue('end', $dates[1]); + } + } elseif ($this->startAttribute && $this->endAttribute) { + $start = $this->getRangeValue('start'); + $end = $this->getRangeValue('end'); + $this->value = $start . $this->_separator . $end; + if ($this->hasModel()) { + $attr = $this->attribute; + $this->model->$attr = $this->value; + } + $this->pluginOptions['startDate'] = $start; + $this->pluginOptions['endDate'] = $end; + } + $value = empty($this->value) ? '' : $this->value; + $this->containerTemplate = str_replace('{value}', $value, $this->containerTemplate); + + // Set `autoUpdateInput` to false for certain settings + if (!$this->autoUpdateOnInit || $this->hideInput || $this->useWithAddon) { + $this->pluginOptions['autoUpdateInput'] = false; + } + $this->_startInput = $this->getRangeInput('start'); + $this->_endInput = $this->getRangeInput('end'); + if (empty($this->containerOptions['id'])) { + $this->containerOptions['id'] = $this->options['id'] . '-container'; + } + if (empty($this->containerOptions['class'])) { + $css = $this->useWithAddon && !$this->presetDropdown && !$this->hideInput ? ' input-group' : ''; + $this->containerOptions['class'] = 'kv-drp-container' . $css; + } + $this->initRange(); + $this->registerAssets(); + } + + /** + * Initialize locale settings + */ + protected function initLocale() + { + $this->setLanguage(''); + if (empty($this->_langFile)) { + return; + } + $localeSettings = ArrayHelper::getValue($this->pluginOptions, 'locale', []); + $localeSettings += [ + 'applyLabel' => Yii::t('kvdrp', 'Apply'), + 'cancelLabel' => Yii::t('kvdrp', 'Cancel'), + 'fromLabel' => Yii::t('kvdrp', 'From'), + 'toLabel' => Yii::t('kvdrp', 'To'), + 'weekLabel' => Yii::t('kvdrp', 'W'), + 'customRangeLabel' => Yii::t('kvdrp', 'Custom Range'), + 'daysOfWeek' => new JsExpression('moment.weekdaysMin()'), + 'monthNames' => new JsExpression('moment.monthsShort()'), + 'firstDay' => new JsExpression('moment.localeData()._week.dow'), + ]; + $this->pluginOptions['locale'] = $localeSettings; + } + + /** + * Initializes the pluginOptions range list + */ + protected function initRange() + { + if (isset($dummyValidation)) { + /** @noinspection PhpUnusedLocalVariableInspection */ + $msg = Yii::t('kvdrp', 'Select Date Range'); + } + $m = 'moment()'; + if ($this->presetDropdown) { + $this->initRangeExpr = $this->hideInput = true; + $this->pluginOptions['opens'] = 'left'; + $this->pluginOptions['ranges'] = [ + Yii::t('kvdrp', 'Today') => ["{$m}.startOf('day')", $m], + Yii::t('kvdrp', 'Yesterday') => [ + "{$m}.startOf('day').subtract(1,'days')", + "{$m}.endOf('day').subtract(1,'days')", + ], + Yii::t('kvdrp', 'Last {n} Days', ['n' => 7]) => ["{$m}.startOf('day').subtract(6, 'days')", $m], + Yii::t('kvdrp', 'Last {n} Days', ['n' => 30]) => ["{$m}.startOf('day').subtract(29, 'days')", $m], + Yii::t('kvdrp', 'This Month') => ["{$m}.startOf('month')", "{$m}.endOf('month')"], + Yii::t('kvdrp', 'Last Month') => [ + "{$m}.subtract(1, 'month').startOf('month')", + "{$m}.subtract(1, 'month').endOf('month')", + ], + ]; + if (empty($this->value)) { + $this->pluginOptions['startDate'] = new JsExpression("{$m}.startOf('day')"); + $this->pluginOptions['endDate'] = new JsExpression($m); + } + } + $opts = $this->pluginOptions; + if (!$this->initRangeExpr || empty($opts['ranges']) || !is_array($opts['ranges'])) { + return; + } + $range = []; + foreach ($opts['ranges'] as $key => $value) { + if (!is_array($value) || empty($value[0]) || empty($value[1])) { + throw new InvalidConfigException( + "Invalid settings for pluginOptions['ranges']. Each range value must be a two element array." + ); + } + $range[$key] = [static::parseJsExpr($value[0]), static::parseJsExpr($value[1])]; + } + $this->pluginOptions['ranges'] = $range; + } + /** * Renders the input * @@ -508,7 +509,7 @@ protected function getRangeJs($type = '') $options = $this->getInputOpts($type); $input = "jQuery('#" . $this->options['id'] . "')"; return "var v={$input}.val() ? {$type}.format('{$this->_format}') : '';jQuery('#" . $options['id'] . - "').val(v).trigger('change');"; + "').val(v).trigger('change');"; } /** diff --git a/DateRangePickerAsset.php b/DateRangePickerAsset.php index 874f7af..5e39b3f 100644 --- a/DateRangePickerAsset.php +++ b/DateRangePickerAsset.php @@ -18,6 +18,9 @@ */ class DateRangePickerAsset extends AssetBundle { + /** + * @inheritdoc + */ public $depends = [ '\kartik\daterange\MomentAsset', 'yii\web\JqueryAsset' diff --git a/LanguageAsset.php b/LanguageAsset.php index 68b1e80..ef120af 100644 --- a/LanguageAsset.php +++ b/LanguageAsset.php @@ -19,7 +19,13 @@ */ class LanguageAsset extends AssetBundle { + /** + * @inheritdoc + */ public $jsOptions = ['position' => View::POS_HEAD]; + /** + * @inheritdoc + */ public $depends = ['\kartik\daterange\MomentAsset']; /** diff --git a/MomentAsset.php b/MomentAsset.php index a5d39ef..b324c46 100644 --- a/MomentAsset.php +++ b/MomentAsset.php @@ -19,7 +19,13 @@ */ class MomentAsset extends AssetBundle { + /** + * @inheritdoc + */ public $jsOptions = ['position' => View::POS_HEAD]; + /** + * @inheritdoc + */ public $depends = []; /**