Commit e506aa4c authored by Sebastian Castro's avatar Sebastian Castro

Element import: map to multiple categories

parent 38b50147
......@@ -30,9 +30,9 @@ class ImportAdmin extends AbstractAdmin
$repo = $dm->getRepository('BiopenGeoDirectoryBundle:Element');
$formProperties = json_encode($repo->findFormProperties());
$elementProperties = json_encode($repo->findDataCustomProperties());
$optionsList = $dm->getRepository('BiopenGeoDirectoryBundle:Option')->createQueryBuilder()
->select('name')->hydrate(false)->getQuery()->execute()->toArray();
$optionsList = json_encode($optionsList);
$taxonomy = $dm->getRepository('BiopenGeoDirectoryBundle:Taxonomy')->findTaxonomy();
$optionsList = $taxonomy->getTaxonomyJson();
$isDynamic = $this->getSubject()->isDynamicImport();
$title = $isDynamic ? "Import Dynamique, pour afficher des données gérées par quelqu'un d'autre" : "Importer des données en dur, depuis un fichier CSV ou une API Json";
......
......@@ -93,7 +93,13 @@ class ImportAdminController extends Controller
// ----- CUSTOM -------
$object->setOntologyMapping($request->get('ontology'));
$object->setTaxonomyMapping($request->get('taxonomy'));
$taxonomy = array_map(function($value) {
$array = explode(',', $value[0]);
return array_filter($array, function($el) { return $el != '/'; });
}, $request->get('taxonomy'));
$object->setTaxonomyMapping($taxonomy);
$object->setNewOntologyToMap(false);
$object->setNewTaxonomyToMap(false);
......
......@@ -6,7 +6,7 @@
<table class="table">
<thead>
<tr>
<th>Catégories lues depuis le fichier d'origine</th>
<th class="original">Catégories lues depuis le fichier d'origine</th>
<th style="width:20px"></th>
<th>Catégories du site</th>
<th style="width:20px"></th>
......@@ -17,7 +17,7 @@
<tr>
<td class="original">{{originName}}</td>
<td>></td>
<td class="mapped"><input type="text" name="taxonomy[{{originName}}]" class="form-control category-selector" value={{mappedName}} /></td>
<td class="mapped"><input type="text" name="taxonomy[{{originName}}][]" class="form-control category-selector" value="{{mappedName|join(',')}}"/></td>
<td><i class="clear-icon fa fa-ban" title="Ne pas utiliser cette catégorie"></i></td>
</tr>
{% endfor %}
......@@ -28,20 +28,48 @@
<div class="alert alert-info">Veuillez d'abords sélectionner le champs correspondant aux catégories dans l'onglet 'Table de correspondance des champs' puis cliquer sur 'Lire les données'</div>
{% endif %}
<style>
.original { width: 30%; }
</style>
<script>
jQuery(document).ready(function() {
var optionsList = {{ form.vars.attr["data-options"]|raw }};
var taxonomy = {{ form.vars.attr["data-options"]|raw }};
console.log(taxonomy);
var formData = [{ id: "", text: ""}]
for(var key in optionsList) {
var option = optionsList[key];
formData.push({id: option['_id'], text: option.name});
function recursivelyAddOption(category, parentOption)
{
console.log("category", category.name);
for(var i = 0; i < category.options.length; i++)
{
var option = category.options[i];
console.log("option", option.name);
var optionName = parentOption ? parentOption + '/' : '';
optionName += option.name;
formData.push({id: option.id, text: optionName});
if (option.subcategories)
{
for(var j = 0; j < option.subcategories.length; j++)
{
recursivelyAddOption(option.subcategories[j], optionName);
}
}
}
}
for(var k = 0; k < taxonomy.length; k++) recursivelyAddOption(taxonomy[k], null);
console.log(formData);
// for(var key in taxonomy) {
// var option = taxonomy[key];
// formData.push({id: option['_id'], text: option.name});
// }
$(".category-selector").select2({
data: formData
data: formData,
multiple: true
});
$('.clear-icon').click(function() {
......
......@@ -257,20 +257,25 @@ class ElementImportMappingService
foreach ($row['categories'] as $category)
{
$val = is_array($category) ? $category['name'] : $category;
if (array_key_exists($val, $mapping) && array_key_exists($mapping[$val], $this->mappingTableIds))
if (isset($mapping[$val]) && $mapping[$val])
{
$newcat['originalValue'] = $val;
$newcat['mappedName'] = $this->mappingTableIds[$mapping[$val]]['name'];
$newcat['mappedId'] = $this->mappingTableIds[$mapping[$val]]['id'];
if (isset($category['index'])) $newcat['index'] = $category['index'];
if (isset($category['description'])) $newcat['description'] = $category['description'];
$categories[] = $newcat;
$categoriesIds[] = $newcat['mappedId'];
$parentIds = $this->mappingTableIds[$mapping[$val]]['idAndParentsId'];
foreach ($parentIds as $id) {
if (!in_array($id, $categoriesIds)) {
$categories[] = ['mappedId' => $id, 'info' => "Automatiquement ajoutée (category parente d'une category importée)"];
$categoriesIds[] = $id;
foreach ($mapping[$val] as $mappedCategory) {
if (array_key_exists($mappedCategory, $this->mappingTableIds))
{
$newcat['originalValue'] = $val;
$newcat['mappedName'] = $this->mappingTableIds[$mappedCategory]['name'];
$newcat['mappedId'] = $this->mappingTableIds[$mappedCategory]['id'];
if (isset($category['index'])) $newcat['index'] = $category['index'];
if (isset($category['description'])) $newcat['description'] = $category['description'];
$categories[] = $newcat;
$categoriesIds[] = $newcat['mappedId'];
$parentIds = $this->mappingTableIds[$mappedCategory]['idAndParentsId'];
foreach ($parentIds as $id) {
if (!in_array($id, $categoriesIds)) {
$categories[] = ['mappedId' => $id, 'info' => "Automatiquement ajoutée (category parente d'une category importée)"];
$categoriesIds[] = $id;
}
}
}
}
}
......
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