forked from Snck3rs/contao-leaflet-maps
Implement an insert tag.
This commit is contained in:
@@ -13,6 +13,7 @@ TemplateLoader::addFiles(
|
|||||||
array(
|
array(
|
||||||
'ce_leaflet_map' => 'system/modules/leaflet/templates',
|
'ce_leaflet_map' => 'system/modules/leaflet/templates',
|
||||||
'leaflet_map_js' => 'system/modules/leaflet/templates',
|
'leaflet_map_js' => 'system/modules/leaflet/templates',
|
||||||
|
'leaflet_map_html' => 'system/modules/leaflet/templates',
|
||||||
'mod_leaflet_map' => 'system/modules/leaflet/templates',
|
'mod_leaflet_map' => 'system/modules/leaflet/templates',
|
||||||
'be_leaflet_geocode' => 'system/modules/leaflet/templates',
|
'be_leaflet_geocode' => 'system/modules/leaflet/templates',
|
||||||
'be_leaflet_credits' => 'system/modules/leaflet/templates',
|
'be_leaflet_credits' => 'system/modules/leaflet/templates',
|
||||||
|
|||||||
@@ -61,6 +61,10 @@ $GLOBALS['TL_CTE']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\M
|
|||||||
*/
|
*/
|
||||||
$GLOBALS['FE_MOD']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\MapModule';
|
$GLOBALS['FE_MOD']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\MapModule';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Hooks
|
||||||
|
*/
|
||||||
|
$GLOBALS['TL_HOOKS']['replaceInsertTags'][] = array('Netzmacht\Contao\Leaflet\Frontend\Hooks', 'replaceInsertTags');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Models.
|
* Models.
|
||||||
|
|||||||
@@ -6,11 +6,6 @@
|
|||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
<div id="<?php echo $this->mapId; ?>" style="<?php echo $this->mapStyle; ?>"></div>
|
<div id="<?php echo $this->mapId; ?>" style="<?php echo $this->mapStyle; ?>"></div>
|
||||||
<?php if ($this->map): ?>
|
|
||||||
<script>
|
|
||||||
<?php echo $this->map; ?>
|
|
||||||
</script>
|
|
||||||
<?php endif; ?>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<!-- indexer::continue -->
|
<!-- indexer::continue -->
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
<div id="<?php echo $mapId; ?>"></div>
|
<div id="<?php echo $mapId; ?>" style="<?php echo $style; ?>"></div>
|
||||||
|
<?php $GLOBALS['TL_BODY'][] = <<<HTML
|
||||||
<script>
|
<script>
|
||||||
<?php include \Controller::getTemplate('leaflet_map_js'); ?>
|
ContaoLeaflet.addMap('{$mapId}', function() {
|
||||||
</script>
|
{$javascript}
|
||||||
|
return { map: map, layers: layers, controls: controls, icons: icons };
|
||||||
|
}());</script>
|
||||||
|
HTML;
|
||||||
|
|||||||
73
src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php
Normal file
73
src/Netzmacht/Contao/Leaflet/Frontend/Hooks.php
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package dev
|
||||||
|
* @author David Molineus <david.molineus@netzmacht.de>
|
||||||
|
* @copyright 2015 netzmacht creative David Molineus
|
||||||
|
* @license LGPL 3.0
|
||||||
|
* @filesource
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Netzmacht\Contao\Leaflet\Frontend;
|
||||||
|
|
||||||
|
use Netzmacht\Contao\DevTools\ServiceContainerTrait;
|
||||||
|
use Netzmacht\Contao\Leaflet\MapService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Hooks contains hooks for the frontend manipulation.
|
||||||
|
*
|
||||||
|
* @package Netzmacht\Contao\Leaflet\Frontend
|
||||||
|
*/
|
||||||
|
class Hooks
|
||||||
|
{
|
||||||
|
use ServiceContainerTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace the leaflet insert tag and returns the generated map.
|
||||||
|
*
|
||||||
|
* By default it creates the html template, so the script and html are rendered.
|
||||||
|
*
|
||||||
|
* Supported formats are:
|
||||||
|
* - {{leaflet::id|alias}} The map id or alias.
|
||||||
|
* - {{leaflet::id::style}} The style attribute, useful to pass the height and width of the container.
|
||||||
|
* - {{leaflet::id::style::template}} Optional template. Look at leaflet_map_js and leaflet_map_html as example.
|
||||||
|
*
|
||||||
|
* @param string $tag The given insert tag.
|
||||||
|
*
|
||||||
|
* @return bool|string
|
||||||
|
*
|
||||||
|
* @throws \Exception If debug mode is enabled and anything went wrong.
|
||||||
|
*/
|
||||||
|
public function replaceInsertTags($tag)
|
||||||
|
{
|
||||||
|
$parts = explode('::', $tag);
|
||||||
|
|
||||||
|
if ($parts[0] !== 'leaflet' || empty($parts[1])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$mapService = $this->getMapService();
|
||||||
|
$style = empty($parts[2]) ? 'width:400px;height:300px' : $parts[2];
|
||||||
|
$template = empty($parts[3]) ? 'leaflet_map_html' : $parts[3];
|
||||||
|
|
||||||
|
try {
|
||||||
|
return $mapService->generate($parts[1], null, $parts[1], $template, $style);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
if (static::getService('config')->get('debugMode')) {
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the map service.
|
||||||
|
*
|
||||||
|
* @return MapService
|
||||||
|
*/
|
||||||
|
protected function getMapService()
|
||||||
|
{
|
||||||
|
return static::getService('leaflet.map.service');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -103,7 +103,7 @@ trait HybridTrait
|
|||||||
try {
|
try {
|
||||||
RequestUrl::setFor($this->getIdentifier());
|
RequestUrl::setFor($this->getIdentifier());
|
||||||
$mapId = 'map_' . ($this->cssID[0] ?: ('ce_' . $this->id));
|
$mapId = 'map_' . ($this->cssID[0] ?: ('ce_' . $this->id));
|
||||||
$map = $this->mapService->getJavascript($this->leaflet_map, null, $mapId);
|
$map = $this->mapService->generate($this->leaflet_map, null, $mapId);
|
||||||
RequestUrl::setFor(null);
|
RequestUrl::setFor(null);
|
||||||
|
|
||||||
$GLOBALS['TL_BODY'][] = '<script>' . $map .'</script>';
|
$GLOBALS['TL_BODY'][] = '<script>' . $map .'</script>';
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ class MapService
|
|||||||
/**
|
/**
|
||||||
* Get map model.
|
* Get map model.
|
||||||
*
|
*
|
||||||
* @param int $mapId Model id.
|
* @param int|string $mapId Model id or alias.
|
||||||
*
|
*
|
||||||
* @return MapModel
|
* @return MapModel
|
||||||
*
|
*
|
||||||
@@ -95,7 +95,7 @@ class MapService
|
|||||||
*/
|
*/
|
||||||
public function getModel($mapId)
|
public function getModel($mapId)
|
||||||
{
|
{
|
||||||
$model = MapModel::findByPk($mapId);
|
$model = MapModel::findByIdOrAlias($mapId);
|
||||||
|
|
||||||
if ($model === null) {
|
if ($model === null) {
|
||||||
throw new \InvalidArgumentException(sprintf('Model "%s" not found', $mapId));
|
throw new \InvalidArgumentException(sprintf('Model "%s" not found', $mapId));
|
||||||
@@ -110,17 +110,27 @@ class MapService
|
|||||||
* @param MapModel|int $mapId The map database id. MapModel accepted as well.
|
* @param MapModel|int $mapId The map database id. MapModel accepted as well.
|
||||||
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
* @param LatLngBounds $bounds Optional bounds where elements should be in.
|
||||||
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
* @param string $elementId Optional element id. If none given the mapId or alias is used.
|
||||||
|
* @param string $template The template being used for generating.
|
||||||
|
* @param string $style Optional style attributes.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function getJavascript($mapId, LatLngBounds $bounds = null, $elementId = null, $template = 'leaflet_map_js')
|
public function generate(
|
||||||
{
|
$mapId,
|
||||||
|
LatLngBounds $bounds = null,
|
||||||
|
$elementId = null,
|
||||||
|
$template = 'leaflet_map_js',
|
||||||
|
$style = ''
|
||||||
|
) {
|
||||||
$definition = $this->getDefinition($mapId, $bounds, $elementId);
|
$definition = $this->getDefinition($mapId, $bounds, $elementId);
|
||||||
$assets = new ContaoAssets();
|
$assets = new ContaoAssets();
|
||||||
|
|
||||||
$template = \Controller::getTemplate($template);
|
$template = \Controller::getTemplate($template);
|
||||||
|
|
||||||
|
// @codingStandardsIgnoreStart - Set for the template.
|
||||||
$javascript = $this->leaflet->build($definition, $assets);
|
$javascript = $this->leaflet->build($definition, $assets);
|
||||||
$mapId = $definition->getId();
|
$mapId = $definition->getId();
|
||||||
|
// @codingStandardsIgnoreEnd
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
include $template;
|
include $template;
|
||||||
|
|||||||
Reference in New Issue
Block a user