@@ -1418,7 +1418,7 @@ protected function initMediaGalleryResources()
1418
1418
}
1419
1419
1420
1420
/**
1421
- * Get existing images for current bucnh
1421
+ * Get existing images for current bunch.
1422
1422
*
1423
1423
* @param array $bunch
1424
1424
* @return array
@@ -1438,7 +1438,21 @@ protected function getExistingImages($bunch)
1438
1438
)->joinInner (
1439
1439
['mgvte ' => $ this ->mediaGalleryEntityToValueTableName ],
1440
1440
'(mg.value_id = mgvte.value_id) ' ,
1441
- [$ this ->getProductEntityLinkField () => 'mgvte. ' . $ this ->getProductEntityLinkField ()]
1441
+ [
1442
+ $ this ->getProductEntityLinkField () => 'mgvte. ' . $ this ->getProductEntityLinkField (),
1443
+ 'value_id ' => 'mgvte.value_id ' ,
1444
+ ]
1445
+ )->joinLeft (
1446
+ ['mgv ' => $ this ->mediaGalleryValueTableName ],
1447
+ sprintf (
1448
+ '(mg.value_id = mgv.value_id AND mgv.%s = mgvte.%s AND mgv.store_id = %d) ' ,
1449
+ $ this ->getProductEntityLinkField (),
1450
+ $ this ->getProductEntityLinkField (),
1451
+ \Magento \Store \Model \Store::DEFAULT_STORE_ID
1452
+ ),
1453
+ [
1454
+ 'label ' => 'mgv.label ' ,
1455
+ ]
1442
1456
)->joinInner (
1443
1457
['pe ' => $ this ->productEntityTableName ],
1444
1458
"(mgvte. {$ this ->getProductEntityLinkField ()} = pe. {$ this ->getProductEntityLinkField ()}) " ,
@@ -1449,7 +1463,7 @@ protected function getExistingImages($bunch)
1449
1463
);
1450
1464
1451
1465
foreach ($ this ->_connection ->fetchAll ($ select ) as $ image ) {
1452
- $ result [$ image ['sku ' ]][$ image ['value ' ]] = true ;
1466
+ $ result [$ image ['sku ' ]][$ image ['value ' ]] = $ image ;
1453
1467
}
1454
1468
1455
1469
return $ result ;
@@ -1464,22 +1478,21 @@ public function getImagesFromRow(array $rowData)
1464
1478
$ images = [];
1465
1479
$ labels = [];
1466
1480
foreach ($ this ->_imagesArrayKeys as $ column ) {
1467
- $ images [$ column ] = [];
1468
- $ labels [$ column ] = [];
1469
1481
if (!empty ($ rowData [$ column ])) {
1470
1482
$ images [$ column ] = array_unique (
1471
- explode ($ this ->getMultipleValueSeparator (), $ rowData [$ column ])
1483
+ array_map (
1484
+ 'trim ' ,
1485
+ explode ($ this ->getMultipleValueSeparator (), $ rowData [$ column ])
1486
+ )
1472
1487
);
1473
- }
1474
1488
1475
- if (!empty ($ rowData [$ column . '_label ' ])) {
1476
- $ labels [$ column ] = explode ($ this ->getMultipleValueSeparator (), $ rowData [$ column . '_label ' ]);
1477
- }
1489
+ if (!empty ($ rowData [$ column . '_label ' ])) {
1490
+ $ labels [$ column ] = $ this ->parseMultipleValues ($ rowData [$ column . '_label ' ]);
1478
1491
1479
- if (count ($ labels [$ column ]) > count ($ images [$ column ])) {
1480
- $ labels [$ column ] = array_slice ($ labels [$ column ], 0 , count ($ images [$ column ]));
1481
- } elseif ( count ( $ labels [ $ column ]) < count ( $ images [ $ column ])) {
1482
- $ labels [ $ column ] = array_pad ( $ labels [ $ column ], count ( $ images [ $ column ]), '' );
1492
+ if (count ($ labels [$ column ]) > count ($ images [$ column ])) {
1493
+ $ labels [$ column ] = array_slice ($ labels [$ column ], 0 , count ($ images [$ column ]));
1494
+ }
1495
+ }
1483
1496
}
1484
1497
}
1485
1498
@@ -1509,6 +1522,7 @@ protected function _saveProducts()
1509
1522
$ this ->categoriesCache = [];
1510
1523
$ tierPrices = [];
1511
1524
$ mediaGallery = [];
1525
+ $ labelsForUpdate = [];
1512
1526
$ uploadedImages = [];
1513
1527
$ previousType = null ;
1514
1528
$ prevAttributeSet = null ;
@@ -1624,7 +1638,7 @@ protected function _saveProducts()
1624
1638
foreach ($ rowImages as $ column => $ columnImages ) {
1625
1639
foreach ($ columnImages as $ columnImageKey => $ columnImage ) {
1626
1640
if (!isset ($ uploadedImages [$ columnImage ])) {
1627
- $ uploadedFile = $ this ->uploadMediaFiles (trim ( $ columnImage) , true );
1641
+ $ uploadedFile = $ this ->uploadMediaFiles ($ columnImage , true );
1628
1642
if ($ uploadedFile ) {
1629
1643
$ uploadedImages [$ columnImage ] = $ uploadedFile ;
1630
1644
} else {
@@ -1644,22 +1658,30 @@ protected function _saveProducts()
1644
1658
$ rowData [$ column ] = $ uploadedFile ;
1645
1659
}
1646
1660
1647
- $ imageNotAssigned = !isset ($ existingImages [$ rowSku ][$ uploadedFile ]);
1648
-
1649
- if ($ uploadedFile && $ imageNotAssigned ) {
1650
- if ($ column == self ::COL_MEDIA_IMAGE ) {
1651
- $ rowData [$ column ][] = $ uploadedFile ;
1661
+ if ($ uploadedFile && !isset ($ mediaGallery [$ rowSku ][$ uploadedFile ])) {
1662
+ if (isset ($ existingImages [$ rowSku ][$ uploadedFile ])) {
1663
+ if (isset ($ rowLabels [$ column ][$ columnImageKey ])
1664
+ && $ rowLabels [$ column ][$ columnImageKey ]
1665
+ != $ existingImages [$ rowSku ][$ uploadedFile ]['label ' ]
1666
+ ) {
1667
+ $ labelsForUpdate [] = [
1668
+ 'label ' => $ rowLabels [$ column ][$ columnImageKey ],
1669
+ 'imageData ' => $ existingImages [$ rowSku ][$ uploadedFile ]
1670
+ ];
1671
+ }
1672
+ } else {
1673
+ if ($ column == self ::COL_MEDIA_IMAGE ) {
1674
+ $ rowData [$ column ][] = $ uploadedFile ;
1675
+ }
1676
+ $ mediaGallery [$ rowSku ][$ uploadedFile ] = [
1677
+ 'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1678
+ 'label ' => isset ($ rowLabels [$ column ][$ columnImageKey ])
1679
+ ? $ rowLabels [$ column ][$ columnImageKey ] : '' ,
1680
+ 'position ' => ++$ position ,
1681
+ 'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1682
+ 'value ' => $ uploadedFile ,
1683
+ ];
1652
1684
}
1653
- $ label = isset ($ rowLabels [$ column ][$ columnImageKey ]) ?
1654
- $ rowLabels [$ column ][$ columnImageKey ] : '' ;
1655
- $ mediaGallery [$ rowSku ][] = [
1656
- 'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1657
- 'label ' => $ label ,
1658
- 'position ' => ++$ position ,
1659
- 'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1660
- 'value ' => $ uploadedFile ,
1661
- ];
1662
- $ existingImages [$ rowSku ][$ uploadedFile ] = true ;
1663
1685
}
1664
1686
}
1665
1687
}
@@ -1777,6 +1799,8 @@ protected function _saveProducts()
1777
1799
$ mediaGallery
1778
1800
)->_saveProductAttributes (
1779
1801
$ attributes
1802
+ )->updateMediaGalleryLabels (
1803
+ $ labelsForUpdate
1780
1804
);
1781
1805
1782
1806
$ this ->_eventManager ->dispatch (
@@ -2263,9 +2287,7 @@ public function validateRow(array $rowData, $rowNum)
2263
2287
$ this ->_processedEntitiesCount ++;
2264
2288
2265
2289
$ sku = $ rowData [self ::COL_SKU ];
2266
-
2267
- $ isNewProduct = !isset ($ this ->_oldSku [$ sku ]) || (Import::BEHAVIOR_REPLACE == $ this ->getBehavior ());
2268
- if (!$ isNewProduct ) {
2290
+ if (isset ($ this ->_oldSku [$ sku ]) && Import::BEHAVIOR_REPLACE !== $ this ->getBehavior ()) {
2269
2291
// can we get all necessary data from existent DB product?
2270
2292
// check for supported type of existing product
2271
2293
if (isset ($ this ->_productTypeModels [$ this ->_oldSku [$ sku ]['type_id ' ]])) {
@@ -2315,7 +2337,7 @@ public function validateRow(array $rowData, $rowNum)
2315
2337
$ rowAttributesValid = $ this ->_productTypeModels [$ newSku ['type_id ' ]]->isRowValid (
2316
2338
$ rowData ,
2317
2339
$ rowNum ,
2318
- $ isNewProduct
2340
+ !( isset ( $ this -> _oldSku [ $ sku ]) && Import:: BEHAVIOR_REPLACE !== $ this -> getBehavior ())
2319
2341
);
2320
2342
if (!$ rowAttributesValid && self ::SCOPE_DEFAULT == $ rowScope ) {
2321
2343
// mark SCOPE_DEFAULT row as invalid for future child rows if product not in DB already
@@ -2484,18 +2506,20 @@ private function parseAttributesWithWrappedValues($attributesData)
2484
2506
* Parse values of multiselect attributes depends on "Fields Enclosure" parameter
2485
2507
*
2486
2508
* @param string $values
2509
+ * @param string $delimiter
2487
2510
* @return array
2488
2511
*/
2489
- public function parseMultiselectValues ($ values )
2512
+ public function parseMultiselectValues ($ values, $ delimiter = self :: PSEUDO_MULTI_LINE_SEPARATOR )
2490
2513
{
2491
2514
if (empty ($ this ->_parameters [Import::FIELDS_ENCLOSURE ])) {
2492
- return explode (self :: PSEUDO_MULTI_LINE_SEPARATOR , $ values );
2515
+ return explode ($ delimiter , $ values );
2493
2516
}
2494
2517
if (preg_match_all ('~"((?:[^"]|"")*)"~ ' , $ values , $ matches )) {
2495
2518
return $ values = array_map (function ($ value ) {
2496
2519
return str_replace ('"" ' , '" ' , $ value );
2497
2520
}, $ matches [1 ]);
2498
2521
}
2522
+
2499
2523
return [$ values ];
2500
2524
}
2501
2525
@@ -2771,4 +2795,64 @@ private function formatBunchToStockDataRows(
2771
2795
$ indexer ->reindexList ($ productIdsToReindex );
2772
2796
}
2773
2797
}
2798
+
2799
+ /**
2800
+ * Update media gallery labels.
2801
+ *
2802
+ * @param array $labels
2803
+ * @return void
2804
+ */
2805
+ private function updateMediaGalleryLabels (array $ labels )
2806
+ {
2807
+ if (empty ($ labels )) {
2808
+ return ;
2809
+ }
2810
+
2811
+ $ insertData = [];
2812
+ foreach ($ labels as $ label ) {
2813
+ $ imageData = $ label ['imageData ' ];
2814
+
2815
+ if ($ imageData ['label ' ] === null ) {
2816
+ $ insertData [] = [
2817
+ 'label ' => $ label ['label ' ],
2818
+ $ this ->getProductEntityLinkField () => $ imageData [$ this ->getProductEntityLinkField ()],
2819
+ 'value_id ' => $ imageData ['value_id ' ],
2820
+ 'store_id ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID ,
2821
+ ];
2822
+ } else {
2823
+ $ this ->_connection ->update (
2824
+ $ this ->mediaGalleryValueTableName ,
2825
+ [
2826
+ 'label ' => $ label ['label ' ],
2827
+ ],
2828
+ [
2829
+ $ this ->getProductEntityLinkField () . ' = ? ' => $ imageData [$ this ->getProductEntityLinkField ()],
2830
+ 'value_id = ? ' => $ imageData ['value_id ' ],
2831
+ 'store_id = ? ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID ,
2832
+ ]
2833
+ );
2834
+ }
2835
+ }
2836
+
2837
+ if (!empty ($ insertData )) {
2838
+ $ this ->_connection ->insertMultiple (
2839
+ $ this ->mediaGalleryValueTableName ,
2840
+ $ insertData
2841
+ );
2842
+ }
2843
+ }
2844
+
2845
+ /**
2846
+ * Parse values from multiple attributes fields.
2847
+ *
2848
+ * @param string $labelRow
2849
+ * @return array
2850
+ */
2851
+ private function parseMultipleValues ($ labelRow )
2852
+ {
2853
+ return $ this ->parseMultiselectValues (
2854
+ $ labelRow ,
2855
+ $ this ->getMultipleValueSeparator ()
2856
+ );
2857
+ }
2774
2858
}
0 commit comments