Skip to content

Commit eb5d155

Browse files
luigifabfballiano
andauthored
Rewrote afterSave of product and category image attributes (#3301)
* Stop trying to upload image when there are no images Allow to override extensions and models * added return type for method * added return type for method --------- Co-authored-by: Fabrizio Balliano <[email protected]>
1 parent 7f73fa5 commit eb5d155

File tree

2 files changed

+63
-41
lines changed
  • app/code/core/Mage/Catalog/Model

2 files changed

+63
-41
lines changed

app/code/core/Mage/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,51 @@
2222
class Mage_Catalog_Model_Category_Attribute_Backend_Image extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
2323
{
2424
/**
25-
* Save uploaded file and set its name to category
25+
* @return array
26+
*/
27+
public function getAllowedExtensions(): array
28+
{
29+
return ['jpg', 'jpeg', 'gif', 'png'];
30+
}
31+
32+
/**
33+
* Save uploaded file and set its name to category attribute
2634
* @param Varien_Object $object
2735
* @return $this
2836
*/
2937
public function afterSave($object)
3038
{
31-
$value = $object->getData($this->getAttribute()->getName());
32-
if (empty($value) && empty($_FILES)) {
33-
return $this;
34-
}
39+
$name = $this->getAttribute()->getName();
40+
$value = $object->getData($name);
3541

3642
if (is_array($value) && !empty($value['delete'])) {
37-
$object->setData($this->getAttribute()->getName(), '');
38-
$this->getAttribute()->getEntity()
39-
->saveAttribute($object, $this->getAttribute()->getName());
43+
$object->setData($name, '');
44+
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
4045
return $this;
4146
}
4247

43-
$path = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS;
48+
if (!empty($_FILES[$name])) {
49+
try {
50+
$validator = Mage::getModel('core/file_validator_image');
51+
$uploader = Mage::getModel('core/file_uploader', $name);
52+
$uploader->setAllowedExtensions($this->getAllowedExtensions());
53+
$uploader->setAllowRenameFiles(true);
54+
$uploader->setFilesDispersion(false);
55+
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
56+
$uploader->save(Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category');
4457

45-
try {
46-
$validator = Mage::getModel('core/file_validator_image');
47-
$uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());
48-
$uploader->setAllowedExtensions(['jpg','jpeg','gif','png']);
49-
$uploader->setAllowRenameFiles(true);
50-
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
51-
$result = $uploader->save($path);
52-
53-
$object->setData($this->getAttribute()->getName(), $result['file']);
54-
$this->getAttribute()->getEntity()->saveAttribute($object, $this->getAttribute()->getName());
55-
} catch (Exception $e) {
56-
if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
57-
Mage::logException($e);
58+
$fileName = $uploader->getUploadedFileName();
59+
if ($fileName) {
60+
$object->setData($name, $fileName);
61+
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
62+
}
63+
} catch (Exception $e) {
64+
if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
65+
Mage::logException($e);
66+
}
5867
}
5968
}
69+
6070
return $this;
6171
}
6272
}

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,49 @@
2222
class Mage_Catalog_Model_Resource_Product_Attribute_Backend_Image extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
2323
{
2424
/**
25+
* @return array
26+
*/
27+
public function getAllowedExtensions(): array
28+
{
29+
return ['jpg', 'jpeg', 'gif', 'png'];
30+
}
31+
32+
/**
33+
* Save uploaded file and set its name to product attribute
2534
* @param Varien_Object $object
2635
* @return $this
2736
*/
2837
public function afterSave($object)
2938
{
30-
$value = $object->getData($this->getAttribute()->getName());
39+
$name = $this->getAttribute()->getName();
40+
$value = $object->getData($name);
3141

3242
if (is_array($value) && !empty($value['delete'])) {
33-
$object->setData($this->getAttribute()->getName(), '');
34-
$this->getAttribute()->getEntity()
35-
->saveAttribute($object, $this->getAttribute()->getName());
43+
$object->setData($name, '');
44+
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
3645
return $this;
3746
}
3847

39-
try {
40-
$validator = Mage::getModel('core/file_validator_image');
41-
$uploader = new Mage_Core_Model_File_Uploader($this->getAttribute()->getName());
42-
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
43-
$uploader->setAllowRenameFiles(true);
44-
$uploader->setFilesDispersion(true);
45-
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
46-
$uploader->save(Mage::getBaseDir('media') . '/catalog/product');
48+
if (!empty($_FILES[$name])) {
49+
try {
50+
$validator = Mage::getModel('core/file_validator_image');
51+
$uploader = Mage::getModel('core/file_uploader', $name);
52+
$uploader->setAllowedExtensions($this->getAllowedExtensions());
53+
$uploader->setAllowRenameFiles(true);
54+
$uploader->setFilesDispersion(true);
55+
$uploader->addValidateCallback(Mage_Core_Model_File_Validator_Image::NAME, $validator, 'validate');
56+
$uploader->save(Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product');
4757

48-
$fileName = $uploader->getUploadedFileName();
49-
if ($fileName) {
50-
$object->setData($this->getAttribute()->getName(), $fileName);
51-
$this->getAttribute()->getEntity()
52-
->saveAttribute($object, $this->getAttribute()->getName());
58+
$fileName = $uploader->getUploadedFileName();
59+
if ($fileName) {
60+
$object->setData($name, $fileName);
61+
$this->getAttribute()->getEntity()->saveAttribute($object, $name);
62+
}
63+
} catch (Exception $e) {
64+
if ($e->getCode() != UPLOAD_ERR_NO_FILE) {
65+
Mage::logException($e);
66+
}
5367
}
54-
} catch (Exception $e) {
55-
return $this;
5668
}
5769

5870
return $this;

0 commit comments

Comments
 (0)