* @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\DevTools\ServiceContainerTrait; use Netzmacht\Contao\Leaflet\Model\LayerModel; class Layer { use ServiceContainerTrait; private $layers; /** * @var \Database */ private $database; public function __construct() { $this->layers = &$GLOBALS['LEAFLET_LAYERS']; $this->database = static::getService('database.connection'); \Controller::loadLanguageFile('leaflet_layer'); } public function getVariants($dataContainer) { if ($dataContainer->activeRecord && $dataContainer->activeRecord->tile_provider && !empty($GLOBALS['LEAFLET_TILE_PROVIDERS'][$dataContainer->activeRecord->tile_provider]['variants']) ) { return $GLOBALS['LEAFLET_TILE_PROVIDERS'][$dataContainer->activeRecord->tile_provider]['variants']; } return array(); } public function generateRow($row, $label) { $alt = empty($GLOBALS['TL_LANG']['leaflet_layer'][$row['type']][0]) ? $row['type'] : $GLOBALS['TL_LANG']['leaflet_layer'][$row['type']][0]; 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); } $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; } public function getMarkerClusterLayers() { $types = array_keys( array_filter( $GLOBALS['LEAFLET_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(); } // Call paste_button_callback (&$dc, $row, $table, $cr, $childs, $previous, $next) public function getPasteButtons($dataContainer, $row, $table, $whatever, $children) { $pasteAfterUrl = \Controller::addToUrl( 'act='.$children['mode'].'&mode=1&pid='.$row['id'] .(!is_array($children['id']) ? '&id='.$children['id'] : '') ); $buffer = sprintf( '%s ', $pasteAfterUrl, specialchars(sprintf($GLOBALS['TL_LANG'][$table]['pasteafter'][1], $row['id'])), \Image::getHtml( 'pasteafter.gif', sprintf($GLOBALS['TL_LANG'][$table]['pasteafter'][1], $row['id']) ) ); if (!empty($this->layers[$row['type']]['children'])) { $pasteIntoUrl = \Controller::addToUrl( sprintf( 'act=%s&mode=2&pid=%s%s', $children['mode'], $row['id'], !is_array($children['id']) ? '&id='.$children['id'] : '' ) ); $buffer .= sprintf( '%s ', $pasteIntoUrl, specialchars(sprintf($GLOBALS['TL_LANG'][$table]['pasteinto'][1], $row['id'])), \Image::getHtml( 'pasteinto.gif', sprintf($GLOBALS['TL_LANG'][$table]['pasteinto'][1], $row['id']) ) ); } elseif ($row['id'] > 0) { $buffer .= \Image::getHtml('pasteinto_.gif'); } return $buffer; } 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); } 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); } public function getLayers($dataContainer) { $collection = LayerModel::findBy('id !', $dataContainer->id); return OptionsBuilder::fromCollection($collection, 'id', 'title') ->asTree() ->getOptions(); } 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); } /** * @param $row * @param $href * @param $label * @param $title * @param $icon * @param $attributes * * @return string */ protected function generateButton($row, $href, $label, $title, $icon, $attributes) { return sprintf( '%s ', \Backend::addToUrl($href . '&id=' . $row['id']), $title, \Image::getHtml($icon, $label, $attributes) ); } }