Unverified Commit 8017f445 authored by Sebastian Castro's avatar Sebastian Castro
Browse files

[import] map multiple attribute to "categories" and concatenate them

parent 2191f553
......@@ -110,7 +110,6 @@ class ElementImportMappingService
unset($data[$key]);
}
}
// Ontology
$this->collectOntology($data, $import);
$data = $this->mapOntology($data);
......@@ -248,64 +247,73 @@ class ElementImportMappingService
{
$mapping = $this->import->getOntologyMapping();
foreach ($data as $key => $row) {
$newRow = [];
// First map nested fields
foreach ($mapping as $search => $replace) {
$searchKeys = explode('/', $search);
if (count($searchKeys) == 2) {
$searchkey = $searchKeys[0]; $subkey = $searchKeys[1];
if (!in_array($replace, ['/', '']) && isset($data[$key][$searchkey]) && isset($row[$searchkey][$subkey]) && !isset($data[$key][$replace])) {
$data[$key][$replace] = $row[$searchkey][$subkey];
unset($data[$key][$searchkey][$subkey]);
if (!in_array($replace, ['/', '']) && isset($data[$key][$searchkey]) && isset($row[$searchkey][$subkey]))
{
$newRow = $this->mapAttribute($newRow, $replace, $data[$key][$searchkey][$subkey]);
}
}
}
// Then remove non mapped fields
foreach ($mapping as $search => $replace) {
if (in_array($replace, ['/', ''])) unset($data[$key][$search]);
}
// Finally map non nested fields
foreach ($mapping as $search => $replace) {
$searchKeys = explode('/', $search);
if (count($searchKeys) == 1) {
$searchkey = $search;
if (!in_array($replace, ['/', '']) && isset($data[$key][$searchkey]) && !isset($data[$key][$replace])) {
$data[$key][$replace] = $row[$searchkey];
unset($data[$key][$searchkey]);
if (!in_array($replace, ['/', '']) && isset($data[$key][$searchkey])) {
$newRow = $this->mapAttribute($newRow, $replace, $data[$key][$searchkey]);
}
}
}
// add streetNumber into streetAddress
if (isset($data[$key]['streetNumber']) && isset($data[$key]['streetAddress'])) {
$data[$key]['streetAddress'] = $data[$key]['streetNumber'] . ' ' . $data[$key]['streetAddress'];
unset($data[$key]['streetNumber']);
if (isset($newRow['streetNumber']) && isset($newRow['streetAddress'])) {
$newRow['streetAddress'] = $newRow['streetNumber'] . ' ' . $newRow['streetAddress'];
}
if (isset($data[$key]['fullAddress'])) {
$data[$key]['streetAddress'] = $data[$key]['fullAddress'];
unset($data[$key]['fullAddress']);
if (isset($newRow['fullAddress'])) {
$newRow['streetAddress'] = $newRow['fullAddress'];
}
// convert lat/long numeric into string
if (isset($data[$key]['latitude']) && is_numeric($data[$key]['latitude']))
$data[$key]['latitude'] = '' . $data[$key]['latitude'];
if (isset($data[$key]['longitude']) && is_numeric($data[$key]['longitude']))
$data[$key]['longitude'] = '' . $data[$key]['longitude'];
}
if (isset($newRow['latitude']) && is_numeric($newRow['latitude']))
$newRow['latitude'] = '' . $newRow['latitude'];
if (isset($newRow['longitude']) && is_numeric($newRow['longitude']))
$newRow['longitude'] = '' . $newRow['longitude'];
$data[$key] = $newRow;
}
return $data;
}
// map $value inside $newKey attribute
private function mapAttribute($newRow, $newKey, $value)
{
// We allow that multiple keys maps to categories. In this case they will be concatenated
if ($newKey == 'categories') {
if (is_string($value)) $value = explode(',', $value);
$oldVal = isset($newRow[$newKey]) ? $newRow[$newKey] : [];
$value = array_merge($oldVal, $value);
}
// replacing existing value only if not set, or if categories because values have been merged
if (!isset($newRow[$newKey]) || $newKey == 'categories') {
$newRow[$newKey] = $value;
}
return $newRow;
}
private function mapTaxonomy($data)
{
$mapping = $this->import->getTaxonomyMapping();
foreach ($data as $key => $row)
{
if (isset($data[$key]['categories'])) {
if (is_string($row['categories'])) $row['categories'] = explode(',', $row['categories']);
$categories = []; $categoriesIds = [];
foreach ($row['categories'] as $category)
{
......
......@@ -6,6 +6,7 @@ use Doctrine\ODM\MongoDB\DocumentManager;
use Biopen\GeoDirectoryBundle\Document\Element;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\GeoDirectoryBundle\Document\UserInteractionContribution;
use Biopen\GeoDirectoryBundle\Document\UserInteraction;
use Biopen\GeoDirectoryBundle\Document\ImportState;
use Biopen\CoreBundle\Document\GoGoLogImport;
use Biopen\GeoDirectoryBundle\Document\ModerationState;
......
......@@ -11,10 +11,8 @@
"website": "mysite.fr",
"description": "oh yeah",
"tarara": 452,
"categories": [
{ "name": "Nature", "index": 5, "description": "balade en forêt"},
{ "name": "Numérique", "index": 1}
],
"categories": ["one", "two"],
"categories2": ["orange"],
"website": "https:\/\/www.asso-entropie.fr\/",
"images": ["https:\/\/www.capoupascap.info\/wp-content\/uploads\/2018\/12\/entropie.png"],
"files": ["https:\/\/toto.fr\/wp-content\/file.pdf", "https:\/\/aabjka.fr\/wp-content\/file.pdf"],
......@@ -33,7 +31,8 @@
"address": "rue Bernard, Tours",
"sourceKey":"CapOuPasCap",
"updatedAt": "10/08/2019",
"categories": [6]
"categories": ["one"],
"categories2": ["blue"]
},
{
"id":"sa",
......@@ -41,7 +40,8 @@
"coord":{"latitude":46.18679,"longitude":5.70548},
"address": "rue Jacquet, Labrit",
"sourceKey":"CapOuPasCap",
"categories": [2],
"categories": ["two"],
"categories2": ["blue", "orange"],
"updatedAt": "11/08/2019",
"website": "https:\/\/www.asso-cala.fr\/"
}
......
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