Skip to content

Commit 21f6525

Browse files
author
Volodymyr Klymenko
authored
Merge pull request #1194 from magento-tsg/2.1.8-develop-pr17
[TSG] Backporting for 2.1 (pr17) (2.1.8)
2 parents 38108c7 + ad09918 commit 21f6525

File tree

84 files changed

+3790
-632
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+3790
-632
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Image/Upload.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@ protected function _isAllowed()
4444
}
4545

4646
/**
47-
* Upload file controller action
47+
* Upload file controller action.
4848
*
4949
* @return \Magento\Framework\Controller\ResultInterface
5050
*/
5151
public function execute()
5252
{
53+
$imageId = $this->_request->getParam('param_name', 'image');
5354
try {
54-
$result = $this->imageUploader->saveFileToTmpDir('image');
55+
$result = $this->imageUploader->saveFileToTmpDir($imageId);
5556

5657
$result['cookie'] = [
5758
'name' => $this->_getSession()->getName(),

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Catalog\Controller\Adminhtml\Category;
77

88
use Magento\Store\Model\StoreManagerInterface;
9+
use Magento\Catalog\Api\Data\CategoryAttributeInterface;
910

1011
/**
1112
* Class Save
@@ -48,6 +49,13 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
4849
*/
4950
private $storeManager;
5051

52+
/**
53+
* Config instance holder.
54+
*
55+
* @var \Magento\Eav\Model\Config
56+
*/
57+
private $eavConfig;
58+
5159
/**
5260
* Constructor
5361
*
@@ -72,8 +80,9 @@ public function __construct(
7280
}
7381

7482
/**
75-
* Filter category data
83+
* Filter category data.
7684
*
85+
* @deprecated
7786
* @param array $rawData
7887
* @return array
7988
*/
@@ -126,7 +135,7 @@ public function execute()
126135
$this->storeManager->setCurrentStore($store->getCode());
127136
$parentId = isset($categoryPostData['parent']) ? $categoryPostData['parent'] : null;
128137
if ($categoryPostData) {
129-
$category->addData($this->_filterCategoryPostData($categoryPostData));
138+
$category->addData($categoryPostData);
130139
if ($isNewCategory) {
131140
$parentCategory = $this->getParentCategory($parentId, $storeId);
132141
$category->setPath($parentCategory->getPath());
@@ -248,21 +257,48 @@ public function execute()
248257
}
249258

250259
/**
251-
* Image data preprocessing
260+
* Sets image attribute data to false, if image was removed.
252261
*
253262
* @param array $data
254263
*
255264
* @return array
256265
*/
257-
public function imagePreprocessing($data)
266+
public function imagePreprocessing(array $data)
258267
{
259-
if (empty($data['image'])) {
260-
unset($data['image']);
261-
$data['image']['delete'] = true;
268+
$emptyImageAttributes = $this->getEmptyImageAttributes($data);
269+
$attributeCodes = array_keys($emptyImageAttributes);
270+
foreach ($attributeCodes as $attributeCode) {
271+
$data[$attributeCode] = false;
262272
}
273+
263274
return $data;
264275
}
265276

277+
/**
278+
* Get image attributes without value.
279+
*
280+
* @param array $data
281+
* @return array
282+
*/
283+
private function getEmptyImageAttributes(array $data)
284+
{
285+
$result = [];
286+
$entityType = $this->getConfig()->getEntityType(CategoryAttributeInterface::ENTITY_TYPE_CODE);
287+
foreach ($entityType->getAttributeCollection() as $attribute) {
288+
$attributeCode = $attribute->getAttributeCode();
289+
$backendModel = $attribute->getBackend();
290+
if (isset($data[$attributeCode])) {
291+
continue;
292+
}
293+
if (!$backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
294+
continue;
295+
}
296+
$result[$attributeCode] = $attribute;
297+
}
298+
299+
return $result;
300+
}
301+
266302
/**
267303
* Converting inputs from string to boolean
268304
*
@@ -346,4 +382,20 @@ protected function getRedirectParams($isNewCategory, $hasError, $categoryId, $pa
346382
}
347383
return ['path' => $path, 'params' => $params];
348384
}
385+
386+
/**
387+
* Get Config instance.
388+
*
389+
* @return \Magento\Eav\Model\Config
390+
*/
391+
private function getConfig()
392+
{
393+
if (null === $this->eavConfig) {
394+
$this->eavConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(
395+
\Magento\Eav\Model\Config::class
396+
);
397+
}
398+
399+
return $this->eavConfig;
400+
}
349401
}

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,18 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
208208
$customOptions = [];
209209
foreach ($options as $customOptionData) {
210210
if (empty($customOptionData['is_delete'])) {
211+
if (empty($customOptionData['option_id'])) {
212+
$customOptionData['option_id'] = null;
213+
}
214+
211215
if (isset($customOptionData['values'])) {
212216
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
213217
return empty($valueData['is_delete']);
214218
});
215219
}
220+
216221
$customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]);
217222
$customOption->setProductSku($product->getSku());
218-
$customOption->setOptionId(null);
219223
$customOptions[] = $customOption;
220224
}
221225
}
@@ -330,21 +334,59 @@ public function mergeProductOptions($productOptions, $overwriteOptions)
330334
return $productOptions;
331335
}
332336

333-
foreach ($productOptions as $index => $option) {
337+
foreach ($productOptions as $optionIndex => $option) {
334338
$optionId = $option['option_id'];
339+
$option = $this->overwriteValue(
340+
$optionId,
341+
$option,
342+
$overwriteOptions
343+
);
335344

336-
if (!isset($overwriteOptions[$optionId])) {
337-
continue;
345+
if (isset($option['values']) && isset($overwriteOptions[$optionId]['values'])) {
346+
foreach ($option['values'] as $valueIndex => $value) {
347+
if (isset($value['option_type_id'])) {
348+
$valueId = $value['option_type_id'];
349+
$value = $this->overwriteValue(
350+
$valueId,
351+
$value,
352+
$overwriteOptions[$optionId]['values']
353+
);
354+
355+
$option['values'][$valueIndex] = $value;
356+
}
357+
}
338358
}
339359

360+
$productOptions[$optionIndex] = $option;
361+
}
362+
363+
return $productOptions;
364+
}
365+
366+
/**
367+
* Overwrite values of fields to default, if there are option id and field
368+
* name in array overwriteOptions.
369+
*
370+
* @param int $optionId
371+
* @param array $option
372+
* @param array $overwriteOptions
373+
*
374+
* @return array
375+
*/
376+
private function overwriteValue($optionId, $option, $overwriteOptions)
377+
{
378+
if (isset($overwriteOptions[$optionId])) {
340379
foreach ($overwriteOptions[$optionId] as $fieldName => $overwrite) {
341380
if ($overwrite && isset($option[$fieldName]) && isset($option['default_' . $fieldName])) {
342-
$productOptions[$index][$fieldName] = $option['default_' . $fieldName];
381+
$option[$fieldName] = $option['default_' . $fieldName];
382+
if ('title' == $fieldName) {
383+
$option['is_delete_store_title'] = 1;
384+
}
343385
}
344386
}
345387
}
346388

347-
return $productOptions;
389+
return $option;
348390
}
349391

350392
/**
@@ -354,8 +396,9 @@ private function getCustomOptionFactory()
354396
{
355397
if (null === $this->customOptionFactory) {
356398
$this->customOptionFactory = \Magento\Framework\App\ObjectManager::getInstance()
357-
->get('Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory');
399+
->get(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
358400
}
401+
359402
return $this->customOptionFactory;
360403
}
361404

@@ -366,8 +409,9 @@ private function getProductLinkFactory()
366409
{
367410
if (null === $this->productLinkFactory) {
368411
$this->productLinkFactory = \Magento\Framework\App\ObjectManager::getInstance()
369-
->get('Magento\Catalog\Api\Data\ProductLinkInterfaceFactory');
412+
->get(\Magento\Catalog\Api\Data\ProductLinkInterfaceFactory::class);
370413
}
414+
371415
return $this->productLinkFactory;
372416
}
373417

@@ -378,8 +422,9 @@ private function getProductRepository()
378422
{
379423
if (null === $this->productRepository) {
380424
$this->productRepository = \Magento\Framework\App\ObjectManager::getInstance()
381-
->get('Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
425+
->get('\Magento\Catalog\Api\ProductRepositoryInterface\Proxy');
382426
}
427+
383428
return $this->productRepository;
384429
}
385430

@@ -392,6 +437,7 @@ private function getLinkResolver()
392437
if (!is_object($this->linkResolver)) {
393438
$this->linkResolver = ObjectManager::getInstance()->get(LinkResolver::class);
394439
}
440+
395441
return $this->linkResolver;
396442
}
397443

@@ -406,6 +452,7 @@ private function getDateTimeFilter()
406452
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
407453
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
408454
}
455+
409456
return $this->dateTimeFilter;
410457
}
411458
}

app/code/Magento/Catalog/Model/Category.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,16 @@ public function formatUrlKey($str)
652652
}
653653

654654
/**
655-
* Retrieve image URL
655+
* Get image url by attribute code.
656656
*
657+
* @param string $attributeCode
657658
* @return string
659+
* @throws \Magento\Framework\Exception\LocalizedException
658660
*/
659-
public function getImageUrl()
661+
public function getImageUrl($attributeCode = 'image')
660662
{
661663
$url = false;
662-
$image = $this->getImage();
664+
$image = $this->getData($attributeCode);
663665
if ($image) {
664666
if (is_string($image)) {
665667
$url = $this->_storeManager->getStore()->getBaseUrl(
@@ -671,6 +673,7 @@ public function getImageUrl()
671673
);
672674
}
673675
}
676+
674677
return $url;
675678
}
676679

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,45 @@ public function __construct(
7070
}
7171

7272
/**
73-
* Get image uploader
73+
* Avoiding saving potential upload data to DB.
74+
* Will set empty image attribute value if image was not uploaded.
75+
*
76+
* @param \Magento\Framework\DataObject $object
77+
* @return $this
78+
*/
79+
public function beforeSave($object)
80+
{
81+
$attributeName = $this->getAttribute()->getName();
82+
$value = $object->getData($attributeName);
83+
$imageName = $this->getUploadedImageName($value);
84+
85+
if ($imageName) {
86+
$object->setData($attributeName, $imageName);
87+
} else if (!is_string($value)) {
88+
$object->setData($attributeName, '');
89+
}
90+
91+
return parent::beforeSave($object);
92+
}
93+
94+
/**
95+
* Gets image name from $value array.
96+
* Will return empty string in case $value is not an array.
97+
*
98+
* @param array $value Attribute value
99+
* @return string
100+
*/
101+
private function getUploadedImageName($value)
102+
{
103+
if (is_array($value) && isset($value[0]['name'])) {
104+
return $value[0]['name'];
105+
}
106+
107+
return '';
108+
}
109+
110+
/**
111+
* Get image uploader.
74112
*
75113
* @return \Magento\Catalog\Model\ImageUploader
76114
*
@@ -79,26 +117,25 @@ public function __construct(
79117
private function getImageUploader()
80118
{
81119
if ($this->imageUploader === null) {
82-
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
83-
'Magento\Catalog\CategoryImageUpload'
84-
);
120+
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()
121+
->get(\Magento\Catalog\CategoryImageUpload::class);
85122
}
123+
86124
return $this->imageUploader;
87125
}
88126

89127
/**
90-
* Save uploaded file and set its name to category
128+
* Save uploaded file and set its name to category.
91129
*
92130
* @param \Magento\Framework\DataObject $object
93131
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
94132
*/
95133
public function afterSave($object)
96134
{
97-
$image = $object->getData($this->getAttribute()->getName(), null);
98-
99-
if ($image !== null) {
135+
$imageName = $object->getData($this->getAttribute()->getName(), null);
136+
if ($imageName) {
100137
try {
101-
$this->getImageUploader()->moveFileFromTmp($image);
138+
$this->getImageUploader()->moveFileFromTmp($imageName);
102139
} catch (\Exception $e) {
103140
$this->_logger->critical($e);
104141
}

0 commit comments

Comments
 (0)