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

New feature: ConfigurableSwatches now allows for auto-generation of the swatch image file based on color selection #3686

Merged
merged 50 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
25ef34b
add color picker
empiricompany Dec 2, 2023
d529c87
save swatch colors
empiricompany Dec 3, 2023
8a0aa72
fix swatch save if new option
empiricompany Dec 3, 2023
b1329fc
fallback swatches auto-creation
empiricompany Dec 3, 2023
7a95a4a
php-cs
empiricompany Dec 3, 2023
d5500d2
php-cs
empiricompany Dec 3, 2023
f3760ae
Update app/code/core/Mage/Eav/sql/eav_setup/upgrade-1.6.0.1-1.6.0.2.php
empiricompany Dec 4, 2023
c24ba77
Update app/code/core/Mage/Eav/Model/Entity/Attribute/Option/Swatch.php
empiricompany Dec 4, 2023
0f7b05d
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Dec 7, 2023
680dc09
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Dec 8, 2023
31960b5
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Dec 11, 2023
e0b046b
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Dec 19, 2023
9c14a5d
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Dec 21, 2023
797a01d
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Feb 11, 2024
8e04589
fix setup constraint naming issue
empiricompany Feb 12, 2024
e929302
stylish input color picker
empiricompany Feb 12, 2024
4142e60
wip not save all black colors
empiricompany Feb 12, 2024
67976fd
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Feb 21, 2024
c59d99b
Update app/design/adminhtml/default/default/template/eav/attribute/op…
empiricompany Feb 21, 2024
810ff83
Update app/code/core/Mage/Eav/sql/eav_setup/upgrade-1.6.0.1-1.6.0.2.php
empiricompany Feb 21, 2024
4a9d25f
Update app/code/core/Mage/Eav/Model/Entity/Attribute/Option.php
empiricompany Feb 21, 2024
ad08be1
Update app/code/core/Mage/Eav/Model/Entity/Attribute/Option/Swatch.php
empiricompany Feb 21, 2024
e77742d
Update app/code/core/Mage/Eav/Model/Resource/Entity/Attribute/Option/…
empiricompany Feb 21, 2024
434c96e
Update app/code/core/Mage/Eav/Model/Resource/Entity/Attribute.php
empiricompany Feb 21, 2024
f515647
fix return type
empiricompany Feb 22, 2024
74e1e3b
better model name
empiricompany Feb 22, 2024
d7cbb38
add empty color picker placeholder
empiricompany Feb 22, 2024
e45487c
better scss
empiricompany Feb 22, 2024
d418f61
stylish empty placeholder
empiricompany Feb 22, 2024
990ff47
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Mar 1, 2024
0eb8bf8
use variable for swatch table
empiricompany Mar 1, 2024
3926da0
add translation
empiricompany Mar 1, 2024
553988e
allow to delete swatch color
empiricompany Mar 1, 2024
424366b
php-cs
empiricompany Mar 1, 2024
dc63323
better cross-browser color picker
empiricompany Mar 1, 2024
22685f0
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Mar 3, 2024
0153ced
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Mar 6, 2024
0247856
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Mar 11, 2024
79eb30d
not needed docblock
fballiano Mar 14, 2024
1e70f81
empty line necessary
fballiano Mar 14, 2024
5ad246d
Update app/locale/en_US/Mage_ConfigurableSwatches.csv
empiricompany Mar 15, 2024
834cd58
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Mar 23, 2024
e5e16b5
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Apr 2, 2024
0f1387c
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Apr 6, 2024
b353e2b
Merge branch 'main' into configurable-swatches-enhanced
empiricompany Apr 8, 2024
aeea471
Merge branch 'main' into configurable-swatches-enhanced
fballiano Apr 20, 2024
90e37b4
removed prototypejs and unneeded safari workarounds
fballiano Apr 20, 2024
71c0296
whitespace
fballiano Apr 22, 2024
acfa3b0
escape strings
fballiano Apr 22, 2024
4a8de41
Merge branch 'main' into configurable-swatches-enhanced
fballiano Apr 22, 2024
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
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);
fballiano marked this conversation as resolved.
Show resolved Hide resolved
}

$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;
fballiano marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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
Loading