Add a mapping between the model and feature property value model (Close #21).

This commit is contained in:
David Molineus
2015-01-23 13:58:35 +01:00
parent 81c66252f0
commit d0e63ff985
2 changed files with 96 additions and 1 deletions

View File

@@ -287,3 +287,23 @@ require_once TL_ROOT . '/system/modules/leaflet/config/leaflet_providers.php';
if (!isset($GLOBALS['LEAFLET_LIBRARIES'])) {
$GLOBALS['LEAFLET_LIBRARIES'] = array();
}
/*
* When creating a GeoJSON feature of a map object a feature.properties.model object is passed.
* Define the properties you always want to set.
*
* For more control you can subscribe the ConvertToGeoJsonEvent.
*
* The entry can be a string or an array. If an array is passed, the 2nd value is the type. Following types
* are supported.
* - array: Use deserialize before adding the value
* - file: Thread value a uuid and find the path.
* - files: Thread values as a list of file uuids and get an array of paths.
*/
$GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES']['tl_leaflet_marker'][] = 'id';
$GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES']['tl_leaflet_marker'][] = 'title';
$GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES']['tl_leaflet_marker'][] = 'alias';
$GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES']['tl_leaflet_vector'][] = 'id';
$GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES']['tl_leaflet_vector'][] = 'title';
$GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES']['tl_leaflet_vector'][] = 'alias';

View File

@@ -35,7 +35,8 @@ class GeoJsonSubscriber implements EventSubscriberInterface
array('addPopup'),
array('enrichMarker'),
array('enrichVector'),
array('enrichCircle')
array('enrichCircle'),
array('setModelData')
)
);
}
@@ -130,4 +131,78 @@ class GeoJsonSubscriber implements EventSubscriberInterface
$feature->setProperty('arguments', array($definition->getLatLng(), $definition->getRadius()));
}
}
/**
* Pass configured properties on an model to the properties.model key.
*
* @param ConvertToGeoJsonEvent $event The subscribed events.
* @return void
*/
public function setModelData(ConvertToGeoJsonEvent $event)
{
$feature = $event->getGeoJson();
$model = $event->getModel();
if (!$model instanceof \Model || !$feature instanceof Feature
|| empty($GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'][$model->getTable()])) {
return;
}
$mapping = $GLOBALS['LEAFLET_FEATURE_MODEL_PROPERTIES'][$model->getTable()];
$data = (array) $feature->getProperty('model');
foreach ((array) $mapping as $property) {
$value = $this->parseModelValue($model, $property);
// Important: Do not combine with line above as the property can be modified if it's an array.
$data[$property] = $value;
}
$feature->setProperty('model', $data);
}
/**
* Parse the model value based on the config.
*
* @param \Model $model The model.
* @param mixed $property The property config.
*
* @return array|mixed|null
*/
private function parseModelValue(\Model $model, &$property)
{
if (is_array($property)) {
list($property, $type) = $property;
$value = $model->$property;
switch ($type) {
case 'array':
case 'object':
$value = deserialize($value, true);
break;
case 'file':
$file = \FilesModel::findByUuid($value);
$value = $file->path;
break;
case 'files':
$collection = \FilesModel::findMultipleByUuids(deserialize($value, true));
if ($collection) {
$value = $collection->fetchEach('path');
} else {
$value = array();
}
break;
default:
$value = null;
}
} else {
$value = $model->$property;
}
return $value;
}
}