Skip to content

Commit 7f1bc83

Browse files
author
Yaroslav Onischenko
committed
MAGETWO-44427: Create Merchant Beta Pull Request
1 parent 6ef26a7 commit 7f1bc83

File tree

3 files changed

+120
-131
lines changed

3 files changed

+120
-131
lines changed

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

Lines changed: 118 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,10 @@ public function setParameters(array $params)
745745
*/
746746
public function deleteProductsForReplacement()
747747
{
748-
$this->setParameters(array('behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE));
748+
$this->setParameters(array_merge(
749+
$this->getParameters(),
750+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE]
751+
));
749752
$this->_deleteProducts();
750753

751754
return $this;
@@ -823,7 +826,10 @@ protected function _replaceProducts()
823826
$this->deleteProductsForReplacement();
824827
$this->_oldSku = $this->skuProcessor->reloadOldSkus()->getOldSkus();
825828
$this->_validatedRows = null;
826-
$this->setParameters(array('behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND));
829+
$this->setParameters(array_merge(
830+
$this->getParameters(),
831+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND]
832+
));
827833
$this->_saveProductsData();
828834

829835
return $this;
@@ -1005,7 +1011,8 @@ protected function _saveLinks()
10051011
}
10061012

10071013
if ($linkedId == null) {
1008-
// Import file links to a SKU which is skipped for some reason, which leads to a "NULL"
1014+
// Import file links to a SKU which is skipped for some reason,
1015+
// which leads to a "NULL"
10091016
// link causing fatal errors.
10101017
$this->_logger->critical(
10111018
new \Exception(
@@ -1194,42 +1201,40 @@ protected function _saveProductEntity(array $entityRowsIn, array $entityRowsUp)
11941201
* @param $bunch
11951202
* @return array
11961203
*/
1197-
protected function _getAllBunchImages($bunch)
1204+
protected function getBunchImages($bunch)
11981205
{
1199-
$allImagesFromBunch = [];
1200-
foreach ($bunch as $rowData) {
1201-
$rowData = $this->_customFieldsMapping($rowData);
1202-
foreach ($this->_imagesArrayKeys as $image) {
1203-
if (empty($rowData[$image])) {
1206+
$images = [];
1207+
foreach ($bunch as $row) {
1208+
$row = $this->_customFieldsMapping($row);
1209+
foreach ($this->_imagesArrayKeys as $imageColumn) {
1210+
if (empty($row[$imageColumn])) {
12041211
continue;
12051212
}
12061213

1207-
$importImages = explode($this->getMultipleValueSeparator(), $rowData[$image]);
1208-
foreach ($importImages as $importImage) {
1209-
$imageTmp = str_replace('\\', '/', $importImage);
1210-
$imageTmp = explode('/', $imageTmp);
1211-
$importImageFileName = array_pop($imageTmp);
1214+
$rowImages = explode($this->getMultipleValueSeparator(), $row[$imageColumn]);
1215+
foreach ($rowImages as $rowImage) {
1216+
$destinationPath = str_replace('\\', '/', $rowImage);
1217+
$destinationPath = explode('/', $destinationPath);
1218+
$destinationPath = array_pop($destinationPath);
1219+
$destinationPath = preg_replace('/[^a-z0-9\._-]+/i', '', $destinationPath);
12121220

1213-
$dispersionPath =
1214-
\Magento\Framework\File\Uploader::getDispretionPath($importImageFileName);
1221+
$dispersion = \Magento\Framework\File\Uploader::getDispretionPath($destinationPath);
1222+
$destinationPath = mb_strtolower($dispersion . '/' . $destinationPath);
12151223

1216-
$imageSting = mb_strtolower(
1217-
$dispersionPath . '/' . preg_replace('/[^a-z0-9\._-]+/i', '', $importImageFileName)
1218-
);
1219-
$allImagesFromBunch[$importImage] = $imageSting;
1224+
$images[$rowImage] = $destinationPath;
12201225
}
12211226
}
12221227
}
1223-
return $allImagesFromBunch;
1228+
return $images;
12241229
}
12251230

12261231
/**
12271232
* Prepare all media files
12281233
*
1229-
* @param $allImagesFromBunch
1234+
* @param $images
12301235
* @return array
12311236
*/
1232-
protected function _prepareAllMediaFiles($allImagesFromBunch)
1237+
protected function getExistingImages($images)
12331238
{
12341239
static $productMediaGalleryTableName = null;
12351240
static $resource = null;
@@ -1243,28 +1248,61 @@ protected function _prepareAllMediaFiles($allImagesFromBunch)
12431248
->from(
12441249
$productMediaGalleryTableName,
12451250
['entity_id', 'value']
1246-
)->where('value IN (?)', $allImagesFromBunch)
1251+
)->where('value IN (?)', $images)
12471252
);
12481253
$result = array();
12491254
foreach ($allMedia as $image) {
1255+
if (!isset($result[$image['value']])){
12501256
$result[$image['value']] = [];
1257+
}
12511258
foreach ($this->_oldSku as $sku => $oldSkuData) {
1252-
if ($oldSkuData['entity_id'] != $image['entity_id']) {
1253-
continue;
1259+
if ($oldSkuData['entity_id'] == $image['entity_id']) {
1260+
$result[$image['value']][$image['entity_id']] = $sku;
12541261
}
1255-
$result[$image['value']][] = $sku;
12561262
}
12571263
}
12581264
return $result;
12591265
}
12601266

1267+
/*
1268+
* @param array $rowData
1269+
* @return array
1270+
*/
1271+
public function getImagesFromRow($rowData)
1272+
{
1273+
$images = [];
1274+
$labels = [];
1275+
foreach ($this->_imagesArrayKeys as $column) {
1276+
$images[$column] = [];
1277+
$labels[$column] = [];
1278+
if (!empty($rowData[$column])) {
1279+
$images[$column] = array_unique(
1280+
explode($this->getMultipleValueSeparator(), $rowData[$column])
1281+
);
1282+
}
1283+
1284+
if (!empty($rowData[$column . '_label'])) {
1285+
$labels[$column] = explode($this->getMultipleValueSeparator(), $rowData[$column . '_label']);
1286+
}
1287+
1288+
if (count($labels[$column]) > count($images[$column])) {
1289+
$labels[$column] = array_slice($labels[$column], 0, count($images[$column]));
1290+
} elseif (count($labels[$column]) < count($images[$column])) {
1291+
$labels[$column] = array_pad($labels[$column], count($images[$column]), '');
1292+
}
1293+
}
1294+
1295+
return [$images, $labels];
1296+
}
1297+
12611298
/**
12621299
* Gather and save information about product entities.
12631300
*
12641301
* @return $this
12651302
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
12661303
* @SuppressWarnings(PHPMD.NPathComplexity)
12671304
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1305+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
12681306
*/
12691307
protected function _saveProducts()
12701308
{
@@ -1283,11 +1321,11 @@ protected function _saveProducts()
12831321
$tierPrices = [];
12841322
$groupPrices = [];
12851323
$mediaGallery = [];
1286-
$uploadedGalleryFiles = [];
1324+
$uploadedImages = [];
12871325
$previousType = null;
12881326
$prevAttributeSet = null;
1289-
$allImagesFromBunch = $this->_getAllBunchImages($bunch);
1290-
$existingImages = $this->_prepareAllMediaFiles($allImagesFromBunch);
1327+
$bunchImages = $this->getBunchImages($bunch);
1328+
$existingImages = $this->getExistingImages($bunchImages);
12911329

12921330
foreach ($bunch as $rowNum => $rowData) {
12931331
if (!$this->validateRow($rowData, $rowNum)) {
@@ -1383,96 +1421,44 @@ protected function _saveProducts()
13831421
}
13841422

13851423
// 5. Media gallery phase
1386-
$mediaGalleryImages = [];
1387-
$mediaGalleryLabels = [];
1388-
$additionalImageIsDisabled = [];
1389-
if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
1390-
$mediaGalleryImages =
1391-
explode($this->getMultipleValueSeparator(), $rowData[self::COL_MEDIA_IMAGE]);
1392-
if (isset($rowData['_media_image_label'])) {
1393-
$mediaGalleryLabels =
1394-
explode($this->getMultipleValueSeparator(), $rowData['_media_image_label']);
1395-
} else {
1396-
$mediaGalleryLabels = [];
1397-
}
1398-
if (isset($rowData['_media_is_disabled'])) {
1399-
$additionalImageIsDisabled =
1400-
array_flip(explode($this->getMultipleValueSeparator(), $rowData['_media_is_disabled']));
1401-
}
1402-
if (count($mediaGalleryLabels) > count($mediaGalleryImages)) {
1403-
$mediaGalleryLabels = array_slice($mediaGalleryLabels, 0, count($mediaGalleryImages));
1404-
} elseif (count($mediaGalleryLabels) < count($mediaGalleryImages)) {
1405-
$mediaGalleryLabels = array_pad($mediaGalleryLabels, count($mediaGalleryImages), '');
1406-
}
1407-
}
1408-
1409-
foreach ($this->_imagesArrayKeys as $imageCol) {
1410-
if (!empty($rowData[$imageCol])
1411-
&& ($imageCol != self::COL_MEDIA_IMAGE)
1412-
&& !in_array($rowData[$imageCol], $mediaGalleryImages)
1413-
) {
1414-
$mediaGalleryImages[] = $rowData[$imageCol];
1415-
if (isset($mediaGalleryLabels)) {
1416-
$mediaGalleryLabels[] = isset($rowData[$imageCol . '_label'])
1417-
? $rowData[$imageCol . '_label']
1418-
: '';
1419-
} else {
1420-
$mediaGalleryLabels[] = '';
1421-
}
1422-
}
1424+
$disabledImages = [];
1425+
list($rowImages, $rowLabels) = $this->getImagesFromRow($rowData);
1426+
if (isset($rowData['_media_is_disabled'])) {
1427+
$disabledImages = array_flip(
1428+
explode($this->getMultipleValueSeparator(), $rowData['_media_is_disabled'])
1429+
);
14231430
}
1424-
$rowData[self::COL_MEDIA_IMAGE] = array();
1425-
foreach ($mediaGalleryImages as $mediaImage) {
1426-
$imagePath = $allImagesFromBunch[$mediaImage];
1427-
if (isset($existingImages[$imagePath]) && in_array($rowSku, $existingImages[$imagePath])) {
1428-
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1429-
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1430-
trim($mediaImage),
1431-
true
1432-
);
1433-
if ($uploadedGalleryFiles[$mediaImage] == '') {
1434-
$uploadedGalleryFiles[$mediaImage] = $mediaImage;
1431+
$rowData[self::COL_MEDIA_IMAGE] = [];
1432+
foreach ($rowImages as $column => $columnImages) {
1433+
foreach ($columnImages as $position => $columnImage) {
1434+
if (!isset($uploadedImages[$columnImage])) {
1435+
$uploadedFile = $this->uploadMediaFiles(trim($columnImage), true);
1436+
if ($uploadedFile) {
1437+
$uploadedImages[$columnImage] = $uploadedFile;
14351438
}
1439+
} else {
1440+
$uploadedFile = $uploadedImages[$columnImage];
14361441
}
1437-
} elseif (!isset($existingImages[$imagePath])) {
1438-
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1439-
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1440-
trim($mediaImage),
1441-
true
1442-
);
1443-
$newImagePath = $uploadedGalleryFiles[$mediaImage];
1444-
$existingImages[$newImagePath][] = $rowSku;
1445-
}
1446-
}
14471442

1448-
$rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
1449-
1450-
if (!empty($rowData[self::COL_MEDIA_IMAGE]) && is_array($rowData[self::COL_MEDIA_IMAGE])) {
1451-
$position = array_search($mediaImage, $mediaGalleryImages);
1452-
foreach ($rowData[self::COL_MEDIA_IMAGE] as $mediaImage) {
1453-
if (!empty($mediaImage)) {
1454-
$mediaGallery[$rowSku][] = [
1455-
'attribute_id' => $this->getMediaGalleryAttributeId(),
1456-
'label' => isset($mediaGalleryLabels[$position]) ? $mediaGalleryLabels[$position] : '',
1457-
'position' => $position + 1,
1458-
'disabled' => isset($additionalImageIsDisabled[$mediaImage]) ? '1' : '0',
1459-
'value' => $mediaImage,
1460-
];
1461-
}
1443+
if ($uploadedFile && $column !== self::COL_MEDIA_IMAGE) {
1444+
$rowData[$column] = $uploadedFile;
14621445
}
1463-
}
14641446

1465-
foreach ($this->_imagesArrayKeys as $imageCol) {
1466-
if (empty($rowData[$imageCol]) || ($imageCol == self::COL_MEDIA_IMAGE)) {
1467-
continue;
1468-
}
1469-
if (isset($existingImages[$imagePath])
1470-
&& !in_array($rowSku, $existingImages[$imagePath])
1471-
&& (($rowData[$imageCol] == $imagePath) || ($rowData[$imageCol] == $mediaImage))
1472-
) {
1473-
unset($rowData[$imageCol]);
1474-
} elseif (isset($uploadedGalleryFiles[$rowData[$imageCol]])) {
1475-
$rowData[$imageCol] = $uploadedGalleryFiles[$rowData[$imageCol]];
1447+
$imageNotAssigned = !isset($existingImages[$uploadedFile])
1448+
|| !in_array($rowSku, $existingImages[$uploadedFile]);
1449+
1450+
if ($uploadedFile && $imageNotAssigned) {
1451+
if ($column == self::COL_MEDIA_IMAGE) {
1452+
$rowData[$column][] = $uploadedFile;
1453+
}
1454+
$mediaGallery[$rowSku][] = [
1455+
'attribute_id' => $this->getMediaGalleryAttributeId(),
1456+
'label' => isset($rowLabels[$column][$position]) ? $rowLabels[$column][$position] : '',
1457+
'position' => $position + 1,
1458+
'disabled' => isset($disabledImages[$columnImage]) ? '1' : '0',
1459+
'value' => $uploadedFile,
1460+
];
1461+
$existingImages[$uploadedFile][] = $rowSku;
14761462
}
14771463
}
14781464
}
@@ -1744,7 +1730,7 @@ protected function _getUploader()
17441730
* @param string $fileName
17451731
* @return string
17461732
*/
1747-
protected function _uploadMediaFiles($fileName, $renameFileOff = false)
1733+
protected function uploadMediaFiles($fileName, $renameFileOff = false)
17481734
{
17491735
try {
17501736
$res = $this->_getUploader()->move($fileName, $renameFileOff);
@@ -1767,29 +1753,27 @@ protected function _saveMediaGallery(array $mediaGalleryData)
17671753
if (empty($mediaGalleryData)) {
17681754
return $this;
17691755
}
1770-
17711756
static $mediaGalleryTableName = null;
17721757
static $mediaValueTableName = null;
17731758
static $productId = null;
1774-
17751759
if (!$mediaGalleryTableName) {
17761760
$mediaGalleryTableName = $this->_resourceFactory->create()->getTable(
17771761
'catalog_product_entity_media_gallery'
17781762
);
17791763
}
1780-
17811764
if (!$mediaValueTableName) {
17821765
$mediaValueTableName = $this->_resourceFactory->create()->getTable(
17831766
'catalog_product_entity_media_gallery_value'
17841767
);
17851768
}
17861769
$productIds = [];
1770+
$imageNames = [];
17871771
$multiInsertData = [];
1772+
$valueToProductId = [];
17881773
foreach ($mediaGalleryData as $productSku => $mediaGalleryRows) {
17891774
$productId = $this->skuProcessor->getNewSku($productSku)['entity_id'];
17901775
$productIds[] = $productId;
17911776
$insertedGalleryImgs = [];
1792-
17931777
if (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND != $this->getBehavior()) {
17941778
$this->_connection->delete(
17951779
$mediaGalleryTableName,
@@ -1803,28 +1787,33 @@ protected function _saveMediaGallery(array $mediaGalleryData)
18031787
'entity_id' => $productId,
18041788
'value' => $insertValue['value'],
18051789
];
1790+
$valueToProductId[$insertValue['value']][] = $productId;
1791+
$imageNames[] = $insertValue['value'];
18061792
$multiInsertData[] = $valueArr;
18071793
$insertedGalleryImgs[] = $insertValue['value'];
18081794
}
18091795
}
18101796
}
1797+
$oldMediaValues = $this->_connection->fetchAssoc(
1798+
$this->_connection->select()->from($mediaGalleryTableName, ['value_id', 'value', 'entity_id'])
1799+
->where('value IN (?)', $imageNames)
1800+
);
18111801
$this->_connection->insertOnDuplicate($mediaGalleryTableName, $multiInsertData, ['entity_id']);
18121802
$multiInsertData = [];
1813-
$newMediaValues = $this->_connection->fetchAssoc(
1814-
$this->_connection->select()->from(
1815-
$mediaGalleryTableName,
1816-
['value_id', 'value', 'entity_id']
1817-
)->where(
1818-
'entity_id IN (?)',
1819-
$productIds
1820-
)
1821-
);
1803+
$newMediaSelect = $this->_connection->select()
1804+
->from($mediaGalleryTableName, ['value_id', 'value', 'entity_id'])
1805+
->where('value IN (?)', $imageNames);
1806+
if (array_keys($oldMediaValues)) {
1807+
$newMediaSelect->where('value_id NOT IN (?)', array_keys($oldMediaValues));
1808+
}
1809+
1810+
$newMediaValues = $this->_connection->fetchAssoc($newMediaSelect);
18221811
foreach ($mediaGalleryData as $productSku => $mediaGalleryRows) {
18231812
foreach ($mediaGalleryRows as $insertValue) {
18241813
foreach ($newMediaValues as $value_id => $values) {
18251814
if ($values['value'] == $insertValue['value']) {
18261815
$insertValue['value_id'] = $value_id;
1827-
$insertValue['entity_id'] = $values['entity_id'];
1816+
$insertValue['entity_id'] = array_shift($valueToProductId[$values['value']]);
18281817
unset($newMediaValues[$value_id]);
18291818
break;
18301819
}

app/code/Magento/CatalogUrlRewrite/Model/Product/Plugin/Import.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public function afterImportData(Observer $observer)
172172
*
173173
* @return Import
174174
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
175-
* @SuppressWarnings(PHPMD.NpathComplexity)
175+
* @SuppressWarnings(PHPMD.NPathComplexity)
176176
*/
177177
protected function _populateForUrlGeneration($rowData)
178178
{

0 commit comments

Comments
 (0)