Commit 93bb592f authored by Sebastian Castro's avatar Sebastian Castro

small fix re import csv

parent 78252fb4
......@@ -15,7 +15,8 @@
- DB cohérence
-> suppression d'une option ou catégorie doit supprimer toutes les sous options/catégrories, et retirer ces options à tous les éléments. De même pour les USe group
-> empecher de supprimer la catégorie principale
- Source de données
-> Importer des données. Si catégorie existe pas la créer
-> Utiliser une source externe. Configuration de la mise à jour automatique, plus URL d'update manuelle
......
......@@ -3,11 +3,8 @@
namespace Biopen\GeoDirectoryBundle\Controller\Admin;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\GeoDirectoryBundle\Document\Category;
class CategoryAdminController extends Controller
{
......
......@@ -26,7 +26,7 @@ class Import
private $sourceName = "";
/**
* @MongoDB\ReferenceOne(targetDocument="Biopen\GeoDirectoryBundle\Document\Category", inversedBy="options")
* @MongoDB\ReferenceOne(targetDocument="Biopen\GeoDirectoryBundle\Document\Category", cascade={"all"})
*/
private $parentCategoryToCreateOptions;
......
......@@ -7,7 +7,7 @@
*
* @copyright Copyright (c) 2016 Sebastian Castro - 90scastro@gmail.com
* @license MIT License
* @Last Modified time: 2018-01-19 13:04:59
* @Last Modified time: 2018-06-07 16:36:09
*/
......@@ -44,6 +44,12 @@ class TaxonomyRepository extends DocumentRepository
$qb = $this->createQueryBuilder('BiopenGeoDirectoryBundle:Taxonomy');
return $qb->getQuery()->getSingleResult()->getMainCategory();
}
public function findTaxonomy()
{
$qb = $this->createQueryBuilder('BiopenGeoDirectoryBundle:Taxonomy');
return $qb->getQuery()->getSingleResult();
}
}
......@@ -21,7 +21,7 @@ class ConvertCsvToArrayService {
if(!$header) {
$header = $row;
} else {
if (count($header) != count ($row)) return null;
if (count($header) != count ($row)) dump($row);
else $data[] = array_combine($header, $row);
}
}
......
......@@ -61,7 +61,8 @@ class ImportCsvService
$progress->start();
}
$this->parentCategoryToCreateMissingOptions = $import->getParentCategoryToCreateOptions() ? $import->getParentCategoryToCreateOptions() : $this->em->getRepository('BiopenGeoDirectoryBundle:Taxonomy')->findMainCategory();
$this->parentCategoryToCreateMissingOptions = $import->getParentCategoryToCreateOptions() ? $import->getParentCategoryToCreateOptions() : $this->em->getRepository('BiopenGeoDirectoryBundle:Taxonomy')->findMainCategory();
$this->missingOptionDefaultAttributesForCreate = [
"useIconForMarker" => false,
"useColorForMarker" => false,
......@@ -166,9 +167,9 @@ class ImportCsvService
return $data;
}
private function createOptionsMappingTable()
private function createOptionsMappingTable($options = null)
{
$options = $this->em->getRepository('BiopenGeoDirectoryBundle:Option')->findAll();
if ($options === null) $options = $this->em->getRepository('BiopenGeoDirectoryBundle:Option')->findAll();
foreach($options as $option)
{
......@@ -183,19 +184,25 @@ class ImportCsvService
private function createImages($element, $row)
{
if (strlen($row['images']) > 0)
if (strlen($row['images']) > 0) $optionsCsv = explode(',', $row['images']);
else
{
$keys = array_keys($row);
$image_keys = array_filter($keys, function($key) { return $this->startsWith($key, 'image'); });
$optionsCsv = array_map(function($key) use ($row) { return $row[$key]; }, $image_keys);
}
if (count($optionsCsv) == 0) return;
foreach($optionsCsv as $imageUrl)
{
$optionsCsv = explode(',', $row['images']);
foreach($optionsCsv as $imageUrl)
if (strlen($imageUrl) > 5)
{
if (strlen($imageUrl) > 5)
{
$elementImage = new ElementImage();
$elementImage->setExternalImageUrl($imageUrl);
$element->addImage($elementImage);
}
}
}
$elementImage = new ElementImage();
$elementImage->setExternalImageUrl($imageUrl);
$element->addImage($elementImage);
}
}
}
private function createUrls($element, $row)
......@@ -261,6 +268,7 @@ class ImportCsvService
private function createOption($name)
{
$this->em->persist($this->parentCategoryToCreateMissingOptions);
$option = new Option();
$option->setName($name);
$option->setParent($this->parentCategoryToCreateMissingOptions);
......@@ -268,8 +276,9 @@ class ImportCsvService
$option->setUseColorForMarker($this->missingOptionDefaultAttributesForCreate["useColorForMarker"]);
$option->setDisplayOption($this->missingOptionDefaultAttributesForCreate["displayOption"]);
$this->em->persist($option);
$this->em->flush();
$this->createOptionsMappingTable();
// $this->em->flush();
// dump("new option", $option);
$this->createOptionsMappingTable([$option]);
}
private function slugify($text)
......
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