Skip to content

Commit c181df2

Browse files
committed
Merge branch 'MAGNIMEX-145-Fix-Images'
2 parents 426b670 + fad7292 commit c181df2

File tree

2 files changed

+118
-141
lines changed

2 files changed

+118
-141
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ atlassian*
3131
!/pub/media/customer/.htaccess
3232
/pub/media/downloadable/*
3333
!/pub/media/downloadable/.htaccess
34+
/pub/media/import/*
35+
!/pub/media/import/.htaccess
3436
/pub/media/theme/*
3537
/pub/media/theme_customization/*
3638
!/pub/media/theme_customization/.htaccess

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

Lines changed: 116 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,39 +1221,65 @@ protected function _saveProductEntity(array $entityRowsIn, array $entityRowsUp)
12211221
return $this;
12221222
}
12231223

1224+
/**
1225+
* Retrieving images from all columns and rows
1226+
*
1227+
* @param $bunch
1228+
* @return array
1229+
*/
1230+
protected function _getAllBunchImages($bunch)
1231+
{
1232+
$allImagesFromBunch = [];
1233+
foreach ($bunch as $rowData) {
1234+
$rowData = $this->_customFieldsMapping($rowData);
1235+
foreach ($this->_imagesArrayKeys as $image) {
1236+
$dispersionPath =
1237+
\Magento\Framework\File\Uploader::getDispretionPath($rowData[$image]);
1238+
$importImages = explode($this->getMultipleValueSeparator(), $rowData[$image]);
1239+
foreach ($importImages as $importImage) {
1240+
$imageSting = mb_strtolower(
1241+
$dispersionPath . '/' . preg_replace('/[^a-z0-9\._-]+/i', '', $importImage)
1242+
);
1243+
$allImagesFromBunch[$importImage] = $imageSting;
1244+
}
1245+
}
1246+
}
1247+
return $allImagesFromBunch;
1248+
}
1249+
12241250
/**
12251251
* Prepare all media files
12261252
*
1227-
* @return $this
1253+
* @param $allImagesFromBunch
1254+
* @return array
12281255
*/
1229-
protected function _prepareAllMediaFiles()
1256+
protected function _prepareAllMediaFiles($allImagesFromBunch)
12301257
{
1231-
static $productEntityTableName = null;
12321258
static $productMediaGalleryTableName = null;
12331259
static $resource = null;
12341260
if (!$resource) {
12351261
$resource = $this->_resourceFactory->create();
12361262
}
1237-
if (!$productEntityTableName) {
1238-
$productEntityTableName = $resource->getTable('catalog_product_entity');
1239-
}
12401263
if (!$productMediaGalleryTableName) {
12411264
$productMediaGalleryTableName = $resource->getTable('catalog_product_entity_media_gallery');
12421265
}
1243-
if(empty($this->cachedImages)) {
1244-
$allMedia = $this->_connection->fetchAll($this->_connection->select()
1245-
->from(
1246-
["entity" => $productEntityTableName],
1247-
['sku']
1248-
)->joinLeft(
1249-
["media_gallery" => $productMediaGalleryTableName],
1250-
"entity.entity_id = media_gallery.entity_id",
1251-
['value']
1252-
)
1253-
);
1254-
$this->cachedImages = $allMedia;
1266+
$allMedia = $this->_connection->fetchAll($this->_connection->select()
1267+
->from(
1268+
$productMediaGalleryTableName,
1269+
['entity_id', 'value']
1270+
)->where('value IN (?)', $allImagesFromBunch)
1271+
);
1272+
$result = array();
1273+
foreach ($allMedia as $image) {
1274+
$result[$image['value']] = [];
1275+
foreach ($this->_oldSku as $sku => $oldSkuData) {
1276+
if ($oldSkuData['entity_id'] != $image['entity_id']) {
1277+
continue;
1278+
}
1279+
$result[$image['value']][] = $sku;
1280+
}
12551281
}
1256-
return $this;
1282+
return $result;
12571283
}
12581284

12591285
/**
@@ -1269,7 +1295,6 @@ protected function _saveProducts()
12691295
/** @var $resource \Magento\CatalogImportExport\Model\Import\Proxy\Product\Resource */
12701296
$resource = $this->_resourceFactory->create();
12711297
$priceIsGlobal = $this->_catalogData->isPriceGlobal();
1272-
$this->_prepareAllMediaFiles();
12731298
$productLimit = null;
12741299
$productsQty = null;
12751300

@@ -1285,6 +1310,8 @@ protected function _saveProducts()
12851310
$uploadedGalleryFiles = [];
12861311
$previousType = null;
12871312
$prevAttributeSet = null;
1313+
$allImagesFromBunch = $this->_getAllBunchImages($bunch);
1314+
$existingImages = $this->_prepareAllMediaFiles($allImagesFromBunch);
12881315

12891316
foreach ($bunch as $rowNum => $rowData) {
12901317
if (!$this->validateRow($rowData, $rowNum)) {
@@ -1334,9 +1361,9 @@ protected function _saveProducts()
13341361
$this->websitesCache[$rowSku] = [];
13351362
// 2. Product-to-Website phase
13361363
if (!empty($rowData[self::COL_PRODUCT_WEBSITES])) {
1337-
$websiteIds = explode($this->getMultipleValueSeparator(), $rowData[self::COL_PRODUCT_WEBSITES]);
1338-
foreach ($websiteIds as $websiteId) {
1339-
$websiteId = $this->storeResolver->getWebsiteCodeToId($rowData[self::COL_PRODUCT_WEBSITES]);
1364+
$websiteCodes = explode($this->getMultipleValueSeparator(), $rowData[self::COL_PRODUCT_WEBSITES]);
1365+
foreach ($websiteCodes as $websiteCode) {
1366+
$websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
13401367
$this->websitesCache[$rowSku][$websiteId] = true;
13411368
}
13421369
}
@@ -1375,145 +1402,93 @@ protected function _saveProducts()
13751402
];
13761403
}
13771404

1378-
// 5. Media gallery phase
1405+
if (!$this->validateRow($rowData, $rowNum)) {
1406+
continue;
1407+
}
13791408

1380-
$fullDispersionPath = '';
1381-
$imageIsSet = null;
1382-
$imageFromProduct = null;
1383-
$imageInProductIsSet = null;
1409+
// 5. Media gallery phase
1410+
$mediaGalleryImages = array();
1411+
$mediaGalleryLabels = array();
13841412
if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
1385-
$dispersionPath =
1386-
\Magento\Framework\File\Uploader::getDispretionPath($rowData[self::COL_MEDIA_IMAGE]);
1387-
$imageName = preg_replace('/[^a-z0-9\._-]+/i', '', $rowData[self::COL_MEDIA_IMAGE]);
1388-
$fullDispersionPath = mb_strtolower($dispersionPath . '/' . $imageName);
1389-
foreach ($this->cachedImages as $image) {
1390-
if (($image['sku'] == $rowData[self::COL_SKU])
1391-
&& ($image['value'] == $fullDispersionPath)
1392-
) {
1393-
$imageInProductIsSet = true;
1394-
$imageFromProduct = $image['value'];
1395-
break;
1396-
} elseif (($image['sku'] == $rowData[self::COL_SKU])
1397-
&& (preg_replace('/_[0-9]?\./', '.', $image['value']) == $fullDispersionPath)
1398-
) {
1399-
$imageInProductIsSet = true;
1400-
$imageFromProduct = preg_replace('/_[0-9]?\./', '.', $image['value']);
1401-
break;
1402-
} elseif (in_array($fullDispersionPath, $image)) {
1403-
$imageIsSet = true;
1404-
break;
1405-
}
1413+
$mediaGalleryImages =
1414+
explode($this->getMultipleValueSeparator(), $rowData[self::COL_MEDIA_IMAGE]);
1415+
if (isset($rowData['_media_image_label'])) {
1416+
$mediaGalleryLabels =
1417+
explode($this->getMultipleValueSeparator(), $rowData['_media_image_label']);
1418+
} else {
1419+
$mediaGalleryLabels = [];
14061420
}
1407-
}
1408-
if (($imageInProductIsSet && ($imageFromProduct != $fullDispersionPath))
1409-
|| (!isset($imageIsSet) && !isset($imageInProductIsSet))
1410-
) {
1411-
$mediaGalleryImages = array();
1412-
$mediaGalleryLabels = array();
1413-
if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
1414-
$mediaGalleryImages =
1415-
explode($this->getMultipleValueSeparator(), $rowData[self::COL_MEDIA_IMAGE]);
1416-
if (isset($rowData['_media_image_label'])) {
1417-
$mediaGalleryLabels =
1418-
explode($this->getMultipleValueSeparator(), $rowData['_media_image_label']);
1419-
} else {
1420-
$mediaGalleryLabels = [];
1421-
}
1422-
if (count($mediaGalleryLabels) > count($mediaGalleryImages)) {
1423-
$mediaGalleryLabels = array_slice($mediaGalleryLabels, 0, count($mediaGalleryImages));
1424-
} elseif (count($mediaGalleryLabels) < count($mediaGalleryImages)) {
1425-
$mediaGalleryLabels = array_pad($mediaGalleryLabels, count($mediaGalleryImages), '');
1426-
}
1421+
if (count($mediaGalleryLabels) > count($mediaGalleryImages)) {
1422+
$mediaGalleryLabels = array_slice($mediaGalleryLabels, 0, count($mediaGalleryImages));
1423+
} elseif (count($mediaGalleryLabels) < count($mediaGalleryImages)) {
1424+
$mediaGalleryLabels = array_pad($mediaGalleryLabels, count($mediaGalleryImages), '');
14271425
}
1426+
}
14281427

1429-
foreach ($this->_imagesArrayKeys as $imageCol) {
1430-
if (!empty($rowData[$imageCol])
1431-
&& ($imageCol != self::COL_MEDIA_IMAGE)
1432-
&& !in_array($rowData[$imageCol], $mediaGalleryImages)) {
1433-
$mediaGalleryImages[] = $rowData[$imageCol];
1434-
if (isset($mediaGalleryLabels)) {
1435-
$mediaGalleryLabels[] = isset($rowData[$imageCol . '_label'])
1428+
foreach ($this->_imagesArrayKeys as $imageCol) {
1429+
if (!empty($rowData[$imageCol])
1430+
&& ($imageCol != self::COL_MEDIA_IMAGE)
1431+
&& !in_array($rowData[$imageCol], $mediaGalleryImages)
1432+
) {
1433+
$mediaGalleryImages[] = $rowData[$imageCol];
1434+
if (isset($mediaGalleryLabels)) {
1435+
$mediaGalleryLabels[] = isset($rowData[$imageCol . '_label'])
14361436
? $rowData[$imageCol . '_label']
14371437
: '';
1438-
} else {
1439-
$mediaGalleryLabels[] = '';
1440-
}
1438+
} else {
1439+
$mediaGalleryLabels[] = '';
14411440
}
14421441
}
1443-
1444-
$rowData[self::COL_MEDIA_IMAGE] = array();
1445-
foreach ($mediaGalleryImages as $mediaImage) {
1442+
}
1443+
$rowData[self::COL_MEDIA_IMAGE] = array();
1444+
foreach ($mediaGalleryImages as $mediaImage) {
1445+
$imagePath = $allImagesFromBunch[$mediaImage];
1446+
if (isset($existingImages[$imagePath]) && in_array($rowSku, $existingImages[$imagePath])) {
14461447
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
14471448
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1448-
trim($mediaImage)
1449+
trim($mediaImage),
1450+
true
14491451
);
14501452
}
1451-
$rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
1452-
}
1453-
1454-
foreach ($this->_imagesArrayKeys as $imageCol) {
1455-
if (!empty($rowData[$imageCol]) && ($imageCol != self::COL_MEDIA_IMAGE)) {
1456-
$rowData[$imageCol] = $uploadedGalleryFiles[$rowData[$imageCol]];
1457-
}
1458-
}
1459-
1460-
if (!empty($rowData[self::COL_MEDIA_IMAGE]) && is_array($rowData[self::COL_MEDIA_IMAGE])) {
1461-
$position = 0;
1462-
1463-
foreach ($rowData[self::COL_MEDIA_IMAGE] as $media_image) {
1464-
$mediaGallery[$rowSku][] = [
1465-
'attribute_id' => $this->getMediaGalleryAttributeId(),
1466-
'label' => isset($mediaGalleryLabels[$position]) ? $mediaGalleryLabels[$position] : '',
1467-
'position' => $position++,
1468-
'disabled' => '',
1469-
'value' => $media_image,
1470-
];
1471-
}
1472-
}
1473-
} elseif ($imageInProductIsSet && $imageFromProduct == $fullDispersionPath) {
1474-
$mediaGalleryImages = array();
1475-
$mediaGalleryLabels = array();
1476-
if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
1477-
$mediaGalleryImages =
1478-
explode($this->getMultipleValueSeparator(), $rowData[self::COL_MEDIA_IMAGE]);
1479-
if (isset($rowData['_media_image_label'])) {
1480-
$mediaGalleryLabels =
1481-
explode($this->getMultipleValueSeparator(), $rowData['_media_image_label']);
1482-
} else {
1483-
$mediaGalleryLabels = array();
1484-
}
1485-
if (count($mediaGalleryLabels) > count($mediaGalleryImages)) {
1486-
$mediaGalleryLabels = array_slice($mediaGalleryLabels, 0, count($mediaGalleryImages));
1487-
} elseif (count($mediaGalleryLabels) < count($mediaGalleryImages)) {
1488-
$mediaGalleryLabels = array_pad($mediaGalleryLabels, count($mediaGalleryImages), '');
1489-
}
1490-
}
1491-
1492-
foreach ($this->_imagesArrayKeys as $imageCol) {
1493-
if (!empty($rowData[$imageCol])
1494-
&& ($imageCol != self::COL_MEDIA_IMAGE)
1495-
&& !in_array($rowData[$imageCol], $mediaGalleryImages)) {
1496-
$mediaGalleryImages[] = $rowData[$imageCol];
1497-
if (isset($rowData[$imageCol . '_label'])) {
1498-
$mediaGalleryLabels[] = $rowData[$imageCol . '_label'];
1499-
} else {
1500-
$mediaGalleryLabels[] = '';
1501-
}
1502-
}
1503-
}
1504-
1505-
$rowData[self::COL_MEDIA_IMAGE] = array();
1506-
foreach ($mediaGalleryImages as $mediaImage) {
1453+
} elseif (!isset($existingImages[$imagePath])) {
15071454
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
15081455
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
15091456
trim($mediaImage),
15101457
true
15111458
);
1459+
$newImagePath = $uploadedGalleryFiles[$mediaImage];
1460+
$existingImages[$newImagePath][] = $rowSku;
15121461
}
15131462
$rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
1463+
if (!empty($rowData[self::COL_MEDIA_IMAGE]) && is_array($rowData[self::COL_MEDIA_IMAGE])) {
1464+
$position = array_search($mediaImage, $mediaGalleryImages);
1465+
foreach ($rowData[self::COL_MEDIA_IMAGE] as $mediaImage) {
1466+
$mediaGallery[$rowSku][] = [
1467+
'attribute_id' => $this->getMediaGalleryAttributeId(),
1468+
'label' => isset($mediaGalleryLabels[$position]) ? $mediaGalleryLabels[$position] : '',
1469+
'position' => $position,
1470+
'disabled' => '',
1471+
'value' => $mediaImage,
1472+
];
1473+
}
1474+
}
1475+
} else {
1476+
foreach ($this->_imagesArrayKeys as $imageCol) {
1477+
if (!empty($rowData[$imageCol]) && ($rowData[$imageCol] == $imagePath)) {
1478+
$rowData[$imageCol] = '';
1479+
}
1480+
}
1481+
}
1482+
if (!isset($existingImages[$imagePath]) || in_array($rowSku, $existingImages[$imagePath])) {
1483+
foreach ($this->_imagesArrayKeys as $imageCol) {
1484+
if (!empty($rowData[$imageCol])
1485+
&& ($imageCol != self::COL_MEDIA_IMAGE)
1486+
&& isset($uploadedGalleryFiles[$rowData[$imageCol]])
1487+
) {
1488+
$rowData[$imageCol] = $uploadedGalleryFiles[$rowData[$imageCol]];
1489+
}
1490+
}
15141491
}
1515-
} else {
1516-
$this->addRowError(__("Image already exists for '%s'"), $rowNum, self::COL_MEDIA_IMAGE);
15171492
}
15181493

15191494
// 6. Attributes phase

0 commit comments

Comments
 (0)