Store layer relations in extra table.

This commit is contained in:
David Molineus
2015-01-09 22:33:57 +01:00
parent c7f37238d9
commit ff2c3fb8c3
12 changed files with 307 additions and 29 deletions

View File

@@ -13,11 +13,24 @@ namespace Netzmacht\Contao\Leaflet\Dca;
use Netzmacht\Contao\DevTools\Dca\Options\OptionsBuilder;
use Netzmacht\Contao\DevTools\ServiceContainerTrait;
use Netzmacht\Contao\Leaflet\Model\ControlModel;
use Netzmacht\Contao\Leaflet\Model\LayerModel;
class Control
{
use ServiceContainerTrait;
/**
* @var \Database
*/
private $database;
public function __construct()
{
$this->database = static::getService('database.connection');
}
public function generateRow($row)
{
return sprintf(
@@ -47,4 +60,74 @@ class Control
return OptionsBuilder::fromCollection($collection, 'id', 'title')->getOptions();
}
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();
}
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']]) || $values[$layer['layer']]['mode'] != $layer['mode']) {
$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;
}
}