Skip to content

Commit 6aaf635

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-97625' into chaika_january
2 parents 36ad542 + 29060f4 commit 6aaf635

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
@@ -318,7 +318,7 @@ protected function duplicate($product)
318318

319319
$this->resourceModel->duplicate(
320320
$this->getAttribute()->getAttributeId(),
321-
isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : [],
321+
$mediaGalleryData['duplicate'] ?? [],
322322
$product->getOriginalLinkId(),
323323
$product->getData($this->metadata->getLinkField())
324324
);

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;
@@ -149,7 +150,7 @@ public function loadProductGalleryByAttributeId($product, $attributeId)
149150
*/
150151
protected function createBaseLoadSelect($entityId, $storeId, $attributeId)
151152
{
152-
$select = $this->createBatchBaseSelect($storeId, $attributeId);
153+
$select = $this->createBatchBaseSelect($storeId, $attributeId);
153154

154155
$select = $select->where(
155156
'entity.' . $this->metadata->getLinkField() . ' = ?',
@@ -378,9 +379,9 @@ public function deleteGalleryValueInStore($valueId, $entityId, $storeId)
378379
$conditions = implode(
379380
' AND ',
380381
[
381-
$this->getConnection()->quoteInto('value_id = ?', (int) $valueId),
382-
$this->getConnection()->quoteInto($this->metadata->getLinkField() . ' = ?', (int) $entityId),
383-
$this->getConnection()->quoteInto('store_id = ?', (int) $storeId)
382+
$this->getConnection()->quoteInto('value_id = ?', (int)$valueId),
383+
$this->getConnection()->quoteInto($this->metadata->getLinkField() . ' = ?', (int)$entityId),
384+
$this->getConnection()->quoteInto('store_id = ?', (int)$storeId)
384385
]
385386
);
386387

@@ -408,7 +409,7 @@ public function duplicate($attributeId, $newFiles, $originalProductId, $newProdu
408409

409410
$select = $this->getConnection()->select()->from(
410411
[$this->getMainTableAlias() => $this->getMainTable()],
411-
['value_id', 'value']
412+
['value_id', 'value', 'media_type', 'disabled']
412413
)->joinInner(
413414
['entity' => $this->getTable(self::GALLERY_VALUE_TO_ENTITY_TABLE)],
414415
$this->getMainTableAlias() . '.value_id = entity.value_id',
@@ -425,16 +426,16 @@ public function duplicate($attributeId, $newFiles, $originalProductId, $newProdu
425426

426427
// Duplicate main entries of gallery
427428
foreach ($this->getConnection()->fetchAll($select) as $row) {
428-
$data = [
429-
'attribute_id' => $attributeId,
430-
'value' => isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'],
431-
];
429+
$data = $row;
430+
$data['attribute_id'] = $attributeId;
431+
$data['value'] = $newFiles[$row['value_id']] ?? $row['value'];
432+
unset($data['value_id']);
432433

433434
$valueIdMap[$row['value_id']] = $this->insertGallery($data);
434435
$this->bindValueToEntity($valueIdMap[$row['value_id']], $newProductId);
435436
}
436437

437-
if (count($valueIdMap) == 0) {
438+
if (count($valueIdMap) === 0) {
438439
return [];
439440
}
440441

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
@@ -19,6 +19,8 @@ class CreateHandler extends AbstractHandler
1919
const ADDITIONAL_STORE_DATA_KEY = 'additional_store_data';
2020

2121
/**
22+
* Execute before Plugin
23+
*
2224
* @param \Magento\Catalog\Model\Product\Gallery\CreateHandler $mediaGalleryCreateHandler
2325
* @param \Magento\Catalog\Model\Product $product
2426
* @param array $arguments
@@ -44,6 +46,8 @@ public function beforeExecute(
4446
}
4547

4648
/**
49+
* Execute plugin
50+
*
4751
* @param \Magento\Catalog\Model\Product\Gallery\CreateHandler $mediaGalleryCreateHandler
4852
* @param \Magento\Catalog\Model\Product $product
4953
* @return \Magento\Catalog\Model\Product
@@ -58,6 +62,9 @@ public function afterExecute(
5862
);
5963

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

@@ -70,6 +77,8 @@ public function afterExecute(
7077
}
7178

7279
/**
80+
* Saves video data
81+
*
7382
* @param array $videoDataCollection
7483
* @param int $storeId
7584
* @return void
@@ -83,6 +92,8 @@ protected function saveVideoData(array $videoDataCollection, $storeId)
8392
}
8493

8594
/**
95+
* Saves additioanal video data
96+
*
8697
* @param array $videoDataCollection
8798
* @return void
8899
*/
@@ -99,6 +110,8 @@ protected function saveAdditionalStoreData(array $videoDataCollection)
99110
}
100111

101112
/**
113+
* Saves video data
114+
*
102115
* @param array $item
103116
* @return void
104117
*/
@@ -111,6 +124,8 @@ protected function saveVideoValuesItem(array $item)
111124
}
112125

113126
/**
127+
* Excludes current store data
128+
*
114129
* @param array $mediaCollection
115130
* @param int $currentStoreId
116131
* @return array
@@ -126,6 +141,8 @@ function ($item) use ($currentStoreId) {
126141
}
127142

128143
/**
144+
* Prepare video data for saving
145+
*
129146
* @param array $rowData
130147
* @return array
131148
*/
@@ -143,6 +160,8 @@ protected function prepareVideoRowDataForSave(array $rowData)
143160
}
144161

145162
/**
163+
* Loads video data
164+
*
146165
* @param array $mediaCollection
147166
* @param int $excludedStore
148167
* @return array
@@ -165,6 +184,8 @@ protected function loadStoreViewVideoData(array $mediaCollection, $excludedStore
165184
}
166185

167186
/**
187+
* Collect video data
188+
*
168189
* @param array $mediaCollection
169190
* @return array
170191
*/
@@ -182,6 +203,8 @@ protected function collectVideoData(array $mediaCollection)
182203
}
183204

184205
/**
206+
* Extract video data
207+
*
185208
* @param array $rowData
186209
* @return array
187210
*/
@@ -194,6 +217,8 @@ protected function extractVideoDataFromRowData(array $rowData)
194217
}
195218

196219
/**
220+
* Collect items for additional data adding
221+
*
197222
* @param array $mediaCollection
198223
* @return array
199224
*/
@@ -209,6 +234,8 @@ protected function collectVideoEntriesIdsToAdditionalLoad(array $mediaCollection
209234
}
210235

211236
/**
237+
* Add additional data
238+
*
212239
* @param array $mediaCollection
213240
* @param array $data
214241
* @return array
@@ -229,6 +256,8 @@ protected function addAdditionalStoreData(array $mediaCollection, array $data):
229256
}
230257

231258
/**
259+
* Creates additional video data
260+
*
232261
* @param array $storeData
233262
* @param int $valueId
234263
* @return array
@@ -247,6 +276,8 @@ protected function createAdditionalStoreDataCollection(array $storeData, $valueI
247276
}
248277

249278
/**
279+
* Collect new videos
280+
*
250281
* @param array $mediaCollection
251282
* @return array
252283
*/
@@ -262,6 +293,8 @@ private function collectNewVideos(array $mediaCollection): array
262293
}
263294

264295
/**
296+
* Checks if gallery item is video
297+
*
265298
* @param array $item
266299
* @return bool
267300
*/
@@ -273,6 +306,8 @@ private function isVideoItem(array $item): bool
273306
}
274307

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

0 commit comments

Comments
 (0)