Skip to content

Commit b5a8d42

Browse files
Dmitry KologrivovA-Komar
authored andcommitted
MAGNIMEX-145: Fix bug of duplicate images
1 parent e43524b commit b5a8d42

File tree

1 file changed

+153
-165
lines changed
  • app/code/Magento/CatalogImportExport/Model/Import

1 file changed

+153
-165
lines changed

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

Lines changed: 153 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
362362
*/
363363
protected $_imagesArrayKeys = ['_media_image', 'image', 'small_image', 'thumbnail'];
364364

365+
/**
366+
* Column names that holds images files names from bunch
367+
*
368+
* @var string[]
369+
*/
370+
protected $_imagesArrayKeysFromBunch = ['base_image', 'small_image', 'thumbnail_image', 'additional_images'];
371+
365372
/**
366373
* Permanent entity columns.
367374
*
@@ -1224,36 +1231,26 @@ protected function _saveProductEntity(array $entityRowsIn, array $entityRowsUp)
12241231
/**
12251232
* Prepare all media files
12261233
*
1227-
* @return $this
1234+
* @param $allImegesFromBunch
1235+
* @return array
12281236
*/
1229-
protected function _prepareAllMediaFiles()
1237+
protected function _prepareAllMediaFiles($allImegesFromBunch)
12301238
{
1231-
static $productEntityTableName = null;
12321239
static $productMediaGalleryTableName = null;
12331240
static $resource = null;
12341241
if (!$resource) {
12351242
$resource = $this->_resourceFactory->create();
12361243
}
1237-
if (!$productEntityTableName) {
1238-
$productEntityTableName = $resource->getTable('catalog_product_entity');
1239-
}
12401244
if (!$productMediaGalleryTableName) {
12411245
$productMediaGalleryTableName = $resource->getTable('catalog_product_entity_media_gallery');
12421246
}
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;
1255-
}
1256-
return $this;
1247+
$allMedia = $this->_connection->fetchAll($this->_connection->select()
1248+
->from(
1249+
$productMediaGalleryTableName,
1250+
['entity_id', 'value']
1251+
)->where('value IN (?)', $allImegesFromBunch)
1252+
);
1253+
return $allMedia;
12571254
}
12581255

12591256
/**
@@ -1269,7 +1266,7 @@ protected function _saveProducts()
12691266
/** @var $resource \Magento\CatalogImportExport\Model\Import\Proxy\Product\Resource */
12701267
$resource = $this->_resourceFactory->create();
12711268
$priceIsGlobal = $this->_catalogData->isPriceGlobal();
1272-
$this->_prepareAllMediaFiles();
1269+
//$this->_prepareAllMediaFiles();
12731270
$productLimit = null;
12741271
$productsQty = null;
12751272

@@ -1375,147 +1372,6 @@ protected function _saveProducts()
13751372
];
13761373
}
13771374

1378-
// 5. Media gallery phase
1379-
1380-
$fullDispersionPath = '';
1381-
$imageIsSet = null;
1382-
$imageFromProduct = null;
1383-
$imageInProductIsSet = null;
1384-
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-
}
1406-
}
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-
}
1427-
}
1428-
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'])
1436-
? $rowData[$imageCol . '_label']
1437-
: '';
1438-
} else {
1439-
$mediaGalleryLabels[] = '';
1440-
}
1441-
}
1442-
}
1443-
1444-
$rowData[self::COL_MEDIA_IMAGE] = array();
1445-
foreach ($mediaGalleryImages as $mediaImage) {
1446-
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1447-
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1448-
trim($mediaImage)
1449-
);
1450-
}
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) {
1507-
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1508-
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1509-
trim($mediaImage),
1510-
true
1511-
);
1512-
}
1513-
$rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
1514-
}
1515-
} else {
1516-
$this->addRowError(__("Image already exists for '%s'"), $rowNum, self::COL_MEDIA_IMAGE);
1517-
}
1518-
15191375
// 6. Attributes phase
15201376
$rowStore = (self::SCOPE_STORE == $rowScope)
15211377
? $this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])
@@ -1622,7 +1478,7 @@ protected function _saveProducts()
16221478
)->_saveProductGroupPrices(
16231479
$groupPrices
16241480
)->_saveMediaGallery(
1625-
$mediaGallery
1481+
$bunch
16261482
)->_saveProductAttributes(
16271483
$attributes
16281484
);
@@ -1802,16 +1658,148 @@ protected function _uploadMediaFiles($fileName, $renameFileOff = false)
18021658
}
18031659
}
18041660

1661+
/**
1662+
* Upload images
1663+
*
1664+
* @param $bunch
1665+
* @return mixed
1666+
*/
1667+
protected function getMediaGalleryData($bunch)
1668+
{
1669+
$allImegesFromBunch = [];
1670+
foreach ($bunch as $rowData) {
1671+
foreach ($this->_imagesArrayKeysFromBunch as $image) {
1672+
$dispersionPath =
1673+
\Magento\Framework\File\Uploader::getDispretionPath($rowData[$image]);
1674+
$importImages = explode(",", $rowData[$image]);
1675+
foreach ($importImages as $importImage) {
1676+
$imageSting = mb_strtolower(
1677+
$dispersionPath . '/' . preg_replace('/[^a-z0-9\._-]+/i', '', $importImage)
1678+
);
1679+
$allImegesFromBunch[$importImage] = $imageSting;
1680+
}
1681+
}
1682+
}
1683+
$existedImages = $this->_prepareAllMediaFiles($allImegesFromBunch);
1684+
$mediaGallery = [];
1685+
foreach ($bunch as $rowNum => $rowData) {
1686+
$uploadedGalleryFiles = [];
1687+
if (!$this->validateRow($rowData, $rowNum)) {
1688+
continue;
1689+
}
1690+
$rowSku = $rowData[self::COL_SKU];
1691+
$mediaGalleryImages = array();
1692+
$mediaGalleryLabels = array();
1693+
if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
1694+
$mediaGalleryImages =
1695+
explode($this->getMultipleValueSeparator(), $rowData[self::COL_MEDIA_IMAGE]);
1696+
if (isset($rowData['_media_image_label'])) {
1697+
$mediaGalleryLabels =
1698+
explode($this->getMultipleValueSeparator(), $rowData['_media_image_label']);
1699+
} else {
1700+
$mediaGalleryLabels = [];
1701+
}
1702+
if (count($mediaGalleryLabels) > count($mediaGalleryImages)) {
1703+
$mediaGalleryLabels = array_slice($mediaGalleryLabels, 0, count($mediaGalleryImages));
1704+
} elseif (count($mediaGalleryLabels) < count($mediaGalleryImages)) {
1705+
$mediaGalleryLabels = array_pad($mediaGalleryLabels, count($mediaGalleryImages), '');
1706+
}
1707+
}
1708+
1709+
foreach ($this->_imagesArrayKeys as $imageCol) {
1710+
if (!empty($rowData[$imageCol])
1711+
&& ($imageCol != self::COL_MEDIA_IMAGE)
1712+
&& !in_array($rowData[$imageCol], $mediaGalleryImages)) {
1713+
$mediaGalleryImages[] = $rowData[$imageCol];
1714+
if (isset($mediaGalleryLabels)) {
1715+
$mediaGalleryLabels[] = isset($rowData[$imageCol . '_label'])
1716+
? $rowData[$imageCol . '_label']
1717+
: '';
1718+
} else {
1719+
$mediaGalleryLabels[] = '';
1720+
}
1721+
}
1722+
}
1723+
$newSku = $this->getNewSku();
1724+
$entityIdNewProduct =
1725+
isset($newSku[$rowData[self::COL_SKU]]['entity_id'])
1726+
? $newSku[$rowData[self::COL_SKU]]['entity_id']
1727+
: '';
1728+
$rowData[self::COL_MEDIA_IMAGE] = array();
1729+
foreach ($mediaGalleryImages as $mediaImage) {
1730+
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1731+
$imageIsSet = 0;
1732+
$idIsSet = 0;
1733+
foreach ($existedImages as $currentImage) {
1734+
if ($currentImage['value'] == $allImegesFromBunch[$mediaImage]) {
1735+
$imageIsSet = 1;
1736+
if ($currentImage['entity_id'] == $entityIdNewProduct) {
1737+
$idIsSet = 1;
1738+
}
1739+
break;
1740+
}
1741+
}
1742+
if ($imageIsSet && $idIsSet) {
1743+
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1744+
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1745+
trim($mediaImage),
1746+
true
1747+
);
1748+
}
1749+
$rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
1750+
} elseif (!$imageIsSet && !$idIsSet) {
1751+
$newAddedImages = [];
1752+
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1753+
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1754+
trim($mediaImage),
1755+
true
1756+
);
1757+
$newAddedImages['entity_id'] = $entityIdNewProduct;
1758+
$newAddedImages['value'] = $uploadedGalleryFiles[$mediaImage];
1759+
$existedImages[] = $newAddedImages;
1760+
}
1761+
$rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
1762+
1763+
foreach ($this->_imagesArrayKeys as $imageCol) {
1764+
if (!empty($rowData[$imageCol]) && ($imageCol != self::COL_MEDIA_IMAGE)) {
1765+
if(isset($uploadedGalleryFiles[$rowData[$imageCol]])) {
1766+
$rowData[$imageCol] = $uploadedGalleryFiles[$rowData[$imageCol]];
1767+
}
1768+
}
1769+
}
1770+
if (!empty($rowData[self::COL_MEDIA_IMAGE]) && is_array($rowData[self::COL_MEDIA_IMAGE])) {
1771+
$position = 0;
1772+
1773+
foreach ($rowData[self::COL_MEDIA_IMAGE] as $media_image) {
1774+
$mediaGallery[$rowSku][] = [
1775+
'attribute_id' => $this->getMediaGalleryAttributeId(),
1776+
'label' => isset($mediaGalleryLabels[$position]) ? $mediaGalleryLabels[$position] : '',
1777+
'position' => $position++,
1778+
'disabled' => '',
1779+
'value' => $media_image,
1780+
];
1781+
}
1782+
}
1783+
}
1784+
}
1785+
}
1786+
}
1787+
if (isset($mediaGallery)) {
1788+
return $mediaGallery;
1789+
}
1790+
}
1791+
18051792
/**
18061793
* Save product media gallery.
18071794
*
1808-
* @param array $mediaGalleryData
1795+
* @param array $bunch
18091796
* @return $this
18101797
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
18111798
* @SuppressWarnings(PHPMD.NPathComplexity)
18121799
*/
1813-
protected function _saveMediaGallery(array $mediaGalleryData)
1800+
protected function _saveMediaGallery($bunch)
18141801
{
1802+
$mediaGalleryData = $this->getMediaGalleryData($bunch);
18151803
if (empty($mediaGalleryData)) {
18161804
return $this;
18171805
}

0 commit comments

Comments
 (0)