Skip to content

Commit be048d6

Browse files
committed
Merge remote-tracking branch 'github-magento/MAGETWO-87974' into EPAM-PR-8
2 parents 8eb6373 + be6bf03 commit be048d6

File tree

2 files changed

+135
-22
lines changed

2 files changed

+135
-22
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,8 @@ public function isAttributeValid($attrCode, array $attrParams, array $rowData, $
898898
}
899899

900900
/**
901-
*
902901
* Multiple value separator getter.
902+
*
903903
* @return string
904904
*/
905905
public function getMultipleValueSeparator()
@@ -949,6 +949,8 @@ public function getMediaGalleryAttributeId()
949949
}
950950

951951
/**
952+
* Get product by type name.
953+
*
952954
* @param string $name
953955
* @return Product\Type\AbstractType
954956
*/
@@ -1189,8 +1191,10 @@ protected function _initErrorTemplates()
11891191
}
11901192

11911193
/**
1192-
* Set valid attribute set and product type to rows with all scopes
1193-
* to ensure that existing products doesn't changed.
1194+
* Set valid attribute set and product type to rows.
1195+
*
1196+
* Set valid attribute set and product type to rows with all
1197+
* scopes to ensure that existing products doesn't changed.
11941198
*
11951199
* @param array $rowData
11961200
* @return array
@@ -1220,6 +1224,7 @@ protected function _prepareRowForDb(array $rowData)
12201224

12211225
/**
12221226
* Gather and save information about product links.
1227+
*
12231228
* Must be called after ALL products saving done.
12241229
*
12251230
* @return $this
@@ -1468,6 +1473,7 @@ public function saveProductEntity(array $entityRowsIn, array $entityRowsUp)
14681473

14691474
/**
14701475
* Return additional data, needed to select.
1476+
*
14711477
* @return array
14721478
*/
14731479
private function getOldSkuFieldsForSelect()
@@ -1477,6 +1483,7 @@ private function getOldSkuFieldsForSelect()
14771483

14781484
/**
14791485
* Adds newly created products to _oldSku
1486+
*
14801487
* @param array $newProducts
14811488
* @return void
14821489
*/
@@ -1514,6 +1521,7 @@ private function getNewSkuFieldsForSelect()
15141521

15151522
/**
15161523
* Init media gallery resources
1524+
*
15171525
* @return void
15181526
* @since 100.0.4
15191527
* @deprecated
@@ -1544,6 +1552,8 @@ protected function getExistingImages($bunch)
15441552
}
15451553

15461554
/**
1555+
* Retrieve image from row.
1556+
*
15471557
* @param array $rowData
15481558
* @return array
15491559
*/
@@ -1599,6 +1609,7 @@ protected function _saveProducts()
15991609
$tierPrices = [];
16001610
$mediaGallery = [];
16011611
$labelsForUpdate = [];
1612+
$imagesForChangeVisibility = [];
16021613
$uploadedImages = [];
16031614
$previousType = null;
16041615
$prevAttributeSet = null;
@@ -1718,21 +1729,24 @@ protected function _saveProducts()
17181729
}
17191730

17201731
// 5. Media gallery phase
1721-
$disabledImages = [];
17221732
list($rowImages, $rowLabels) = $this->getImagesFromRow($rowData);
17231733
$storeId = !empty($rowData[self::COL_STORE])
17241734
? $this->getStoreIdByCode($rowData[self::COL_STORE])
17251735
: Store::DEFAULT_STORE_ID;
1726-
if (isset($rowData['_media_is_disabled']) && strlen(trim($rowData['_media_is_disabled']))) {
1727-
$disabledImages = array_flip(
1728-
explode($this->getMultipleValueSeparator(), $rowData['_media_is_disabled'])
1729-
);
1736+
$imageHiddenStates = $this->getImagesHiddenStates($rowData);
1737+
foreach (array_keys($imageHiddenStates) as $image) {
1738+
if (array_key_exists($rowSku, $existingImages)
1739+
&& array_key_exists($image, $existingImages[$rowSku])
1740+
) {
1741+
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
1742+
$uploadedImages[$image] = $image;
1743+
}
1744+
17301745
if (empty($rowImages)) {
1731-
foreach (array_keys($disabledImages) as $disabledImage) {
1732-
$rowImages[self::COL_MEDIA_IMAGE][] = $disabledImage;
1733-
}
1746+
$rowImages[self::COL_MEDIA_IMAGE][] = $image;
17341747
}
17351748
}
1749+
17361750
$rowData[self::COL_MEDIA_IMAGE] = [];
17371751

17381752
/*
@@ -1766,13 +1780,23 @@ protected function _saveProducts()
17661780

17671781
if ($uploadedFile && !isset($mediaGallery[$storeId][$rowSku][$uploadedFile])) {
17681782
if (isset($existingImages[$rowSku][$uploadedFile])) {
1783+
$currentFileData = $existingImages[$rowSku][$uploadedFile];
17691784
if (isset($rowLabels[$column][$columnImageKey])
17701785
&& $rowLabels[$column][$columnImageKey] !=
1771-
$existingImages[$rowSku][$uploadedFile]['label']
1786+
$currentFileData['label']
17721787
) {
17731788
$labelsForUpdate[] = [
17741789
'label' => $rowLabels[$column][$columnImageKey],
1775-
'imageData' => $existingImages[$rowSku][$uploadedFile]
1790+
'imageData' => $currentFileData
1791+
];
1792+
}
1793+
1794+
if (array_key_exists($uploadedFile, $imageHiddenStates)
1795+
&& $currentFileData['disabled'] != $imageHiddenStates[$uploadedFile]
1796+
) {
1797+
$imagesForChangeVisibility[] = [
1798+
'disabled' => $imageHiddenStates[$uploadedFile],
1799+
'imageData' => $currentFileData
17761800
];
17771801
}
17781802
} else {
@@ -1785,7 +1809,8 @@ protected function _saveProducts()
17851809
? $rowLabels[$column][$columnImageKey]
17861810
: '',
17871811
'position' => ++$position,
1788-
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
1812+
'disabled' => isset($imageHiddenStates[$columnImage])
1813+
? $imageHiddenStates[$columnImage] : '0',
17891814
'value' => $uploadedFile,
17901815
];
17911816
}
@@ -1905,6 +1930,8 @@ protected function _saveProducts()
19051930
$mediaGallery
19061931
)->_saveProductAttributes(
19071932
$attributes
1933+
)->updateMediaGalleryVisibility(
1934+
$imagesForChangeVisibility
19081935
)->updateMediaGalleryLabels(
19091936
$labelsForUpdate
19101937
);
@@ -1919,6 +1946,34 @@ protected function _saveProducts()
19191946
}
19201947

19211948
/**
1949+
* Prepare array with image states (visible or hidden from product page)
1950+
*
1951+
* @param array $rowData
1952+
* @return array
1953+
*/
1954+
private function getImagesHiddenStates($rowData)
1955+
{
1956+
$statesArray = [];
1957+
$mappingArray = [
1958+
'_media_is_disabled' => '1'
1959+
];
1960+
1961+
foreach ($mappingArray as $key => $value) {
1962+
if (isset($rowData[$key]) && strlen(trim($rowData[$key]))) {
1963+
$items = explode($this->getMultipleValueSeparator(), $rowData[$key]);
1964+
1965+
foreach ($items as $item) {
1966+
$statesArray[$item] = $value;
1967+
}
1968+
}
1969+
}
1970+
1971+
return $statesArray;
1972+
}
1973+
1974+
/**
1975+
* Process row categories.
1976+
*
19221977
* @param array $rowData
19231978
* @return array
19241979
*/
@@ -1946,6 +2001,8 @@ protected function processRowCategories($rowData)
19462001
}
19472002

19482003
/**
2004+
* Get product websites.
2005+
*
19492006
* @param string $productSku
19502007
* @return array
19512008
*/
@@ -1955,6 +2012,8 @@ public function getProductWebsites($productSku)
19552012
}
19562013

19572014
/**
2015+
* Retrieve product categories.
2016+
*
19582017
* @param string $productSku
19592018
* @return array
19602019
*/
@@ -1964,6 +2023,8 @@ public function getProductCategories($productSku)
19642023
}
19652024

19662025
/**
2026+
* Get store id by code.
2027+
*
19672028
* @param string $storeCode
19682029
* @return array|int|null|string
19692030
*/
@@ -2055,6 +2116,8 @@ protected function _getUploader()
20552116
}
20562117

20572118
/**
2119+
* Retrieve uploader.
2120+
*
20582121
* @return Uploader
20592122
* @throws \Magento\Framework\Exception\LocalizedException
20602123
*/
@@ -2065,6 +2128,7 @@ public function getUploader()
20652128

20662129
/**
20672130
* Uploading files into the "catalog/product" media folder.
2131+
*
20682132
* Return a new file name if the same file is already exists.
20692133
*
20702134
* @param string $fileName
@@ -2259,7 +2323,7 @@ public function getEntityTypeCode()
22592323
* Returns array of new products data with SKU as key. All SKU keys are in lowercase for avoiding creation of
22602324
* new products with the same SKU in different letter cases.
22612325
*
2262-
* @var string $sku
2326+
* @param string $sku
22632327
* @return array
22642328
*/
22652329
public function getNewSku($sku = null)
@@ -2452,6 +2516,8 @@ public function validateRow(array $rowData, $rowNum)
24522516
}
24532517

24542518
/**
2519+
* Check if need to validate url key.
2520+
*
24552521
* @param array $rowData
24562522
* @return bool
24572523
*/
@@ -2763,8 +2829,11 @@ protected function getProductUrlSuffix($storeId = null)
27632829
}
27642830

27652831
/**
2832+
* Get url key.
2833+
*
27662834
* @param array $rowData
27672835
* @return string
2836+
*
27682837
* @since 100.0.3
27692838
*/
27702839
protected function getUrlKey($rowData)
@@ -2781,7 +2850,10 @@ protected function getUrlKey($rowData)
27812850
}
27822851

27832852
/**
2853+
* Retrieve resource.
2854+
*
27842855
* @return Proxy\Product\ResourceModel
2856+
*
27852857
* @since 100.0.3
27862858
*/
27872859
protected function getResource()
@@ -2835,6 +2907,21 @@ private function updateMediaGalleryLabels(array $labels)
28352907
}
28362908
}
28372909

2910+
/**
2911+
* Update 'disabled' field for media gallery entity
2912+
*
2913+
* @param array $images
2914+
* @return $this
2915+
*/
2916+
private function updateMediaGalleryVisibility(array $images)
2917+
{
2918+
if (!empty($images)) {
2919+
$this->mediaProcessor->updateMediaGalleryVisibility($images);
2920+
}
2921+
2922+
return $this;
2923+
}
2924+
28382925
/**
28392926
* Parse values from multiple attributes fields
28402927
*

app/code/Magento/CatalogImportExport/Model/Import/Product/MediaGalleryProcessor.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function __construct(
103103
/**
104104
* Save product media gallery.
105105
*
106-
* @param $mediaGalleryData
106+
* @param array $mediaGalleryData
107107
* @return void
108108
*/
109109
public function saveMediaGallery(array $mediaGalleryData)
@@ -152,14 +152,37 @@ public function saveMediaGallery(array $mediaGalleryData)
152152
* @return void
153153
*/
154154
public function updateMediaGalleryLabels(array $labels)
155+
{
156+
$this->updateMediaGalleryField($labels, 'label');
157+
}
158+
159+
/**
160+
* Update 'disabled' field for media gallery entity
161+
*
162+
* @param array $images
163+
* @return void
164+
*/
165+
public function updateMediaGalleryVisibility(array $images)
166+
{
167+
$this->updateMediaGalleryField($images, 'disabled');
168+
}
169+
170+
/**
171+
* Update value for requested field in media gallery entities
172+
*
173+
* @param array $data
174+
* @param string $field
175+
* @return void
176+
*/
177+
private function updateMediaGalleryField(array $data, $field)
155178
{
156179
$insertData = [];
157-
foreach ($labels as $label) {
158-
$imageData = $label['imageData'];
180+
foreach ($data as $datum) {
181+
$imageData = $datum['imageData'];
159182

160-
if ($imageData['label'] === null) {
183+
if ($imageData[$field] === null) {
161184
$insertData[] = [
162-
'label' => $label['label'],
185+
$field => $datum[$field],
163186
$this->getProductEntityLinkField() => $imageData[$this->getProductEntityLinkField()],
164187
'value_id' => $imageData['value_id'],
165188
'store_id' => Store::DEFAULT_STORE_ID,
@@ -168,7 +191,7 @@ public function updateMediaGalleryLabels(array $labels)
168191
$this->connection->update(
169192
$this->mediaGalleryValueTableName,
170193
[
171-
'label' => $label['label'],
194+
$field => $datum[$field],
172195
],
173196
[
174197
$this->getProductEntityLinkField() . ' = ?' => $imageData[$this->getProductEntityLinkField()],
@@ -224,6 +247,7 @@ public function getExistingImages(array $bunch)
224247
),
225248
[
226249
'label' => 'mgv.label',
250+
'disabled' => 'mgv.disabled',
227251
]
228252
)->joinInner(
229253
['pe' => $this->productEntityTableName],
@@ -263,7 +287,7 @@ private function initMediaGalleryResources()
263287
/**
264288
* Save media gallery data per store.
265289
*
266-
* @param $storeId
290+
* @param int $storeId
267291
* @param array $mediaGalleryData
268292
* @param array $newMediaValues
269293
* @param array $valueToProductId
@@ -339,6 +363,8 @@ private function getProductEntityLinkField()
339363
}
340364

341365
/**
366+
* Get resource.
367+
*
342368
* @return \Magento\CatalogImportExport\Model\Import\Proxy\Product\ResourceModel
343369
*/
344370
private function getResource()

0 commit comments

Comments
 (0)