@@ -1416,7 +1416,7 @@ protected function initMediaGalleryResources()
1416
1416
}
1417
1417
1418
1418
/**
1419
- * Get existing images for current bucnh
1419
+ * Get existing images for current bunch
1420
1420
*
1421
1421
* @param array $bunch
1422
1422
* @return array
@@ -1436,7 +1436,21 @@ protected function getExistingImages($bunch)
1436
1436
)->joinInner (
1437
1437
['mgvte ' => $ this ->mediaGalleryEntityToValueTableName ],
1438
1438
'(mg.value_id = mgvte.value_id) ' ,
1439
- [$ this ->getProductEntityLinkField () => 'mgvte. ' . $ this ->getProductEntityLinkField ()]
1439
+ [
1440
+ $ this ->getProductEntityLinkField () => 'mgvte. ' . $ this ->getProductEntityLinkField (),
1441
+ 'value_id ' => 'mgvte.value_id '
1442
+ ]
1443
+ )->joinLeft (
1444
+ ['mgv ' => $ this ->mediaGalleryValueTableName ],
1445
+ sprintf (
1446
+ '(mg.value_id = mgv.value_id AND mgv.%s = mgvte.%s AND mgv.store_id = %d) ' ,
1447
+ $ this ->getProductEntityLinkField (),
1448
+ $ this ->getProductEntityLinkField (),
1449
+ \Magento \Store \Model \Store::DEFAULT_STORE_ID
1450
+ ),
1451
+ [
1452
+ 'label ' => 'mgv.label '
1453
+ ]
1440
1454
)->joinInner (
1441
1455
['pe ' => $ this ->productEntityTableName ],
1442
1456
"(mgvte. {$ this ->getProductEntityLinkField ()} = pe. {$ this ->getProductEntityLinkField ()}) " ,
@@ -1447,7 +1461,7 @@ protected function getExistingImages($bunch)
1447
1461
);
1448
1462
1449
1463
foreach ($ this ->_connection ->fetchAll ($ select ) as $ image ) {
1450
- $ result [$ image ['sku ' ]][$ image ['value ' ]] = true ;
1464
+ $ result [$ image ['sku ' ]][$ image ['value ' ]] = $ image ;
1451
1465
}
1452
1466
1453
1467
return $ result ;
@@ -1462,22 +1476,21 @@ public function getImagesFromRow(array $rowData)
1462
1476
$ images = [];
1463
1477
$ labels = [];
1464
1478
foreach ($ this ->_imagesArrayKeys as $ column ) {
1465
- $ images [$ column ] = [];
1466
- $ labels [$ column ] = [];
1467
1479
if (!empty ($ rowData [$ column ])) {
1468
1480
$ images [$ column ] = array_unique (
1469
- explode ($ this ->getMultipleValueSeparator (), $ rowData [$ column ])
1481
+ array_map (
1482
+ 'trim ' ,
1483
+ explode ($ this ->getMultipleValueSeparator (), $ rowData [$ column ])
1484
+ )
1470
1485
);
1471
- }
1472
1486
1473
- if (!empty ($ rowData [$ column . '_label ' ])) {
1474
- $ labels [$ column ] = explode ($ this ->getMultipleValueSeparator (), $ rowData [$ column . '_label ' ]);
1475
- }
1487
+ if (!empty ($ rowData [$ column . '_label ' ])) {
1488
+ $ labels [$ column ] = $ this ->parseMultipleValues ($ rowData [$ column . '_label ' ]);
1476
1489
1477
- if (count ($ labels [$ column ]) > count ($ images [$ column ])) {
1478
- $ labels [$ column ] = array_slice ($ labels [$ column ], 0 , count ($ images [$ column ]));
1479
- } elseif ( count ( $ labels [ $ column ]) < count ( $ images [ $ column ])) {
1480
- $ labels [ $ column ] = array_pad ( $ labels [ $ column ], count ( $ images [ $ column ]), '' );
1490
+ if (count ($ labels [$ column ]) > count ($ images [$ column ])) {
1491
+ $ labels [$ column ] = array_slice ($ labels [$ column ], 0 , count ($ images [$ column ]));
1492
+ }
1493
+ }
1481
1494
}
1482
1495
}
1483
1496
@@ -1507,6 +1520,7 @@ protected function _saveProducts()
1507
1520
$ this ->categoriesCache = [];
1508
1521
$ tierPrices = [];
1509
1522
$ mediaGallery = [];
1523
+ $ labelsForUpdate = [];
1510
1524
$ uploadedImages = [];
1511
1525
$ previousType = null ;
1512
1526
$ prevAttributeSet = null ;
@@ -1616,7 +1630,7 @@ protected function _saveProducts()
1616
1630
foreach ($ rowImages as $ column => $ columnImages ) {
1617
1631
foreach ($ columnImages as $ position => $ columnImage ) {
1618
1632
if (!isset ($ uploadedImages [$ columnImage ])) {
1619
- $ uploadedFile = $ this ->uploadMediaFiles (trim ( $ columnImage) , true );
1633
+ $ uploadedFile = $ this ->uploadMediaFiles ($ columnImage , true );
1620
1634
if ($ uploadedFile ) {
1621
1635
$ uploadedImages [$ columnImage ] = $ uploadedFile ;
1622
1636
} else {
@@ -1636,20 +1650,28 @@ protected function _saveProducts()
1636
1650
$ rowData [$ column ] = $ uploadedFile ;
1637
1651
}
1638
1652
1639
- $ imageNotAssigned = !isset ($ existingImages [$ rowSku ][$ uploadedFile ]);
1640
-
1641
- if ($ uploadedFile && $ imageNotAssigned ) {
1642
- if ($ column == self ::COL_MEDIA_IMAGE ) {
1643
- $ rowData [$ column ][] = $ uploadedFile ;
1653
+ if ($ uploadedFile && !isset ($ mediaGallery [$ rowSku ][$ uploadedFile ])) {
1654
+ if (isset ($ existingImages [$ rowSku ][$ uploadedFile ])) {
1655
+ if (isset ($ rowLabels [$ column ][$ position ])
1656
+ && $ rowLabels [$ column ][$ position ] != $ existingImages [$ rowSku ][$ uploadedFile ]['label ' ]
1657
+ ) {
1658
+ $ labelsForUpdate [] = [
1659
+ 'label ' => $ rowLabels [$ column ][$ position ],
1660
+ 'imageData ' => $ existingImages [$ rowSku ][$ uploadedFile ]
1661
+ ];
1662
+ }
1663
+ } else {
1664
+ if ($ column == self ::COL_MEDIA_IMAGE ) {
1665
+ $ rowData [$ column ][] = $ uploadedFile ;
1666
+ }
1667
+ $ mediaGallery [$ rowSku ][$ uploadedFile ] = [
1668
+ 'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1669
+ 'label ' => isset ($ rowLabels [$ column ][$ position ]) ? $ rowLabels [$ column ][$ position ] : '' ,
1670
+ 'position ' => $ position + 1 ,
1671
+ 'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1672
+ 'value ' => $ uploadedFile ,
1673
+ ];
1644
1674
}
1645
- $ mediaGallery [$ rowSku ][] = [
1646
- 'attribute_id ' => $ this ->getMediaGalleryAttributeId (),
1647
- 'label ' => isset ($ rowLabels [$ column ][$ position ]) ? $ rowLabels [$ column ][$ position ] : '' ,
1648
- 'position ' => $ position + 1 ,
1649
- 'disabled ' => isset ($ disabledImages [$ columnImage ]) ? '1 ' : '0 ' ,
1650
- 'value ' => $ uploadedFile ,
1651
- ];
1652
- $ existingImages [$ rowSku ][$ uploadedFile ] = true ;
1653
1675
}
1654
1676
}
1655
1677
}
@@ -1767,6 +1789,8 @@ protected function _saveProducts()
1767
1789
$ mediaGallery
1768
1790
)->_saveProductAttributes (
1769
1791
$ attributes
1792
+ )->updateMediaGalleryLabels (
1793
+ $ labelsForUpdate
1770
1794
);
1771
1795
1772
1796
$ this ->_eventManager ->dispatch (
@@ -2535,12 +2559,13 @@ private function parseAttributesWithWrappedValues($attributesData)
2535
2559
* Parse values of multiselect attributes depends on "Fields Enclosure" parameter
2536
2560
*
2537
2561
* @param string $values
2562
+ * @param string $delimiter
2538
2563
* @return array
2539
2564
*/
2540
- public function parseMultiselectValues ($ values )
2565
+ public function parseMultiselectValues ($ values, $ delimiter = self :: PSEUDO_MULTI_LINE_SEPARATOR )
2541
2566
{
2542
2567
if (empty ($ this ->_parameters [Import::FIELDS_ENCLOSURE ])) {
2543
- return explode (self :: PSEUDO_MULTI_LINE_SEPARATOR , $ values );
2568
+ return explode ($ delimiter , $ values );
2544
2569
}
2545
2570
if (preg_match_all ('~"((?:[^"]|"")*)"~ ' , $ values , $ matches )) {
2546
2571
return $ values = array_map (function ($ value ) {
@@ -2752,4 +2777,64 @@ private function getProductIdentifierField()
2752
2777
}
2753
2778
return $ this ->productEntityIdentifierField ;
2754
2779
}
2780
+
2781
+ /**
2782
+ * Update media gallery labels
2783
+ *
2784
+ * @param array $labels
2785
+ * @return void
2786
+ */
2787
+ private function updateMediaGalleryLabels (array $ labels )
2788
+ {
2789
+ if (empty ($ labels )) {
2790
+ return ;
2791
+ }
2792
+
2793
+ $ insertData = [];
2794
+ foreach ($ labels as $ label ) {
2795
+ $ imageData = $ label ['imageData ' ];
2796
+
2797
+ if ($ imageData ['label ' ] === null ) {
2798
+ $ insertData [] = [
2799
+ 'label ' => $ label ['label ' ],
2800
+ $ this ->getProductEntityLinkField () => $ imageData [$ this ->getProductEntityLinkField ()],
2801
+ 'value_id ' => $ imageData ['value_id ' ],
2802
+ 'store_id ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID
2803
+ ];
2804
+ } else {
2805
+ $ this ->_connection ->update (
2806
+ $ this ->mediaGalleryValueTableName ,
2807
+ [
2808
+ 'label ' => $ label ['label ' ]
2809
+ ],
2810
+ [
2811
+ $ this ->getProductEntityLinkField () . ' = ? ' => $ imageData [$ this ->getProductEntityLinkField ()],
2812
+ 'value_id = ? ' => $ imageData ['value_id ' ],
2813
+ 'store_id = ? ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID
2814
+ ]
2815
+ );
2816
+ }
2817
+ }
2818
+
2819
+ if (!empty ($ insertData )) {
2820
+ $ this ->_connection ->insertMultiple (
2821
+ $ this ->mediaGalleryValueTableName ,
2822
+ $ insertData
2823
+ );
2824
+ }
2825
+ }
2826
+
2827
+ /**
2828
+ * Parse values from multiple attributes fields
2829
+ *
2830
+ * @param string $labelRow
2831
+ * @return array
2832
+ */
2833
+ private function parseMultipleValues ($ labelRow )
2834
+ {
2835
+ return $ this ->parseMultiselectValues (
2836
+ $ labelRow ,
2837
+ $ this ->getMultipleValueSeparator ()
2838
+ );
2839
+ }
2755
2840
}
0 commit comments