Skip to content

Commit

Permalink
fix wrong container parameter determination, see #150 (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Jun 4, 2021
1 parent 5ac9242 commit 53219e1
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
1 change: 1 addition & 0 deletions UPGRADE.md
Expand Up @@ -11,6 +11,7 @@ Just click the "update" button or execute the migration command to finish the bu
- **[IMPROVEMENT]**: use no-cookie domain for youtube videos [@ghettopro](https://github.com/dachcom-digital/pimcore-toolbox/pull/153)
- **[BUG FIX]**: Fix invalid asset video markup [@gpalmisano](https://github.com/dachcom-digital/pimcore-toolbox/pull/154)
- **[BUG FIX]**: Fix google maps locations with single quotes [#151](https://github.com/dachcom-digital/pimcore-toolbox/issues/151)
- **[BUG FIX]**: Fix wrong container parameter concatenation [#150](https://github.com/dachcom-digital/pimcore-toolbox/issues/150)

#### Update from Version 3.2.4 to Version 3.2.5
- **[BUG FIX]**: Fix column adjuster column_store availability check
Expand Down
6 changes: 3 additions & 3 deletions docs/11_ElementsOverview.md
Expand Up @@ -209,16 +209,16 @@ parameters:
```

#### III. Direct Element Configuration
If you don't want do define them via parameters, you need to override the default parameters
If you don't want to define them via parameters, you need to override the default parameters
on configuration layer:

```yaml
toolbox:
areas:
googleMap:
config_parameter:
map_api_key: '%toolbox.google_maps.browser_api_key%' # replace here
simple_api_key: '%toolbox.google_maps.simple_api_key%' # replace here
map_api_key: null # replace here
simple_api_key: null # replace here
```

### Script Integration
Expand Down
3 changes: 0 additions & 3 deletions src/ToolboxBundle/DependencyInjection/ToolboxExtension.php
Expand Up @@ -27,9 +27,6 @@ class ToolboxExtension extends Extension implements PrependExtensionInterface
*/
public function prepend(ContainerBuilder $container)
{
$container->setParameter('toolbox.google_maps.browser_api_key', null);
$container->setParameter('toolbox.google_maps.simple_api_key', null);

$selfConfigs = $container->getExtensionConfig($this->getAlias());

$rootConfigs = [];
Expand Down
29 changes: 20 additions & 9 deletions src/ToolboxBundle/Model/Document/Tag/GoogleMap.php
Expand Up @@ -220,28 +220,39 @@ public function getId()
*/
protected function geocodeLocation($location)
{
if ($this->googleLookUpIsDisabled()) {
return $location;
}

/** @var ConfigManager $configManager */
$configManager = \Pimcore::getContainer()->get(ConfigManager::class);
$configNode = $configManager->setAreaNameSpace(ConfigManagerInterface::AREABRICK_NAMESPACE_INTERNAL)->getAreaParameterConfig('googleMap');

$address = $location['street'] . '+' . $location['zip'] . '+' . $location['city'] . '+' . $location['country'];
$address = urlencode($address);

$key = '';
$key = null;
$keyParam = '';

$fallbackSimpleKey = \Pimcore::getContainer()->getParameter('toolbox.google_maps.simple_api_key');
$fallbackBrowserKey = \Pimcore::getContainer()->getParameter('toolbox.google_maps.browser_api_key');

// first try to get server-api-key
if (!empty($configNode) && isset($configNode['simple_api_key']) && !empty($configNode['simple_api_key'])) {
$key = '&key=' . $configNode['simple_api_key'];
}
// if not set, get browser-api-key
if ($key === '' && !empty($configNode) && isset($configNode['map_api_key']) && !empty($configNode['map_api_key'])) {
$key = '&key=' . $configNode['map_api_key'];
$key = $configNode['simple_api_key'];
} elseif (!empty($fallbackSimpleKey)) {
$key = $fallbackSimpleKey;
} elseif (!empty($configNode) && isset($configNode['map_api_key']) && !empty($configNode['map_api_key'])) {
$key = $configNode['map_api_key'];
} elseif (!empty($fallbackBrowserKey)) {
$key = $fallbackBrowserKey;
}

if ($this->googleLookUpIsDisabled()) {
return $location;
if ($key !== null) {
$keyParam = sprintf('&key=%s', $key);
}

$url = 'https://maps.google.com/maps/api/geocode/json?address=' . $address . $key;
$url = sprintf('https://maps.google.com/maps/api/geocode/json?address=%s%s', $address, $keyParam);

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
Expand Down
Expand Up @@ -50,5 +50,5 @@ toolbox:
scrollwheel: false
map_style_url: false
marker_icon: false
map_api_key: '%toolbox.google_maps.browser_api_key%'
simple_api_key: '%toolbox.google_maps.simple_api_key%'
map_api_key: null
simple_api_key: null
2 changes: 2 additions & 0 deletions src/ToolboxBundle/Resources/config/services/twig.yml
Expand Up @@ -45,6 +45,8 @@ services:
- { name: twig.extension }

ToolboxBundle\Twig\Extension\GoogleAPIKeysExtension:
arguments:
$fallbackBrowserKey: '%toolbox.google_maps.browser_api_key%'
public: false
autowire: true
tags:
Expand Down
19 changes: 15 additions & 4 deletions src/ToolboxBundle/Twig/Extension/GoogleAPIKeysExtension.php
Expand Up @@ -8,16 +8,25 @@

class GoogleAPIKeysExtension extends AbstractExtension
{
/**
* @var string
*/
protected $fallbackBrowserKey;

/**
* @var ConfigManagerInterface
*/
protected $configManager;

/**
* @param string|null $fallbackBrowserKey
* @param ConfigManagerInterface $configManager
*/
public function __construct(ConfigManagerInterface $configManager)
{
public function __construct(
?string $fallbackBrowserKey,
ConfigManagerInterface $configManager
) {
$this->fallbackBrowserKey = $fallbackBrowserKey;
$this->configManager = $configManager;
}

Expand All @@ -38,11 +47,13 @@ public function getFunctions()
*/
public function getGoogleMapAPIKey()
{
$browserKey = 'please_configure_key_in_systemsettings';
$configNode = $this->configManager->setAreaNameSpace(ConfigManagerInterface::AREABRICK_NAMESPACE_INTERNAL)->getAreaParameterConfig('googleMap');

$browserKey = 'please_configure_key_in_systemsettings';
if (!empty($configNode) && isset($configNode['map_api_key']) && !empty($configNode['map_api_key'])) {
$browserKey = $configNode['map_api_key'];
return $configNode['map_api_key'];
} elseif (!empty($this->fallbackBrowserKey)) {
return $this->fallbackBrowserKey;
}

return $browserKey;
Expand Down

0 comments on commit 53219e1

Please sign in to comment.