diff --git a/Api/ProductGalleryManagementInterface.php b/Api/ProductGalleryManagementInterface.php index 24ae886..3fc70d2 100644 --- a/Api/ProductGalleryManagementInterface.php +++ b/Api/ProductGalleryManagementInterface.php @@ -36,4 +36,20 @@ public function addItems($items); * @return string */ public function addProductMedia($sku, $urls); + + /** + * Get product gallery items as Cloudinary URLs. + * @method getProductMedia + * @param string $sku + * @return string + */ + public function getProductMedia($sku); + + /** + * Get products gallery items as Cloudinary URLs. + * @method getProductsMedia + * @param mixed $skus + * @return string + */ + public function getProductsMedia($skus); } diff --git a/Model/Api/ProductGalleryManagement.php b/Model/Api/ProductGalleryManagement.php index b8bc606..7bb673a 100644 --- a/Model/Api/ProductGalleryManagement.php +++ b/Model/Api/ProductGalleryManagement.php @@ -247,11 +247,7 @@ public function addProductMedia($sku, $urls) ]; try { $this->checkEnvHeader(); - if (!$this->configuration->isEnabled()) { - throw new LocalizedException( - __("Cloudinary module is disabled. Please enable it first in order to use this API.") - ); - } + $this->checkEnabled(); $urls = (array)$urls; foreach ($urls as $i => $url) { try { @@ -284,6 +280,50 @@ public function addProductMedia($sku, $urls) return $this->jsonHelper->jsonEncode($result); } + /** + * {@inheritdoc} + */ + public function getProductMedia($sku) + { + return $this->_getProductMedia($sku); + } + + /** + * {@inheritdoc} + */ + public function getProductsMedia($skus) + { + return $this->_getProductMedia($skus); + } + + /** + * [_getProductMedia description] + * @method _getProductMedia + * @param mixed $sku + * @return string (json result) + */ + private function _getProductMedia($sku) + { + $result = ["data" => []]; + + try { + $this->checkEnvHeader(); + $this->checkEnabled(); + if (is_array($sku) || is_object($sku)) { + foreach ($sku as $key => $_sku) { + $result['data'][$_sku] = $this->getProductCldUrlsBySku($_sku); + } + } else { + $result['data'] = $this->getProductCldUrlsBySku($sku); + } + } catch (\Exception $e) { + $result["error"] = 1; + $result["message"] = $e->getMessage(); + } + + return $this->jsonHelper->jsonEncode($result); + } + /** * {@inheritdoc} */ @@ -312,11 +352,7 @@ public function addItems($items) ]; try { $this->checkEnvHeader(); - if (!$this->configuration->isEnabled()) { - throw new LocalizedException( - __("Cloudinary module is disabled. Please enable it first in order to use this API.") - ); - } + $this->checkEnabled(); $items = (array)$items; foreach ($items as $i => $item) { try { @@ -568,6 +604,20 @@ private function checkEnvHeader() return $this; } + /** + * @method checkEnabled + * @return $this + */ + private function checkEnabled() + { + if (!$this->configuration->isEnabled()) { + throw new LocalizedException( + __("Cloudinary module is disabled. Please enable it first in order to use this API.") + ); + } + return $this; + } + private function getLocalTmpFileName($remoteFileUrl) { $localFileName = Uploader::getCorrectFileName(basename($remoteFileUrl)); @@ -682,6 +732,42 @@ private function saveCloudinaryMapping() ->save(); } + /** + * @method getProductMediaBySku + * @param string $sku + * @return array + */ + private function getProductCldUrlsBySku($sku) + { + $urls = [ + 'image' => null, + 'small_image' => null, + 'thumbnail' => null, + 'media_gallery' => [], + ]; + try { + $product = $this->productRepository->get($sku); + foreach ($product->getMediaGalleryImages() as $gallItem) { + $urls['media_gallery'][] = $gallItem->getUrl(); + if ($product->getData('image') === $gallItem->getFile()) { + $urls['image'] = $gallItem->getUrl(); + } + if ($product->getData('small_image') === $gallItem->getFile()) { + $urls['small_image'] = $gallItem->getUrl(); + } + if ($product->getData('thumbnail') === $gallItem->getFile()) { + $urls['thumbnail'] = $gallItem->getUrl(); + } + } + } catch (\Exception $e) { + $urls = [ + 'error' => 1, + 'message' => $e->getMessage(), + ]; + } + return $urls; + } + /////////////////////////////// // App Environment Emulation // /////////////////////////////// diff --git a/composer.json b/composer.json index 72a2733..945c01d 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "cloudinary/cloudinary-magento2", "description": "Cloudinary Magento 2 Integration.", "type": "magento2-module", - "version": "1.14.12", + "version": "1.14.13", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0" diff --git a/etc/module.xml b/etc/module.xml index 4311dda..1ebb8ab 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/etc/webapi.xml b/etc/webapi.xml index d3b782b..afc1e3b 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -47,6 +47,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/marketplace.composer.json b/marketplace.composer.json index 9f2fac3..f42ef50 100644 --- a/marketplace.composer.json +++ b/marketplace.composer.json @@ -2,7 +2,7 @@ "name": "cloudinary/cloudinary", "description": "Cloudinary Magento 2 Integration.", "type": "magento2-module", - "version": "1.14.12", + "version": "1.14.13", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0"