|
19 | 19 | use Magento\Framework\App\ResourceConnection;
|
20 | 20 | use Magento\Framework\DataObject;
|
21 | 21 | use Magento\Framework\Exception\LocalizedException;
|
| 22 | +use Magento\Framework\Exception\NoSuchEntityException; |
22 | 23 | use Magento\Framework\Filesystem;
|
23 | 24 | use Magento\Framework\HTTP\Adapter\Curl;
|
24 | 25 | use Magento\Framework\Image\AdapterFactory as ImageAdapterFactory;
|
@@ -233,6 +234,88 @@ public function __construct(
|
233 | 234 | $this->resourceConnection = $resourceConnection;
|
234 | 235 | }
|
235 | 236 |
|
| 237 | + |
| 238 | + /** |
| 239 | + * Remove Gallery Item |
| 240 | + * @param $product |
| 241 | + * @param $url |
| 242 | + * @param $removeAllGallery |
| 243 | + * @return int|void |
| 244 | + * @throws NoSuchEntityException . |
| 245 | + */ |
| 246 | + private function removeGalleryItem($product, $url, $removeAllGallery) |
| 247 | + { |
| 248 | + $unlinked = 0; |
| 249 | + if ($product) { |
| 250 | + $mediaGalleryEntries = $product->getMediaGalleryEntries(); |
| 251 | + |
| 252 | + if (is_array($mediaGalleryEntries)) { |
| 253 | + foreach ($mediaGalleryEntries as $key => $entry) { |
| 254 | + $image = $entry->getFile(); |
| 255 | + $url = preg_replace('/\?.*/', '', $url); |
| 256 | + $image = $this->storeManager->getStore()->getBaseUrl() . 'pub/media/catalog/product' . $image; |
| 257 | + $basename = basename($image); |
| 258 | + $ext = pathinfo($image, PATHINFO_EXTENSION); |
| 259 | + // removing unique id from original filename |
| 260 | + $pattern = '/^' . $this->configuration::CLD_UNIQID_PREFIX . '.*?_/'; |
| 261 | + $basename = preg_replace($pattern, '', $basename); |
| 262 | + $filename = basename($url); |
| 263 | + $filename = preg_replace($pattern, '', $filename); |
| 264 | + // $filename = $url . '.' . $ext; |
| 265 | + |
| 266 | + if ($basename == $filename || $removeAllGallery) { |
| 267 | + unset($mediaGalleryEntries[$key]); |
| 268 | + $this->mediaGalleryProcessor->removeImage($product, $image); |
| 269 | + $unlinked++; |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + if ($unlinked) { |
| 274 | + $product->setMediaGalleryEntries($mediaGalleryEntries); |
| 275 | + try { |
| 276 | + $product = $this->productRepository->save($product); |
| 277 | + } catch (\Exception $e) { |
| 278 | + $message = ['type' => 'error', 'message' => 'Falied Delete Image Error: ' . $e->getMessage() . ' line ' . $e->getLine()]; |
| 279 | + } |
| 280 | + } |
| 281 | + return $unlinked; |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * {@inheritdoc} |
| 287 | + */ |
| 288 | + public function removeProductMedia($sku, $urls, $delete_all_gallery = 0) |
| 289 | + { |
| 290 | + $result = [ |
| 291 | + "passed" => 0, |
| 292 | + "failed" => [ |
| 293 | + "count" => 0, |
| 294 | + "urls" => [] |
| 295 | + ] |
| 296 | + ]; |
| 297 | + |
| 298 | + $urls = (array) $urls; |
| 299 | + |
| 300 | + try { |
| 301 | + $product = $this->productRepository->get($sku); |
| 302 | + $this->checkEnvHeader(); |
| 303 | + $this->checkEnabled(); |
| 304 | + |
| 305 | + foreach ($urls as $i => $url) { |
| 306 | + $unlinked = $this->removeGalleryItem($product, $url, $delete_all_gallery); |
| 307 | + } |
| 308 | + |
| 309 | + $result["passed"] = $unlinked; |
| 310 | + } catch (\Exception $e) { |
| 311 | + $result["failed"]["count"]++; |
| 312 | + $result["error"] = $e->getMessage(); |
| 313 | + $result["failed"]["public_id"][] = $url; |
| 314 | + } |
| 315 | + |
| 316 | + return $this->jsonHelper->jsonEncode($result); |
| 317 | + } |
| 318 | + |
236 | 319 | /**
|
237 | 320 | * {@inheritdoc}
|
238 | 321 | */
|
@@ -487,6 +570,7 @@ public function addGalleryItem($url, $sku, $publicId = null, $roles = null, $lab
|
487 | 570 | );
|
488 | 571 |
|
489 | 572 | $mediaGalleryData = $product->getMediaGallery();
|
| 573 | + // last gallery value from array |
490 | 574 | $galItem = array_pop($mediaGalleryData["images"]);
|
491 | 575 |
|
492 | 576 | if ($this->parsedRemoteFileUrl["type"] === "video") {
|
@@ -812,4 +896,6 @@ private function emulateAdminhtmlArea($force = true)
|
812 | 896 | $this->startEnvironmentEmulation(0, Area::AREA_ADMINHTML, $force);
|
813 | 897 | return $this;
|
814 | 898 | }
|
| 899 | + |
| 900 | + |
815 | 901 | }
|
0 commit comments