-
Notifications
You must be signed in to change notification settings - Fork 91
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 #52 from diazwatson/29_UserConfig
#29 User configures Adobe Stock integration
- Loading branch information
Showing
11 changed files
with
291 additions
and
10 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
AdobeStockAsset/Controller/Adminhtml/System/Config/TestConnection.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,90 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\AdobeStockAsset\Controller\Adminhtml\System\Config; | ||
|
||
use Magento\AdobeStockAssetApi\Api\ClientInterface; | ||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\Controller\Result\Json; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use Magento\Framework\Controller\ResultInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Filter\StripTags; | ||
|
||
class TestConnection extends Action | ||
{ | ||
/** | ||
* Authorization level of a basic admin session. | ||
* | ||
* @see _isAllowed() | ||
*/ | ||
const ADMIN_RESOURCE = 'Magento_AdobeStockAsset::config'; | ||
|
||
/** | ||
* @var JsonFactory | ||
*/ | ||
private $resultJsonFactory; | ||
|
||
/** | ||
* @var StripTags | ||
*/ | ||
private $tagFilter; | ||
|
||
/** | ||
* @var ClientInterface | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* TestConnection constructor. | ||
* | ||
* @param Context $context | ||
* @param ClientInterface $client | ||
* @param JsonFactory $resultJsonFactory | ||
* @param StripTags $tagFilter | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
ClientInterface $client, | ||
JsonFactory $resultJsonFactory, | ||
StripTags $tagFilter | ||
) { | ||
parent::__construct($context); | ||
$this->resultJsonFactory = $resultJsonFactory; | ||
$this->tagFilter = $tagFilter; | ||
$this->client = $client; | ||
} | ||
|
||
/** | ||
* Check for connection to server | ||
* | ||
* @return ResultInterface|ResponseInterface | ||
*/ | ||
public function execute() | ||
{ | ||
$result = [ | ||
'success' => false, | ||
'errorMessage' => '', | ||
]; | ||
|
||
try { | ||
if (!$this->client->testConnection()) { | ||
throw new LocalizedException(__('Invalid API Key.')); | ||
} | ||
$result['success'] = true; | ||
} catch (\Exception $e) { | ||
$message = __('Invalid API Key.'); | ||
$result['errorMessage'] = $this->tagFilter->filter($message); | ||
} | ||
|
||
/** @var Json $resultJson */ | ||
$resultJson = $this->resultJsonFactory->create(); | ||
|
||
return $resultJson->setData($result); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
AdobeStockAsset/Model/Block/Adminhtml/System/Config/TestConnection.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,49 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\AdobeStockAsset\Model\Block\Adminhtml\System\Config; | ||
|
||
use Magento\Config\Block\System\Config\Form\Field; | ||
use Magento\Framework\Data\Form\Element\AbstractElement; | ||
|
||
/** | ||
* Adobe Stock test connection block | ||
*/ | ||
class TestConnection extends Field | ||
{ | ||
|
||
/** | ||
* Set template to itself | ||
* | ||
* @return TestConnection | ||
*/ | ||
protected function _prepareLayout() | ||
{ | ||
parent::_prepareLayout(); | ||
$this->setTemplate('Magento_AdobeStockAsset::system/config/testconnection.phtml'); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get the button and scripts contents | ||
* | ||
* @param AbstractElement $element | ||
* @return string | ||
*/ | ||
protected function _getElementHtml(AbstractElement $element) | ||
{ | ||
$originalData = $element->getOriginalData(); | ||
$this->addData( | ||
[ | ||
'button_label' => __($originalData['button_label']), | ||
'html_id' => $element->getHtmlId(), | ||
'ajax_url' => $this->_urlBuilder->getUrl('adobe_stock/system_config/testconnection') . 'form_key/' . $this->getFormKey(), | ||
] | ||
); | ||
|
||
return $this->_toHtml(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd"> | ||
<acl> | ||
<resources> | ||
<resource id="Magento_Backend::admin"> | ||
<resource id="Magento_Backend::stores"> | ||
<resource id="Magento_Backend::stores_settings"> | ||
<resource id="Magento_Config::config"> | ||
<resource id="Magento_AdobeStockAsset::config" title="Adobe Stock Section" translate="title"/> | ||
</resource> | ||
</resource> | ||
</resource> | ||
</resource> | ||
</resources> | ||
</acl> | ||
</config> |
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,14 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> | ||
<router id="admin"> | ||
<route id="adobe_stock" frontName="adobe_stock"> | ||
<module name="Magento_AdobeStockAsset" before="Magento_Backend" /> | ||
</route> | ||
</router> | ||
</config> |
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,37 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | ||
<system> | ||
<section id="system"> | ||
<group id="adobe_stock_integration" translate="label" type="text" sortOrder="1100" showInDefault="1" showInWebsite="0" showInStore="0"> | ||
<label>Adobe Stock Integration</label> | ||
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0"> | ||
<label>Enabled Adobe Stock</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
<config_path>adobe_stock/integration/enabled</config_path> | ||
</field> | ||
<field id="api_key" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0"> | ||
<label>Api Key</label> | ||
<config_path>adobe_stock/integration/api_key</config_path> | ||
<validate>required-entry</validate> | ||
<depends> | ||
<field id="enabled">1</field> | ||
</depends> | ||
</field> | ||
<field id="adobe_stock_test_connect_wizard" translate="button_label" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0"> | ||
<label/> | ||
<button_label>Test Connection</button_label> | ||
<frontend_model>Magento\AdobeStockAsset\Model\Block\Adminhtml\System\Config\TestConnection</frontend_model> | ||
<depends> | ||
<field id="enabled">1</field> | ||
</depends> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</config> |
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
22 changes: 22 additions & 0 deletions
22
AdobeStockAsset/view/adminhtml/templates/system/config/testconnection.phtml
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,22 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
/** @var $block \Magento\AdobeStockImageAdminUi\Model\Block\Adminhtml\System\Config\TestConnection */ | ||
?> | ||
<button class="scalable" type="button" id="<?= $block->escapeUrl($block->getData('html_id')) ?>" data-mage-init='{"testConnection":{ | ||
"url": "<?= /* @escapeNotVerified */ $block->getData('ajax_url') ?>", | ||
"elementId": "<?= $block->escapeHtmlAttr($block->getData('html_id')) ?>", | ||
"successText": "<?= $block->escapeHtmlAttr(__('Successful! Test again?')) ?>", | ||
"fieldToCheck": "success", | ||
"failedText": "<?= $block->escapeHtmlAttr(__('Connection failed! Test again?')) ?>", "validation": {}}}'> | ||
<span> | ||
<span> | ||
<span id="<?= $block->escapeHtmlAttr($block->getData('html_id')) ?>_result"> | ||
<?= $block->escapeHtml($block->getData('button_label')) ?> | ||
</span> | ||
</span> | ||
</span> | ||
</button> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd"> | ||
<acl> | ||
<resources> | ||
<resource id="Magento_Backend::admin"> | ||
<resource id="Magento_AdobeStockImage::actions_view" title="View" translate="title" /> | ||
</resource> | ||
</resources> | ||
</acl> | ||
</config> |
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