Commit 9ace1d3d authored by Sebastian Castro's avatar Sebastian Castro

Import Mapping: ability to map subkeys as geo/latitude

parent d988c813
...@@ -15,7 +15,7 @@ class ImportDynamicAdminController extends Controller ...@@ -15,7 +15,7 @@ class ImportDynamicAdminController extends Controller
$result = $this->get('biopen.element_import')->collectData($object); $result = $this->get('biopen.element_import')->collectData($object);
$this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès. Vous pouvez maintenant compléter les tables de correspondances, puis importer les données."); $this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès. Vous pouvez maintenant compléter les tables de correspondances, puis importer les données.");
$url = $this->admin->generateUrl('edit', ['id' => $object->getId()]) . "#tab_2"; $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]) . "#tab_3";
return $this->redirect($url); return $this->redirect($url);
} }
...@@ -165,7 +165,7 @@ class ImportDynamicAdminController extends Controller ...@@ -165,7 +165,7 @@ class ImportDynamicAdminController extends Controller
$this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès. Vous pouvez maintenant compléter les tables de correspondances, puis importer les données."); $this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès. Vous pouvez maintenant compléter les tables de correspondances, puis importer les données.");
$url = $this->admin->generateUrl('edit', ['id' => $object->getId()]) . "#tab_2"; $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]) . "#tab_3";
return $this->redirect($url); return $this->redirect($url);
} catch (ModelManagerException $e) { } catch (ModelManagerException $e) {
$this->handleModelManagerException($e); $this->handleModelManagerException($e);
......
...@@ -84,7 +84,8 @@ ...@@ -84,7 +84,8 @@
} }
{% endfor %} {% endfor %}
options = [{ text: "Attributs du formulaire", children: formData}]; options = [{ id: "/", text: ""}]
options.push({ text: "Attributs du formulaire", children: formData});
if (otherData.length > 0) options.push({text: "Autres attributs", children: otherData}); if (otherData.length > 0) options.push({text: "Autres attributs", children: otherData});
options.push({ text: "Attributs importés", children: importedData}); options.push({ text: "Attributs importés", children: importedData});
......
...@@ -25,6 +25,8 @@ class ElementImportMappingService ...@@ -25,6 +25,8 @@ class ElementImportMappingService
protected $createMissingOptions; protected $createMissingOptions;
protected $parentCategoryIdToCreateMissingOptions; protected $parentCategoryIdToCreateMissingOptions;
protected $em; protected $em;
protected $ontologyMapping;
protected $allNewFields;
protected $coreFields = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'owner', 'source']; protected $coreFields = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'owner', 'source'];
protected $mappedCoreFields = [ protected $mappedCoreFields = [
'title' => 'name', 'title' => 'name',
...@@ -99,26 +101,41 @@ class ElementImportMappingService ...@@ -99,26 +101,41 @@ class ElementImportMappingService
public function collectOntology($data, $import) public function collectOntology($data, $import)
{ {
$ontologyMapping = $import->getOntologyMapping(); $this->ontologyMapping = $import->getOntologyMapping();
$allNewFields = []; $this->allNewFields = [];
foreach($data as $row) foreach($data as $row)
{ {
foreach ($row as $key => $value) { foreach ($row as $key => $value) {
if (!in_array($key, $allNewFields)) $allNewFields[] = $key; $this->collectKey($key);
if (!array_key_exists($key, $ontologyMapping)) { if ($this->isAssociativeArray($value)) {
$value = in_array($key, $this->coreFields) ? $key : ""; foreach ($value as $subkey => $subvalue) { $this->collectKey($subkey, $key); }
if (!$value && array_key_exists($key, $this->mappedCoreFields) && in_array($this->mappedCoreFields[$key], $this->coreFields))
$value = $this->mappedCoreFields[$key];
$ontologyMapping[$key] = $value;
} }
} }
} }
// delete no more used fields // delete no more used fields
foreach($ontologyMapping as $field => $mappedField) { foreach($this->ontologyMapping as $field => $mappedField) {
if (!in_array($field, $allNewFields)) unset($ontologyMapping[$field]); if (!in_array($field, $this->allNewFields)) unset($this->ontologyMapping[$field]);
} }
$import->setOntologyMapping($ontologyMapping); $import->setOntologyMapping($this->ontologyMapping);
}
private function collectKey($key, $parentKey = null) {
$keyName = $parentKey ? $parentKey . '/' . $key : $key;
if (!in_array($keyName, $this->allNewFields)) $this->allNewFields[] = $keyName;
if (!array_key_exists($keyName, $this->ontologyMapping)) {
$value = in_array($key, $this->coreFields) ? $key : "";
if (!$value && array_key_exists($key, $this->mappedCoreFields) && in_array($this->mappedCoreFields[$key], $this->coreFields))
$value = $this->mappedCoreFields[$key];
$this->ontologyMapping[$keyName] = $value;
}
}
private function isAssociativeArray($a) {
if (!is_array($a)) return false;
foreach(array_keys($a) as $key)
if (!is_int($key)) return TRUE;
return FALSE;
} }
public function collectTaxonomy($data, $import) public function collectTaxonomy($data, $import)
...@@ -164,12 +181,21 @@ class ElementImportMappingService ...@@ -164,12 +181,21 @@ class ElementImportMappingService
foreach ($data as $key => $row) { foreach ($data as $key => $row) {
foreach ($mapping as $search => $replace) { foreach ($mapping as $search => $replace) {
$searchKeys = explode('/', $search);
if (count($searchKeys) == 2) { $searchkey = $searchKeys[0]; $subkey = $searchKeys[1]; }
else { $searchkey = $searchKeys[0]; $subkey = null; }
if ($replace == '/' || $replace == '') { if ($replace == '/' || $replace == '') {
unset($data[$key][$search]); unset($data[$key][$search]);
} }
else if (isset($row[$search]) && !isset($row[$replace])) { else if (isset($row[$searchkey]) && !isset($row[$replace])) {
$data[$key][$replace] = $data[$key][$search]; if ($subkey) {
unset($data[$key][$search]); $data[$key][$replace] = $row[$searchkey][$subkey];
unset($data[$key][$searchkey][$subkey]);
} else {
$data[$key][$replace] = $row[$searchkey];
unset($data[$key][$searchkey]);
}
} }
} }
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"id":"Rhm", "id":"Rhm",
"newfields": "balam", "newfields": "balam",
"name":"Entropie", "name":"Entropie",
"geo":{"latitude":45.18679,"longitude":5.70548}, "coord":{"latitude":45.18679,"longitude":5.70548},
"sourceKey":"CapOuPasCap", "sourceKey":"CapOuPasCap",
"address":{"streetAddress":"5 rue Georges Jacquet Grenoble"}, "address":{"streetAddress":"5 rue Georges Jacquet Grenoble"},
"createdAt":"21/12/2018 à 18:26", "createdAt":"21/12/2018 à 18:26",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment