Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
sreichel committed Dec 15, 2022
1 parent a3d398b commit 57b96f4
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

/**
* OpenMage
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @copyright Copyright (c) 2022 The OpenMage Contributors (https://www.openmage.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* @category Mage
* @package Mage_Adminhtml
*/
abstract class Mage_Adminhtml_Block_System_Config_Form_Field_AbstractDate extends Mage_Adminhtml_Block_System_Config_Form_Field
{
/**
* @return Varien_Data_Form_Element_Abstract
*/
abstract protected function getDateClass(): Varien_Data_Form_Element_Abstract;

/**
* @param string $format
* @return string
*/
abstract protected function getDateFormat(string $format): string;

/**
* @return bool Show date with time
*/
abstract protected function isShowTime(): bool;

/**
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
/** @var Mage_Core_Model_Config_Element $field */
$field = $element->getFieldConfig();

$type = $element->getType();
if ($type !== 'date' && $type !== 'datetime' && $type !== 'text') {
Mage::throwException(
Mage::helper('adminhtml')->__(
'Invalid frontend type for field "%s". Onyl "text", "date" and "datetime" are allowed.',
$field->descend('label')
)
);
}

$format = Mage_Core_Helper_Date::getDateFormatFromString($field->format->__toString());
$format = $this->getDateFormat($format);

if (empty($field->editable)) {
return $this->getLocale()
->date((int)$element->getValue())
->toString($format);
}

$date = $this->getDateClass();
$date->setData([
'name' => $element->getName(),
'html_id' => $element->getId(),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'time' => $this->isShowTime()
]);
$date->setFormat($format);
$date->setValue($element->getValue());
$date->setForm($element->getForm());

return $date->getElementHtml();
}

/**
* @return Mage_Core_Model_Locale
*/
public function getLocale()
{
return Mage::app()->getLocale();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,54 +27,31 @@
*
* @category Mage
* @package Mage_Adminhtml
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Adminhtml_Block_System_Config_Form_Field_Date extends Mage_Adminhtml_Block_System_Config_Form_Field
class Mage_Adminhtml_Block_System_Config_Form_Field_Date extends Mage_Adminhtml_Block_System_Config_Form_Field_AbstractDate
{
/**
* Output date with time
* @var bool
* @return Varien_Data_Form_Element_Date
*/
protected $showTime = false;
protected function getDateClass(): Varien_Data_Form_Element_Date
{
return new Varien_Data_Form_Element_Date();
}

/**
* @param Varien_Data_Form_Element_Abstract $element
* @param string $format
* @return string
*/
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
protected function getDateFormat(string $format): string
{
$field = $element->getFieldConfig();
if (!empty($format = $field->format) && defined('Mage_Core_Model_Locale::' . $format)) {
$format = constant('Mage_Core_Model_Locale::' . $format);
} else {
$format = Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM;
}

$locale = Mage::app()->getLocale();

// original way: getDateTimeFormat with FORMAT_TYPE_MEDIUM and return
$format = $this->showTime ? $locale->getDateTimeFormat($format) : $locale->getDateFormat($format);
if (empty($field->editable)) {
return $locale->date((int)$element->getValue())->toString($format);
}

$class = (!empty($field->frontend_type) && ($field->frontend_type != 'text')) ? 'Varien_Data_Form_Element_' . ucfirst(strtolower($field->frontend_type)) : '';
if (class_exists($class)) {
$date = new $class();
} else {
$date = $this->showTime ? new Varien_Data_Form_Element_Datetime() : new Varien_Data_Form_Element_Date();
}

$date->setData([
'name' => $element->getName(),
'html_id' => $element->getId(),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'time' => $this->showTime,
]);
$date->setFormat($format);
$date->setValue($element->getValue());
$date->setForm($element->getForm());
return $this->getLocale()->getDateFormat($format);
}

return $date->getElementHtml();
/**
* @return false
*/
protected function isShowTime(): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,31 @@
*
* @category Mage
* @package Mage_Adminhtml
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Adminhtml_Block_System_Config_Form_Field_Datetime extends Mage_Adminhtml_Block_System_Config_Form_Field_Date
class Mage_Adminhtml_Block_System_Config_Form_Field_Datetime extends Mage_Adminhtml_Block_System_Config_Form_Field_AbstractDate
{
protected $showTime = true;
/**
* @return Varien_Data_Form_Element_Datetime
*/
protected function getDateClass(): Varien_Data_Form_Element_Datetime
{
return new Varien_Data_Form_Element_Datetime();
}

/**
* @param string $format
* @return string
*/
protected function getDateFormat(string $format): string
{
return $this->getLocale()->getDateTimeFormat($format);
}

/**
* @return true
*/
protected function isShowTime(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ class Mage_Adminhtml_Block_System_Config_Form_Field_Label extends Mage_Adminhtml
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$field = $element->getFieldConfig();
$class = (!empty($field->frontend_type) && ($field->frontend_type != 'text')) ? 'Varien_Data_Form_Element_' . ucfirst(strtolower($field->frontend_type)) : '';
if (class_exists($class)) {
$label = new $class();
} else {
$label = new Varien_Data_Form_Element_Label();

$type = $element->getType();
if ($type !== 'label' && $type !== 'text') {
Mage::throwException(
Mage::helper('adminhtml')->__(
'Invalid frontend type for field "%s". Onyl "text" and "label" are allowed.',
$field->descend('label')
)
);
}

$label->setValue($field->value);
$label->setBold(!empty($field->bold));
$label = new Varien_Data_Form_Element_Label();
$label->setValue($field->descend('value'));
$label->setBold(!empty($field->descend('bold')));

return $label->getElementHtml();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

/**
* OpenMage
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @copyright Copyright (c) 2022 The OpenMage Contributors (https://www.openmage.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* @category Mage
* @package Mage_Adminhtml
*/
abstract class Mage_Adminhtml_Model_System_Config_Backend_AbstractDate extends Mage_Core_Model_Config_Data
{
/**
* @param string $format
* @return string
*/
abstract protected function getDateFormat(string $format): string;

/**
* @return $this
*/
protected function _afterLoad()
{
$value = (string)$this->getValue();
if ($value !== '') {
$this->setValue($this->getLocale()->date($value)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT));
}

return $this;
}

/**
* @return $this
*/
protected function _beforeSave()
{
$value = (string)$this->getValue();
if ($value !== '') {
$this->setValue($this->filterDateTime($value));
}

return $this;
}

/**
* @param string $date
* @return string
*/
protected function filterDateTime(string $date): string
{
/** @var Mage_Core_Model_Config_Element $field */
$field = $this->getFieldConfig();

$format = Mage_Core_Helper_Date::getDateFormatFromString($field->format->__toString());
$format = $this->getDateFormat($format);

$filterInput = new Zend_Filter_LocalizedToNormalized([
'date_format' => $format
]);

$date = $filterInput->filter($date);

// convert to utc
return $this->getLocale()->utcDate(null, $date, true)->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
}

/**
* @return Mage_Core_Model_Locale
*/
public function getLocale()
{
return Mage::app()->getLocale();
}
}
Loading

0 comments on commit 57b96f4

Please sign in to comment.