-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from cloudinary/v1.16.0
V1.16.0
- Loading branch information
Showing
9 changed files
with
281 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
130
Controller/Adminhtml/Ajax/System/Config/AutoUploadMapping.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.