@@ -164,6 +164,30 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
164
164
*/
165
165
protected $ _attrSetNameToId = [];
166
166
167
+ /**
168
+ * @var string
169
+ */
170
+ protected $ mediaGalleryTableName ;
171
+
172
+ /**
173
+ * @var string
174
+ */
175
+ protected $ mediaGalleryValueTableName ;
176
+ /**
177
+ * @var string
178
+ */
179
+ protected $ mediaGalleryEntityToValueTableName ;
180
+
181
+ /**
182
+ * @var string
183
+ */
184
+ protected $ productEntityTableName ;
185
+
186
+ /**
187
+ * @var string
188
+ */
189
+ protected $ productEntityLinkField ;
190
+
167
191
/**
168
192
* Attributes with index (not label) value.
169
193
*
@@ -1262,80 +1286,61 @@ public function saveProductEntity(array $entityRowsIn, array $entityRowsUp)
1262
1286
}
1263
1287
1264
1288
/**
1265
- * Retrieving images from all columns and rows
1266
- *
1267
- * @param array $bunch
1268
- * @return array
1289
+ * Init media gallery resources
1290
+ * @return void
1269
1291
*/
1270
- protected function getBunchImages ( $ bunch )
1292
+ protected function initMediaGalleryResources ( )
1271
1293
{
1272
- $ images = [];
1273
- foreach ($ bunch as $ row ) {
1274
- $ row = $ this ->_customFieldsMapping ($ row );
1275
- foreach ($ this ->_imagesArrayKeys as $ imageColumn ) {
1276
- if (empty ($ row [$ imageColumn ])) {
1277
- continue ;
1278
- }
1279
-
1280
- $ rowImages = explode ($ this ->getMultipleValueSeparator (), $ row [$ imageColumn ]);
1281
- foreach ($ rowImages as $ rowImage ) {
1282
- $ destinationPath = str_replace ('\\' , '/ ' , $ rowImage );
1283
- $ destinationPath = explode ('/ ' , $ destinationPath );
1284
- $ destinationPath = array_pop ($ destinationPath );
1285
- $ destinationPath = preg_replace ('/[^a-z0-9\._-]+/i ' , '' , $ destinationPath );
1286
-
1287
- $ dispersion = \Magento \Framework \File \Uploader::getDispretionPath ($ destinationPath );
1288
- $ destinationPath = mb_strtolower ($ dispersion . '/ ' . $ destinationPath );
1289
-
1290
- $ images [$ rowImage ] = $ destinationPath ;
1291
- }
1292
- }
1294
+ if (null == $ this ->mediaGalleryTableName ) {
1295
+ $ this ->productEntityTableName = $ this ->getResource ()->getTable ('catalog_product_entity ' );
1296
+ $ this ->mediaGalleryTableName = $ this ->getResource ()->getTable ('catalog_product_entity_media_gallery ' );
1297
+ $ this ->mediaGalleryValueTableName = $ this ->getResource ()->getTable (
1298
+ 'catalog_product_entity_media_gallery_value '
1299
+ );
1300
+ $ this ->mediaGalleryEntityToValueTableName = $ this ->getResource ()->getTable (
1301
+ 'catalog_product_entity_media_gallery_value_to_entity '
1302
+ );
1303
+ $ this ->productEntityLinkField = $ this ->metadataPool
1304
+ ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class)
1305
+ ->getLinkField ();
1293
1306
}
1294
- return $ images ;
1295
1307
}
1296
1308
1297
1309
/**
1298
- * Prepare all media files
1310
+ * Get existing images for current bucnh
1299
1311
*
1300
- * @param array $images
1312
+ * @param array $bunch
1301
1313
* @return array
1302
1314
*/
1303
- protected function getExistingImages ($ images )
1315
+ protected function getExistingImages ($ bunch )
1304
1316
{
1305
- static $ productMediaGalleryTableName = null ;
1306
- static $ resource = null ;
1307
- if (!$ resource ) {
1308
- $ resource = $ this ->_resourceFactory ->create ();
1309
- }
1310
- if (!$ productMediaGalleryTableName ) {
1311
- $ productMediaGalleryTableName = $ resource ->getTable ('catalog_product_entity_media_gallery ' );
1317
+ $ result = [];
1318
+ if ($ this ->getErrorAggregator ()->hasToBeTerminated ()) {
1319
+ return $ result ;
1312
1320
}
1313
- $ linkField = $ this -> metadataPool
1314
- -> getMetadata (\ Magento \ Catalog \ Api \ Data \ProductInterface::class)
1315
- -> getLinkField ( );
1321
+
1322
+ $ this -> initMediaGalleryResources ();
1323
+ $ productSKUs = array_map ( ' strval ' , array_column ( $ bunch , self :: COL_SKU ) );
1316
1324
$ select = $ this ->_connection ->select ()->from (
1317
- ['mg ' => $ resource -> getTable ( ' catalog_product_entity_media_gallery ' ) ],
1325
+ ['mg ' => $ this -> mediaGalleryTableName ],
1318
1326
['value ' => 'mg.value ' ]
1319
- )->joinLeft (
1320
- ['mgvte ' => $ resource -> getTable ( ' catalog_product_entity_media_gallery_value_to_entity ' ) ],
1327
+ )->joinInner (
1328
+ ['mgvte ' => $ this -> mediaGalleryEntityToValueTableName ],
1321
1329
'(mg.value_id = mgvte.value_id) ' ,
1322
- [$ linkField => 'mgvte. ' . $ linkField ]
1330
+ [$ this ->productEntityLinkField => 'mgvte. ' . $ this ->productEntityLinkField ]
1331
+ )->joinInner (
1332
+ ['pe ' => $ this ->productEntityTableName ],
1333
+ "(mgvte. {$ this ->productEntityLinkField } = pe. {$ this ->productEntityLinkField }) " ,
1334
+ ['sku ' => 'pe.sku ' ]
1323
1335
)->where (
1324
- 'mg.value IN(?) ' ,
1325
- $ images
1336
+ 'pe.sku IN (?) ' ,
1337
+ $ productSKUs
1326
1338
);
1327
- $ allMedia = $ this ->_connection ->fetchAll ($ select );
1328
- $ result = [];
1329
- foreach ($ allMedia as $ image ) {
1330
- if (!isset ($ result [$ image ['value ' ]])){
1331
- $ result [$ image ['value ' ]] = [];
1332
- }
1333
- foreach ($ this ->_oldSku as $ sku => $ oldSkuData ) {
1334
- if ($ oldSkuData ['entity_id ' ] == $ image ['entity_id ' ]) {
1335
- $ result [$ image ['value ' ]][$ image ['entity_id ' ]] = $ sku ;
1336
- }
1337
- }
1339
+
1340
+ foreach ($ this ->_connection ->fetchAll ($ select ) as $ image ) {
1341
+ $ result [$ image ['sku ' ]][$ image ['value ' ]] = true ;
1338
1342
}
1343
+
1339
1344
return $ result ;
1340
1345
}
1341
1346
@@ -1396,8 +1401,7 @@ protected function _saveProducts()
1396
1401
$ uploadedImages = [];
1397
1402
$ previousType = null ;
1398
1403
$ prevAttributeSet = null ;
1399
- $ bunchImages = $ this ->getBunchImages ($ bunch );
1400
- $ existingImages = $ this ->getExistingImages ($ bunchImages );
1404
+ $ existingImages = $ this ->getExistingImages ($ bunch );
1401
1405
1402
1406
foreach ($ bunch as $ rowNum => $ rowData ) {
1403
1407
if (!$ this ->validateRow ($ rowData , $ rowNum )) {
@@ -1517,8 +1521,7 @@ protected function _saveProducts()
1517
1521
$ rowData [$ column ] = $ uploadedFile ;
1518
1522
}
1519
1523
1520
- $ imageNotAssigned = !isset ($ existingImages [$ uploadedFile ])
1521
- || !in_array ($ rowSku , $ existingImages [$ uploadedFile ]);
1524
+ $ imageNotAssigned = !isset ($ existingImages [$ rowSku ][$ uploadedFile ]);
1522
1525
1523
1526
if ($ uploadedFile && $ imageNotAssigned ) {
1524
1527
if ($ column == self ::COL_MEDIA_IMAGE ) {
@@ -1531,7 +1534,7 @@ protected function _saveProducts()
1531
1534
'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1532
1535
'value ' => $ uploadedFile ,
1533
1536
];
1534
- $ existingImages [$ uploadedFile ][ ] = $ rowSku ;
1537
+ $ existingImages [$ rowSku ][ $ uploadedFile ] = true ;
1535
1538
}
1536
1539
}
1537
1540
}
@@ -1812,18 +1815,7 @@ protected function _saveMediaGallery(array $mediaGalleryData)
1812
1815
if (empty ($ mediaGalleryData )) {
1813
1816
return $ this ;
1814
1817
}
1815
- static $ mediaGalleryTableName = null ;
1816
- static $ mediaValueTableName = null ;
1817
- static $ mediaEntityToValueTableName = null ;
1818
- $ mediaGalleryTableName = $ mediaGalleryTableName ?: $ this ->_resourceFactory ->create ()->getTable (
1819
- 'catalog_product_entity_media_gallery '
1820
- );
1821
- $ mediaValueTableName = $ mediaValueTableName ?: $ this ->_resourceFactory ->create ()->getTable (
1822
- 'catalog_product_entity_media_gallery_value '
1823
- );
1824
- $ mediaEntityToValueTableName = $ mediaEntityToValueTableName ?: $ this ->_resourceFactory ->create ()->getTable (
1825
- 'catalog_product_entity_media_gallery_value_to_entity '
1826
- );
1818
+ $ this ->initMediaGalleryResources ();
1827
1819
$ productIds = [];
1828
1820
$ imageNames = [];
1829
1821
$ multiInsertData = [];
@@ -1832,12 +1824,6 @@ protected function _saveMediaGallery(array $mediaGalleryData)
1832
1824
$ productId = $ this ->skuProcessor ->getNewSku ($ productSku )['entity_id ' ];
1833
1825
$ productIds [] = $ productId ;
1834
1826
$ insertedGalleryImgs = [];
1835
- if (Import::BEHAVIOR_APPEND != $ this ->getBehavior ()) {
1836
- $ this ->_connection ->delete (
1837
- $ mediaGalleryTableName ,
1838
- $ this ->_connection ->quoteInto ('entity_id IN (?) ' , $ productId )
1839
- );
1840
- }
1841
1827
foreach ($ mediaGalleryRows as $ insertValue ) {
1842
1828
if (!in_array ($ insertValue ['value ' ], $ insertedGalleryImgs )) {
1843
1829
$ valueArr = [
@@ -1852,17 +1838,18 @@ protected function _saveMediaGallery(array $mediaGalleryData)
1852
1838
}
1853
1839
}
1854
1840
$ oldMediaValues = $ this ->_connection ->fetchAssoc (
1855
- $ this ->_connection ->select ()->from ($ mediaGalleryTableName , ['value_id ' , 'value ' ])
1841
+ $ this ->_connection ->select ()->from ($ this -> mediaGalleryTableName , ['value_id ' , 'value ' ])
1856
1842
->where ('value IN (?) ' , $ imageNames )
1857
1843
);
1858
- $ this ->_connection ->insertOnDuplicate ($ mediaGalleryTableName , $ multiInsertData , []);
1844
+ $ this ->_connection ->insertOnDuplicate ($ this -> mediaGalleryTableName , $ multiInsertData , []);
1859
1845
$ multiInsertData = [];
1860
- $ newMediaSelect = $ this ->_connection ->select ()->from ($ mediaGalleryTableName , ['value_id ' , 'value ' ])
1846
+ $ newMediaSelect = $ this ->_connection ->select ()->from ($ this -> mediaGalleryTableName , ['value_id ' , 'value ' ])
1861
1847
->where ('value IN (?) ' , $ imageNames );
1862
1848
if (array_keys ($ oldMediaValues )) {
1863
1849
$ newMediaSelect ->where ('value_id NOT IN (?) ' , array_keys ($ oldMediaValues ));
1864
1850
}
1865
1851
1852
+ $ dataForSkinnyTable = [];
1866
1853
$ newMediaValues = $ this ->_connection ->fetchAssoc ($ newMediaSelect );
1867
1854
foreach ($ mediaGalleryData as $ productSku => $ mediaGalleryRows ) {
1868
1855
foreach ($ mediaGalleryRows as $ insertValue ) {
@@ -1893,14 +1880,18 @@ protected function _saveMediaGallery(array $mediaGalleryData)
1893
1880
}
1894
1881
try {
1895
1882
$ this ->_connection ->insertOnDuplicate (
1896
- $ mediaValueTableName ,
1883
+ $ this -> mediaGalleryValueTableName ,
1897
1884
$ multiInsertData ,
1898
1885
['value_id ' , 'store_id ' , 'entity_id ' , 'label ' , 'position ' , 'disabled ' ]
1899
1886
);
1900
- $ this ->_connection ->insertOnDuplicate ($ mediaEntityToValueTableName , $ dataForSkinnyTable , ['value_id ' ]);
1887
+ $ this ->_connection ->insertOnDuplicate (
1888
+ $ this ->mediaGalleryEntityToValueTableName ,
1889
+ $ dataForSkinnyTable ,
1890
+ ['value_id ' ]
1891
+ );
1901
1892
} catch (\Exception $ e ) {
1902
1893
$ this ->_connection ->delete (
1903
- $ mediaGalleryTableName ,
1894
+ $ this -> mediaGalleryTableName ,
1904
1895
$ this ->_connection ->quoteInto ('value_id IN (?) ' , $ newMediaValues )
1905
1896
);
1906
1897
}
0 commit comments