Add frontend module (Close #7).

This commit is contained in:
David Molineus
2015-01-09 23:20:07 +01:00
parent 47d27e9c4a
commit 91ccfa56f4
9 changed files with 222 additions and 12 deletions

View File

@@ -3,6 +3,7 @@
TemplateLoader::addFiles(
array(
'ce_leaflet_map' => 'system/modules/leaflet/templates',
'mod_leaflet_map' => 'system/modules/leaflet/templates',
'be_leaflet_geocode' => 'system/modules/leaflet/templates',
'be_leaflet_credits' => 'system/modules/leaflet/templates',
)

View File

@@ -47,6 +47,11 @@ array_insert(
*/
$GLOBALS['TL_CTE']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\MapElement';
/*
* Frontend modules
*/
$GLOBALS['FE_MOD']['includes']['leaflet'] = 'Netzmacht\Contao\Leaflet\Frontend\MapModule';
/*
* Models.

46
module/dca/tl_module.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
$GLOBALS['TL_DCA']['tl_module']['metapalettes']['leaflet'] = array(
'type' => array('name', 'type', 'headline'),
'leaflet' => array('leaflet_map', 'leaflet_width', 'leaflet_height'),
'templates' => array(':hide', 'customTpl'),
'protected' => array(':hide', 'protected'),
'expert' => array(':hide', 'guests', 'cssID', 'space'),
'invisible' => array(':hide', 'invisible', 'start', 'start')
);
$GLOBALS['TL_DCA']['tl_module']['fields']['leaflet_map'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_module']['leaflet_map'],
'inputType' => 'select',
'exclude' => true,
'options_callback' => array('Netzmacht\Contao\Leaflet\Dca\Module', 'getMaps'),
'wizard' => array(
array('Netzmacht\Contao\Leaflet\Dca\Module', 'getEditMapLink'),
),
'eval' => array(
'tl_class' => 'w50 wizard',
'chosen' => true,
),
'sql' => "int(10) unsigned NOT NULL default '0'"
);
$GLOBALS['TL_DCA']['tl_module']['fields']['leaflet_width'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_module']['leaflet_width'],
'inputType' => 'inputUnit',
'options' => array('px', '%', 'em', 'pt', 'pc', 'in', 'cm', 'mm'),
'search' => false,
'exclude' => true,
'eval' => array('rgxp' => 'digit', 'tl_class' => 'clr w50'),
'sql' => "varchar(64) NOT NULL default ''"
);
$GLOBALS['TL_DCA']['tl_module']['fields']['leaflet_height'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_module']['leaflet_height'],
'inputType' => 'inputUnit',
'options' => array('px', '%', 'em', 'pt', 'pc', 'in', 'cm', 'mm'),
'search' => false,
'exclude' => true,
'eval' => array('rgxp' => 'digit', 'tl_class' => 'w50'),
'sql' => "varchar(64) NOT NULL default ''"
);

View File

@@ -0,0 +1,17 @@
<!-- indexer::stop -->
<div class="<?php echo $this->class; ?> block"<?php echo $this->cssID; ?><?php if ($this->style): ?> style="<?php echo $this->style; ?>"<?php endif; ?>>
<?php if ($this->headline): ?>
<<?php echo $this->hl; ?>><?php echo $this->headline; ?></<?php echo $this->hl; ?>>
<?php endif; ?>
<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>
<!-- indexer::continue -->

View File

@@ -11,6 +11,7 @@
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\DevTools\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\MapModel;
/**
@@ -26,16 +27,9 @@ class Content
*/
public function getMaps()
{
$options = array();
$collection = MapModel::findAll();
if ($collection) {
foreach ($collection as $map) {
$options[$map->id] = $map->title;
}
}
return $options;
return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions();
}
public function getEditMapLink($dataContainer)

View File

@@ -0,0 +1,64 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\DevTools\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\MapModel;
/**
* Class Module
* @package Netzmacht\Contao\Leaflet\Dca
*/
class Module
{
/**
* Get all leaflet maps.
*
* @return array
*/
public function getMaps()
{
$collection = MapModel::findAll();
return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions();
}
public function getEditMapLink($dataContainer)
{
if ($dataContainer->value < 1) {
return '';
}
return sprintf(
'<a href="%s%s&amp;popup=1&amp;rt=%s" %s>%s</a>',
'contao/main.php?do=leaflet&amp;table=tl_leaflet_map&amp;act=edit&amp;id=',
$dataContainer->value,
\RequestToken::get(),
sprintf(
'title="%s" style="padding-left: 3px" '
. 'onclick="Backend.openModalIframe({\'width\':768,\'title\':\'%s\',\'url\':this.href});return false"',
specialchars(sprintf($GLOBALS['TL_LANG']['tl_content']['editalias'][1], $dataContainer->value)),
specialchars(
str_replace(
"'",
"\\'",
sprintf($GLOBALS['TL_LANG']['tl_content']['editalias'][1], $dataContainer->value)
)
)
),
\Image::getHtml(
'alias.gif',
$GLOBALS['TL_LANG']['tl_content']['editalias'][0], 'style="vertical-align:top"'
)
);
}
}

View File

@@ -78,7 +78,7 @@ class MapElement extends \ContentElement
protected function compile()
{
try {
$mapId = 'map_' . ($this->cssID[0] ?: $this->id);
$mapId = 'ce_map_' . ($this->cssID[0] ?: $this->id);
$map = $this->mapService->getJavascript($this->leaflet_map, null, $mapId);
$GLOBALS['TL_BODY'][] = '<script>' . $map .'</script>';

View File

@@ -12,7 +12,90 @@
namespace Netzmacht\Contao\Leaflet\Frontend;
class MapModule
{
use Netzmacht\Contao\Leaflet\MapService;
use Netzmacht\Contao\Leaflet\Model\MapModel;
class MapModule extends \Module
{
/**
* Template name.
*
* @var string
*/
protected $strTemplate = 'mod_leaflet_map';
/**
* @var MapService
*/
private $mapService;
/**
* Construct.
*
* @param \ModuleModel $objElement Module model.
* @param string $strColumn Layout column.
*/
public function __construct($objElement, $strColumn = 'main')
{
parent::__construct($objElement, $strColumn);
$this->mapService = $GLOBALS['container']['leaflet.map.service'];
}
public function generate()
{
if (TL_MODE === 'BE') {
$model = MapModel::findByPK($this->leaflet_map);
$template = new \BackendTemplate('be_wildcard');
if ($model) {
$href = 'contao/main.php?do=leaflet&amp;table=tl_leaflet_map&amp;act=edit&amp;id=' . $model->id;
$template->wildcard = '### LEAFLET MAP ' . $model->title . ' ###';
$template->title = $this->headline;
$template->id = $model->id;
$template->link = $model->title;
$template->href = $href;
}
return $template->parse();
}
return parent::generate();
}
/**
* Compile the content element.
*
* @return void
*
* @throws \Exception
*/
protected function compile()
{
try {
$mapId = 'mod_map_' . ($this->cssID[0] ?: $this->id);
$map = $this->mapService->getJavascript($this->leaflet_map, null, $mapId);
$GLOBALS['TL_BODY'][] = '<script>' . $map .'</script>';
$this->Template->mapId = $mapId;
$style = '';
$height = deserialize($this->leaflet_height, true);
$width = deserialize($this->leaflet_width, true);
if (!empty($width['value'])) {
$style .= 'width:' . $width['value'] . $width['unit'] . ';';
}
if (!empty($height['value'])) {
$style .= 'height:' . $height['value'] . $height['unit'] . ';';
}
$this->Template->mapStyle = $style;
} catch(\Exception $e) {
throw $e;
}
}
}

View File

@@ -82,7 +82,7 @@ class DefinitionMapper
*/
public function handle(\Model $model, LatLngBounds $bounds = null, $elementId = null)
{
$hash = $model->getTable() . '.' . $model->{$model->getPk()};
$hash = $model->getTable() . '.' . $model->{$model->getPk()} . ($elementId ? ('.' . $elementId) : '');
if (isset($this->mapped[$hash])) {
return $this->mapped[$hash];