From 1c53997b1ef0436727a41ff522b9ca581f6522fa Mon Sep 17 00:00:00 2001 From: Francesco Zerbinati Date: Wed, 16 Feb 2022 14:47:25 +0100 Subject: [PATCH 1/4] working graphql, broken rest --- Api/ProductGalleryManagementInterface.php | 2 +- Model/Api/ProductGalleryManagement.php | 32 +++++++++++--- .../ProductAttributeCldDataResolver.php | 42 +++++++++++++++++++ .../ProductAttributeCldResolver.php | 2 +- etc/schema.graphqls | 29 +++++++++++++ 5 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 Model/GraphQLResolver/ProductAttributeCldDataResolver.php diff --git a/Api/ProductGalleryManagementInterface.php b/Api/ProductGalleryManagementInterface.php index 3fc70d2..0b0f62b 100644 --- a/Api/ProductGalleryManagementInterface.php +++ b/Api/ProductGalleryManagementInterface.php @@ -43,7 +43,7 @@ public function addProductMedia($sku, $urls); * @param string $sku * @return string */ - public function getProductMedia($sku); + public function getProductMedia($sku, $onlyUrls = true); /** * Get products gallery items as Cloudinary URLs. diff --git a/Model/Api/ProductGalleryManagement.php b/Model/Api/ProductGalleryManagement.php index 7bb673a..eaae786 100644 --- a/Model/Api/ProductGalleryManagement.php +++ b/Model/Api/ProductGalleryManagement.php @@ -283,9 +283,9 @@ public function addProductMedia($sku, $urls) /** * {@inheritdoc} */ - public function getProductMedia($sku) + public function getProductMedia($sku, $onlyUrls = true) { - return $this->_getProductMedia($sku); + return $this->_getProductMedia($sku, $onlyUrls); } /** @@ -302,7 +302,7 @@ public function getProductsMedia($skus) * @param mixed $sku * @return string (json result) */ - private function _getProductMedia($sku) + private function _getProductMedia($sku, $onlyUrls = true) { $result = ["data" => []]; @@ -311,10 +311,10 @@ private function _getProductMedia($sku) $this->checkEnabled(); if (is_array($sku) || is_object($sku)) { foreach ($sku as $key => $_sku) { - $result['data'][$_sku] = $this->getProductCldUrlsBySku($_sku); + $result['data'][$_sku] = ($onlyUrls ? $this->getProductCldUrlsBySku($_sku) : $this->getProductCldDataBySku($_sku)); } } else { - $result['data'] = $this->getProductCldUrlsBySku($sku); + $result['data'] = ($onlyUrls ? $this->getProductCldUrlsBySku($sku) : $this->getProductCldDataBySku($sku)); } } catch (\Exception $e) { $result["error"] = 1; @@ -768,6 +768,28 @@ private function getProductCldUrlsBySku($sku) return $urls; } + /** + * @method getProductCldDataBySku + * @param string $sku + * @return array + */ + private function getProductCldDataBySku($sku) + { + $data = []; + try { + $product = $this->productRepository->get($sku); + foreach ($product->getMediaGalleryImages() as $gallItem) { + array_push($data, $gallItem->getData()); + } + } catch (\Exception $e) { + $data = [ + 'error' => 1, + 'message' => $e->getMessage() + ]; + } + return $data; + } + /////////////////////////////// // App Environment Emulation // /////////////////////////////// diff --git a/Model/GraphQLResolver/ProductAttributeCldDataResolver.php b/Model/GraphQLResolver/ProductAttributeCldDataResolver.php new file mode 100644 index 0000000..0a48445 --- /dev/null +++ b/Model/GraphQLResolver/ProductAttributeCldDataResolver.php @@ -0,0 +1,42 @@ +productGalleryManagement = $productGalleryManagement; + } + + /** + * @inheritdoc + */ + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) + { + $productId = $value['sku']; + $productMediaStr = $this->productGalleryManagement->getProductMedia($productId, false); + $jsonDecoder = new \Magento\Framework\Serialize\Serializer\Json(); + $productMedia = $jsonDecoder->unserialize($productMediaStr); + return $productMedia['data']; + } + } \ No newline at end of file diff --git a/Model/GraphQLResolver/ProductAttributeCldResolver.php b/Model/GraphQLResolver/ProductAttributeCldResolver.php index 5520e6f..345dce2 100644 --- a/Model/GraphQLResolver/ProductAttributeCldResolver.php +++ b/Model/GraphQLResolver/ProductAttributeCldResolver.php @@ -34,7 +34,7 @@ public function __construct( public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { $productId = $value['sku']; - $productMediaStr = $this->productGalleryManagement->getProductMedia($productId); + $productMediaStr = $this->productGalleryManagement->getProductMedia($productId, true); $jsonDecoder = new \Magento\Framework\Serialize\Serializer\Json(); $productMedia = $jsonDecoder->unserialize($productMediaStr); return $productMedia['data']; diff --git a/etc/schema.graphqls b/etc/schema.graphqls index 8d2e385..0bb97cc 100644 --- a/etc/schema.graphqls +++ b/etc/schema.graphqls @@ -5,8 +5,37 @@ type CloudinaryData { media_gallery: [String] } +type CloudinaryImage { + disabled: String + disabled_default: String + entity_id: String + file: String + id: String + label: String + label_default: String + media_type: String + path: String + position: String + position_default: String + url: String + value_id: String + video_description: String + video_description_default: String + video_metadata: String + video_metadata_default: String + video_provider: String + video_provider_default: String + video_title: String + video_title_default: String + video_url: String + video_url_default: String +} + interface ProductInterface { cld_data: CloudinaryData @resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldResolver") @doc(description: "Cloudinary urls generated for product images") + cld_data_all: [CloudinaryImage] + @resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldDataResolver") + @doc(description: "Cloudinary urls generated for product images") } \ No newline at end of file From d48be1981da4c62fe76eaabb4a91856a22ade80c Mon Sep 17 00:00:00 2001 From: Francesco Zerbinati Date: Wed, 16 Feb 2022 14:51:57 +0100 Subject: [PATCH 2/4] working rest and graphql --- Api/ProductGalleryManagementInterface.php | 10 +++++++++- Model/Api/ProductGalleryManagement.php | 12 ++++++++++-- .../ProductAttributeCldDataResolver.php | 2 +- .../GraphQLResolver/ProductAttributeCldResolver.php | 2 +- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Api/ProductGalleryManagementInterface.php b/Api/ProductGalleryManagementInterface.php index 0b0f62b..dd20b79 100644 --- a/Api/ProductGalleryManagementInterface.php +++ b/Api/ProductGalleryManagementInterface.php @@ -43,7 +43,15 @@ public function addProductMedia($sku, $urls); * @param string $sku * @return string */ - public function getProductMedia($sku, $onlyUrls = true); + public function getProductMedia($sku); + + /** + * Get product gallery items. + * @method getProductMedia + * @param string $sku + * @return string + */ + public function getProductMediaData($sku); /** * Get products gallery items as Cloudinary URLs. diff --git a/Model/Api/ProductGalleryManagement.php b/Model/Api/ProductGalleryManagement.php index eaae786..ed61466 100644 --- a/Model/Api/ProductGalleryManagement.php +++ b/Model/Api/ProductGalleryManagement.php @@ -283,9 +283,17 @@ public function addProductMedia($sku, $urls) /** * {@inheritdoc} */ - public function getProductMedia($sku, $onlyUrls = true) + public function getProductMedia($sku) { - return $this->_getProductMedia($sku, $onlyUrls); + return $this->_getProductMedia($sku, true); + } + + /** + * {@inheritdoc} + */ + public function getProductMediaData($sku) + { + return $this->_getProductMedia($sku, false); } /** diff --git a/Model/GraphQLResolver/ProductAttributeCldDataResolver.php b/Model/GraphQLResolver/ProductAttributeCldDataResolver.php index 0a48445..23eaf47 100644 --- a/Model/GraphQLResolver/ProductAttributeCldDataResolver.php +++ b/Model/GraphQLResolver/ProductAttributeCldDataResolver.php @@ -34,7 +34,7 @@ public function __construct( public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { $productId = $value['sku']; - $productMediaStr = $this->productGalleryManagement->getProductMedia($productId, false); + $productMediaStr = $this->productGalleryManagement->getProductMediaData($productId); $jsonDecoder = new \Magento\Framework\Serialize\Serializer\Json(); $productMedia = $jsonDecoder->unserialize($productMediaStr); return $productMedia['data']; diff --git a/Model/GraphQLResolver/ProductAttributeCldResolver.php b/Model/GraphQLResolver/ProductAttributeCldResolver.php index 345dce2..5520e6f 100644 --- a/Model/GraphQLResolver/ProductAttributeCldResolver.php +++ b/Model/GraphQLResolver/ProductAttributeCldResolver.php @@ -34,7 +34,7 @@ public function __construct( public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { $productId = $value['sku']; - $productMediaStr = $this->productGalleryManagement->getProductMedia($productId, true); + $productMediaStr = $this->productGalleryManagement->getProductMedia($productId); $jsonDecoder = new \Magento\Framework\Serialize\Serializer\Json(); $productMedia = $jsonDecoder->unserialize($productMediaStr); return $productMedia['data']; From 13173ba08950a97b6a472d24d19bf01ad4589e3b Mon Sep 17 00:00:00 2001 From: Francesco Zerbinati Date: Wed, 16 Feb 2022 15:17:54 +0100 Subject: [PATCH 3/4] working cloudinary images --- Model/Api/ProductGalleryManagement.php | 10 +++++++++- etc/schema.graphqls | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Model/Api/ProductGalleryManagement.php b/Model/Api/ProductGalleryManagement.php index ed61466..aad0d1e 100644 --- a/Model/Api/ProductGalleryManagement.php +++ b/Model/Api/ProductGalleryManagement.php @@ -786,8 +786,16 @@ private function getProductCldDataBySku($sku) $data = []; try { $product = $this->productRepository->get($sku); + $attributes = $product->getAttributes(); foreach ($product->getMediaGalleryImages() as $gallItem) { - array_push($data, $gallItem->getData()); + foreach ($attributes as $attribute) { + $attrCode = $attribute->getAttributeCode(); + if($product->getData($attrCode) === $gallItem->getFile()) { + $gallItemData = $gallItem->getData(); + $gallItemData['role'] = $attrCode; + array_push($data, $gallItemData); + } + } } } catch (\Exception $e) { $data = [ diff --git a/etc/schema.graphqls b/etc/schema.graphqls index 0bb97cc..c68abcf 100644 --- a/etc/schema.graphqls +++ b/etc/schema.graphqls @@ -6,6 +6,7 @@ type CloudinaryData { } type CloudinaryImage { + role: String disabled: String disabled_default: String entity_id: String @@ -35,7 +36,7 @@ interface ProductInterface { cld_data: CloudinaryData @resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldResolver") @doc(description: "Cloudinary urls generated for product images") - cld_data_all: [CloudinaryImage] + cloudinary_images: [CloudinaryImage] @resolver(class: "\\Cloudinary\\Cloudinary\\Model\\GraphQLResolver\\ProductAttributeCldDataResolver") @doc(description: "Cloudinary urls generated for product images") } \ No newline at end of file From 2d45dd843df5ddf51485789c4f515237a6f326c6 Mon Sep 17 00:00:00 2001 From: Francesco Zerbinati Date: Wed, 16 Feb 2022 15:39:10 +0100 Subject: [PATCH 4/4] fix get all images --- Model/Api/ProductGalleryManagement.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/Api/ProductGalleryManagement.php b/Model/Api/ProductGalleryManagement.php index aad0d1e..23bdf84 100644 --- a/Model/Api/ProductGalleryManagement.php +++ b/Model/Api/ProductGalleryManagement.php @@ -788,14 +788,14 @@ private function getProductCldDataBySku($sku) $product = $this->productRepository->get($sku); $attributes = $product->getAttributes(); foreach ($product->getMediaGalleryImages() as $gallItem) { + $gallItemData = $gallItem->getData(); foreach ($attributes as $attribute) { $attrCode = $attribute->getAttributeCode(); if($product->getData($attrCode) === $gallItem->getFile()) { - $gallItemData = $gallItem->getData(); $gallItemData['role'] = $attrCode; - array_push($data, $gallItemData); } } + array_push($data, $gallItemData); } } catch (\Exception $e) { $data = [