Skip to content

Commit

Permalink
Fixed faulty media gallery label/description management for mutistore…
Browse files Browse the repository at this point in the history
… projects (#2481)

Co-authored-by: Fabrizio Balliano <[email protected]>
Co-authored-by: Tony <[email protected]>
  • Loading branch information
fballiano and empiricompany authored Apr 19, 2024
1 parent ca663ca commit bd7430d
Show file tree
Hide file tree
Showing 5 changed files with 507 additions and 465 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Gallery extends Varien_Da
{
public function getElementHtml()
{
//$html.= $this->getAfterElementHtml();
return $this->getContentHtml();
}

Expand All @@ -38,7 +37,8 @@ public function getContentHtml()
$content = Mage::getSingleton('core/layout')
->createBlock('adminhtml/catalog_product_helper_form_gallery_content');

$content->setId($this->getHtmlId() . '_content')
$content
->setId($this->getHtmlId() . '_content')
->setElement($this);
return $content->toHtml();
}
Expand Down Expand Up @@ -66,19 +66,31 @@ public function canDisplayUseDefault($attribute)
/**
* Check default value usage fact
*
* @param Mage_Eav_Model_Entity_Attribute $attribute
* @param Mage_Eav_Model_Entity_Attribute|string $attribute
* @return bool
*/
public function usedDefault($attribute)
{
$attributeCode = $attribute->getAttributeCode();
$defaultValue = $this->getDataObject()->getAttributeDefaultValue($attributeCode);
if (is_string($attribute)) {
$attributeCode = $attribute;
} else {
$attributeCode = $attribute->getAttributeCode();
}

// special management for "label" and "position" since they're columns of the
// catalog_product_entity_media_gallery_value database table
if ($attributeCode == "label" || $attributeCode == "position") {
$media_gallery = $this->getDataObject()->getMediaGallery();
if (!count($media_gallery["images"])) {
return true;
}
return $media_gallery["images"][0]["{$attributeCode}_use_default"];
}

$defaultValue = $this->getDataObject()->getAttributeDefaultValue($attributeCode);
if (!$this->getDataObject()->getExistsStoreValueFlag($attributeCode)) {
return true;
} elseif ($this->getValue() == $defaultValue &&
$this->getDataObject()->getStoreId() != $this->_getDefaultStoreId()
) {
} elseif ($this->getValue() == $defaultValue && $this->getDataObject()->getStoreId() != $this->_getDefaultStoreId()) {
return false;
}
if ($defaultValue === false && !$attribute->getIsRequired() && $this->getValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,6 @@ protected function _initProductSave()
$this->_filterStockData($productData['stock_data']);
}

/**
* Websites
*/
if (!isset($productData['website_ids'])) {
$productData['website_ids'] = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public function afterLoad($object)
foreach ($this->_getResource()->loadGallery($object, $this) as $image) {
foreach ($localAttributes as $localAttribute) {
if (is_null($image[$localAttribute])) {
$image[$localAttribute . '_use_default'] = true;
$image[$localAttribute] = $this->_getDefaultValue($localAttribute, $image);
} else {
$image[$localAttribute . '_use_default'] = false;
}
}
$value['images'][] = $image;
Expand Down Expand Up @@ -107,6 +110,10 @@ public function beforeSave($object)
$value['images'] = Mage::helper('core')->jsonDecode($value['images']);
}

if (!is_array($value['values']) && strlen($value['values']) > 0) {
$value['values'] = Mage::helper('core')->jsonDecode($value['values']);
}

if (!is_array($value['images'])) {
$value['images'] = [];
}
Expand All @@ -119,11 +126,11 @@ public function beforeSave($object)
if (!empty($image['removed'])) {
$clearImages[] = $image['file'];
} elseif (!isset($image['value_id'])) {
$newFile = $this->_moveImageFromTmp($image['file']);
$newFile = $this->_moveImageFromTmp($image['file']);
$image['new_file'] = $newFile;
$newImages[$image['file']] = $image;
$this->_renamedImages[$image['file']] = $newFile;
$image['file'] = $newFile;
$image['file'] = $newFile;
} else {
$existImages[$image['file']] = $image;
}
Expand All @@ -142,8 +149,6 @@ public function beforeSave($object)
];
$duplicate[$image['value_id']] = $newFile;
}

$value['duplicate'] = $duplicate;
}

foreach ($object->getMediaAttributes() as $mediaAttribute) {
Expand All @@ -157,21 +162,21 @@ public function beforeSave($object)
if (in_array($attrData, $clearImages)) {
$object->setData($mediaAttrCode, 'no_selection');
}
}

foreach ($value['values'] as $mediaAttrCode => $attrData) {
if (array_key_exists($attrData, $newImages)) {
$object->setData($mediaAttrCode, $newImages[$attrData]['new_file']);
$object->setData($mediaAttrCode . '_label', $newImages[$attrData]['label']);
$object->setData($mediaAttrCode . '_label', ($newImages[$attrData]['label'] === null || $newImages[$attrData]['label_use_default']) ? $newImages[$attrData]['label_default'] : $newImages[$attrData]['label']);
}

if (array_key_exists($attrData, $existImages)) {
$object->setData($mediaAttrCode . '_label', $existImages[$attrData]['label']);
$object->setData($mediaAttrCode . '_label', ($existImages[$attrData]['label'] === null || $existImages[$attrData]['label_use_default']) ? $existImages[$attrData]['label_default'] : $existImages[$attrData]['label']);
}
}

Mage::dispatchEvent('catalog_product_media_save_before', ['product' => $object, 'images' => $value]);

$object->setData($attrCode, $value);

return $this;
}

Expand Down Expand Up @@ -204,7 +209,6 @@ public function afterSave($object)
}

$storeId = $object->getStoreId();

$storeIds = $object->getStoreIds();
$storeIds[] = Mage_Core_Model_App::ADMIN_STORE_ID;

Expand Down Expand Up @@ -243,8 +247,8 @@ public function afterSave($object)
// Add per store labels, position, disabled
$data = [];
$data['value_id'] = $image['value_id'];
$data['label'] = $image['label'];
$data['position'] = (int) $image['position'];
$data['label'] = ($image['label'] === null || $image["label_use_default"]) ? null : $image['label'];
$data['position'] = ($image['position'] === null || $image["position_use_default"]) ? null : (int) $image['position'];
$data['disabled'] = (int) $image['disabled'];
$data['store_id'] = (int) $object->getStoreId();

Expand Down
Loading

0 comments on commit bd7430d

Please sign in to comment.