Skip to content
Snippets Groups Projects
Unverified Commit f117f102 authored by Sebastian Castro's avatar Sebastian Castro
Browse files

Clean import admin

parent 47c6d43e
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,9 @@ use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Sonata\AdminBundle\Admin\AbstractAdmin;
class ImportAdmin extends ImportAbstractAdmin
class ImportAdmin extends AbstractAdmin
{
public function getTemplate($name)
{
......@@ -104,12 +105,6 @@ class ImportAdmin extends ImportAbstractAdmin
}
}
$formMapper
->tab('Aide')
->with("Aide", ['box_class' => 'box box-default', "description" => $this->getInstructions('13154fa0-13c2-41f1-a4ad-e04c35c86e89')])
->end()
->end();
}
protected function configureRoutes(RouteCollection $collection)
......
<?php
namespace Biopen\GeoDirectoryBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
class ImportAdmin extends ImportAbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with("Importer des données en dur, depuis un fichier CSV ou une API Json")
->add('sourceName', 'text', array(
'required' => true,
'label' => 'Nom de la source des données'))
->add('file', 'file', array('label' => 'Fichier à importer', 'required' => false))
->add('url', 'text', array('label' => 'Ou Url vers une API Json', 'required' => false))
->add('geocodeIfNecessary', null, array('required' => false, 'label' => 'Géocoder les élements sans latitude ni longitude à partir de leur adresse'))
// ->add('parentCategoryToCreateOptions', 'sonata_type_model', array(
// 'class'=> 'Biopen\GeoDirectoryBundle\Document\Category',
// 'required' => false,
// 'btn_add' => false,
// 'label' => 'Groupe dans lequel créer les catégories manquantes',
// 'mapped' => true), array('admin_code' => 'admin.category'))
->add('createMissingOptions', null, array('required' => false, 'label' => 'Créer les catégories manquantes à partir des catégories renseignées dans chaque élément'))
->add('optionsToAddToEachElement', 'sonata_type_model', array(
'class'=> 'Biopen\GeoDirectoryBundle\Document\Option',
'required' => false,
'choices_as_values' => true,
'multiple' => true,
'btn_add' => false,
'label' => 'Catégories à ajouter à chaque élément importé'), array('admin_code' => 'admin.option'))
->end()
->with('Aide', ["description" => $this->getInstructions('05a0895f-7b11-4a96-a98b-43fef68840a1')])
->end()
;
}
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('execute', $this->getRouterIdParameter().'/execute');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('id')
;
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('fileName')
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
)
))
;
}
}
\ No newline at end of file
<?php
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\ImportState;
class ImportAdminController extends Controller
{
public function listAction()
{
return $this->redirect($this->admin->generateUrl('create'));
}
protected function configureRoutes(RouteCollection $collection)
{
$collection->remove('edit');
}
private function executeImport($import)
{
$object = $import;
$em = $this->get('doctrine_mongodb')->getManager();
$em->flush();
$object->setCurrState(ImportState::Started);
$object->setCurrMessage("En attente...");
$em->persist($object);
$em->flush();
$this->get('biopen.async')->callCommand('app:elements:importSource', [$object->getId()]);
// $result = $this->get('biopen.element_import')->startImport($object);
$redirectionUrl = $this->admin->generateUrl('create');
$stateUrl = $this->generateUrl('biopen_import_state', ['id' => $object->getId()]);
return $this->render('@BiopenAdmin/pages/import/import-progress.html.twig', [
'import' => $object,
'redirectUrl' => $redirectionUrl,
'stateUrl' => $stateUrl
]);
}
// This method is just an overwrite of the SonataAdminCRUDController for calling the executeImport once the document is created
public function createAction()
{
$request = $this->getRequest();
$this->admin->checkAccess('create');
$object = $this->admin->getNewInstance();
$this->admin->setSubject($object);
$form = $this->admin->getForm();
$form->setData($object);
$form->handleRequest($request);
if ($form->isSubmitted()) {
$isFormValid = $form->isValid();
// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid) {
try {
$object = $this->admin->create($object);
return $this->executeImport($object);
} catch (ModelManagerException $e) {
$this->handleModelManagerException($e);
$isFormValid = false;
}
}
if (!$isFormValid) { // show an error message if the form failed validation
$text = $this->trans('flash_create_error', array('%name%' => $this->escapeHtml($this->admin->toString($object))), 'SonataAdminBundle');
$this->addFlash('sonata_flash_error', $text);
}
}
$view = $form->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
return $this->render('@BiopenAdmin/edit/edit_import.html.twig', array(
'action' => 'create',
'form' => $view,
'object' => $object,
), null);
}
}
\ No newline at end of file
......@@ -49,20 +49,25 @@
var formProperties = {{ form.vars.attr["data-form-props"]|raw }};
var dataProperties = {{ form.vars.attr["data-props"]|raw }};
var coreFields = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'owner', 'source']
var formData = [], allProperties = [], otherData = []; var importedData = [];
formData.push({id: 'id', text: 'Identifiant unique'});
formData.push({id: 'name', text: 'Titre de la fiche'});
formData.push({id: 'categories', text: 'Liste des catégories (en tableau ou séparées par des virgules)'});
formData.push({id: 'streetNumber', text: 'Adresse: numéro de rue'});
formData.push({id: 'streetAddress', text: 'Adresse: rue'});
formData.push({id: 'addressLocality', text: 'Adresse: ville'});
formData.push({id: 'postalCode', text: 'Adresse: code postal'});
formData.push({id: 'addressCountry', text: 'Adresse: pays'});
formData.push({id: 'latitude', text: 'Latitude'});
formData.push({id: 'longitude', text: 'Longitude'});
var coreData = [], allProperties = [], otherData = []; var importedData = [];
coreData.push({id: 'id', text: 'Identifiant unique'});
coreData.push({id: 'name', text: 'Titre de la fiche'});
coreData.push({id: 'categories', text: 'Liste des catégories (en tableau ou séparées par des virgules)'});
coreData.push({id: 'fullAddress', text: 'Adresse complète'});
coreData.push({id: 'streetNumber', text: 'Adresse: numéro de rue'});
coreData.push({id: 'streetAddress', text: 'Adresse: rue'});
coreData.push({id: 'addressLocality', text: 'Adresse: ville'});
coreData.push({id: 'postalCode', text: 'Adresse: code postal'});
coreData.push({id: 'addressCountry', text: 'Adresse: pays'});
coreData.push({id: 'latitude', text: 'Latitude'});
coreData.push({id: 'longitude', text: 'Longitude'});
coreData.push({id: 'email', text: "Email de l'élement"});
coreData.push({id: 'owner', text: "Email de l'utilisateur propriétaire de la fiche"});
coreData.push({id: 'source', text: "Origine de l'élément (source)"});
allProperties = $.map(formData, function(el) { return el.id });
allProperties = $.map(coreData, function(el) { return el.id });
var formData = [];
for(var i = 0; i < formProperties.length; i++) {
var prop = formProperties[i];
allProperties.push(prop);
......@@ -92,6 +97,7 @@
{% endfor %}
options = [{ id: "/", text: ""}]
options.push({ text: "Attributs principaux", children: coreData});
options.push({ text: "Attributs du formulaire", children: formData});
if (otherData.length > 0) options.push({text: "Autres attributs", children: otherData});
options.push({ text: "Attributs importés", children: importedData});
......
......@@ -226,6 +226,10 @@ class ElementImportMappingService
$data[$key]['streetAddress'] = $data[$key]['streetNumber'] . ' ' . $data[$key]['streetAddress'];
unset($data[$key]['streetNumber']);
}
if (isset($data[$key]['fullAddress'])) {
$data[$key]['streetAddress'] = $data[$key]['fullAddress'];
unset($data[$key]['fullAddress']);
}
}
return $data;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment