From cd750a284f6794d0dbdde32e3933c095bce00ffd Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Sun, 8 Jan 2017 12:07:23 +0530 Subject: [PATCH] Code enhancements --- DateControl.php | 92 ++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/DateControl.php b/DateControl.php index baf0cb7..8942b33 100644 --- a/DateControl.php +++ b/DateControl.php @@ -158,11 +158,7 @@ class DateControl extends InputWidget /** * @inheritdoc */ - protected $_pluginName = 'datecontrol'; - /** - * @var string display attribute name. - */ - protected $_displayAttribName; + public $pluginName = 'datecontrol'; /** * @var Module the `datecontrol` module instance. */ @@ -286,8 +282,7 @@ public function init() } parent::init(); $this->initLanguage(); - $this->setDataVar($this->_pluginName); - $this->_displayAttribName = (($this->hasModel()) ? $this->attribute : $this->name) . '-' . $this->options['id']; + $this->setDataVar($this->pluginName); $this->saveOptions['id'] = $this->options['id']; $this->options['id'] .= '-disp'; if ($this->isWidget()) { @@ -310,6 +305,32 @@ public function run() parent::run(); } + /** + * Gets the formatted display date value. + * + * @param string $data the input date data. + * + * @return string + */ + public function getDisplayValue($data) + { + $saveDate = $data; + $saveFormat = $this->saveFormat; + $settings = $this->_doTranslate ? ArrayHelper::getValue($this->pluginOptions, 'dateSettings', []) : []; + $date = static::getTimestamp($saveDate, $saveFormat, $this->saveTimezone, $settings); + if ($date && $date instanceof DateTime) { + if ($this->displayTimezone != null) { + $date->setTimezone(new DateTimeZone($this->displayTimezone)); + } + $value = $date->format($this->displayFormat); + if ($this->_doTranslate) { + $value = $this->translateDate($value, $this->displayFormat); + } + return $value; + } + return null; + } + /** * Initializes widget based on module settings. * @@ -386,38 +407,41 @@ protected function isWidget() */ protected function getDisplayInput() { + $name = ($this->hasModel() ? $this->attribute : $this->name) . '-' . $this->options['id']; $value = empty($this->value) ? '' : $this->getDisplayValue($this->value); if (!$this->isWidget()) { if (empty($this->options['class'])) { $this->options['class'] = 'form-control'; } - return Html::textInput($this->_displayAttribName, $value, $this->options); + return Html::textInput($name, $value, $this->options); } + $opts = $this->widgetOptions; if (!empty($this->displayFormat) && $this->autoWidget) { $defaultOptions = Module::defaultWidgetOptions($this->type, $this->displayFormat); - $this->widgetOptions = ArrayHelper::merge($defaultOptions, $this->widgetOptions); + $this->widgetOptions = ArrayHelper::merge($defaultOptions, $opts); } if (!empty($this->_widgetSettings[$this->type]['options'])) { - $this->widgetOptions = ArrayHelper::merge( - $this->_widgetSettings[$this->type]['options'], $this->widgetOptions + $opts = ArrayHelper::merge( + $this->_widgetSettings[$this->type]['options'], $opts ); } - unset($this->widgetOptions['model'], $this->widgetOptions['attribute']); - $this->widgetOptions['name'] = $this->_displayAttribName; - $this->widgetOptions['value'] = $value; - $this->widgetOptions['disabled'] = $this->disabled; - $this->widgetOptions['readonly'] = $this->readonly; + unset($opts['model'], $opts['attribute']); + $opts['name'] = $name; + $opts['value'] = $value; + $opts['disabled'] = $this->disabled; + $opts['readonly'] = $this->readonly; /** * @var InputWidget $class */ $class = $this->widgetClass; if (!property_exists($class, 'disabled')) { - unset($this->widgetOptions['disabled']); + unset($opts['disabled']); } if (!property_exists($class, 'readonly')) { - unset($this->widgetOptions['readonly']); + unset($opts['readonly']); } - return $class::widget($this->widgetOptions); + $this->widgetOptions = $opts; + return $class::widget($opts); } /** @@ -441,32 +465,6 @@ protected function getSaveInput() Html::hiddenInput($this->name, $this->value, $this->saveOptions); } - /** - * Gets the formatted display date value. - * - * @param string $data the input date data. - * - * @return string - */ - protected function getDisplayValue($data) - { - $saveDate = $data; - $saveFormat = $this->saveFormat; - $settings = $this->_doTranslate ? ArrayHelper::getValue($this->pluginOptions, 'dateSettings', []) : []; - $date = static::getTimestamp($saveDate, $saveFormat, $this->saveTimezone, $settings); - if ($date && $date instanceof DateTime) { - if ($this->displayTimezone != null) { - $date->setTimezone(new DateTimeZone($this->displayTimezone)); - } - $value = $date->format($this->displayFormat); - if ($this->_doTranslate) { - $value = $this->translateDate($value, $this->displayFormat); - } - return $value; - } - return null; - } - /** * Translate the date string. * @@ -538,8 +536,8 @@ protected function registerAssets() 'asyncRequest' => $this->asyncRequest, ], $pluginOptions ); - $this->registerPlugin($this->_pluginName); - $pluginData = 'data-krajee-' . $this->_pluginName; + $this->registerPlugin($this->pluginName); + $pluginData = 'data-krajee-' . $this->pluginName; if (!empty($this->options[$pluginData])) { $this->widgetOptions['options'][$pluginData] = $this->options[$pluginData]; }