Skip to content

Commit ca48d02

Browse files
author
Vadim Zubovich
committed
MAGETWO-44274: The issue fixed
1 parent 377abce commit ca48d02

File tree

6 files changed

+167
-33
lines changed

6 files changed

+167
-33
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Attribute/Backend/Media.php

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -298,41 +298,29 @@ public function deleteGalleryValueInStore($valueId, $entityId, $storeId)
298298
* @param array $newFiles
299299
* @param int $originalProductId
300300
* @param int $newProductId
301-
* @return $this
301+
* @return array
302302
*/
303303
public function duplicate($attributeId, $newFiles, $originalProductId, $newProductId)
304304
{
305-
$mainTableAlias = $this->getMainTableAlias();
305+
$mediaGalleryEntities = $this->loadMediaGalleryEntities($attributeId, $originalProductId);
306306

307-
$select = $this->getConnection()->select()->from(
308-
[$mainTableAlias => $this->getMainTable()],
309-
['value_id', 'value']
310-
)->joinInner(
311-
['entity' => $this->getTable(self::GALLERY_VALUE_TO_ENTITY_TABLE)],
312-
$mainTableAlias . '.value_id = entity.value_id',
313-
['entity_id' => 'entity_id']
314-
)->where(
315-
'attribute_id = ?',
316-
$attributeId
317-
)->where(
318-
'entity.entity_id = ?',
319-
$originalProductId
320-
);
321-
322-
$valueIdMap = [];
323307
// Duplicate main entries of gallery
324-
foreach ($this->getConnection()->fetchAll($select) as $row) {
308+
$valueIdMap = [];
309+
foreach ($mediaGalleryEntities as $row) {
310+
$valueId = $row['value_id'];
325311
$data = [
326312
'attribute_id' => $attributeId,
327-
'entity_id' => $newProductId,
328-
'value' => isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'],
313+
'media_type' => $row['media_type'],
314+
'disabled' => $row['disabled'],
315+
'value' => isset($newFiles[$valueId]) ? $newFiles[$valueId] : $row['value'],
329316
];
330-
331-
$valueIdMap[$row['value_id']] = $this->insertGallery($data);
317+
$valueIdMap[$valueId] = $this->insertGallery($data);
332318
}
333319

320+
321+
334322
if (count($valueIdMap) == 0) {
335-
return $this;
323+
return [];
336324
}
337325

338326
// Duplicate per store gallery values
@@ -350,6 +338,49 @@ public function duplicate($attributeId, $newFiles, $originalProductId, $newProdu
350338
$this->bindValueToEntity($row['value_id'], $newProductId);
351339
}
352340

353-
return $this;
341+
return $valueIdMap;
342+
}
343+
344+
/**
345+
* @param array $valueIds
346+
* @return array
347+
* @throws \Magento\Framework\Exception\LocalizedException
348+
*/
349+
public function loadMediaGalleryEntitiesbyId($valueIds)
350+
{
351+
$select = $this->getConnection()->select()->from(
352+
$this->getMainTable()
353+
)->where(
354+
'value_id IN(?)',
355+
$valueIds
356+
);
357+
358+
return $this->getConnection()->fetchAll($select);
359+
}
360+
361+
/**
362+
* @param int $attributeId
363+
* @param int $productId
364+
* @return array
365+
* @throws \Magento\Framework\Exception\LocalizedException
366+
*/
367+
public function loadMediaGalleryEntities($attributeId, $productId)
368+
{
369+
$mainTableAlias = $this->getMainTableAlias();
370+
$select = $this->getConnection()->select()->from(
371+
[$mainTableAlias => $this->getMainTable()]
372+
)->joinInner(
373+
['entity' => $this->getTable(self::GALLERY_VALUE_TO_ENTITY_TABLE)],
374+
$mainTableAlias . '.value_id = entity.value_id',
375+
['entity_id' => 'entity_id']
376+
)->where(
377+
'attribute_id = ?',
378+
$attributeId
379+
)->where(
380+
'entity.entity_id = ?',
381+
$productId
382+
);
383+
384+
return $this->getConnection()->fetchAll($select);
354385
}
355386
}

app/code/Magento/ProductVideo/Model/Plugin/BaseImage.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Magento\ProductVideo\Model\Plugin;
88

9-
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\BaseImage as OriginalBloc;
9+
use Magento\Catalog\Block\Adminhtml\Product\Helper\Form\BaseImage as OriginalBlock;
1010
use Magento\Framework\View\Element\Template;
1111

1212
/**
@@ -20,13 +20,13 @@ class BaseImage
2020
const ELEMENT_OUTPUT_TEMPLATE = 'Magento_ProductVideo::product/edit/base_image.phtml';
2121

2222
/**
23-
* @param OriginalBloc $baseImage
23+
* @param OriginalBlock $baseImage
2424
* @param Template $block
2525
* @return Template
2626
*
2727
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2828
*/
29-
public function afterAssignBlockVariables(OriginalBloc $baseImage, Template $block)
29+
public function afterAssignBlockVariables(OriginalBlock $baseImage, Template $block)
3030
{
3131
$block->assign([
3232
'videoPlaceholderText' => __('Click here to add videos.'),
@@ -37,13 +37,13 @@ public function afterAssignBlockVariables(OriginalBloc $baseImage, Template $blo
3737
}
3838

3939
/**
40-
* @param OriginalBloc $baseImage
40+
* @param OriginalBlock $baseImage
4141
* @param Template $block
4242
* @return Template
4343
*
4444
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4545
*/
46-
public function afterCreateElementHtmlOutputBlock(OriginalBloc $baseImage, Template $block)
46+
public function afterCreateElementHtmlOutputBlock(OriginalBlock $baseImage, Template $block)
4747
{
4848
$block->setTemplate(self::ELEMENT_OUTPUT_TEMPLATE);
4949
return $block;

app/code/Magento/ProductVideo/Model/Plugin/ExternalVideoEntryProcessor.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ class ExternalVideoEntryProcessor
4141
*/
4242
protected $resourceEntryMediaGallery;
4343

44+
/**
45+
* @var \Magento\ProductVideo\Model\ResourceModel\Video
46+
*/
47+
protected $videoResourceModel;
48+
4449
/**
4550
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media $resourceEntryMediaGallery
4651
*/
4752
public function __construct(
48-
\Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media $resourceEntryMediaGallery
53+
\Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media $resourceEntryMediaGallery,
54+
\Magento\ProductVideo\Model\ResourceModel\Video $videoResourceModel
4955
) {
5056
$this->resourceEntryMediaGallery = $resourceEntryMediaGallery;
57+
$this->videoResourceModel = $videoResourceModel;
5158
}
5259

5360
/**
@@ -141,8 +148,7 @@ protected function saveAdditionalStoreData(array $videoDataCollection)
141148
*/
142149
protected function saveVideoValuesItem(array $item)
143150
{
144-
$this->resourceEntryMediaGallery->saveDataRow(
145-
InstallSchema::GALLERY_VALUE_VIDEO_TABLE,
151+
$this->videoResourceModel->insertOnDuplicate(
146152
$this->prepareVideoRowDataForSave($item)
147153
);
148154
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\ProductVideo\Model\Plugin;
8+
9+
use Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media;
10+
use Magento\ProductVideo\Setup\InstallSchema;
11+
12+
/**
13+
* Attribute Media Resource decorator
14+
*/
15+
class ExternalVideoResourceBackend
16+
{
17+
/**
18+
* @var \Magento\ProductVideo\Model\ResourceModel\Video
19+
*/
20+
protected $videoResourceModel;
21+
22+
/**
23+
* @param \Magento\ProductVideo\Model\ResourceModel\Video $videoResourceModel
24+
*/
25+
public function __construct(\Magento\ProductVideo\Model\ResourceModel\Video $videoResourceModel)
26+
{
27+
$this->videoResourceModel = $videoResourceModel;
28+
}
29+
30+
/**
31+
* @param Media $originalResourceModel
32+
* @param array $valueIdMap
33+
* @return array
34+
*
35+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
36+
*/
37+
public function afterDuplicate(Media $originalResourceModel, array $valueIdMap)
38+
{
39+
$mediaGalleryEntitiesData = $this->videoResourceModel->loadByIds(array_keys($valueIdMap));
40+
foreach($mediaGalleryEntitiesData as $row) {
41+
$row['value_id'] = $valueIdMap[$row['value_id']];
42+
$this->videoResourceModel->insertOnDuplicate($row);
43+
}
44+
45+
return $valueIdMap;
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ProductVideo\Model\ResourceModel;
7+
8+
class Video extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
9+
{
10+
/**
11+
* Resource initialization
12+
*
13+
* @return void
14+
*/
15+
public function _construct()
16+
{
17+
$this->_init(\Magento\ProductVideo\Setup\InstallSchema::GALLERY_VALUE_VIDEO_TABLE, 'value_id');
18+
}
19+
20+
/**
21+
* @param array $data
22+
* @param array $fields
23+
* @return int
24+
* @throws \Magento\Framework\Exception\LocalizedException
25+
*/
26+
public function insertOnDuplicate(array $data, array $fields = [])
27+
{
28+
return $this->getConnection()->insertOnDuplicate($this->getMainTable(), $data, $fields);
29+
}
30+
31+
/**
32+
* @param array $ids
33+
* @return array
34+
* @throws \Magento\Framework\Exception\LocalizedException
35+
*/
36+
public function loadByIds(array $ids)
37+
{
38+
$select = $this->getConnection()->select()->from(
39+
$this->getMainTable()
40+
)->where(
41+
'value_id IN(?)',
42+
$ids
43+
);
44+
45+
return $this->getConnection()->fetchAll($select);
46+
}
47+
}

app/code/Magento/ProductVideo/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<type name="Magento\Catalog\Model\Product\Attribute\Backend\Media">
4040
<plugin name="external_video_media_entry_processor" type="Magento\ProductVideo\Model\Plugin\ExternalVideoEntryProcessor" />
4141
</type>
42+
<type name="Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media">
43+
<plugin name="external_video_media_resource_backend" type="Magento\ProductVideo\Model\Plugin\ExternalVideoResourceBackend" />
44+
</type>
4245
<type name="Magento\Catalog\Block\Adminhtml\Product\Helper\Form\BaseImage">
4346
<plugin name="base_image_element_render" type="Magento\ProductVideo\Model\Plugin\BaseImage" />
4447
</type>

0 commit comments

Comments
 (0)