3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Model \Product \Gallery ;
7
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
8
11
use Magento \Framework \App \Filesystem \DirectoryList ;
12
+ use Magento \Framework \App \ObjectManager ;
9
13
use Magento \Framework \EntityManager \Operation \ExtensionInterface ;
10
14
use Magento \MediaStorage \Model \File \Uploader as FileUploader ;
15
+ use Magento \Store \Model \StoreManagerInterface ;
11
16
12
17
/**
13
18
* Create handler for catalog product gallery
@@ -74,6 +79,16 @@ class CreateHandler implements ExtensionInterface
74
79
*/
75
80
private $ mediaAttributeCodes ;
76
81
82
+ /**
83
+ * @var array
84
+ */
85
+ private $ imagesGallery ;
86
+
87
+ /**
88
+ * @var \Magento\Store\Model\StoreManagerInterface
89
+ */
90
+ private $ storeManager ;
91
+
77
92
/**
78
93
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
79
94
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
@@ -82,6 +97,8 @@ class CreateHandler implements ExtensionInterface
82
97
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
83
98
* @param \Magento\Framework\Filesystem $filesystem
84
99
* @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
100
+ * @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
101
+ * @throws \Magento\Framework\Exception\FileSystemException
85
102
*/
86
103
public function __construct (
87
104
\Magento \Framework \EntityManager \MetadataPool $ metadataPool ,
@@ -90,7 +107,8 @@ public function __construct(
90
107
\Magento \Framework \Json \Helper \Data $ jsonHelper ,
91
108
\Magento \Catalog \Model \Product \Media \Config $ mediaConfig ,
92
109
\Magento \Framework \Filesystem $ filesystem ,
93
- \Magento \MediaStorage \Helper \File \Storage \Database $ fileStorageDb
110
+ \Magento \MediaStorage \Helper \File \Storage \Database $ fileStorageDb ,
111
+ \Magento \Store \Model \StoreManagerInterface $ storeManager = null
94
112
) {
95
113
$ this ->metadata = $ metadataPool ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class);
96
114
$ this ->attributeRepository = $ attributeRepository ;
@@ -99,6 +117,7 @@ public function __construct(
99
117
$ this ->mediaConfig = $ mediaConfig ;
100
118
$ this ->mediaDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::MEDIA );
101
119
$ this ->fileStorageDb = $ fileStorageDb ;
120
+ $ this ->storeManager = $ storeManager ?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
102
121
}
103
122
104
123
/**
@@ -137,6 +156,10 @@ public function execute($product, $arguments = [])
137
156
138
157
if ($ product ->getIsDuplicate () != true ) {
139
158
foreach ($ value ['images ' ] as &$ image ) {
159
+ if (!empty ($ image ['removed ' ]) && !$ this ->canRemoveImage ($ product , $ image ['file ' ])) {
160
+ $ image ['removed ' ] = '' ;
161
+ }
162
+
140
163
if (!empty ($ image ['removed ' ])) {
141
164
$ clearImages [] = $ image ['file ' ];
142
165
} elseif (empty ($ image ['value_id ' ])) {
@@ -152,6 +175,10 @@ public function execute($product, $arguments = [])
152
175
// For duplicating we need copy original images.
153
176
$ duplicate = [];
154
177
foreach ($ value ['images ' ] as &$ image ) {
178
+ if (!empty ($ image ['removed ' ]) && !$ this ->canRemoveImage ($ product , $ image ['file ' ])) {
179
+ $ image ['removed ' ] = '' ;
180
+ }
181
+
155
182
if (empty ($ image ['value_id ' ]) || !empty ($ image ['removed ' ])) {
156
183
continue ;
157
184
}
@@ -538,4 +565,46 @@ private function processMediaAttributeLabel(
538
565
);
539
566
}
540
567
}
568
+
569
+ /**
570
+ * Get product images for all stores
571
+ *
572
+ * @param ProductInterface $product
573
+ * @return array
574
+ */
575
+ private function getImagesForAllStores (ProductInterface $ product )
576
+ {
577
+ if ($ this ->imagesGallery === null ) {
578
+ $ storeIds = array_keys ($ this ->storeManager ->getStores ());
579
+ $ storeIds [] = 0 ;
580
+
581
+ $ this ->imagesGallery = $ this ->resourceModel ->getProductImages ($ product , $ storeIds );
582
+ }
583
+
584
+ return $ this ->imagesGallery ;
585
+ }
586
+
587
+ /**
588
+ * Check possibility to remove image
589
+ *
590
+ * @param ProductInterface $product
591
+ * @param string $imageFile
592
+ * @return bool
593
+ */
594
+ private function canRemoveImage (ProductInterface $ product , string $ imageFile ) :bool
595
+ {
596
+ $ canRemoveImage = true ;
597
+ $ gallery = $ this ->getImagesForAllStores ($ product );
598
+ $ storeId = $ product ->getStoreId ();
599
+
600
+ if (!empty ($ gallery )) {
601
+ foreach ($ gallery as $ image ) {
602
+ if ($ image ['filepath ' ] === $ imageFile && (int ) $ image ['store_id ' ] !== $ storeId ) {
603
+ $ canRemoveImage = false ;
604
+ }
605
+ }
606
+ }
607
+
608
+ return $ canRemoveImage ;
609
+ }
541
610
}
0 commit comments