Skip to content

Commit f79efef

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-96984' into 2.2-develop-pr19
2 parents 57175a5 + d26aef2 commit f79efef

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/CreateHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ protected function duplicate($product)
308308

309309
$this->resourceModel->duplicate(
310310
$this->getAttribute()->getAttributeId(),
311-
isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : [],
311+
$mediaGalleryData['duplicate'] ?? [],
312312
$product->getOriginalLinkId(),
313313
$product->getData($this->metadata->getLinkField())
314314
);

app/code/Magento/Catalog/Model/ResourceModel/Product/Gallery.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Catalog\Model\ResourceModel\Product;
78

89
use Magento\Store\Model\Store;
@@ -141,7 +142,7 @@ public function loadProductGalleryByAttributeId($product, $attributeId)
141142
*/
142143
protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
143144
{
144-
$select = $this->createBatchBaseSelect($storeId, $attributeId);
145+
$select = $this->createBatchBaseSelect($storeId, $attributeId);
145146

146147
$select = $select->where(
147148
'entity.' . $this->metadata->getLinkField() . ' = ?',
@@ -362,9 +363,9 @@ public function deleteGalleryValueInStore($valueId, $entityId, $storeId)
362363
$conditions = implode(
363364
' AND ',
364365
[
365-
$this->getConnection()->quoteInto('value_id = ?', (int) $valueId),
366-
$this->getConnection()->quoteInto($this->metadata->getLinkField() . ' = ?', (int) $entityId),
367-
$this->getConnection()->quoteInto('store_id = ?', (int) $storeId)
366+
$this->getConnection()->quoteInto('value_id = ?', (int)$valueId),
367+
$this->getConnection()->quoteInto($this->metadata->getLinkField() . ' = ?', (int)$entityId),
368+
$this->getConnection()->quoteInto('store_id = ?', (int)$storeId)
368369
]
369370
);
370371

@@ -392,7 +393,7 @@ public function duplicate($attributeId, $newFiles, $originalProductId, $newProdu
392393

393394
$select = $this->getConnection()->select()->from(
394395
[$this->getMainTableAlias() => $this->getMainTable()],
395-
['value_id', 'value']
396+
['value_id', 'value', 'media_type', 'disabled']
396397
)->joinInner(
397398
['entity' => $this->getTable(self::GALLERY_VALUE_TO_ENTITY_TABLE)],
398399
$this->getMainTableAlias() . '.value_id = entity.value_id',
@@ -409,16 +410,16 @@ public function duplicate($attributeId, $newFiles, $originalProductId, $newProdu
409410

410411
// Duplicate main entries of gallery
411412
foreach ($this->getConnection()->fetchAll($select) as $row) {
412-
$data = [
413-
'attribute_id' => $attributeId,
414-
'value' => isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'],
415-
];
413+
$data = $row;
414+
$data['attribute_id'] = $attributeId;
415+
$data['value'] = $newFiles[$row['value_id']] ?? $row['value'];
416+
unset($data['value_id']);
416417

417418
$valueIdMap[$row['value_id']] = $this->insertGallery($data);
418419
$this->bindValueToEntity($valueIdMap[$row['value_id']], $newProductId);
419420
}
420421

421-
if (count($valueIdMap) == 0) {
422+
if (count($valueIdMap) === 0) {
422423
return [];
423424
}
424425

app/code/Magento/ProductVideo/Model/Plugin/Catalog/Product/Gallery/CreateHandler.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class CreateHandler extends AbstractHandler
2020
const ADDITIONAL_STORE_DATA_KEY = 'additional_store_data';
2121

2222
/**
23+
* Execute before Plugin
24+
*
2325
* @param \Magento\Catalog\Model\Product\Gallery\CreateHandler $mediaGalleryCreateHandler
2426
* @param \Magento\Catalog\Model\Product $product
2527
* @param array $arguments
@@ -45,6 +47,8 @@ public function beforeExecute(
4547
}
4648

4749
/**
50+
* Execute plugin
51+
*
4852
* @param \Magento\Catalog\Model\Product\Gallery\CreateHandler $mediaGalleryCreateHandler
4953
* @param \Magento\Catalog\Model\Product $product
5054
* @return \Magento\Catalog\Model\Product
@@ -59,6 +63,9 @@ public function afterExecute(
5963
);
6064

6165
if (!empty($mediaCollection)) {
66+
if ($product->getIsDuplicate() === true) {
67+
$mediaCollection = $this->makeAllNewVideos($product->getId(), $mediaCollection);
68+
}
6269
$newVideoCollection = $this->collectNewVideos($mediaCollection);
6370
$this->saveVideoData($newVideoCollection, 0);
6471

@@ -71,6 +78,8 @@ public function afterExecute(
7178
}
7279

7380
/**
81+
* Saves video data
82+
*
7483
* @param array $videoDataCollection
7584
* @param int $storeId
7685
* @return void
@@ -84,6 +93,8 @@ protected function saveVideoData(array $videoDataCollection, $storeId)
8493
}
8594

8695
/**
96+
* Saves additioanal video data
97+
*
8798
* @param array $videoDataCollection
8899
* @return void
89100
*/
@@ -100,6 +111,8 @@ protected function saveAdditionalStoreData(array $videoDataCollection)
100111
}
101112

102113
/**
114+
* Saves video data
115+
*
103116
* @param array $item
104117
* @return void
105118
*/
@@ -112,6 +125,8 @@ protected function saveVideoValuesItem(array $item)
112125
}
113126

114127
/**
128+
* Excludes current store data
129+
*
115130
* @param array $mediaCollection
116131
* @param int $currentStoreId
117132
* @return array
@@ -127,6 +142,8 @@ function ($item) use ($currentStoreId) {
127142
}
128143

129144
/**
145+
* Prepare video data for saving
146+
*
130147
* @param array $rowData
131148
* @return array
132149
*/
@@ -144,6 +161,8 @@ protected function prepareVideoRowDataForSave(array $rowData)
144161
}
145162

146163
/**
164+
* Loads video data
165+
*
147166
* @param array $mediaCollection
148167
* @param int $excludedStore
149168
* @return array
@@ -166,6 +185,8 @@ protected function loadStoreViewVideoData(array $mediaCollection, $excludedStore
166185
}
167186

168187
/**
188+
* Collect video data
189+
*
169190
* @param array $mediaCollection
170191
* @return array
171192
*/
@@ -183,6 +204,8 @@ protected function collectVideoData(array $mediaCollection)
183204
}
184205

185206
/**
207+
* Extract video data
208+
*
186209
* @param array $rowData
187210
* @return array
188211
*/
@@ -195,6 +218,8 @@ protected function extractVideoDataFromRowData(array $rowData)
195218
}
196219

197220
/**
221+
* Collect items for additional data adding
222+
*
198223
* @param array $mediaCollection
199224
* @return array
200225
*/
@@ -210,6 +235,8 @@ protected function collectVideoEntriesIdsToAdditionalLoad(array $mediaCollection
210235
}
211236

212237
/**
238+
* Add additional data
239+
*
213240
* @param array $mediaCollection
214241
* @param array $data
215242
* @return array
@@ -230,6 +257,8 @@ protected function addAdditionalStoreData(array $mediaCollection, array $data):
230257
}
231258

232259
/**
260+
* Creates additional video data
261+
*
233262
* @param array $storeData
234263
* @param int $valueId
235264
* @return array
@@ -248,6 +277,8 @@ protected function createAdditionalStoreDataCollection(array $storeData, $valueI
248277
}
249278

250279
/**
280+
* Collect new videos
281+
*
251282
* @param array $mediaCollection
252283
* @return array
253284
*/
@@ -263,6 +294,8 @@ private function collectNewVideos(array $mediaCollection): array
263294
}
264295

265296
/**
297+
* Checks if gallery item is video
298+
*
266299
* @param $item
267300
* @return bool
268301
*/
@@ -274,6 +307,8 @@ private function isVideoItem($item): bool
274307
}
275308

276309
/**
310+
* Checks if video is new
311+
*
277312
* @param $item
278313
* @return bool
279314
*/
@@ -283,4 +318,23 @@ private function isNewVideo($item): bool
283318
|| empty($item['video_url_default'])
284319
|| empty($item['video_title_default']);
285320
}
321+
322+
/**
323+
* Mark all videos as new
324+
*
325+
* @param int $entityId
326+
* @param array $mediaCollection
327+
* @return array
328+
*/
329+
private function makeAllNewVideos($entityId, array $mediaCollection): array
330+
{
331+
foreach ($mediaCollection as $key => $video) {
332+
if ($this->isVideoItem($video)) {
333+
unset($video['video_url_default'], $video['video_title_default']);
334+
$video['entity_id'] = $entityId;
335+
$mediaCollection[$key] = $video;
336+
}
337+
}
338+
return $mediaCollection;
339+
}
286340
}

0 commit comments

Comments
 (0)