Skip to content

Commit bd7430d

Browse files
Fixed faulty media gallery label/description management for mutistore projects (#2481)
Co-authored-by: Fabrizio Balliano <fabrizio.balliano@gmail.com> Co-authored-by: Tony <github@magemega.com>
1 parent ca663ca commit bd7430d

File tree

5 files changed

+507
-465
lines changed

5 files changed

+507
-465
lines changed

app/code/core/Mage/Adminhtml/Block/Catalog/Product/Helper/Form/Gallery.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Gallery extends Varien_Da
2323
{
2424
public function getElementHtml()
2525
{
26-
//$html.= $this->getAfterElementHtml();
2726
return $this->getContentHtml();
2827
}
2928

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

41-
$content->setId($this->getHtmlId() . '_content')
40+
$content
41+
->setId($this->getHtmlId() . '_content')
4242
->setElement($this);
4343
return $content->toHtml();
4444
}
@@ -66,19 +66,31 @@ public function canDisplayUseDefault($attribute)
6666
/**
6767
* Check default value usage fact
6868
*
69-
* @param Mage_Eav_Model_Entity_Attribute $attribute
69+
* @param Mage_Eav_Model_Entity_Attribute|string $attribute
7070
* @return bool
7171
*/
7272
public function usedDefault($attribute)
7373
{
74-
$attributeCode = $attribute->getAttributeCode();
75-
$defaultValue = $this->getDataObject()->getAttributeDefaultValue($attributeCode);
74+
if (is_string($attribute)) {
75+
$attributeCode = $attribute;
76+
} else {
77+
$attributeCode = $attribute->getAttributeCode();
78+
}
7679

80+
// special management for "label" and "position" since they're columns of the
81+
// catalog_product_entity_media_gallery_value database table
82+
if ($attributeCode == "label" || $attributeCode == "position") {
83+
$media_gallery = $this->getDataObject()->getMediaGallery();
84+
if (!count($media_gallery["images"])) {
85+
return true;
86+
}
87+
return $media_gallery["images"][0]["{$attributeCode}_use_default"];
88+
}
89+
90+
$defaultValue = $this->getDataObject()->getAttributeDefaultValue($attributeCode);
7791
if (!$this->getDataObject()->getExistsStoreValueFlag($attributeCode)) {
7892
return true;
79-
} elseif ($this->getValue() == $defaultValue &&
80-
$this->getDataObject()->getStoreId() != $this->_getDefaultStoreId()
81-
) {
93+
} elseif ($this->getValue() == $defaultValue && $this->getDataObject()->getStoreId() != $this->_getDefaultStoreId()) {
8294
return false;
8395
}
8496
if ($defaultValue === false && !$attribute->getIsRequired() && $this->getValue()) {

app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,6 @@ protected function _initProductSave()
579579
$this->_filterStockData($productData['stock_data']);
580580
}
581581

582-
/**
583-
* Websites
584-
*/
585582
if (!isset($productData['website_ids'])) {
586583
$productData['website_ids'] = [];
587584
}

app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public function afterLoad($object)
4343
foreach ($this->_getResource()->loadGallery($object, $this) as $image) {
4444
foreach ($localAttributes as $localAttribute) {
4545
if (is_null($image[$localAttribute])) {
46+
$image[$localAttribute . '_use_default'] = true;
4647
$image[$localAttribute] = $this->_getDefaultValue($localAttribute, $image);
48+
} else {
49+
$image[$localAttribute . '_use_default'] = false;
4750
}
4851
}
4952
$value['images'][] = $image;
@@ -107,6 +110,10 @@ public function beforeSave($object)
107110
$value['images'] = Mage::helper('core')->jsonDecode($value['images']);
108111
}
109112

113+
if (!is_array($value['values']) && strlen($value['values']) > 0) {
114+
$value['values'] = Mage::helper('core')->jsonDecode($value['values']);
115+
}
116+
110117
if (!is_array($value['images'])) {
111118
$value['images'] = [];
112119
}
@@ -119,11 +126,11 @@ public function beforeSave($object)
119126
if (!empty($image['removed'])) {
120127
$clearImages[] = $image['file'];
121128
} elseif (!isset($image['value_id'])) {
122-
$newFile = $this->_moveImageFromTmp($image['file']);
129+
$newFile = $this->_moveImageFromTmp($image['file']);
123130
$image['new_file'] = $newFile;
124131
$newImages[$image['file']] = $image;
125132
$this->_renamedImages[$image['file']] = $newFile;
126-
$image['file'] = $newFile;
133+
$image['file'] = $newFile;
127134
} else {
128135
$existImages[$image['file']] = $image;
129136
}
@@ -142,8 +149,6 @@ public function beforeSave($object)
142149
];
143150
$duplicate[$image['value_id']] = $newFile;
144151
}
145-
146-
$value['duplicate'] = $duplicate;
147152
}
148153

149154
foreach ($object->getMediaAttributes() as $mediaAttribute) {
@@ -157,21 +162,21 @@ public function beforeSave($object)
157162
if (in_array($attrData, $clearImages)) {
158163
$object->setData($mediaAttrCode, 'no_selection');
159164
}
165+
}
160166

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

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

171178
Mage::dispatchEvent('catalog_product_media_save_before', ['product' => $object, 'images' => $value]);
172-
173179
$object->setData($attrCode, $value);
174-
175180
return $this;
176181
}
177182

@@ -204,7 +209,6 @@ public function afterSave($object)
204209
}
205210

206211
$storeId = $object->getStoreId();
207-
208212
$storeIds = $object->getStoreIds();
209213
$storeIds[] = Mage_Core_Model_App::ADMIN_STORE_ID;
210214

@@ -243,8 +247,8 @@ public function afterSave($object)
243247
// Add per store labels, position, disabled
244248
$data = [];
245249
$data['value_id'] = $image['value_id'];
246-
$data['label'] = $image['label'];
247-
$data['position'] = (int) $image['position'];
250+
$data['label'] = ($image['label'] === null || $image["label_use_default"]) ? null : $image['label'];
251+
$data['position'] = ($image['position'] === null || $image["position_use_default"]) ? null : (int) $image['position'];
248252
$data['disabled'] = (int) $image['disabled'];
249253
$data['store_id'] = (int) $object->getStoreId();
250254

0 commit comments

Comments
 (0)