@@ -745,7 +745,10 @@ public function setParameters(array $params)
745
745
*/
746
746
public function deleteProductsForReplacement ()
747
747
{
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
+ ));
749
752
$ this ->_deleteProducts ();
750
753
751
754
return $ this ;
@@ -823,7 +826,10 @@ protected function _replaceProducts()
823
826
$ this ->deleteProductsForReplacement ();
824
827
$ this ->_oldSku = $ this ->skuProcessor ->reloadOldSkus ()->getOldSkus ();
825
828
$ 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
+ ));
827
833
$ this ->_saveProductsData ();
828
834
829
835
return $ this ;
@@ -1005,7 +1011,8 @@ protected function _saveLinks()
1005
1011
}
1006
1012
1007
1013
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"
1009
1016
// link causing fatal errors.
1010
1017
$ this ->_logger ->critical (
1011
1018
new \Exception (
@@ -1194,42 +1201,40 @@ protected function _saveProductEntity(array $entityRowsIn, array $entityRowsUp)
1194
1201
* @param $bunch
1195
1202
* @return array
1196
1203
*/
1197
- protected function _getAllBunchImages ($ bunch )
1204
+ protected function getBunchImages ($ bunch )
1198
1205
{
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 ])) {
1204
1211
continue ;
1205
1212
}
1206
1213
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 );
1212
1220
1213
- $ dispersionPath =
1214
- \ Magento \ Framework \ File \Uploader:: getDispretionPath ( $ importImageFileName );
1221
+ $ dispersion = \ Magento \ Framework \ File \Uploader:: getDispretionPath ( $ destinationPath );
1222
+ $ destinationPath = mb_strtolower ( $ dispersion . ' / ' . $ destinationPath );
1215
1223
1216
- $ imageSting = mb_strtolower (
1217
- $ dispersionPath . '/ ' . preg_replace ('/[^a-z0-9\._-]+/i ' , '' , $ importImageFileName )
1218
- );
1219
- $ allImagesFromBunch [$ importImage ] = $ imageSting ;
1224
+ $ images [$ rowImage ] = $ destinationPath ;
1220
1225
}
1221
1226
}
1222
1227
}
1223
- return $ allImagesFromBunch ;
1228
+ return $ images ;
1224
1229
}
1225
1230
1226
1231
/**
1227
1232
* Prepare all media files
1228
1233
*
1229
- * @param $allImagesFromBunch
1234
+ * @param $images
1230
1235
* @return array
1231
1236
*/
1232
- protected function _prepareAllMediaFiles ( $ allImagesFromBunch )
1237
+ protected function getExistingImages ( $ images )
1233
1238
{
1234
1239
static $ productMediaGalleryTableName = null ;
1235
1240
static $ resource = null ;
@@ -1243,28 +1248,61 @@ protected function _prepareAllMediaFiles($allImagesFromBunch)
1243
1248
->from (
1244
1249
$ productMediaGalleryTableName ,
1245
1250
['entity_id ' , 'value ' ]
1246
- )->where ('value IN (?) ' , $ allImagesFromBunch )
1251
+ )->where ('value IN (?) ' , $ images )
1247
1252
);
1248
1253
$ result = array ();
1249
1254
foreach ($ allMedia as $ image ) {
1255
+ if (!isset ($ result [$ image ['value ' ]])){
1250
1256
$ result [$ image ['value ' ]] = [];
1257
+ }
1251
1258
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 ;
1254
1261
}
1255
- $ result [$ image ['value ' ]][] = $ sku ;
1256
1262
}
1257
1263
}
1258
1264
return $ result ;
1259
1265
}
1260
1266
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
+
1261
1298
/**
1262
1299
* Gather and save information about product entities.
1263
1300
*
1264
1301
* @return $this
1265
1302
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
1266
1303
* @SuppressWarnings(PHPMD.NPathComplexity)
1267
1304
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
1305
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
1268
1306
*/
1269
1307
protected function _saveProducts ()
1270
1308
{
@@ -1283,11 +1321,11 @@ protected function _saveProducts()
1283
1321
$ tierPrices = [];
1284
1322
$ groupPrices = [];
1285
1323
$ mediaGallery = [];
1286
- $ uploadedGalleryFiles = [];
1324
+ $ uploadedImages = [];
1287
1325
$ previousType = null ;
1288
1326
$ prevAttributeSet = null ;
1289
- $ allImagesFromBunch = $ this ->_getAllBunchImages ($ bunch );
1290
- $ existingImages = $ this ->_prepareAllMediaFiles ( $ allImagesFromBunch );
1327
+ $ bunchImages = $ this ->getBunchImages ($ bunch );
1328
+ $ existingImages = $ this ->getExistingImages ( $ bunchImages );
1291
1329
1292
1330
foreach ($ bunch as $ rowNum => $ rowData ) {
1293
1331
if (!$ this ->validateRow ($ rowData , $ rowNum )) {
@@ -1383,96 +1421,44 @@ protected function _saveProducts()
1383
1421
}
1384
1422
1385
1423
// 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
+ );
1423
1430
}
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 ;
1435
1438
}
1439
+ } else {
1440
+ $ uploadedFile = $ uploadedImages [$ columnImage ];
1436
1441
}
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
- }
1447
1442
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 ;
1462
1445
}
1463
- }
1464
1446
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 ;
1476
1462
}
1477
1463
}
1478
1464
}
@@ -1744,7 +1730,7 @@ protected function _getUploader()
1744
1730
* @param string $fileName
1745
1731
* @return string
1746
1732
*/
1747
- protected function _uploadMediaFiles ($ fileName , $ renameFileOff = false )
1733
+ protected function uploadMediaFiles ($ fileName , $ renameFileOff = false )
1748
1734
{
1749
1735
try {
1750
1736
$ res = $ this ->_getUploader ()->move ($ fileName , $ renameFileOff );
@@ -1767,29 +1753,27 @@ protected function _saveMediaGallery(array $mediaGalleryData)
1767
1753
if (empty ($ mediaGalleryData )) {
1768
1754
return $ this ;
1769
1755
}
1770
-
1771
1756
static $ mediaGalleryTableName = null ;
1772
1757
static $ mediaValueTableName = null ;
1773
1758
static $ productId = null ;
1774
-
1775
1759
if (!$ mediaGalleryTableName ) {
1776
1760
$ mediaGalleryTableName = $ this ->_resourceFactory ->create ()->getTable (
1777
1761
'catalog_product_entity_media_gallery '
1778
1762
);
1779
1763
}
1780
-
1781
1764
if (!$ mediaValueTableName ) {
1782
1765
$ mediaValueTableName = $ this ->_resourceFactory ->create ()->getTable (
1783
1766
'catalog_product_entity_media_gallery_value '
1784
1767
);
1785
1768
}
1786
1769
$ productIds = [];
1770
+ $ imageNames = [];
1787
1771
$ multiInsertData = [];
1772
+ $ valueToProductId = [];
1788
1773
foreach ($ mediaGalleryData as $ productSku => $ mediaGalleryRows ) {
1789
1774
$ productId = $ this ->skuProcessor ->getNewSku ($ productSku )['entity_id ' ];
1790
1775
$ productIds [] = $ productId ;
1791
1776
$ insertedGalleryImgs = [];
1792
-
1793
1777
if (\Magento \ImportExport \Model \Import::BEHAVIOR_APPEND != $ this ->getBehavior ()) {
1794
1778
$ this ->_connection ->delete (
1795
1779
$ mediaGalleryTableName ,
@@ -1803,28 +1787,33 @@ protected function _saveMediaGallery(array $mediaGalleryData)
1803
1787
'entity_id ' => $ productId ,
1804
1788
'value ' => $ insertValue ['value ' ],
1805
1789
];
1790
+ $ valueToProductId [$ insertValue ['value ' ]][] = $ productId ;
1791
+ $ imageNames [] = $ insertValue ['value ' ];
1806
1792
$ multiInsertData [] = $ valueArr ;
1807
1793
$ insertedGalleryImgs [] = $ insertValue ['value ' ];
1808
1794
}
1809
1795
}
1810
1796
}
1797
+ $ oldMediaValues = $ this ->_connection ->fetchAssoc (
1798
+ $ this ->_connection ->select ()->from ($ mediaGalleryTableName , ['value_id ' , 'value ' , 'entity_id ' ])
1799
+ ->where ('value IN (?) ' , $ imageNames )
1800
+ );
1811
1801
$ this ->_connection ->insertOnDuplicate ($ mediaGalleryTableName , $ multiInsertData , ['entity_id ' ]);
1812
1802
$ 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 );
1822
1811
foreach ($ mediaGalleryData as $ productSku => $ mediaGalleryRows ) {
1823
1812
foreach ($ mediaGalleryRows as $ insertValue ) {
1824
1813
foreach ($ newMediaValues as $ value_id => $ values ) {
1825
1814
if ($ values ['value ' ] == $ insertValue ['value ' ]) {
1826
1815
$ insertValue ['value_id ' ] = $ value_id ;
1827
- $ insertValue ['entity_id ' ] = $ values ['entity_id ' ] ;
1816
+ $ insertValue ['entity_id ' ] = array_shift ( $ valueToProductId [ $ values ['value ' ]]) ;
1828
1817
unset($ newMediaValues [$ value_id ]);
1829
1818
break ;
1830
1819
}
0 commit comments