Skip to content

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed May 1, 2024
2 parents fc0305a + 25fd6bb commit 458f9e6
Show file tree
Hide file tree
Showing 27 changed files with 1,195 additions and 271 deletions.
3 changes: 2 additions & 1 deletion app/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ public static function run($code = '', $type = 'store', $options = [])
if (isset($options['edition'])) {
self::$_currentEdition = $options['edition'];
}
self::$_app = new Mage_Core_Model_App();
self::$_app = new Mage_Core_Model_App();
if (isset($options['request'])) {
self::$_app->setRequest($options['request']);
}
Expand All @@ -748,6 +748,7 @@ public static function run($code = '', $type = 'store', $options = [])
die();
} catch (Exception $e) {
if (self::isInstalled()) {
self::dispatchEvent('mage_run_installed_exception', ['exception' => $e]);
self::printException($e);
exit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ protected function _prepareCollection()
'product_id=entity_id',
'category_id=' . (int) $this->getRequest()->getParam('id', 0),
'left'
);
)
->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner')
->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
$this->setCollection($collection);

if ($this->getCategory()->getProductsReadonly()) {
Expand Down Expand Up @@ -112,35 +114,100 @@ protected function _prepareColumns()
'index' => 'entity_id'
]);
}

$this->addColumn('entity_id', [
'header' => Mage::helper('catalog')->__('ID'),
'sortable' => true,
'width' => '60',
'index' => 'entity_id'
]);

$this->addColumn('name', [
'header' => Mage::helper('catalog')->__('Name'),
'index' => 'name'
]);

$this->addColumn('type', [
'header' => Mage::helper('catalog')->__('Type'),
'width' => 100,
'index' => 'type_id',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
]);

$sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
->load()
->toOptionHash();

$this->addColumn('set_name', [
'header' => Mage::helper('catalog')->__('Attrib. Set Name'),
'width' => 130,
'index' => 'attribute_set_id',
'type' => 'options',
'options' => $sets,
]);

$this->addColumn('status', [
'header' => Mage::helper('catalog')->__('Status'),
'width' => 90,
'index' => 'status',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
]);

$this->addColumn('visibility', [
'header' => Mage::helper('catalog')->__('Visibility'),
'width' => 90,
'index' => 'visibility',
'type' => 'options',
'options' => Mage::getSingleton('catalog/product_visibility')->getOptionArray(),
]);

$this->addColumn('sku', [
'header' => Mage::helper('catalog')->__('SKU'),
'width' => '80',
'index' => 'sku'
]);

$this->addColumn('price', [
'header' => Mage::helper('catalog')->__('Price'),
'type' => 'currency',
'width' => '1',
'currency_code' => (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE),
'index' => 'price'
]);

$this->addColumn('position', [
'header' => Mage::helper('catalog')->__('Position'),
'width' => '1',
'type' => 'number',
'index' => 'position',
'editable' => !$this->getCategory()->getProductsReadonly()
//'renderer' => 'adminhtml/widget_grid_column_renderer_input'
]);

$this->addColumn('action', [
'header' => Mage::helper('catalog')->__('Action'),
'width' => '50px',
'type' => 'action',
'getter' => 'getId',
'actions' => [
[
'caption' => Mage::helper('catalog')->__('Edit'),
'id' => 'editlink',
'field' => 'id',
'onclick' => "popWin(this.href,'win','width=1000,height=700,resizable=1,scrollbars=1');return false;",
'url' => [
'base' => 'adminhtml/catalog_product/edit',
'params' => [
'store' => $this->getRequest()->getParam('store'),
'popup' => 1
],
],
],
],
'filter' => false,
'sortable' => false,
'index' => 'stores',
]);

return parent::_prepareColumns();
Expand Down
46 changes: 46 additions & 0 deletions app/code/core/Mage/ConfigurableSwatches/Helper/Productimg.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ public function getGlobalSwatchUrl(
do {
$filename = Mage::helper('configurableswatches')->getHyphenatedString($value) . $fileExt;
$swatchImage = $this->_resizeSwatchImage($filename, 'media', $width, $height);
if (!$swatchImage) {
$swatchImage = $this->createSwatchImage($value, $width, $height);
}
if (!$swatchImage && $defaultValue == $value) {
return ''; // no image found and no further fallback
} elseif (!$swatchImage) {
Expand All @@ -263,6 +266,49 @@ public function getGlobalSwatchUrl(
return Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . $swatchImage;
}

/**
* Create a swatch image for the given filename
*
* @param string $value
* @param int $width
* @param int $height
* @return string|false $destPath
* @throws Mage_Core_Exception
*/
public function createSwatchImage($value, $width, $height)
{
$filename = Mage::helper('configurableswatches')->getHyphenatedString($value) . self::SWATCH_FILE_EXT;
$optionSwatch = Mage::getModel('eav/entity_attribute_option_swatch')
->load($filename, 'filename');
if (!$optionSwatch->getValue()) {
return false;
}

// Form full path to where we want to cache resized version
$destPathArr = [
self::SWATCH_CACHE_DIR,
Mage::app()->getStore()->getId(),
$width . 'x' . $height,
'media',
trim($filename, '/'),
];
$destPath = implode('/', $destPathArr);
if (!is_dir(Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . dirname($destPath))) {
$io = new Varien_Io_File();
$io->mkdir(Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . dirname($destPath), 0777, true);
}

$newImage = imagecreatetruecolor($width, $height);
list($r, $g, $b) = sscanf($optionSwatch->getValue(), "#%02x%02x%02x");
$backgroundColor = imagecolorallocate($newImage, (int)$r, (int)$g, (int)$b);
imagefill($newImage, 0, 0, $backgroundColor);
imagepng($newImage, Mage::getBaseDir(Mage_Core_Model_Store::URL_TYPE_MEDIA) . DS . $destPath);
imagedestroy($newImage);
Mage::helper('core/file_storage_database')->saveFile($destPath);

return $destPath;
}

/**
* Performs the resize operation on the given swatch image file and returns a
* relative path to the resulting image file
Expand Down
158 changes: 158 additions & 0 deletions app/code/core/Mage/Core/Helper/EnvironmentConfigLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/**
* 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_Core
* @copyright Copyright (c) 2024 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/**
* Core Environment helper
*
* @category Mage
* @package Mage_Core
*/
class Mage_Core_Helper_EnvironmentConfigLoader extends Mage_Core_Helper_Abstract
{
protected const ENV_STARTS_WITH = 'OPENMAGE_CONFIG';
protected const ENV_KEY_SEPARATOR = '__';
protected const CONFIG_KEY_DEFAULT = 'DEFAULT';
protected const CONFIG_KEY_WEBSITES = 'WEBSITES';
protected const CONFIG_KEY_STORES = 'STORES';
/**
* To be used as regex condition
*/
protected const ALLOWED_CHARS = ['A-Z', '-', '_'];
protected $_moduleName = 'Mage_Core';
protected array $envStore = [];

/**
* Load configuration values from ENV variables into xml config object
*
* Environment variables work on this schema:
*
* self::ENV_STARTS_WITH . self::ENV_KEY_SEPARATOR (OPENMAGE_CONFIG__)
* ^ Prefix (required)
* <SCOPE>__
* ^ Where scope is DEFAULT, WEBSITES__<WEBSITE_CODE> or STORES__<STORE_CODE>
* <SYSTEM_VARIABLE_NAME>
* ^ Where GROUP, SECTION and FIELD are separated by self::ENV_KEY_SEPARATOR
*
* Each example will override the 'general/store_information/name' value.
* Override from the default configuration:
* @example OPENMAGE_CONFIG__DEFAULT__GENERAL__STORE_INFORMATION__NAME=default
* Override the website 'base' configuration:
* @example OPENMAGE_CONFIG__WEBSITES__BASE__GENERAL__STORE_INFORMATION__NAME=website
* Override the store 'german' configuration:
* @example OPENMAGE_CONFIG__STORES__GERMAN__GENERAL__STORE_INFORMATION__NAME=store_german
*
* @param Varien_Simplexml_Config $xmlConfig
* @return void
*/
public function overrideEnvironment(Varien_Simplexml_Config $xmlConfig)
{
$env = $this->getEnv();

foreach ($env as $configKey => $value) {
if (!$this->isConfigKeyValid($configKey)) {
continue;
}

list($configKeyParts, $scope) = $this->getConfigKey($configKey);

switch ($scope) {
case static::CONFIG_KEY_DEFAULT:
list($_, $_, $section, $group, $field) = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
$xmlConfig->setNode($this->buildNodePath($scope, $path), $value);
break;

case static::CONFIG_KEY_WEBSITES:
case static::CONFIG_KEY_STORES:
list($_, $_, $code, $section, $group, $field) = $configKeyParts;
$path = $this->buildPath($section, $group, $field);
$nodePath = sprintf('%s/%s/%s', strtolower($scope), strtolower($code), $path);
$xmlConfig->setNode($nodePath, $value);
break;
}
}
}

/**
* @internal method mostly for mocking
*/
public function setEnvStore(array $envStorage): void
{
$this->envStore = $envStorage;
}

public function getEnv(): array
{
if (empty($this->envStore)) {
$this->envStore = getenv();
}
return $this->envStore;
}

protected function getConfigKey(string $configKey): array
{
$configKeyParts = array_filter(
explode(
static::ENV_KEY_SEPARATOR,
$configKey
),
'trim'
);
list($_, $scope) = $configKeyParts;
return array($configKeyParts, $scope);
}

protected function isConfigKeyValid(string $configKey): bool
{
if (!str_starts_with($configKey, static::ENV_STARTS_WITH)) {
return false;
}

$sectionGroupFieldRegexp = sprintf('([%s]*)', implode('', static::ALLOWED_CHARS));
$allowedChars = sprintf('[%s]', implode('', static::ALLOWED_CHARS));
$regexp = '/' . static::ENV_STARTS_WITH . static::ENV_KEY_SEPARATOR . '(WEBSITES' . static::ENV_KEY_SEPARATOR
. $allowedChars . '+|DEFAULT|STORES' . static::ENV_KEY_SEPARATOR . $allowedChars . '+)'
. static::ENV_KEY_SEPARATOR . $sectionGroupFieldRegexp
. static::ENV_KEY_SEPARATOR . $sectionGroupFieldRegexp
. static::ENV_KEY_SEPARATOR . $sectionGroupFieldRegexp . '/';
// /OPENMAGE_CONFIG__(WEBSITES__[A-Z-_]+|DEFAULT|STORES__[A-Z-_]+)__([A-Z-_]*)__([A-Z-_]*)__([A-Z-_]*)/

return preg_match($regexp, $configKey);
}

/**
* Build configuration path.
*
* @param string $section
* @param string $group
* @param string $field
* @return string
*/
protected function buildPath(string $section, string $group, string $field): string
{
return strtolower(implode('/', [$section, $group, $field]));
}

/**
* Build configuration node path.
*
* @param string $scope
* @param string $path
* @return string
*/
protected function buildNodePath(string $scope, string $path): string
{
return strtolower($scope) . '/' . $path;
}
}
1 change: 1 addition & 0 deletions app/code/core/Mage/Core/Model/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ protected function _initModules()
Varien_Profiler::stop('mage::app::init::apply_db_schema_updates');
}
$this->_config->loadDb();
$this->_config->loadEnv();
$this->_config->saveCache();
}
} finally {
Expand Down
18 changes: 18 additions & 0 deletions app/code/core/Mage/Core/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public function init($options = [])
$this->_useCache = false;
$this->loadModules();
$this->loadDb();
$this->loadEnv();
$this->saveCache();
}
} finally {
Expand Down Expand Up @@ -422,6 +423,23 @@ public function loadDb()
return $this;
}

/**
* Load environment variables and override config
*
* @return self
*/
public function loadEnv(): Mage_Core_Model_Config
{
if ($this->_isLocalConfigLoaded && Mage::isInstalled()) {
Varien_Profiler::start('config/load-env');
/** @var Mage_Core_Helper_EnvironmentConfigLoader $environmentConfigLoaderHelper */
$environmentConfigLoaderHelper = Mage::helper('core/environmentConfigLoader');
$environmentConfigLoaderHelper->overrideEnvironment($this);
Varien_Profiler::stop('config/load-env');
}
return $this;
}

/**
* Reinitialize configuration
*
Expand Down
Loading

0 comments on commit 458f9e6

Please sign in to comment.