Commit 1aef2621 authored by Sebastian Castro's avatar Sebastian Castro

Import mapping: create new categories when they do not exist

parent 704f5069
......@@ -22,6 +22,8 @@ use Biopen\CoreBundle\Document\GoGoLogLevel;
class ElementImportMappingService
{
protected $import;
protected $createMissingOptions;
protected $parentCategoryIdToCreateMissingOptions;
protected $em;
protected $coreFields = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'owner', 'source'];
......@@ -33,15 +35,18 @@ class ElementImportMappingService
public function transform($data, $import)
{
$this->import = $import;
$this->createMissingOptions = $import->getCreateMissingOptions();
$parent = $import->getParentCategoryToCreateOptions() ?: $this->em->getRepository('BiopenGeoDirectoryBundle:Category')->findOneByIsRootCategory(true);
$this->parentCategoryIdToCreateMissingOptions = $parent->getId();
$this->collectOntology($data, $import);
$data = $this->mapOntology($data);
// remove empty row, i.e. without name
$data = array_filter($data, function($row) { return array_key_exists('name', $row); });
$data = $this->addMissingFieldsToData($data);
dump($data);
$this->collectTaxonomy($data, $import);
$data = $this->mapTaxonomy($data);
dump($data);
$this->em->persist($import);
$this->em->flush();
return $data;
......@@ -81,11 +86,22 @@ class ElementImportMappingService
$categories = is_array($categories) ? $categories : explode(',', $categories);
foreach($categories as $category) {
if (!in_array($category, $allNewCategories)) $allNewCategories[] = $category;
if ($category && !array_key_exists($category, $taxonomyMapping)) {
if ($category && !array_key_exists($category, $taxonomyMapping))
{
$categorySlug = $this->slugify($category);
$value = array_key_exists($categorySlug, $this->mappingTableIds) ? $this->mappingTableIds[$categorySlug]['id'] : '';
// create option if does not exist
if ($value == '' && $this->createMissingOptions) $value = $this->createOption($category);
$taxonomyMapping[$category] = $value;
}
// create options for previously imported non mapped options
if (array_key_exists($category, $taxonomyMapping)
&& (!$taxonomyMapping[$category] || $taxonomyMapping[$category] == '/')
&& $this->createMissingOptions) {
$taxonomyMapping[$category] = $this->createOption($category);
}
}
}
// delete no more used categories
......@@ -154,26 +170,6 @@ class ElementImportMappingService
}
}
public function automapTaxonomy()
{
foreach($options as $optionName)
{
if ($optionName)
{
$optionNameSlug = $this->slugify($optionName);
$optionExists = array_key_exists($optionNameSlug, $this->mappingTableIds);
// create option if does not exist
if (!$optionExists && $this->createMissingOptions) { $this->createOption($optionName); $optionExists = true; }
if ($optionExists)
// we add option id and parent options if not already added (because import works only with the lower level of options)
foreach ($this->mappingTableIds[$optionNameSlug]['idAndParentsId'] as $key => $optionId)
if (!in_array($optionId, $optionsIdAdded)) $optionsIdAdded[] = $this->addOptionValue($element, $optionId);
}
}
}
private function createOption($name)
{
$option = new Option();
......@@ -184,6 +180,7 @@ class ElementImportMappingService
$option->setUseColorForMarker(false);
$this->em->persist($option);
$this->createOptionsMappingTable([$option]);
return $option->getId();
}
private function slugify($text)
......
......@@ -19,9 +19,7 @@ class ElementImportOneService
private $geocoder;
private $interactionService;
protected $createMissingOptions;
protected $optionIdsToAddToEachElement = [];
protected $parentCategoryIdToCreateMissingOptions;
protected $coreFields = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'owner', 'source'];
protected $privateDataProps;
......@@ -39,10 +37,6 @@ class ElementImportOneService
public function initialize($import)
{
// initialize create missing options configuration
$this->createMissingOptions = $import->getCreateMissingOptions();
$parent = $import->getParentCategoryToCreateOptions() ?: $this->em->getRepository('BiopenGeoDirectoryBundle:Category')->findOneByIsRootCategory(true);
$this->parentCategoryIdToCreateMissingOptions = $parent->getId();
$this->optionIdsToAddToEachElement = [];
foreach ($import->getOptionsToAddToEachElement() as $option) {
$this->optionIdsToAddToEachElement[] = $option->getId();
......@@ -225,7 +219,7 @@ class ElementImportOneService
private function addOptionValue($element, $id)
{
if (!$id) return;
if (!$id || $id == "0" || $id == 0) return;
$optionValue = new OptionValue();
$optionValue->setOptionId($id);
$optionValue->setIndex(0);
......
......@@ -10,7 +10,7 @@
"address":{"streetAddress":"5 rue Georges Jacquet Grenoble"},
"createdAt":"21/12/2018 à 18:26",
"updatedAt":"17/03/2019 à 13:38",
"categories": ["vert", "tandem", "Totoé"],
"categories": ["vert", "tandem", "new"],
"abstract": "Ateliers bricole",
"openHoursString": "",
"openHours": null,
......
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