Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Block/Adminhtml/System/Config/AutoUploadMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Cloudinary\Cloudinary\Block\Adminhtml\System\Config;

use Cloudinary\Cloudinary\Core\ConfigurationInterface;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

class AutoUploadMapping extends Field
{
/**
* @var string
*/
protected $_template = 'Cloudinary_Cloudinary::config/auto-upload-mapping-btn.phtml';

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @method __construct
* @param Context $context
* @param ConfigurationInterface $configuration
* @param array $data
*/
public function __construct(
Context $context,
ConfigurationInterface $configuration,
array $data = []
) {
$this->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();
}
}
130 changes: 130 additions & 0 deletions Controller/Adminhtml/Ajax/System/Config/AutoUploadMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Cloudinary\Cloudinary\Controller\Adminhtml\Ajax\System\Config;

use Cloudinary\Cloudinary\Core\AutoUploadMapping\RequestProcessor;
use Cloudinary\Cloudinary\Model\Configuration;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Message\ManagerInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AutoUploadMapping extends Action
{
const AUTO_UPLOAD_SETUP_FAIL_MESSAGE = 'Error. Unable to setup auto upload mapping.';

/**
* @var JsonFactory
*/
private $jsonResultFactory;

/**
* @var RequestProcessor
*/
protected $requestProcessor;

/**
* @var ManagerInterface
*/
protected $messageManager;

/**
* @var Configuration
*/
protected $configuration;

/**
* Application config
*
* @var ScopeConfigInterface
*/
protected $appConfig;

/**
* @var TypeListInterface
*/
protected $cacheTypeList;

/**
* @method __construct
* @param Context $context
* @param JsonFactory $jsonResultFactory
* @param RequestProcessor $requestProcessor
* @param ManagerInterface $messageManager
* @param Configuration $configuration
* @param TypeListInterface $cacheTypeList
* @param ReinitableConfigInterface $config
*/
public function __construct(
Context $context,
JsonFactory $jsonResultFactory,
RequestProcessor $requestProcessor,
ManagerInterface $messageManager,
Configuration $configuration,
TypeListInterface $cacheTypeList,
ReinitableConfigInterface $config
) {
parent::__construct($context);
$this->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);
}
}
}
104 changes: 0 additions & 104 deletions Model/Observer/Configuration.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions etc/adminhtml/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@
<observer name="delete_images_from_cloudinary" instance="Cloudinary\Cloudinary\Model\Observer\DeleteProductImage" />
</event>

<event name="admin_system_config_changed_section_cloudinary">
<observer name="cloudinary_configuration_observer" instance="Cloudinary\Cloudinary\Model\Observer\Configuration" />
</event>

</config>
9 changes: 8 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@
</field>
<field id="cloudinary_auto_upload_mapping_request" translate="label comment" type="select" sortOrder="3" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Enable auto-upload</label>
<comment>Automatically upload your existing Magento images to your Cloudinary account when an image is requested by a user.</comment>
<comment>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.</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="cloudinary_auto_upload_mapping_button" translate="label comment" type="button" sortOrder="5" showInDefault="1" showInWebsite="0" showInStore="0">
<frontend_model>Cloudinary\Cloudinary\Block\Adminhtml\System\Config\AutoUploadMapping</frontend_model>
<depends>
<field id="cloudinary_enabled">1</field>
<field id="cloudinary_auto_upload_mapping_request">1</field>
</depends>
</field>
</group>
<group id="transformations" translate="label" sortOrder="4" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Default Image Transformations</label>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Cloudinary_Cloudinary" setup_version="1.15.2">
<module name="Cloudinary_Cloudinary" setup_version="1.16.0">
<sequence>
<module name="Magento_ProductVideo"/>
<module name="Magento_PageBuilder"/>
Expand Down
2 changes: 1 addition & 1 deletion marketplace.composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading