@@ -1221,6 +1221,32 @@ protected function _saveProductEntity(array $entityRowsIn, array $entityRowsUp)
1221
1221
return $ this ;
1222
1222
}
1223
1223
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
+
1224
1250
/**
1225
1251
* Prepare all media files
1226
1252
*
@@ -1243,7 +1269,17 @@ protected function _prepareAllMediaFiles($allImagesFromBunch)
1243
1269
['entity_id ' , 'value ' ]
1244
1270
)->where ('value IN (?) ' , $ allImagesFromBunch )
1245
1271
);
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 ;
1247
1283
}
1248
1284
1249
1285
/**
@@ -1274,6 +1310,8 @@ protected function _saveProducts()
1274
1310
$ uploadedGalleryFiles = [];
1275
1311
$ previousType = null ;
1276
1312
$ prevAttributeSet = null ;
1313
+ $ allImagesFromBunch = $ this ->_getAllBunchImages ($ bunch );
1314
+ $ existingImages = $ this ->_prepareAllMediaFiles ($ allImagesFromBunch );
1277
1315
1278
1316
foreach ($ bunch as $ rowNum => $ rowData ) {
1279
1317
if (!$ this ->validateRow ($ rowData , $ rowNum )) {
@@ -1364,6 +1402,95 @@ protected function _saveProducts()
1364
1402
];
1365
1403
}
1366
1404
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
+
1367
1494
// 6. Attributes phase
1368
1495
$ rowStore = (self ::SCOPE_STORE == $ rowScope )
1369
1496
? $ this ->storeResolver ->getStoreCodeToId ($ rowData [self ::COL_STORE ])
@@ -1470,7 +1597,7 @@ protected function _saveProducts()
1470
1597
)->_saveProductGroupPrices (
1471
1598
$ groupPrices
1472
1599
)->_saveMediaGallery (
1473
- $ bunch
1600
+ $ mediaGallery
1474
1601
)->_saveProductAttributes (
1475
1602
$ attributes
1476
1603
);
@@ -1650,146 +1777,16 @@ protected function _uploadMediaFiles($fileName, $renameFileOff = false)
1650
1777
}
1651
1778
}
1652
1779
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
-
1782
1780
/**
1783
1781
* Save product media gallery.
1784
1782
*
1785
- * @param array $bunch
1783
+ * @param array $mediaGalleryData
1786
1784
* @return $this
1787
1785
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1788
1786
* @SuppressWarnings(PHPMD.NPathComplexity)
1789
1787
*/
1790
- protected function _saveMediaGallery ($ bunch )
1788
+ protected function _saveMediaGallery (array $ mediaGalleryData )
1791
1789
{
1792
- $ mediaGalleryData = $ this ->getMediaGalleryData ($ bunch );
1793
1790
if (empty ($ mediaGalleryData )) {
1794
1791
return $ this ;
1795
1792
}
0 commit comments