Skip to content

Commit

Permalink
change category image attribute implementation to work with any attri…
Browse files Browse the repository at this point in the history
…bute code rather than working only with 'image'
  • Loading branch information
Allan Paiste committed Aug 4, 2016
1 parent f2d309a commit 70d3468
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ protected function _isAllowed()
*/
public function execute()
{
$imageId = $this->_request->getParam('param_name', 'image');

try {
$result = $this->imageUploader->saveFileToTmpDir('image');
$result = $this->imageUploader->saveFileToTmpDir($imageId);

$result['cookie'] = [
'name' => $this->_getSession()->getName(),
Expand Down
61 changes: 28 additions & 33 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Catalog\Controller\Adminhtml\Category;

use Magento\Store\Model\StoreManagerInterface;
use \Magento\Catalog\Api\Data\CategoryAttributeInterface;

/**
* Class Save
Expand Down Expand Up @@ -48,6 +49,11 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
*/
private $storeManager;

/**
* @var \Magento\Eav\Model\Config
*/
private $eavConfig;

/**
* Constructor
*
Expand All @@ -56,43 +62,22 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param StoreManagerInterface $storeManager
* @param \Magento\Eav\Model\Config $eavConfig
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
\Magento\Eav\Model\Config $eavConfig
) {
parent::__construct($context);
$this->resultRawFactory = $resultRawFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
$this->storeManager = $storeManager;
}

/**
* Filter category data
*
* @param array $rawData
* @return array
*/
protected function _filterCategoryPostData(array $rawData)
{
$data = $rawData;
// @todo It is a workaround to prevent saving this data in category model and it has to be refactored in future
if (isset($data['image']) && is_array($data['image'])) {
if (!empty($data['image']['delete'])) {
$data['image'] = null;
} else {
if (isset($data['image'][0]['name']) && isset($data['image'][0]['tmp_name'])) {
$data['image'] = $data['image'][0]['name'];
} else {
unset($data['image']);
}
}
}
return $data;
$this->eavConfig = $eavConfig;
}

/**
Expand Down Expand Up @@ -126,7 +111,7 @@ public function execute()
$this->storeManager->setCurrentStore($store->getCode());
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
if ($categoryPostData) {
$category->addData($this->_filterCategoryPostData($categoryPostData));
$category->addData($categoryPostData);
if ($isNewCategory) {
$parentCategory = $this->getParentCategory($parentId, $storeId);
$category->setPath($parentCategory->getPath());
Expand Down Expand Up @@ -248,18 +233,28 @@ public function execute()
}

/**
* Image data preprocessing
*
* @param array $data
*
* @param $data
* @return array
*/
public function imagePreprocessing($data)
{
if (empty($data['image'])) {
unset($data['image']);
$data['image']['delete'] = true;
$entityType = $this->eavConfig->getEntityType(CategoryAttributeInterface::ENTITY_TYPE_CODE);

foreach ($entityType->getAttributeCollection() as $attributeModel) {
$attributeCode = $attributeModel->getAttributeCode();
$backendModel = $attributeModel->getBackend();

if (isset($data[$attributeCode])) {
continue;
}

if (!$backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
continue;
}

$data[$attributeCode] = false;
}

return $data;
}

Expand Down
10 changes: 5 additions & 5 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,14 +652,14 @@ public function formatUrlKey($str)
}

/**
* Retrieve image URL
*
* @return string
* @param string $attributeCode
* @return bool|string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getImageUrl()
public function getImageUrl($attributeCode = 'image')
{
$url = false;
$image = $this->getImage();
$image = $this->getData($attributeCode);
if ($image) {
if (is_string($image)) {
$url = $this->_storeManager->getStore()->getBaseUrl(
Expand Down
74 changes: 57 additions & 17 deletions app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
const ADDITIONAL_DATA_SUFFIX = '_additional_data';

/**
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*
Expand All @@ -21,17 +23,13 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
protected $_uploaderFactory;

/**
* Filesystem facade
*
* @var \Magento\Framework\Filesystem
*
* @deprecated
*/
protected $_filesystem;

/**
* File Uploader factory
*
* @var \Magento\MediaStorage\Model\File\UploaderFactory
*
* @deprecated
Expand All @@ -46,15 +44,11 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
protected $_logger;

/**
* Image uploader
*
* @var \Magento\Catalog\Model\ImageUploader
*/
private $imageUploader;

/**
* Image constructor.
*
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
Expand All @@ -70,8 +64,50 @@ public function __construct(
}

/**
* Get image uploader
* @param $value
* @return string|bool
*/
protected function getUploadedImageName($value)
{
if (!is_array($value)) {
return false;
}

if (!count($value)) {
return false;
}

$imageData = reset($value);

if (!isset($imageData['name'])) {
return false;
}

return $imageData['name'];
}

/**
* Avoiding saving potential upload data to DB
*
* @param \Magento\Framework\DataObject $object
* @return $this
*/
public function beforeSave($object)
{
$attributeName = $this->getAttribute()->getName();
$value = $object->getData($attributeName);

if ($value === false || (is_array($value) && isset($value['delete']) && $value['delete'] === true)) {
$object->setData($attributeName, '');
} else if ($imageName = $this->getUploadedImageName($value)) {
$object->setData($attributeName . self::ADDITIONAL_DATA_SUFFIX, $value);
$object->setData($attributeName, $imageName);
}

return parent::beforeSave($object);
}

/**
* @return \Magento\Catalog\Model\ImageUploader
*
* @deprecated
Expand All @@ -83,6 +119,7 @@ private function getImageUploader()
\Magento\Catalog\CategoryImageUpload::class
);
}

return $this->imageUploader;
}

Expand All @@ -94,15 +131,18 @@ private function getImageUploader()
*/
public function afterSave($object)
{
$image = $object->getData($this->getAttribute()->getName(), null);

if ($image !== null) {
try {
$this->getImageUploader()->moveFileFromTmp($image);
} catch (\Exception $e) {
$this->_logger->critical($e);
}
$value = $object->getData($this->getAttribute()->getName() . self::ADDITIONAL_DATA_SUFFIX);

if (!$imageName = $this->getUploadedImageName($value)) {
return $this;
}

try {
$this->getImageUploader()->moveFileFromTmp($imageName);
} catch (\Exception $e) {
$this->_logger->critical($e);
}

return $this;
}
}
19 changes: 14 additions & 5 deletions app/code/Magento/Catalog/Model/Category/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,20 @@ public function getData()
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
$categoryData = $this->addUseConfigSettings($categoryData);
$categoryData = $this->filterFields($categoryData);
if (isset($categoryData['image'])) {
unset($categoryData['image']);
$categoryData['image'][0]['name'] = $category->getData('image');
$categoryData['image'][0]['url'] = $category->getImageUrl();
}

foreach ($category->getAttributes() as $attributeCode => $attribute) {
$backendModel = $attribute->getBackend();

if ($backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
if (isset($categoryData[$attributeCode])) {
unset($categoryData[$attributeCode]);

$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
}
}
}

$this->loadedData[$category->getId()] = $categoryData;
}
return $this->loadedData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,21 @@ define([
/**
* Handler which is invoked prior to the start of a file upload.
*
* @param {Event} e - Event obejct.
* @param {Event} e - Event object.
* @param {Object} data - File data that will be uploaded.
*/
onBeforeFileUpload: function (e, data) {
var file = data.files[0],
allowed = this.isFileAllowed(file);

if (allowed.passed) {
$(e.target).fileupload('process', data).done(function () {
var $target = $(e.target);

$target.on('fileuploadsend', function(event, postData) {
postData.data.set('param_name', this.paramName);
}.bind(data));

$target.fileupload('process', data).done(function () {
data.submit();
});
} else {
Expand Down

0 comments on commit 70d3468

Please sign in to comment.