Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add label, date and datetime picker to system.xml #2739

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ Most important changes will be listed here, all other changes since `19.4.0` can
- bug fixes and PHP 7.x, 8.0, 8.1 and 8.2 compatibility
- added config cache for system.xml ([#1916](https://github.com/OpenMage/magento-lts/pull/1916))
- added frontend_type color ([#2945](https://github.com/OpenMage/magento-lts/pull/2945))
- added `label`, `date` and `datetime` picker field types to system.xml [#2739](https://github.com/OpenMage/magento-lts/pull/2739)
- search for "NULL" in backend grids ([#1203](https://github.com/OpenMage/magento-lts/pull/1203))
- removed `lib/flex` containing unused ActionScript "file uploader" files ([#2271](https://github.com/OpenMage/magento-lts/pull/2271))
- Mage_Catalog_Model_Resource_Abstract::getAttributeRawValue() now returns `'0'` instead of `false` if the value stored in the database is `0` ([#572](https://github.com/OpenMage/magento-lts/pull/572))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

/**
* OpenMage
*
* 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 at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2023 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
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* OpenMage
*
* 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 at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2023 The OpenMage Contributors (https://www.openmage.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Adminhtml system config date field renderer
*
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Block_System_Config_Form_Field_Date extends Mage_Adminhtml_Block_System_Config_Form_Field_AbstractDate
{
/**
* @return Varien_Data_Form_Element_Date
*/
protected function getDateClass(): Varien_Data_Form_Element_Date
{
return new Varien_Data_Form_Element_Date();
}

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

/**
* @return false
*/
protected function isShowTime(): bool
{
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,30 @@
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Block_System_Config_Form_Field_Datetime extends Mage_Adminhtml_Block_System_Config_Form_Field
class Mage_Adminhtml_Block_System_Config_Form_Field_Datetime extends Mage_Adminhtml_Block_System_Config_Form_Field_AbstractDate
{
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
/**
* @return Varien_Data_Form_Element_Datetime
*/
protected function getDateClass(): Varien_Data_Form_Element_Datetime
{
$format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
return Mage::app()->getLocale()->date((int) $element->getValue())->toString($format);
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
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* OpenMage
*
* 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 at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2023 The OpenMage Contributors (https://www.openmage.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Adminhtml system config label field renderer
*
* @category Mage
* @package Mage_Adminhtml
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Adminhtml_Block_System_Config_Form_Field_Label extends Mage_Adminhtml_Block_System_Config_Form_Field
{
/**
* @param Varien_Data_Form_Element_Abstract $element
* @return string
*/
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
$field = $element->getFieldConfig();

$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 = new Varien_Data_Form_Element_Label();
$label->setValue($field->descend('value'));
$label->setBold(!empty($field->descend('bold')));

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

declare(strict_types=1);

/**
* OpenMage
*
* 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 at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2023 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();
}
}
34 changes: 34 additions & 0 deletions app/code/core/Mage/Adminhtml/Model/System/Config/Backend/Date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* OpenMage
*
* 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 at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2023 The OpenMage Contributors (https://www.openmage.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Date config field backend model
*
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Model_System_Config_Backend_Date extends Mage_Adminhtml_Model_System_Config_Backend_AbstractDate
{
/**
* @param string $format
* @return string
*/
protected function getDateFormat(string $format): string
{
return $this->getLocale()->getDateFormat($format);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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.
fballiano marked this conversation as resolved.
Show resolved Hide resolved
*
* @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)
fballiano marked this conversation as resolved.
Show resolved Hide resolved
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Datetime config field backend model
*
* @category Mage
* @package Mage_Adminhtml
*/
class Mage_Adminhtml_Model_System_Config_Backend_Datetime extends Mage_Adminhtml_Model_System_Config_Backend_AbstractDate
{
/**
* @param string $format
* @return string
*/
protected function getDateFormat(string $format): string
{
return $this->getLocale()->getDateTimeFormat($format);
}
}
Loading