Skip to content

Commit 1cf142a

Browse files
authored
Merge pull request #62 from magento-obsessive-owls/cms-team-2-sprint-8
[CMS Team 2 - Sprint 8] Maps API and Product Bug
2 parents 7afcd5b + 003565d commit 1cf142a

31 files changed

+1203
-57
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Block\Adminhtml\System\Config\Form\Field;
9+
10+
class GoogleMapsApiKey extends \Magento\Config\Block\System\Config\Form\Field
11+
{
12+
/**
13+
* @inheritdoc
14+
*/
15+
protected function _prepareLayout()
16+
{
17+
parent::_prepareLayout();
18+
$this->setTemplate('Magento_PageBuilder::system/config/google_maps_api_key.phtml');
19+
return $this;
20+
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
26+
{
27+
$element = clone $element;
28+
$element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
29+
return parent::render($element);
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
36+
{
37+
$originalData = $element->getOriginalData();
38+
$this->addData(
39+
[
40+
'button_label' => __($originalData['button_label']),
41+
'valid_label' => __($originalData['valid_label']),
42+
'invalid_label' => __($originalData['invalid_label']),
43+
'source_field' => $originalData['source_field'],
44+
'html_id' => $element->getHtmlId(),
45+
'validate_url' => $this->_urlBuilder->getUrl('pagebuilder/googlemaps/validateapi')
46+
]
47+
);
48+
49+
return $this->_toHtml();
50+
}
51+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Block;
9+
10+
/**
11+
* @api
12+
*/
13+
class GoogleMapsApiBlock extends \Magento\Framework\View\Element\Template
14+
{
15+
const GOOGLE_MAPS_API_KEY_PATH = 'cms/pagebuilder/google_maps_api_key';
16+
const GOOGLE_MAPS_LIBRARY_URL = 'https://maps.googleapis.com/maps/api/js?v=3&key=%s';
17+
18+
/**
19+
* Generate URL for retrieving Google Maps Javascript API
20+
*
21+
* @return string
22+
*/
23+
public function getGoogleMapsApiPath(): string
24+
{
25+
$apiKey = $this->_scopeConfig->getValue(self::GOOGLE_MAPS_API_KEY_PATH);
26+
$libraryUrlWithKey = sprintf(self::GOOGLE_MAPS_LIBRARY_URL, $apiKey);
27+
return $libraryUrlWithKey;
28+
}
29+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Component;
10+
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\UrlInterface;
13+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
14+
use Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator;
15+
16+
class GoogleMapsApiKeyValidationContainer extends \Magento\Ui\Component\Container
17+
{
18+
const GOOGLE_MAPS_API_KEY_PATH = 'cms/pagebuilder/google_maps_api_key';
19+
20+
/**
21+
* @var UrlInterface
22+
*/
23+
private $url;
24+
25+
/**
26+
* @var ScopeConfigInterface
27+
*/
28+
private $scopeConfig;
29+
30+
/**
31+
* @var ApiKeyValidator
32+
*/
33+
private $apiKeyValidator;
34+
35+
/**
36+
* Constructor
37+
*
38+
* @param ContextInterface $context
39+
* @param UrlInterface $url
40+
* @param ScopeConfigInterface $scopeConfig
41+
* @param ApiKeyValidator $apiKeyValidator
42+
* @param array $components
43+
* @param array $data
44+
*/
45+
public function __construct(
46+
ContextInterface $context,
47+
UrlInterface $url,
48+
ScopeConfigInterface $scopeConfig,
49+
ApiKeyValidator $apiKeyValidator,
50+
array $components = [],
51+
array $data = []
52+
) {
53+
parent::__construct(
54+
$context,
55+
$components,
56+
$data
57+
);
58+
$this->url = $url;
59+
$this->scopeConfig = $scopeConfig;
60+
$this->apiKeyValidator = $apiKeyValidator;
61+
}
62+
63+
/**
64+
* Prepare component configuration
65+
*
66+
* @return void
67+
*/
68+
public function prepare()
69+
{
70+
parent::prepare();
71+
$config = $this->getData('config');
72+
$apiKey = $this->scopeConfig->getValue(self::GOOGLE_MAPS_API_KEY_PATH) ?: "";
73+
$response = $this->apiKeyValidator->validate($apiKey);
74+
if (!$response['success']) {
75+
$config['visible'] = true;
76+
}
77+
78+
if (isset($config['map_configuration_url'])) {
79+
$config['map_configuration_url'] = $this->url->getUrl($config['map_configuration_url']);
80+
}
81+
if (isset($config['content'])) {
82+
$config['content'] = sprintf($config['content'], $config['map_configuration_url']);
83+
}
84+
85+
$this->setData('config', (array) $config);
86+
}
87+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Controller\Adminhtml\GoogleMaps;
9+
10+
use Magento\Framework\Controller\ResultFactory;
11+
12+
class ValidateApi extends \Magento\Backend\App\Action
13+
{
14+
const ADMIN_RESOURCE = 'Magento_Backend::content';
15+
16+
/**
17+
* @var \Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator
18+
*/
19+
private $apiKeyValidator;
20+
21+
/**
22+
* Constructor
23+
*
24+
* @param \Magento\Backend\App\Action\Context $context
25+
* @param \Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator $apiKeyValidator
26+
*/
27+
public function __construct(
28+
\Magento\Backend\App\Action\Context $context,
29+
\Magento\PageBuilder\Model\GoogleMaps\ApiKeyValidator $apiKeyValidator
30+
) {
31+
parent::__construct($context);
32+
$this->apiKeyValidator = $apiKeyValidator;
33+
}
34+
35+
/**
36+
* Send test request to Google Maps and return response
37+
*
38+
* @return \Magento\Framework\Controller\Result\Json
39+
*/
40+
public function execute()
41+
{
42+
$apiKey = $this->getRequest()->getParam('key');
43+
$validationResult = $this->apiKeyValidator->validate($apiKey);
44+
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($validationResult);
45+
}
46+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Model\GoogleMaps;
9+
10+
class ApiKeyValidator
11+
{
12+
const GOOGLE_MAPS_EMBED_URL = 'https://www.google.com/maps/embed/v1/place?key=%s&q=Austin+TX';
13+
14+
/**
15+
* Send test request to Google Maps and return response
16+
*
17+
* @param string $apiKey
18+
* @return array
19+
*/
20+
public function validate(string $apiKey): array
21+
{
22+
$testUrl = sprintf(self::GOOGLE_MAPS_EMBED_URL, $apiKey);
23+
$curl = curl_init($testUrl);
24+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
25+
$result = curl_exec($curl);
26+
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
27+
$returnArray = [
28+
'responseMessage' => $responseCode !== 200 ? $result : '',
29+
'success' => $responseCode === 200 ? true : false
30+
];
31+
32+
return $returnArray;
33+
}
34+
}

app/code/Magento/PageBuilder/etc/adminhtml/system.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
<![CDATA[Includes advanced tools to design page layouts, add Magento content (Product and Category data, CMS content and Blocks), and format text.<br /><br />For Product Attributes — Page Builder can be selected as the “input type” to design layouts and create content.]]>
1919
</comment>
2020
</field>
21+
<field id="google_maps_api_key" translate="label comment" type="text" sortOrder="2010" showInDefault="1" showInWebsite="0" showInStore="0">
22+
<label>Google Maps API Key</label>
23+
<comment>
24+
<![CDATA[Enter API key to use the Map content block. <a href="https://developers.google.com/maps/documentation/javascript/get-api-key" target="_blank">Get API Key</a>.]]>
25+
</comment>
26+
</field>
27+
<field id="google_maps_api_key_validator" translate="button_label valid_label invalid_label" sortOrder="2011" showInDefault="1" showInWebsite="0" showInStore="0">
28+
<attribute type="button_label">Test Key</attribute>
29+
<attribute type="valid_label">Key is valid</attribute>
30+
<attribute type="invalid_label">Key is invalid. Try different key.</attribute>
31+
<attribute type="source_field">cms_pagebuilder_google_maps_api_key</attribute>
32+
<frontend_model>Magento\PageBuilder\Block\Adminhtml\System\Config\Form\Field\GoogleMapsApiKey</frontend_model>
33+
</field>
2134
</group>
2235
</section>
2336
</system>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* @var \Magento\PageBuilder\Block\Adminhtml\System\Config\Form\Field\GoogleMapsApiKey $block
9+
*/
10+
?>
11+
<button class="scalable"
12+
type="button"
13+
id="<?= $block->escapeHtmlAttr($block->getHtmlId()) ?>"
14+
data-mage-init='{
15+
"Magento_PageBuilder/js/system/config/google-maps-api-validator": {
16+
"elementId": "<?= $block->escapeJs($block->getHtmlId()) ?>",
17+
"validateUrl": "<?= $block->escapeJs($block->getValidateUrl()) ?>",
18+
"buttonLabel": "<?= $block->escapeJs($block->getButtonLabel()) ?>",
19+
"validLabel": "<?= $block->escapeJs($block->getValidLabel()) ?>",
20+
"invalidLabel": "<?= $block->escapeJs($block->getInvalidLabel()) ?>",
21+
"sourceField": "<?= $block->escapeJs($block->getSourceField()) ?>"
22+
}
23+
}'
24+
disabled="disabled"
25+
><span class="result"><?= $block->escapeHtml($block->getButtonLabel()) ?></span></button>

app/code/Magento/PageBuilder/view/adminhtml/ui_component/pagebuilder_map_form.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@
6565
</formElements>
6666
</field>
6767
</fieldset>
68-
<fieldset name="general" sortOrder="20">
68+
<container sortOrder="20" name="google_map_api_key_check" component="Magento_Ui/js/form/components/html" class="Magento\PageBuilder\Component\GoogleMapsApiKeyValidationContainer">
69+
<argument name="data" xsi:type="array">
70+
<item name="config" xsi:type="array">
71+
<item name="additionalClasses" xsi:type="string">message message-warning</item>
72+
<item name="visible" xsi:type="boolean">false</item>
73+
<item name="map_configuration_url" xsi:type="string">admin/system_config/edit/section/cms</item>
74+
<item name="content" xsi:type="string" translate="true"><![CDATA[You must provide the <a href="%s">Google Maps API key</a> in order to use the map]]></item>
75+
</item>
76+
</argument>
77+
</container>
78+
<fieldset name="general" sortOrder="30">
6979
<settings>
7080
<label/>
7181
<collapsible>true</collapsible>
@@ -182,7 +192,7 @@
182192
</settings>
183193
</field>
184194
</fieldset>
185-
<fieldset name="map_settings" sortOrder="30">
195+
<fieldset name="map_settings" sortOrder="40">
186196
<settings>
187197
<label translate="true">Map Settings</label>
188198
<collapsible>true</collapsible>

app/code/Magento/PageBuilder/view/adminhtml/web/css/source/_icons.less

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,19 @@
171171
content: @icon-systems__content;
172172
}
173173
}
174+
175+
.icon-admin-pagebuilder-success {
176+
.abs-icon;
177+
&:before {
178+
color: @success__color;
179+
content: @icon-check-mage__content;
180+
}
181+
}
182+
183+
.icon-admin-pagebuilder-error {
184+
.abs-icon;
185+
&:before {
186+
color: @error__color;
187+
content: @icon-error__content;
188+
}
189+
}

app/code/Magento/PageBuilder/view/adminhtml/web/js/form/element/map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
define([
99
'Magento_Ui/js/form/element/abstract',
10-
'https://maps.googleapis.com/maps/api/js?key=AIzaSyCw10cOO31cpxb2bcwnHPHKtxov8oUbxJw'
10+
'googleMaps'
1111
], function (AbstractField) {
1212
'use strict';
1313

0 commit comments

Comments
 (0)