@@ -109,42 +109,73 @@ public function __construct(
109
109
public function saveMediaGallery (array $ mediaGalleryData )
110
110
{
111
111
$ this ->initMediaGalleryResources ();
112
+ $ mediaGalleryDataGlobal = $ this ->processMediaGallery ($ mediaGalleryData );
112
113
$ imageNames = [];
113
114
$ multiInsertData = [];
114
115
$ valueToProductId = [];
115
- $ mediaGalleryData = $ this ->restoreDisableImage ($ mediaGalleryData );
116
- foreach (array_keys ($ mediaGalleryData ) as $ storeId ) {
117
- foreach ($ mediaGalleryData [$ storeId ] as $ productSku => $ mediaGalleryRows ) {
118
- $ productId = $ this ->skuProcessor ->getNewSku ($ productSku )[$ this ->getProductEntityLinkField ()];
119
-
120
- $ insertedGalleryImgs = [];
121
- foreach ($ mediaGalleryRows as $ insertValue ) {
122
- if (!in_array ($ insertValue ['value ' ], $ insertedGalleryImgs )) {
123
- $ valueArr = [
124
- 'attribute_id ' => $ insertValue ['attribute_id ' ],
125
- 'value ' => $ insertValue ['value ' ],
126
- ];
127
- $ valueToProductId [$ insertValue ['value ' ]][] = $ productId ;
128
- $ imageNames [] = $ insertValue ['value ' ];
129
- $ multiInsertData [] = $ valueArr ;
130
- $ insertedGalleryImgs [] = $ insertValue ['value ' ];
131
- }
116
+ foreach ($ mediaGalleryDataGlobal as $ productSku => $ mediaGalleryRows ) {
117
+ $ productId = $ this ->skuProcessor ->getNewSku ($ productSku )[$ this ->getProductEntityLinkField ()];
118
+ $ insertedGalleryImgs = [];
119
+ foreach ($ mediaGalleryRows as $ insertValue ) {
120
+ if (!in_array ($ insertValue ['value ' ], $ insertedGalleryImgs )) {
121
+ $ valueArr = [
122
+ 'attribute_id ' => $ insertValue ['attribute_id ' ],
123
+ 'value ' => $ insertValue ['value ' ],
124
+ ];
125
+ $ valueToProductId [$ insertValue ['value ' ]][] = $ productId ;
126
+ $ imageNames [] = $ insertValue ['value ' ];
127
+ $ multiInsertData [] = $ valueArr ;
128
+ $ insertedGalleryImgs [] = $ insertValue ['value ' ];
132
129
}
133
130
}
134
131
}
135
- $ multiInsertData = $ this ->filterImageInsertData ($ multiInsertData , $ imageNames );
136
- if ($ multiInsertData ) {
132
+ $ oldMediaValues = $ this ->connection ->fetchAssoc (
133
+ $ this ->connection ->select ()->from ($ this ->mediaGalleryTableName , ['value_id ' , 'value ' ])
134
+ ->where ('value IN (?) ' , $ imageNames )
135
+ );
136
+ if (!empty ($ multiInsertData )) {
137
137
$ this ->connection ->insertOnDuplicate ($ this ->mediaGalleryTableName , $ multiInsertData );
138
138
}
139
+ $ multiInsertData = [];
139
140
$ newMediaSelect = $ this ->connection ->select ()->from ($ this ->mediaGalleryTableName , ['value_id ' , 'value ' ])
140
141
->where ('value IN (?) ' , $ imageNames );
142
+ if (array_keys ($ oldMediaValues )) {
143
+ $ newMediaSelect ->where ('value_id NOT IN (?) ' , array_keys ($ oldMediaValues ));
144
+ }
141
145
146
+ $ dataForSkinnyTable = [];
142
147
$ newMediaValues = $ this ->connection ->fetchAssoc ($ newMediaSelect );
143
- list ($ multiInsertData , $ dataForSkinnyTable ) = $ this ->prepareInsertData (
144
- $ mediaGalleryData ,
145
- $ newMediaValues ,
146
- $ valueToProductId
147
- );
148
+ $ this ->restoreDisableImage ($ mediaGalleryData );
149
+ foreach ($ mediaGalleryData as $ storeId => $ mediaGalleryDataPerStore ) {
150
+ foreach ($ mediaGalleryDataPerStore as $ productSku => $ mediaGalleryRows ) {
151
+ foreach ($ mediaGalleryRows as $ insertValue ) {
152
+ foreach ($ newMediaValues as $ value_id => $ values ) {
153
+ if ($ values ['value ' ] == $ insertValue ['value ' ]) {
154
+ $ insertValue ['value_id ' ] = $ value_id ;
155
+ $ insertValue [$ this ->getProductEntityLinkField ()]
156
+ = array_shift ($ valueToProductId [$ values ['value ' ]]);
157
+ unset($ newMediaValues [$ value_id ]);
158
+ break ;
159
+ }
160
+ }
161
+ if (isset ($ insertValue ['value_id ' ])) {
162
+ $ valueArr = [
163
+ 'value_id ' => $ insertValue ['value_id ' ],
164
+ 'store_id ' => $ storeId ,
165
+ $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
166
+ 'label ' => $ insertValue ['label ' ],
167
+ 'position ' => $ insertValue ['position ' ],
168
+ 'disabled ' => $ insertValue ['disabled ' ],
169
+ ];
170
+ $ multiInsertData [] = $ valueArr ;
171
+ $ dataForSkinnyTable [] = [
172
+ 'value_id ' => $ insertValue ['value_id ' ],
173
+ $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
174
+ ];
175
+ }
176
+ }
177
+ }
178
+ }
148
179
try {
149
180
$ this ->connection ->insertOnDuplicate (
150
181
$ this ->mediaGalleryValueTableName ,
@@ -279,30 +310,6 @@ private function initMediaGalleryResources()
279
310
}
280
311
}
281
312
282
- /**
283
- * Remove existed images from insert data.
284
- *
285
- * @param array $multiInsertData
286
- * @param array $imageNames
287
- * @return array
288
- */
289
- private function filterImageInsertData (array $ multiInsertData , array $ imageNames )
290
- {
291
- $ oldMediaValues = $ this ->connection ->fetchAssoc (
292
- $ this ->connection ->select ()->from ($ this ->mediaGalleryTableName , ['value_id ' , 'value ' , 'attribute_id ' ])
293
- ->where ('value IN (?) ' , $ imageNames )
294
- );
295
- foreach ($ multiInsertData as $ key => $ data ) {
296
- foreach ($ oldMediaValues as $ mediaValue ) {
297
- if ($ data ['value ' ] === $ mediaValue ['value ' ] && $ data ['attribute_id ' ] === $ mediaValue ['attribute_id ' ]) {
298
- unset($ multiInsertData [$ key ]);
299
- }
300
- }
301
- }
302
-
303
- return $ multiInsertData ;
304
- }
305
-
306
313
/**
307
314
* Set product images 'disable' = 0 for specified store.
308
315
*
@@ -336,6 +343,24 @@ private function restoreDisableImage(array $mediaGalleryData)
336
343
return $ mediaGalleryData ;
337
344
}
338
345
346
+ /**
347
+ * Remove store specific information for inserting images.
348
+ *
349
+ * @param array $mediaGalleryData
350
+ * @return array
351
+ */
352
+ private function processMediaGallery (array $ mediaGalleryData )
353
+ {
354
+ $ mediaGalleryDataGlobal = array_merge (...$ mediaGalleryData );
355
+ foreach ($ mediaGalleryDataGlobal as $ sku => $ row ) {
356
+ if (isset ($ mediaGalleryDataGlobal [$ sku ]['all ' ]['restore ' ])) {
357
+ unset($ mediaGalleryDataGlobal [$ sku ]);
358
+ }
359
+ }
360
+
361
+ return $ mediaGalleryDataGlobal ;
362
+ }
363
+
339
364
/**
340
365
* Get product entity link field.
341
366
*
@@ -361,47 +386,4 @@ private function getResource()
361
386
362
387
return $ this ->resourceModel ;
363
388
}
364
-
365
- /**
366
- * @param array $mediaGalleryData
367
- * @param array $newMediaValues
368
- * @param array $valueToProductId
369
- * @return array
370
- */
371
- private function prepareInsertData (array $ mediaGalleryData , array $ newMediaValues , array $ valueToProductId )
372
- {
373
- $ dataForSkinnyTable = [];
374
- $ multiInsertData = [];
375
- foreach (array_keys ($ mediaGalleryData ) as $ storeId ) {
376
- foreach ($ mediaGalleryData [$ storeId ] as $ mediaGalleryRows ) {
377
- foreach ($ mediaGalleryRows as $ insertValue ) {
378
- foreach ($ newMediaValues as $ value_id => $ values ) {
379
- if ($ values ['value ' ] == $ insertValue ['value ' ]) {
380
- $ insertValue ['value_id ' ] = $ value_id ;
381
- $ insertValue [$ this ->getProductEntityLinkField ()]
382
- = array_shift ($ valueToProductId [$ values ['value ' ]]);
383
- break ;
384
- }
385
- }
386
- if (isset ($ insertValue ['value_id ' ])) {
387
- $ valueArr = [
388
- 'value_id ' => $ insertValue ['value_id ' ],
389
- 'store_id ' => $ storeId ,
390
- $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
391
- 'label ' => $ insertValue ['label ' ],
392
- 'position ' => $ insertValue ['position ' ],
393
- 'disabled ' => $ insertValue ['disabled ' ],
394
- ];
395
- $ multiInsertData [] = $ valueArr ;
396
- $ dataForSkinnyTable [] = [
397
- 'value_id ' => $ insertValue ['value_id ' ],
398
- $ this ->getProductEntityLinkField () => $ insertValue [$ this ->getProductEntityLinkField ()],
399
- ];
400
- }
401
- }
402
- }
403
- }
404
-
405
- return [$ multiInsertData , $ dataForSkinnyTable ];
406
- }
407
389
}
0 commit comments