77
88use Magento \Catalog \Model \Config as CatalogConfig ;
99use Magento \Catalog \Model \Product \Visibility ;
10+ use Magento \CatalogImportExport \Model \Import \Product \MediaGalleryProcessor ;
1011use Magento \CatalogImportExport \Model \Import \Product \RowValidatorInterface as ValidatorInterface ;
1112use Magento \Framework \App \Filesystem \DirectoryList ;
1213use Magento \Framework \Filesystem ;
@@ -698,6 +699,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
698699 */
699700 private $ catalogConfig ;
700701
702+ /**
703+ * Provide ability to process and save images during import.
704+ *
705+ * @var MediaGalleryProcessor
706+ */
707+ private $ mediaProcessor ;
708+
701709 /**
702710 * @param \Magento\Framework\Json\Helper\Data $jsonHelper
703711 * @param \Magento\ImportExport\Helper\Data $importExportData
@@ -737,6 +745,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
737745 * @param array $data
738746 * @param array $dateAttrCodes
739747 * @param CatalogConfig $catalogConfig
748+ * @param MediaGalleryProcessor $mediaProcessor
740749 * @throws \Magento\Framework\Exception\LocalizedException
741750 *
742751 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -780,7 +789,8 @@ public function __construct(
780789 \Magento \Catalog \Model \Product \Url $ productUrl ,
781790 array $ data = [],
782791 array $ dateAttrCodes = [],
783- CatalogConfig $ catalogConfig = null
792+ CatalogConfig $ catalogConfig = null ,
793+ MediaGalleryProcessor $ mediaProcessor = null
784794 ) {
785795 $ this ->_eventManager = $ eventManager ;
786796 $ this ->stockRegistry = $ stockRegistry ;
@@ -813,7 +823,8 @@ public function __construct(
813823 $ this ->dateAttrCodes = array_merge ($ this ->dateAttrCodes , $ dateAttrCodes );
814824 $ this ->catalogConfig = $ catalogConfig ?: \Magento \Framework \App \ObjectManager::getInstance ()
815825 ->get (CatalogConfig::class);
816-
826+ $ this ->mediaProcessor = $ mediaProcessor ?: \Magento \Framework \App \ObjectManager::getInstance ()
827+ ->get (MediaGalleryProcessor::class);
817828 parent ::__construct (
818829 $ jsonHelper ,
819830 $ importExportData ,
@@ -1447,6 +1458,7 @@ private function getNewSkuFieldsForSelect()
14471458 * Init media gallery resources
14481459 * @return void
14491460 * @since 100.0.4
1461+ * @deprecated
14501462 */
14511463 protected function initMediaGalleryResources ()
14521464 {
@@ -1470,48 +1482,7 @@ protected function initMediaGalleryResources()
14701482 */
14711483 protected function getExistingImages ($ bunch )
14721484 {
1473- $ result = [];
1474- if ($ this ->getErrorAggregator ()->hasToBeTerminated ()) {
1475- return $ result ;
1476- }
1477-
1478- $ this ->initMediaGalleryResources ();
1479- $ productSKUs = array_map ('strval ' , array_column ($ bunch , self ::COL_SKU ));
1480- $ select = $ this ->_connection ->select ()->from (
1481- ['mg ' => $ this ->mediaGalleryTableName ],
1482- ['value ' => 'mg.value ' ]
1483- )->joinInner (
1484- ['mgvte ' => $ this ->mediaGalleryEntityToValueTableName ],
1485- '(mg.value_id = mgvte.value_id) ' ,
1486- [
1487- $ this ->getProductEntityLinkField () => 'mgvte. ' . $ this ->getProductEntityLinkField (),
1488- 'value_id ' => 'mgvte.value_id '
1489- ]
1490- )->joinLeft (
1491- ['mgv ' => $ this ->mediaGalleryValueTableName ],
1492- sprintf (
1493- '(mg.value_id = mgv.value_id AND mgv.%s = mgvte.%s AND mgv.store_id = %d) ' ,
1494- $ this ->getProductEntityLinkField (),
1495- $ this ->getProductEntityLinkField (),
1496- \Magento \Store \Model \Store::DEFAULT_STORE_ID
1497- ),
1498- [
1499- 'label ' => 'mgv.label '
1500- ]
1501- )->joinInner (
1502- ['pe ' => $ this ->productEntityTableName ],
1503- "(mgvte. {$ this ->getProductEntityLinkField ()} = pe. {$ this ->getProductEntityLinkField ()}) " ,
1504- ['sku ' => 'pe.sku ' ]
1505- )->where (
1506- 'pe.sku IN (?) ' ,
1507- $ productSKUs
1508- );
1509-
1510- foreach ($ this ->_connection ->fetchAll ($ select ) as $ image ) {
1511- $ result [$ image ['sku ' ]][$ image ['value ' ]] = $ image ;
1512- }
1513-
1514- return $ result ;
1485+ return $ this ->mediaProcessor ->getExistingImages ($ bunch );
15151486 }
15161487
15171488 /**
@@ -2082,93 +2053,13 @@ private function getSystemFile($fileName)
20822053 *
20832054 * @param array $mediaGalleryData
20842055 * @return $this
2085- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
2086- * @SuppressWarnings(PHPMD.NPathComplexity)
20872056 */
20882057 protected function _saveMediaGallery (array $ mediaGalleryData )
20892058 {
20902059 if (empty ($ mediaGalleryData )) {
20912060 return $ this ;
20922061 }
2093- $ this ->initMediaGalleryResources ();
2094- $ imageNames = [];
2095- $ multiInsertData = [];
2096- $ valueToProductId = [];
2097- $ mediaGalleryData = $ this ->restoreDisableImage ($ mediaGalleryData );
2098- foreach (array_keys ($ mediaGalleryData ) as $ storeId ) {
2099- foreach ($ mediaGalleryData [$ storeId ] as $ productSku => $ mediaGalleryRows ) {
2100- $ productId = $ this ->skuProcessor ->getNewSku ($ productSku )[$ this ->getProductEntityLinkField ()];
2101-
2102- $ insertedGalleryImgs = [];
2103- foreach ($ mediaGalleryRows as $ insertValue ) {
2104- if (!in_array ($ insertValue ['value ' ], $ insertedGalleryImgs )) {
2105- $ valueArr = [
2106- 'attribute_id ' => $ insertValue ['attribute_id ' ],
2107- 'value ' => $ insertValue ['value ' ],
2108- ];
2109- $ valueToProductId [$ insertValue ['value ' ]][] = $ productId ;
2110- $ imageNames [] = $ insertValue ['value ' ];
2111- $ multiInsertData [] = $ valueArr ;
2112- $ insertedGalleryImgs [] = $ insertValue ['value ' ];
2113- }
2114- }
2115- }
2116- }
2117- $ multiInsertData = $ this ->filterImageInsertData ($ multiInsertData , $ imageNames );
2118- if ($ multiInsertData ) {
2119- $ this ->_connection ->insertOnDuplicate ($ this ->mediaGalleryTableName , $ multiInsertData );
2120- }
2121- $ newMediaSelect = $ this ->_connection ->select ()->from ($ this ->mediaGalleryTableName , ['value_id ' , 'value ' ])
2122- ->where ('value IN (?) ' , $ imageNames );
2123- $ dataForSkinnyTable = [];
2124- $ multiInsertData = [];
2125- $ newMediaValues = $ this ->_connection ->fetchAssoc ($ newMediaSelect );
2126- foreach (array_keys ($ mediaGalleryData ) as $ storeId ) {
2127- foreach ($ mediaGalleryData [$ storeId ] as $ productSku => $ mediaGalleryRows ) {
2128- foreach ($ mediaGalleryRows as $ insertValue ) {
2129- foreach ($ newMediaValues as $ value_id => $ values ) {
2130- if ($ values ['value ' ] == $ insertValue ['value ' ]) {
2131- $ insertValue ['value_id ' ] = $ value_id ;
2132- $ insertValue [$ this ->getProductEntityLinkField ()]
2133- = array_shift ($ valueToProductId [$ values ['value ' ]]);
2134- break ;
2135- }
2136- }
2137- if (isset ($ insertValue ['value_id ' ])) {
2138- $ valueArr = [
2139- 'value_id ' => $ insertValue ['value_id ' ],
2140- 'store_id ' => $ storeId ,
2141- $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
2142- 'label ' => $ insertValue ['label ' ],
2143- 'position ' => $ insertValue ['position ' ],
2144- 'disabled ' => $ insertValue ['disabled ' ],
2145- ];
2146- $ multiInsertData [] = $ valueArr ;
2147- $ dataForSkinnyTable [] = [
2148- 'value_id ' => $ insertValue ['value_id ' ],
2149- $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
2150- ];
2151- }
2152- }
2153- }
2154- }
2155- try {
2156- $ this ->_connection ->insertOnDuplicate (
2157- $ this ->mediaGalleryValueTableName ,
2158- $ multiInsertData ,
2159- ['value_id ' , 'store_id ' , $ this ->getProductEntityLinkField (), 'label ' , 'position ' , 'disabled ' ]
2160- );
2161- $ this ->_connection ->insertOnDuplicate (
2162- $ this ->mediaGalleryEntityToValueTableName ,
2163- $ dataForSkinnyTable ,
2164- ['value_id ' ]
2165- );
2166- } catch (\Exception $ e ) {
2167- $ this ->_connection ->delete (
2168- $ this ->mediaGalleryTableName ,
2169- $ this ->_connection ->quoteInto ('value_id IN (?) ' , $ newMediaValues )
2170- );
2171- }
2062+ $ this ->mediaProcessor ->saveMediaGallery ($ mediaGalleryData );
21722063
21732064 return $ this ;
21742065 }
@@ -2920,39 +2811,7 @@ private function updateMediaGalleryLabels(array $labels)
29202811 if (empty ($ labels )) {
29212812 return ;
29222813 }
2923-
2924- $ insertData = [];
2925- foreach ($ labels as $ label ) {
2926- $ imageData = $ label ['imageData ' ];
2927-
2928- if ($ imageData ['label ' ] === null ) {
2929- $ insertData [] = [
2930- 'label ' => $ label ['label ' ],
2931- $ this ->getProductEntityLinkField () => $ imageData [$ this ->getProductEntityLinkField ()],
2932- 'value_id ' => $ imageData ['value_id ' ],
2933- 'store_id ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID
2934- ];
2935- } else {
2936- $ this ->_connection ->update (
2937- $ this ->mediaGalleryValueTableName ,
2938- [
2939- 'label ' => $ label ['label ' ]
2940- ],
2941- [
2942- $ this ->getProductEntityLinkField () . ' = ? ' => $ imageData [$ this ->getProductEntityLinkField ()],
2943- 'value_id = ? ' => $ imageData ['value_id ' ],
2944- 'store_id = ? ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID
2945- ]
2946- );
2947- }
2948- }
2949-
2950- if (!empty ($ insertData )) {
2951- $ this ->_connection ->insertMultiple (
2952- $ this ->mediaGalleryValueTableName ,
2953- $ insertData
2954- );
2955- }
2814+ $ this ->mediaProcessor ->updateMediaGalleryLabels ($ labels );
29562815 }
29572816
29582817 /**
@@ -2991,64 +2850,4 @@ private function getExistingSku($sku)
29912850 {
29922851 return $ this ->_oldSku [strtolower ($ sku )];
29932852 }
2994-
2995- /**
2996- * Set product images 'disable' = 0 for specified store.
2997- *
2998- * @param array $mediaGalleryData
2999- * @return array
3000- */
3001- private function restoreDisableImage (array $ mediaGalleryData )
3002- {
3003- $ restoreData = [];
3004- foreach (array_keys ($ mediaGalleryData ) as $ storeId ) {
3005- foreach ($ mediaGalleryData [$ storeId ] as $ productSku => $ mediaGalleryRows ) {
3006- $ productId = $ this ->skuProcessor ->getNewSku ($ productSku )[$ this ->getProductEntityLinkField ()];
3007- $ restoreData [] = sprintf (
3008- 'store_id = %s and %s = %s ' ,
3009- $ storeId ,
3010- $ this ->getProductEntityLinkField (),
3011- $ productId
3012- );
3013- if (isset ($ mediaGalleryRows ['all ' ]['restore ' ])) {
3014- unset($ mediaGalleryData [$ storeId ][$ productSku ]);
3015- }
3016- }
3017- }
3018-
3019- $ this ->_connection ->update (
3020- $ this ->mediaGalleryValueTableName ,
3021- ['disabled ' => 0 ],
3022- new \Zend_Db_Expr (implode (' or ' , $ restoreData ))
3023- );
3024-
3025- return $ mediaGalleryData ;
3026- }
3027-
3028- /**
3029- * Remove existed images from insert data.
3030- *
3031- * @param array $multiInsertData
3032- * @param array $imageNames
3033- * @return array
3034- */
3035- private function filterImageInsertData (array $ multiInsertData , array $ imageNames )
3036- {
3037- //Remove image duplicates for stores.
3038- $ multiInsertData = array_map ("unserialize " , array_unique (array_map ("serialize " , $ multiInsertData )));
3039- $ oldMediaValues = $ this ->_connection ->fetchAssoc (
3040- $ this ->_connection ->select ()->from ($ this ->mediaGalleryTableName , ['value_id ' , 'value ' , 'attribute_id ' ])
3041- ->where ('value IN (?) ' , $ imageNames )
3042- );
3043- foreach ($ multiInsertData as $ key => $ data ) {
3044- foreach ($ oldMediaValues as $ mediaValue ) {
3045- if ($ data ['value ' ] == $ mediaValue ['value ' ] && $ data ['attribute_id ' ] == $ mediaValue ['attribute_id ' ]) {
3046- unset($ multiInsertData [$ key ]);
3047- break ;
3048- }
3049- }
3050- }
3051-
3052- return $ multiInsertData ;
3053- }
30542853}
0 commit comments