Skip to content

Commit fad7292

Browse files
committed
MAGNIMEX-145: minor changes
1 parent ddebd16 commit fad7292

File tree

2 files changed

+133
-134
lines changed

2 files changed

+133
-134
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: 131 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,32 @@ 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
*
@@ -1243,7 +1269,17 @@ protected function _prepareAllMediaFiles($allImagesFromBunch)
12431269
['entity_id', 'value']
12441270
)->where('value IN (?)', $allImagesFromBunch)
12451271
);
1246-
return $allMedia;
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+
}
1281+
}
1282+
return $result;
12471283
}
12481284

12491285
/**
@@ -1274,6 +1310,8 @@ protected function _saveProducts()
12741310
$uploadedGalleryFiles = [];
12751311
$previousType = null;
12761312
$prevAttributeSet = null;
1313+
$allImagesFromBunch = $this->_getAllBunchImages($bunch);
1314+
$existingImages = $this->_prepareAllMediaFiles($allImagesFromBunch);
12771315

12781316
foreach ($bunch as $rowNum => $rowData) {
12791317
if (!$this->validateRow($rowData, $rowNum)) {
@@ -1364,6 +1402,95 @@ protected function _saveProducts()
13641402
];
13651403
}
13661404

1405+
if (!$this->validateRow($rowData, $rowNum)) {
1406+
continue;
1407+
}
1408+
1409+
// 5. Media gallery phase
1410+
$mediaGalleryImages = array();
1411+
$mediaGalleryLabels = array();
1412+
if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
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 = [];
1420+
}
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), '');
1425+
}
1426+
}
1427+
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'])
1436+
? $rowData[$imageCol . '_label']
1437+
: '';
1438+
} else {
1439+
$mediaGalleryLabels[] = '';
1440+
}
1441+
}
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])) {
1447+
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1448+
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1449+
trim($mediaImage),
1450+
true
1451+
);
1452+
}
1453+
} elseif (!isset($existingImages[$imagePath])) {
1454+
if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
1455+
$uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(
1456+
trim($mediaImage),
1457+
true
1458+
);
1459+
$newImagePath = $uploadedGalleryFiles[$mediaImage];
1460+
$existingImages[$newImagePath][] = $rowSku;
1461+
}
1462+
$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+
}
1491+
}
1492+
}
1493+
13671494
// 6. Attributes phase
13681495
$rowStore = (self::SCOPE_STORE == $rowScope)
13691496
? $this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE])
@@ -1470,7 +1597,7 @@ protected function _saveProducts()
14701597
)->_saveProductGroupPrices(
14711598
$groupPrices
14721599
)->_saveMediaGallery(
1473-
$bunch
1600+
$mediaGallery
14741601
)->_saveProductAttributes(
14751602
$attributes
14761603
);
@@ -1650,146 +1777,16 @@ protected function _uploadMediaFiles($fileName, $renameFileOff = false)
16501777
}
16511778
}
16521779

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

0 commit comments

Comments
 (0)