|
22 | 22 | class Mage_Catalog_Model_Category_Attribute_Backend_Image extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract |
23 | 23 | { |
24 | 24 | /** |
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 |
26 | 34 | * @param Varien_Object $object |
27 | 35 | * @return $this |
28 | 36 | */ |
29 | 37 | public function afterSave($object) |
30 | 38 | { |
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); |
35 | 41 |
|
36 | 42 | 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); |
40 | 45 | return $this; |
41 | 46 | } |
42 | 47 |
|
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'); |
44 | 57 |
|
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 | + } |
58 | 67 | } |
59 | 68 | } |
| 69 | + |
60 | 70 | return $this; |
61 | 71 | } |
62 | 72 | } |
0 commit comments