Switch to PSR-4.

This commit is contained in:
David Molineus
2017-10-05 14:16:56 +02:00
parent e3344ffd4f
commit 827c746b0d
87 changed files with 4 additions and 6 deletions

View File

@@ -0,0 +1,190 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks;
use Netzmacht\Contao\Toolkit\Dca\Manager;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\ControlModel;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
/**
* Class Control is the helper for the tl_leaflet_control dca.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class ControlCallbacks extends Callbacks
{
/**
* Name of the data container.
*
* @var string
*/
protected static $name = 'tl_leaflet_control';
/**
* Helper service name.
*
* @var string
*/
protected static $serviceName = 'leaflet.dca.control-callbacks';
/**
* The database connection.
*
* @var \Database
*/
private $database;
/**
* Construct.
*
* @param Manager $manager Data container manager.
* @param \Database $database Database connection.
*/
public function __construct(Manager $manager, \Database $database)
{
parent::__construct($manager);
$this->database = $database;
}
/**
* Generate a row.
*
* @param array $row The data row.
*
* @return string
*/
public function generateRow($row)
{
return sprintf(
'%s <span class="tl_gray">[%s]</span>',
$row['title'],
$row['type']
);
}
/**
* Get layers for the layers control.
*
* @return array
*/
public function getLayers()
{
$collection = LayerModel::findAll();
return OptionsBuilder::fromCollection($collection, 'title')
->asTree()
->getOptions();
}
/**
* Get the zoom controls for the reference value of the loading control.
*
* @return array
*/
public function getZoomControls()
{
$collection = ControlModel::findBy('type', 'zoom', array('order' => 'title'));
return OptionsBuilder::fromCollection($collection, 'title')->getOptions();
}
/**
* Load layer relations.
*
* @param mixed $value The actual value.
* @param \DataContainer $dataContainer The data container driver.
*
* @return array
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function loadLayerRelations($value, $dataContainer)
{
$result = $this->database
->prepare('SELECT lid As layer, mode FROM tl_leaflet_control_layer WHERE cid=? ORDER BY sorting')
->execute($dataContainer->id);
return $result->fetchAllAssoc();
}
/**
* Save layer relations.
*
* @param $layers $layers The layer id values.
* @param \DataContainer $dataContainer The dataContainer driver.
*
* @return null
*/
public function saveLayerRelations($layers, $dataContainer)
{
$new = deserialize($layers, true);
$values = array();
$result = $this->database
->prepare('SELECT * FROM tl_leaflet_control_layer WHERE cid=? order BY sorting')
->execute($dataContainer->id);
while ($result->next()) {
$values[$result->lid] = $result->row();
}
$sorting = 0;
foreach ($new as $layer) {
if (!isset($values[$layer['layer']])) {
$this->database
->prepare('INSERT INTO tl_leaflet_control_layer %s')
->set(
array(
'tstamp' => time(),
'lid' => $layer['layer'],
'cid' => $dataContainer->id,
'mode' => $layer['mode'],
'sorting' => $sorting
)
)
->execute();
$sorting += 128;
} else {
$this->database
->prepare('UPDATE tl_leaflet_control_layer %s WHERE id=?')
->set(
array(
'tstamp' => time(),
'sorting' => $sorting,
'mode' => $layer['mode']
)
)
->execute($values[$layer['layer']]['id']);
$sorting += 128;
unset ($values[$layer['layer']]);
}
}
$ids = array_map(
function ($item) {
return $item['id'];
},
$values
);
if ($ids) {
$this->database->query('DELETE FROM tl_leaflet_control_layer WHERE id IN(' . implode(',', $ids) . ')');
}
return null;
}
}

View File

@@ -0,0 +1,106 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator;
use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\MapModel;
/**
* Class Module is the helper for the tl_module dca.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class FrontendIntegration
{
/**
* Translator.
*
* @var Translator
*/
private $translator;
/**
* FrontendIntegration constructor.
*
* @param Translator $translator Translator.
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
/**
* Generate the callback definition.
*
* @param string $methodName Callback method name.
*
* @return callable
*/
public static function callback($methodName)
{
return CallbackFactory::service('leaflet.dca.frontend-integration', $methodName);
}
/**
* Get all leaflet maps.
*
* @return array
*/
public function getMaps()
{
$collection = MapModel::findAll();
return OptionsBuilder::fromCollection($collection, 'title')->getOptions();
}
/**
* Get edit map link wizard.
*
* @param \DataContainer $dataContainer The dataContainer driver.
*
* @return string
*/
public function getEditMapLink($dataContainer)
{
if ($dataContainer->value < 1) {
return '';
}
$pattern = 'title="%s" style="padding-left: 3px" onclick="Backend.openModalIframe(';
$pattern .= '{\'width\':768,\'title\':\'%s\',\'url\':this.href});return false"';
return sprintf(
'<a href="%s%s&amp;popup=1&amp;rt=%s" %s>%s</a>',
'contao/main.php?do=leaflet_map&amp;table=tl_leaflet_map&amp;act=edit&amp;id=',
$dataContainer->value,
\RequestToken::get(),
sprintf(
$pattern,
specialchars($this->translator->translate('editalias.1', 'tl_content', [$dataContainer->value])),
specialchars(
str_replace(
"'",
"\\'",
sprintf($this->translator->translate('editalias.1', 'tl_content', [$dataContainer->value]))
)
)
),
\Image::getHtml(
'alias.gif',
$this->translator->translate('editalias.0', 'tl_content', [$dataContainer->value]),
'style="vertical-align:top"'
)
);
}
}

402
src/Dca/LayerCallbacks.php Normal file
View File

@@ -0,0 +1,402 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator;
use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks;
use Netzmacht\Contao\Toolkit\Dca\Manager;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
/**
* Class Layer is the helper class for the tl_leaflet_layer dca.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class LayerCallbacks extends Callbacks
{
/**
* Name of the data container.
*
* @var string
*/
protected static $name = 'tl_leaflet_layer';
/**
* Helper service name.
*
* @var string
*/
protected static $serviceName = 'leaflet.dca.layer-callbacks';
/**
* Layers definition.
*
* @var array
*/
private $layers;
/**
* The database connection.
*
* @var \Database
*/
private $database;
/**
* Tile providers configuration.
*
* @var array
*/
private $tileProviders;
/**
* Translator.
*
* @var Translator
*/
private $translator;
/**
* OSM amenities.
*
* @var array
*/
private $amenities;
/**
* Construct.
*
* @param Manager $manager Data container manager.
* @param \Database $database Database connection.
* @param Translator $translator Translator.
* @param array $layers Leaflet layer configuration.
* @param array $tileProviders Tile providers.
* @param array $amenities OSM amenities.
*/
public function __construct(
Manager $manager,
\Database $database,
Translator $translator,
array $layers,
array $tileProviders,
array $amenities
) {
parent::__construct($manager);
\Controller::loadLanguageFile('leaflet_layer');
$this->database = $database;
$this->layers = $layers;
$this->tileProviders = $tileProviders;
$this->translator = $translator;
$this->amenities = $amenities;
}
/**
* Get variants of the tile provider.
*
* @param \DataContainer $dataContainer The dataContainer driver.
*
* @return array
*/
public function getVariants($dataContainer)
{
if ($dataContainer->activeRecord
&& $dataContainer->activeRecord->tile_provider
&& !empty($this->tileProviders[$dataContainer->activeRecord->tile_provider]['variants'])
) {
return $this->tileProviders[$dataContainer->activeRecord->tile_provider]['variants'];
}
return array();
}
/**
* Generate a row.
*
* @param array $row The data row.
* @param string $label Current row label.
*
* @return string
*/
public function generateRow($row, $label)
{
if (!empty($this->layers[$row['type']]['icon'])) {
$src = $this->layers[$row['type']]['icon'];
} else {
$src = 'iconPLAIN.gif';
}
if (!$row['active']) {
$src = preg_replace('/(\.[^\.]+)$/', '_1$1', $src);
}
$alt = $this->getFormatter()->formatValue('type', $row['type']);
$icon = \Image::getHtml($src, $alt, sprintf('title="%s"', strip_tags($alt)));
if (!empty($this->layers[$row['type']]['label'])) {
$label = $this->layers[$row['type']]['label']($row, $label);
}
return $icon . ' ' . $label;
}
/**
* Get all marker cluster layers.
*
* @return array
*/
public function getMarkerClusterLayers()
{
$types = array_keys(
array_filter(
$this->layers,
function ($item) {
return !empty($item['markerCluster']);
}
)
);
$collection = LayerModel::findMultipleByTypes($types);
$builder = OptionsBuilder::fromCollection(
$collection,
'id',
function ($row) {
return sprintf('%s [%s]', $row['title'], $row['type']);
}
);
return $builder->getOptions();
}
/**
* Get the paste buttons depending on the layer type.
*
* @param \DataContainer $dataContainer The dataContainer driver.
* @param array $row The data row.
* @param string $table The table name.
* @param null $whatever Who knows what the purpose of this var is.
* @param array $children The child content.
*
* @return string
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getPasteButtons($dataContainer, $row, $table, $whatever, $children)
{
$pasteAfterUrl = \Controller::addToUrl(
'act='.$children['mode'].'&amp;mode=1&amp;pid='.$row['id']
.(!is_array($children['id']) ? '&amp;id='.$children['id'] : '')
);
$buffer = sprintf(
'<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a> ',
$pasteAfterUrl,
specialchars($this->translator->translate('pasteafter.1', $table, [$row['id']])),
\Image::getHtml(
'pasteafter.gif',
$this->translator->translate('pasteafter.1', $table, [$row['id']])
)
);
if (!empty($this->layers[$row['type']]['children'])) {
$pasteIntoUrl = \Controller::addToUrl(
sprintf(
'act=%s&amp;mode=2&amp;pid=%s%s',
$children['mode'],
$row['id'],
!is_array($children['id']) ? '&amp;id='.$children['id'] : ''
)
);
$buffer .= sprintf(
'<a href="%s" title="%s" onclick="Backend.getScrollOffset()">%s</a> ',
$pasteIntoUrl,
specialchars($this->translator->translate('pasteinto.1', $table, [$row['id']])),
\Image::getHtml(
'pasteinto.gif',
$this->translator->translate('pasteinto.1', $table, [$row['id']])
)
);
} elseif ($row['id'] > 0) {
$buffer .= \Image::getHtml('pasteinto_.gif');
}
return $buffer;
}
/**
* Generate the markers button.
*
* @param array $row Current row.
* @param string $href The button href.
* @param string $label The button label.
* @param string $title The button title.
* @param string $icon The button icon.
* @param string $attributes Optional attributes.
*
* @return string
*/
public function generateMarkersButton($row, $href, $label, $title, $icon, $attributes)
{
if (empty($this->layers[$row['type']]['markers'])) {
return '';
}
return $this->generateButton($row, $href, $label, $title, $icon, $attributes);
}
/**
* Generate the vectors button.
*
* @param array $row Current row.
* @param string $href The button href.
* @param string $label The button label.
* @param string $title The button title.
* @param string $icon The button icon.
* @param string $attributes Optional attributes.
*
* @return string
*/
public function generateVectorsButton($row, $href, $label, $title, $icon, $attributes)
{
if (empty($this->layers[$row['type']]['vectors'])) {
return '';
}
return $this->generateButton($row, $href, $label, $title, $icon, $attributes);
}
/**
* Delete the relations when the layer is deleted.
*
* @param \DataContainer $dataContainer The dataContainer driver.
* @param int $undoId The id of the undo entry.
*
* @return void
*/
public function deleteRelations($dataContainer, $undoId)
{
if ($undoId) {
$undo = $this->database
->prepare('SELECT * FROM tl_undo WHERE id=?')
->limit(1)
->execute($undoId)
->row();
$result = $this->database
->prepare('SELECT * FROM tl_leaflet_map_layer WHERE lid=?')
->execute($dataContainer->id);
$undo['data'] = deserialize($undo['data'], true);
while ($result->next()) {
$undo['data']['tl_leaflet_map_layer'][] = $result->row();
}
$result = $this->database
->prepare('SELECT * FROM tl_leaflet_control_layer WHERE lid=?')
->execute($dataContainer->id);
while ($result->next()) {
$undo['data']['tl_leaflet_control_layer'][] = $result->row();
}
$this->database->prepare('UPDATE tl_undo %s WHERE id=?')
->set(array('data' => $undo['data']))
->execute($undo['id']);
}
$this->database
->prepare('DELETE FROM tl_leaflet_map_layer WHERE lid=?')
->execute($dataContainer->id);
$this->database
->prepare('DELETE FROM tl_leaflet_control_layer WHERE lid=?')
->execute($dataContainer->id);
}
/**
* Get bounds modes supported by the layer.
*
* @param \DataContainer $dataContainer The data container.
*
* @return array
*/
public function getBoundsModes($dataContainer)
{
$options = array();
if ($dataContainer->activeRecord && !empty($this->layers[$dataContainer->activeRecord->type]['boundsMode'])) {
foreach ($this->layers[$dataContainer->activeRecord->type]['boundsMode'] as $mode => $enabled) {
if ($enabled === true) {
$options[] = $mode;
} elseif ($enabled === 'deferred' && $dataContainer->activeRecord->deferred) {
$options[] = $mode;
}
}
}
return $options;
}
/**
* Get all layers except of the current layer.
*
* @param \DataContainer $dataContainer The dataContainer driver.
*
* @return array
*/
public function getLayers($dataContainer)
{
$collection = LayerModel::findBy('id !', $dataContainer->id);
return OptionsBuilder::fromCollection($collection, 'title')
->asTree()
->getOptions();
}
/**
* Get all know osm amenities as options.
*
* @return array
*/
public function getAmenities()
{
return $this->amenities;
}
/**
* Generate a button.
*
* @param array $row Current row.
* @param string $href The button href.
* @param string $label The button label.
* @param string $title The button title.
* @param string $icon The button icon.
* @param string $attributes Optional attributes.
*
* @return string
*/
protected function generateButton($row, $href, $label, $title, $icon, $attributes)
{
return sprintf(
'<a href="%s" title="%s">%s</a> ',
\Backend::addToUrl($href . '&amp;id=' . $row['id']),
$title,
\Image::getHtml($icon, $label, $attributes)
);
}
}

View File

@@ -0,0 +1,119 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory;
use Netzmacht\LeafletPHP\Value\LatLng;
/**
* Class Leaflet is the base helper providing different methods.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class LeafletCallbacks
{
/**
* File system.
*
* @var \Files
*/
private $fileSystem;
/**
* LeafletCallbacks constructor.
*
* @param \Files $fileSystem File system.
*/
public function __construct(\Files $fileSystem)
{
$this->fileSystem = $fileSystem;
}
/**
* Generate the callback definition.
*
* @param string $methodName Callback method name.
*
* @return callable
*/
public static function callback($methodName)
{
return CallbackFactory::service('leaflet.dca.common', $methodName);
}
/**
* Create the zoom range.
*
* @return array
*/
public function getZoomLevels()
{
return range(1, 20);
}
/**
* Get the geocoder wizard.
*
* @param \DataContainer $dataContainer The dataContainer driver.
*
* @return string
*/
public function getGeocoder($dataContainer)
{
$template = new \BackendTemplate('be_leaflet_geocode');
$template->field = 'ctrl_' . $dataContainer->field;
try {
$latLng = LatLng::fromString($dataContainer->value);
$template->marker = json_encode($latLng);
} catch (\Exception $e) {
// LatLng throws an exeption of value could not be created. Just let the value empty when.
}
return $template->parse();
}
/**
* Get all layers.
*
* @return array
*/
public function getLayers()
{
$options = array();
$collection = LayerModel::findBy('pid', '0', array('order' => 'title'));
if ($collection) {
foreach ($collection as $model) {
$options[$model->id] = $model->title;
}
}
return $options;
}
/**
* Clear the leaflet cache.
*
* @param mixed $value Value when used as save_callback.
*
* @return mixed
*/
public function clearCache($value = null)
{
$this->fileSystem->rrdir('system/cache/leaflet', true);
return $value;
}
}

159
src/Dca/MapCallbacks.php Normal file
View File

@@ -0,0 +1,159 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks;
use Netzmacht\Contao\Toolkit\Dca\Manager;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
/**
* Class Map is the helper class for the tl_leaflet_map dca.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class MapCallbacks extends Callbacks
{
/**
* Name of the data container.
*
* @var string
*/
protected static $name = 'tl_leaflet_map';
/**
* Helper service name.
*
* @var string
*/
protected static $serviceName = 'leaflet.dca.map-callbacks';
/**
* The database connection.
*
* @var \Database
*/
private $database;
/**
* Construct.
*
* @param Manager $manager Data container manager.
* @param \Database $database Database connection.
*/
public function __construct(Manager $manager, \Database $database)
{
parent::__construct($manager);
$this->database = $database;
}
/**
* Load layer relations.
*
* @param mixed $value The actual value.
* @param \DataContainer $dataContainer The data container driver.
*
* @return array
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function loadLayerRelations($value, $dataContainer)
{
$result = $this->database
->prepare('SELECT lid FROM tl_leaflet_map_layer WHERE mid=? ORDER BY sorting')
->execute($dataContainer->id);
return $result->fetchEach('lid');
}
/**
* Save layer relations.
*
* @param mixed $layerId The layer id values.
* @param \DataContainer $dataContainer The dataContainer driver.
*
* @return null
*/
public function saveLayerRelations($layerId, $dataContainer)
{
$new = deserialize($layerId, true);
$values = array();
$result = $this->database
->prepare('SELECT * FROM tl_leaflet_map_layer WHERE mid=? order BY sorting')
->execute($dataContainer->id);
while ($result->next()) {
$values[$result->lid] = $result->row();
}
$sorting = 0;
foreach ($new as $layerId) {
if (!isset($values[$layerId])) {
$this->database
->prepare('INSERT INTO tl_leaflet_map_layer %s')
->set(
array(
'tstamp' => time(),
'lid' => $layerId,
'mid' => $dataContainer->id,
'sorting' => $sorting
)
)
->execute();
$sorting += 128;
} else {
if ($values[$layerId]['sorting'] <= ($sorting - 128)
|| $values[$layerId]['sorting'] >= ($sorting + 128)) {
$this->database
->prepare('UPDATE tl_leaflet_map_layer %s WHERE id=?')
->set(array('tstamp' => time(), 'sorting' => $sorting))
->execute($values[$layerId]['id']);
}
$sorting += 128;
unset ($values[$layerId]);
}
}
$ids = array_map(
function ($item) {
return $item['id'];
},
$values
);
if ($ids) {
$this->database->query('DELETE FROM tl_leaflet_map_layer WHERE id IN(' . implode(',', $ids) . ')');
}
return null;
}
/**
* Get all layers except of the current layer.
*
* @param \DataContainer $dataContainer The dataContainer driver.
*
* @return array
*/
public function getLayers($dataContainer)
{
$collection = LayerModel::findBy('id !', $dataContainer->id);
return OptionsBuilder::fromCollection($collection, 'title')
->asTree()
->getOptions();
}
}

136
src/Dca/MarkerCallbacks.php Normal file
View File

@@ -0,0 +1,136 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\IconModel;
use Netzmacht\Contao\Leaflet\Model\PopupModel;
/**
* Class Marker is the dca helper class for the tl_leaflet_marker dca.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class MarkerCallbacks
{
/**
* Generate the row label.
*
* @param array $row Current data row.
*
* @return string
*/
public function generateRow($row)
{
return $row['title'];
}
/**
* Get all icons.
*
* @return array
*/
public function getIcons()
{
$collection = IconModel::findAll(array('order' => 'title'));
$builder = OptionsBuilder::fromCollection(
$collection,
function ($model) {
return sprintf('%s [%s]', $model['title'], $model['type']);
}
);
return $builder->getOptions();
}
/**
* Get all popups.
*
* @return array
*/
public function getPopups()
{
$collection = PopupModel::findAll(array('order' => 'title'));
$builder = OptionsBuilder::fromCollection($collection, 'title');
return $builder->getOptions();
}
/**
* Save the coordinates.
*
* @param string $value The raw data.
* @param \DataContainer $dataContainer The data container driver.
*
* @return string
*/
public function saveCoordinates($value, $dataContainer)
{
$combined = array(
'latitude' => null,
'longitude' => null,
'altitude' => null
);
$values = trimsplit(',', $value);
$keys = array_keys($combined);
$count = count($values);
if ($count >= 2 && $count <= 3) {
for ($i = 0; $i < $count; $i++) {
$combined[$keys[$i]] = $values[$i];
}
}
\Database::getInstance()
->prepare('UPDATE tl_leaflet_marker %s WHERE id=?')
->set($combined)
->execute($dataContainer->id);
return null;
}
/**
* Load the coordinates.
*
* @param string $value The raw data.
* @param \DataContainer $dataContainer The data container driver.
*
* @return string
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function loadCoordinates($value, $dataContainer)
{
$result = \Database::getInstance()
->prepare('SELECT latitude, longitude, altitude FROM tl_leaflet_marker WHERE id=?')
->execute($dataContainer->id);
if ($result->numRows) {
$buffer = $result->latitude;
if ($buffer && $result->longitude) {
$buffer .= ',' . $result->longitude;
} else {
return $buffer;
}
if ($buffer && $result->altitude) {
$buffer .= ',' . $result->longitude;
}
return $buffer;
}
return '';
}
}

133
src/Dca/Validator.php Normal file
View File

@@ -0,0 +1,133 @@
<?php
/**
* @package netzmacht
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2016 netzmacht David Molineus. All rights reserved.
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use ContaoCommunityAlliance\Translator\TranslatorInterface as Translator;
use Netzmacht\Contao\Toolkit\Dca\Callback\CallbackFactory;
use Netzmacht\LeafletPHP\Value\LatLng;
/**
* Class Validator.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class Validator
{
/**
* Translator.
*
* @var Translator;
*/
private $translator;
/**
* Validator constructor.
*
* @param Translator $translator Translator.
*/
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
/**
* Generate the callback definition.
*
* @param string $methodName Callback method name.
*
* @return callable
*/
public static function callback($methodName)
{
return CallbackFactory::service('leaflet.dca.validator', $methodName);
}
/**
* Validate coordinates.
*
* @param mixed $value Given value.
*
* @return mixed
* @throws \InvalidArgumentException When invalid coordinates give.
*/
public function validateCoordinates($value)
{
try {
LatLng::fromString($value);
} catch (\Exception $e) {
throw new \InvalidArgumentException(
$this->translator->translate('invalidCoordinates', 'leaflet', [$value]),
0,
$e
);
}
return $value;
}
/**
* Validate multiple coordinates.
*
* @param mixed $values Given value.
*
* @return mixed
*/
public function validateMultipleCoordinates($values)
{
if (!is_array($values)) {
$lines = explode("\n", $values);
} else {
$lines = $values;
}
foreach ($lines as $coordinate) {
$this->validateCoordinates($coordinate);
}
return $values;
}
/**
* Validate multiple coordinate sets.
*
* @param mixed $values Given value.
*
* @return mixed
*/
public function validateMultipleCoordinateSets($values)
{
$sets = deserialize($values, true);
foreach ($sets as $lines) {
$this->validateMultipleCoordinates($lines);
}
return $values;
}
/**
* Validate an alias.
*
* @param string $value Given value.
*
* @return string
* @throws \InvalidArgumentException When invalid value given.
*/
public function validateAlias($value)
{
if (preg_match('/^[A-Za-z_]+[A-Za-z0-9_]+$/', $value) !== 1) {
throw new \InvalidArgumentException(
$this->translator->translate('invalidAlias', 'leaflet')
);
}
return $value;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* @package contao-leaflet-maps
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2016 netzmacht David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\Toolkit\Dca\Callback\Callbacks;
use Netzmacht\Contao\Toolkit\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\Leaflet\Model\StyleModel;
/**
* Helper class for the tl_leaflet_vector dca.
*
* @package Netzmacht\Contao\Leaflet\Dca
*/
class VectorCallbacks extends Callbacks
{
/**
* Name of the data container.
*
* @var string
*/
protected static $name = 'tl_leaflet_vector';
/**
* Helper service name.
*
* @var string
*/
protected static $serviceName = 'leaflet.dca.vector-callbacks';
/**
* Generate the row label.
*
* @param array $row Current data row.
*
* @return string
*/
public function generateRow($row)
{
return sprintf(
'%s <span class="tl_gray">(%s)</span>',
$row['title'],
$this->getFormatter()->formatValue('type', $row['type'])
);
}
/**
* Get all styles.
*
* @return array
*/
public function getStyles()
{
$collection = StyleModel::findAll(array('order' => 'title'));
return OptionsBuilder::fromCollection($collection, 'title')->getOptions();
}
}