-
-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Justin Beaty <51970393+justinbeaty@users.noreply.github.com> Co-authored-by: Sven Reichel <github-sr@hotmail.com>
- Loading branch information
1 parent
6ddee95
commit bac869e
Showing
9 changed files
with
254 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?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: | ||
* https://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) 2023 The OpenMage Contributors (https://www.openmage.org) | ||
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
/** | ||
* Default config helper | ||
* | ||
* @category Mage | ||
* @package Mage_Adminhtml | ||
* @author Magento Core Team <core@magentocommerce.com> | ||
*/ | ||
class Mage_Adminhtml_Helper_Config extends Mage_Core_Helper_Abstract | ||
{ | ||
protected $_moduleName = 'Mage_Adminhtml'; | ||
|
||
/** | ||
* Return information array of input types | ||
* | ||
* @param string $inputType | ||
* @return array | ||
*/ | ||
public function getInputTypes(string $inputType = null): array | ||
{ | ||
$inputTypes = [ | ||
'color' => [ | ||
'backend_model' => 'adminhtml/system_config_backend_color' | ||
] | ||
]; | ||
|
||
if (is_null($inputType)) { | ||
return $inputTypes; | ||
} elseif (isset($inputTypes[$inputType])) { | ||
return $inputTypes[$inputType]; | ||
} | ||
return []; | ||
} | ||
|
||
/** | ||
* Return default backend model by input type | ||
* | ||
* @param string $inputType | ||
* @return string|null | ||
*/ | ||
public function getBackendModelByInputType(string $inputType): ?string | ||
{ | ||
$inputTypes = $this->getInputTypes(); | ||
if (!empty($inputTypes[$inputType]['backend_model'])) { | ||
return $inputTypes[$inputType]['backend_model']; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Get field backend model by field config node | ||
* | ||
* @param Varien_Simplexml_Element $fieldConfig | ||
* @return string|null | ||
*/ | ||
public function getBackendModelByFieldConfig(Varien_Simplexml_Element $fieldConfig): ?string | ||
{ | ||
if (isset($fieldConfig->backend_model)) { | ||
return (string)$fieldConfig->backend_model; | ||
} | ||
if (isset($fieldConfig->frontend_type)) { | ||
return $this->getBackendModelByInputType((string)$fieldConfig->frontend_type); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/code/core/Mage/Adminhtml/Model/System/Config/Backend/Color.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* 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: | ||
* https://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) 2023 The OpenMage Contributors (https://www.openmage.org) | ||
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
/** | ||
* Color config field backend model | ||
* | ||
* @category Mage | ||
* @package Mage_Adminhtml | ||
* @author Magento Core Team <core@magentocommerce.com> | ||
*/ | ||
class Mage_Adminhtml_Model_System_Config_Backend_Color extends Mage_Core_Model_Config_Data | ||
{ | ||
/** | ||
* @throws Mage_Core_Exception | ||
* @return $this | ||
*/ | ||
protected function _beforeSave() | ||
{ | ||
/** @var Mage_Core_Model_Config_Element $config */ | ||
$config = $this->getFieldConfig(); | ||
|
||
$validate = []; | ||
if (isset($config->validate)) { | ||
$validate = array_map('trim', explode(' ', $config->validate)); | ||
} | ||
|
||
if (!(string)$this->getValue() && !in_array('required-entry', $validate)) { | ||
return $this; | ||
} | ||
|
||
$with_hash = true; | ||
if (isset($config->with_hash)) { | ||
$with_hash = $config->is('with_hash', true); | ||
} | ||
|
||
if ($with_hash) { | ||
$regex = Varien_Data_Form_Element_Color::VALIDATION_REGEX_WITH_HASH; | ||
$errorMessage = 'Color must be in hexadecimal format with the hash character'; | ||
} else { | ||
$regex = Varien_Data_Form_Element_Color::VALIDATION_REGEX_WITHOUT_HASH; | ||
$errorMessage = 'Color must be in hexadecimal format without the hash character'; | ||
} | ||
|
||
if (!(bool)preg_match($regex, (string)$this->getValue())) { | ||
Mage::throwException(Mage::helper('adminhtml')->__($errorMessage)); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/** | ||
* 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: | ||
* https://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 Varien | ||
* @package Varien_Data | ||
* @copyright Copyright (c) 2023 The OpenMage Contributors (https://www.openmage.org) | ||
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
/** | ||
* Form text element | ||
* | ||
* @category Varien | ||
* @package Varien_Data | ||
* @author Magento Core Team <core@magentocommerce.com> | ||
*/ | ||
class Varien_Data_Form_Element_Color extends Varien_Data_Form_Element_Abstract | ||
{ | ||
public const VALIDATION_REGEX_WITH_HASH = '/^#[a-f0-9]{6}$/i'; | ||
public const VALIDATION_REGEX_WITHOUT_HASH = '/^[a-f0-9]{6}$/i'; | ||
|
||
/** | ||
* @param array $attributes | ||
*/ | ||
public function __construct($attributes = []) | ||
{ | ||
parent::__construct($attributes); | ||
$this->setType('text'); | ||
$this->setExtType('textfield'); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getHtmlAttributes() | ||
{ | ||
return ['type', 'title', 'class', 'style', 'oninput', 'disabled', 'readonly', 'tabindex']; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getElementHtml() | ||
{ | ||
$id = $this->getHtmlId(); | ||
$with_hash = strtolower((string) ($this->original_data['with_hash'] ?? 1)); | ||
|
||
if (!empty($with_hash) && $with_hash !== 'false' && $with_hash !== 'off') { | ||
$oninput = "document.getElementById('{$id}').value = this.value"; | ||
$regex = self::VALIDATION_REGEX_WITH_HASH; | ||
$this->setOninput("document.getElementById('{$id}:html5').value = {$regex}.test(this.value) ? this.value : '#000000'"); | ||
$this->addClass('validate-hex-color-hash'); | ||
} else { | ||
$oninput = "document.getElementById('{$id}').value = this.value.substring(1)"; | ||
$regex = self::VALIDATION_REGEX_WITHOUT_HASH; | ||
$this->setOninput("document.getElementById('{$id}:html5').value = {$regex}.test(this.value) ? '#'+this.value : '#000000'"); | ||
$this->addClass('validate-hex-color'); | ||
} | ||
|
||
$html = '<input type="color" id="' . $id . ':html5" class="input-color-html5" ' | ||
. 'value="#' . trim($this->getEscapedValue(), '#') . '" oninput="' . $oninput . '" ' | ||
. '/>' . "\n"; | ||
|
||
$this->addClass('input-color'); | ||
$html .= parent::getElementHtml(); | ||
return $html; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters