Skip to content

Commit

Permalink
New feature: ConfigurableSwatches now allows for auto-generation of t…
Browse files Browse the repository at this point in the history
…he swatch image file based on color selection (#3686)

Co-authored-by: Fabrizio Balliano <[email protected]>
  • Loading branch information
empiricompany and fballiano committed Apr 25, 2024
1 parent 6ea8cf0 commit ad57659
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 256 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ public function getOptionValues()
$value['store' . $store->getId()] = isset($storeValues[$option->getId()])
? $helper->escapeHtml($storeValues[$option->getId()]) : '';
}
if ($this->isConfigurableSwatchesEnabled()) {
$value['swatch'] = $option->getSwatchValue();
}
$values[] = new Varien_Object($value);
}
$this->setData('option_values', $values);
}

return $values;
}

Expand Down Expand Up @@ -209,4 +211,13 @@ public function getAttributeObject()
{
return Mage::registry('entity_attribute');
}

/**
* Check if configurable swatches module is enabled and attribute is swatch type
*/
public function isConfigurableSwatchesEnabled(): bool
{
return Mage::helper('core')->isModuleEnabled('Mage_ConfigurableSwatches')
&& Mage::helper('configurableswatches')->attrIsSwatchType($this->getAttributeObject());
}
}
15 changes: 15 additions & 0 deletions app/code/core/Mage/Eav/Model/Entity/Attribute/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ public function _construct()
{
$this->_init('eav/entity_attribute_option');
}

/**
* Retrieve swatch hex value
*
* @return string|false
*/
public function getSwatchValue()
{
$swatch = Mage::getModel('eav/entity_attribute_option_swatch')
->load($this->getId(), 'option_id');
if (!$swatch->getId()) {
return false;
}
return $swatch->getValue();
}
}
30 changes: 30 additions & 0 deletions app/code/core/Mage/Eav/Model/Entity/Attribute/Option/Swatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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_Eav
* @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)
*/

/**
* Entity attribute swatch model
*
* @category Mage
* @package Mage_Eav
*
* @method Mage_Eav_Model_Resource_Entity_Attribute_Option_Swatch _getResource()
* @method Mage_Eav_Model_Resource_Entity_Attribute_Option_Swatch getResource()
*/
class Mage_Eav_Model_Entity_Attribute_Option_Swatch extends Mage_Core_Model_Abstract
{
public function _construct()
{
$this->_init('eav/entity_attribute_option_swatch');
}
}
19 changes: 19 additions & 0 deletions app/code/core/Mage/Eav/Model/Resource/Entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ protected function _saveOption(Mage_Core_Model_Abstract $object)
$adapter = $this->_getWriteAdapter();
$optionTable = $this->getTable('eav/attribute_option');
$optionValueTable = $this->getTable('eav/attribute_option_value');
$optionSwatchTable = $this->getTable('eav/attribute_option_swatch');

$stores = Mage::app()->getStores(true);
if (isset($option['value'])) {
Expand All @@ -301,6 +302,7 @@ protected function _saveOption(Mage_Core_Model_Abstract $object)
if (!empty($option['delete'][$optionId])) {
if ($intOptionId) {
$adapter->delete($optionTable, ['option_id = ?' => $intOptionId]);
$adapter->delete($optionSwatchTable, ['option_id = ?' => $intOptionId]);
}
continue;
}
Expand Down Expand Up @@ -346,11 +348,28 @@ protected function _saveOption(Mage_Core_Model_Abstract $object)
$adapter->insert($optionValueTable, $data);
}
}

// Swatch Value
if (isset($option['swatch'][$optionId])) {
if ($option['swatch'][$optionId]) {
$data = [
'option_id' => $intOptionId,
'value' => $option['swatch'][$optionId],
'filename' => Mage::helper('configurableswatches')->getHyphenatedString($values[0]) . Mage_ConfigurableSwatches_Helper_Productimg::SWATCH_FILE_EXT
];
$adapter->insertOnDuplicate($optionSwatchTable, $data);
} else {
$adapter->delete($optionSwatchTable, ['option_id = ?' => $intOptionId]);
}
}
}
$bind = ['default_value' => implode(',', $attributeDefaultValue)];
$where = ['attribute_id =?' => $object->getId()];
$adapter->update($this->getMainTable(), $bind, $where);
}
if (isset($option['swatch'])) {
Mage::helper('configurableswatches/productimg')->clearSwatchesCache();
}
}

return $this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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_Eav
* @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)
*/

/**
* Entity attribute swatch resource model
*
* @category Mage
* @package Mage_Eav
*/
class Mage_Eav_Model_Resource_Entity_Attribute_Option_Swatch extends Mage_Core_Model_Resource_Db_Abstract
{
protected function _construct()
{
$this->_init('eav/attribute_option_swatch', 'option_id');
}
}
5 changes: 4 additions & 1 deletion app/code/core/Mage/Eav/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<config>
<modules>
<Mage_Eav>
<version>1.6.0.1</version>
<version>1.6.0.2</version>
</Mage_Eav>
</modules>
<global>
Expand Down Expand Up @@ -60,6 +60,9 @@
<attribute_option_value>
<table>eav_attribute_option_value</table>
</attribute_option_value>
<attribute_option_swatch>
<table>eav_attribute_option_swatch</table>
</attribute_option_swatch>
<attribute_label>
<table>eav_attribute_label</table>
</attribute_label>
Expand Down
57 changes: 57 additions & 0 deletions app/code/core/Mage/Eav/sql/eav_setup/upgrade-1.6.0.1-1.6.0.2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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_Eav
* @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)
*/

/** @var Mage_Eav_Model_Entity_Setup $installer */
$installer = $this;
$installer->startSetup();

/**
* Create table 'eav/attribute_option_swatch'
*/
$table = $installer->getConnection()
->newTable($installer->getTable('eav/attribute_option_swatch'))
->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, [
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
], 'Value Id')
->addColumn('option_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, [
'unsigned' => true,
'nullable' => false,
'default' => '0',
], 'Option Id')
->addColumn('value', Varien_Db_Ddl_Table::TYPE_TEXT, 255, [
'nullable' => true,
'default' => null,
], 'Value')
->addColumn('filename', Varien_Db_Ddl_Table::TYPE_TEXT, 255, [
'nullable' => true,
'default' => null,
], 'Filename')
->addIndex(
$installer->getIdxName('eav/attribute_option_value', ['option_id']),
['option_id'],
['type' => Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE]
)
->addForeignKey(
$installer->getFkName('eav/attribute_option_swatch', 'option_id', 'eav/attribute_option', 'option_id'),
'option_id',
$installer->getTable('eav/attribute_option'),
'option_id',
Varien_Db_Ddl_Table::ACTION_CASCADE,
Varien_Db_Ddl_Table::ACTION_CASCADE
)
->setComment('Eav Attribute Option Swatch');
$installer->getConnection()->createTable($table);
Loading

0 comments on commit ad57659

Please sign in to comment.