From 91ccfa56f4e837066b84427e708ed46c51c69ec6 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Fri, 9 Jan 2015 23:20:07 +0100 Subject: [PATCH] Add frontend module (Close #7). --- module/config/autoload.php | 1 + module/config/config.php | 5 ++ module/dca/tl_module.php | 46 ++++++++++ module/templates/mod_leaflet_map.html5 | 17 ++++ src/Netzmacht/Contao/Leaflet/Dca/Content.php | 10 +-- src/Netzmacht/Contao/Leaflet/Dca/Module.php | 64 ++++++++++++++ .../Contao/Leaflet/Frontend/MapElement.php | 2 +- .../Contao/Leaflet/Frontend/MapModule.php | 87 ++++++++++++++++++- .../Leaflet/Mapper/DefinitionMapper.php | 2 +- 9 files changed, 222 insertions(+), 12 deletions(-) create mode 100644 module/dca/tl_module.php create mode 100644 module/templates/mod_leaflet_map.html5 create mode 100644 src/Netzmacht/Contao/Leaflet/Dca/Module.php diff --git a/module/config/autoload.php b/module/config/autoload.php index 7ac1b16..7cdbc3d 100644 --- a/module/config/autoload.php +++ b/module/config/autoload.php @@ -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', ) diff --git a/module/config/config.php b/module/config/config.php index dc54c0d..5cce29e 100644 --- a/module/config/config.php +++ b/module/config/config.php @@ -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. diff --git a/module/dca/tl_module.php b/module/dca/tl_module.php new file mode 100644 index 0000000..f5bfd55 --- /dev/null +++ b/module/dca/tl_module.php @@ -0,0 +1,46 @@ + 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 ''" +); diff --git a/module/templates/mod_leaflet_map.html5 b/module/templates/mod_leaflet_map.html5 new file mode 100644 index 0000000..e88bc5b --- /dev/null +++ b/module/templates/mod_leaflet_map.html5 @@ -0,0 +1,17 @@ + +
cssID; ?>style): ?> style="style; ?>"> + + headline): ?> + <hl; ?>>headline; ?>hl; ?>> + + +
+map): ?> + + + +
+ + diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Content.php b/src/Netzmacht/Contao/Leaflet/Dca/Content.php index f33e35c..cac6e2f 100644 --- a/src/Netzmacht/Contao/Leaflet/Dca/Content.php +++ b/src/Netzmacht/Contao/Leaflet/Dca/Content.php @@ -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) diff --git a/src/Netzmacht/Contao/Leaflet/Dca/Module.php b/src/Netzmacht/Contao/Leaflet/Dca/Module.php new file mode 100644 index 0000000..ebb640d --- /dev/null +++ b/src/Netzmacht/Contao/Leaflet/Dca/Module.php @@ -0,0 +1,64 @@ + + * @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( + '%s', + 'contao/main.php?do=leaflet&table=tl_leaflet_map&act=edit&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"' + ) + ); + } +} diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php index 12672a4..5e6717a 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapElement.php @@ -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'][] = ''; diff --git a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php index 8eca10b..413e42e 100644 --- a/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php +++ b/src/Netzmacht/Contao/Leaflet/Frontend/MapModule.php @@ -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&table=tl_leaflet_map&act=edit&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'][] = ''; + + $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; + } + } } diff --git a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php index 5db90ca..c822f60 100644 --- a/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php +++ b/src/Netzmacht/Contao/Leaflet/Mapper/DefinitionMapper.php @@ -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];