diff --git a/Block/Adminhtml/System/Config/AutoUploadMapping.php b/Block/Adminhtml/System/Config/AutoUploadMapping.php new file mode 100644 index 00000000..55837d21 --- /dev/null +++ b/Block/Adminhtml/System/Config/AutoUploadMapping.php @@ -0,0 +1,97 @@ +configuration = $configuration; + parent::__construct($context, $data); + } + + /** + * Remove scope label + * + * @param AbstractElement $element + * @return string + */ + public function render(AbstractElement $element) + { + $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); + return parent::render($element); + } + + /** + * Return element html + * + * @param AbstractElement $element + * @return string + */ + protected function _getElementHtml(AbstractElement $element) + { + return $this->_toHtml(); + } + + /** + * Return ajax url for collect button + * + * @return string + */ + public function getAjaxUrl() + { + return $this->getUrl('cloudinary/ajax_system_config/autoUploadMapping'); + } + + /** + * @return bool + */ + public function isEnabled() + { + return $this->configuration->isEnabled(); + } + + /** + * Generate collect button html + * + * @return string + */ + public function getButtonHtml() + { + $button = $this->getLayout()->createBlock( + 'Magento\Backend\Block\Widget\Button' + )->setData( + [ + 'id' => 'auto-upload-mapping-btn', + 'label' => __('Map media directory'), + 'disabled' => !$this->configuration->isEnabled(), + ] + ); + + return $button->toHtml(); + } +} diff --git a/Controller/Adminhtml/Ajax/System/Config/AutoUploadMapping.php b/Controller/Adminhtml/Ajax/System/Config/AutoUploadMapping.php new file mode 100644 index 00000000..4512f8a1 --- /dev/null +++ b/Controller/Adminhtml/Ajax/System/Config/AutoUploadMapping.php @@ -0,0 +1,130 @@ +jsonResultFactory = $jsonResultFactory; + $this->requestProcessor = $requestProcessor; + $this->messageManager = $messageManager; + $this->configuration = $configuration; + $this->cacheTypeList = $cacheTypeList; + $this->appConfig = $config; + } + + /** + * @return ResultInterface + */ + public function execute() + { + try { + $this->validateAjaxRequest(); + $this->cleanConfigCache(); + + if ($this->configuration->isEnabled()) { + if (!$this->requestProcessor->handle(DirectoryList::MEDIA, $this->configuration->getMediaBaseUrl(), true)) { + throw new \Exception(self::AUTO_UPLOAD_SETUP_FAIL_MESSAGE); + } + } + } catch (\Exception $e) { + return $this->jsonResultFactory->create() + ->setHttpResponseCode(500) + ->setData(['error' => 1, 'message' => "ERROR during the mapping process: " . $e->getMessage(), 'errorcode' => $e->getCode()]); + } + + return $this->jsonResultFactory->create() + ->setHttpResponseCode(\Magento\Framework\Webapi\Response::HTTP_OK) + ->setData(['error' => 0, 'message' => 'Successfully mapped media directory!']); + } + + protected function cleanConfigCache() + { + $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); + $this->appConfig->reinit(); + return $this; + } + + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Cloudinary_Cloudinary::config_cloudinary'); + } + + /** + * @throws \Exception + */ + private function validateAjaxRequest() + { + if (!$this->getRequest()->isAjax()) { + throw new \Exception(self::NON_AJAX_REQUEST); + } + } +} diff --git a/Model/Observer/Configuration.php b/Model/Observer/Configuration.php deleted file mode 100644 index 9164d19c..00000000 --- a/Model/Observer/Configuration.php +++ /dev/null @@ -1,104 +0,0 @@ -requestProcessor = $requestProcessor; - $this->messageManager = $messageManager; - $this->configuration = $configuration; - $this->cacheTypeList = $cacheTypeList; - $this->appConfig = $config; - } - - /** - * @param Observer $observer - */ - public function execute(Observer $observer) - { - //Clear config cache if needed - $this->changedPaths = (array) $observer->getEvent()->getChangedPaths(); - if (count( - array_intersect( - $this->changedPaths, - [ - \Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENABLED, - \Cloudinary\Cloudinary\Model\Configuration::CONFIG_PATH_ENVIRONMENT_VARIABLE, - \Cloudinary\Cloudinary\Model\AutoUploadMapping\AutoUploadConfiguration::REQUEST_PATH - ] - ) - ) > 0 - ) { - $this->cleanConfigCache(); - } - - if (!$this->configuration->isEnabled()) { - return $this; - } - - if (!$this->requestProcessor->handle(DirectoryList::MEDIA, $this->configuration->getMediaBaseUrl(), true)) { - $this->messageManager->addErrorMessage(self::AUTO_UPLOAD_SETUP_FAIL_MESSAGE); - } - } - - protected function cleanConfigCache() - { - $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); - $this->appConfig->reinit(); - return $this; - } -} diff --git a/composer.json b/composer.json index d6cf5314..1cf35def 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.15.2", + "version": "1.16.0", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0" diff --git a/etc/adminhtml/events.xml b/etc/adminhtml/events.xml index a9c2ee94..748cd58a 100644 --- a/etc/adminhtml/events.xml +++ b/etc/adminhtml/events.xml @@ -14,8 +14,4 @@ - - - - diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 3cf4f357..af50f581 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -42,9 +42,16 @@ - Automatically upload your existing Magento images to your Cloudinary account when an image is requested by a user. + Automatically upload your existing Magento images to your Cloudinary account when an image is requested by a user. Once enabled, click the button below to map your Magento media directory to your Cloudinary account. Magento\Config\Model\Config\Source\Yesno + + Cloudinary\Cloudinary\Block\Adminhtml\System\Config\AutoUploadMapping + + 1 + 1 + + diff --git a/etc/module.xml b/etc/module.xml index 73ad1044..2b436328 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/marketplace.composer.json b/marketplace.composer.json index 2909bdbe..a9c687e3 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.15.2", + "version": "1.16.0", "license": "MIT", "require": { "cloudinary/cloudinary_php": "^1.20.0" diff --git a/view/adminhtml/templates/config/auto-upload-mapping-btn.phtml b/view/adminhtml/templates/config/auto-upload-mapping-btn.phtml new file mode 100644 index 00000000..b7a8551a --- /dev/null +++ b/view/adminhtml/templates/config/auto-upload-mapping-btn.phtml @@ -0,0 +1,43 @@ + +getButtonHtml() ?> +
+ +isEnabled()): ?> + + +

+