From 6f62ba07bd6581018a30ab0502ff751cbdd56a94 Mon Sep 17 00:00:00 2001 From: Sebastian Castro <sebastian.castro@protonmail.com> Date: Thu, 1 Aug 2019 09:47:30 +0200 Subject: [PATCH] Merge Import and importDynamic Admin, so both use mapping tables --- .../SonataAdminBundle/views/layout.html.twig | 1 + .../GeoDirectoryBundle/Admin/ImportAdmin.php | 165 +++++++++-- .../Admin/ImportAdmin_old.php | 63 ++++ .../Admin/ImportDynamicAdmin.php | 138 --------- .../Admin/ImportAdminController.php | 268 +++++++++++++----- .../Admin/ImportAdminController_old.php | 91 ++++++ .../Admin/ImportDynamicAdminController.php | 214 -------------- .../GeoDirectoryBundle/Document/Import.php | 29 ++ .../Document/ImportDynamic.php | 40 +-- .../DatabaseIntegrityWatcher.php | 3 +- .../Resources/config/services_admin.yml | 14 +- .../views/admin/edit/edit_import.html.twig | 35 ++- .../admin/edit/edit_import_dynamic.html.twig | 35 --- .../views/admin/list/list_import.html.twig | 7 + .../admin/list/list_import_dynamic.html.twig | 7 + .../admin/pages/import/show-data.html.twig | 18 ++ .../import/list_last_refresh.html.twig | 6 +- .../partials/list__action_refresh.html.twig | 2 +- .../Services/ElementImportMappingService.php | 4 +- .../Services/ElementImportOneService.php | 4 +- .../Services/ElementImportService.php | 24 +- 21 files changed, 619 insertions(+), 549 deletions(-) create mode 100755 src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin_old.php delete mode 100755 src/Biopen/GeoDirectoryBundle/Admin/ImportDynamicAdmin.php create mode 100755 src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController_old.php delete mode 100755 src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportDynamicAdminController.php delete mode 100755 src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import_dynamic.html.twig create mode 100644 src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import.html.twig create mode 100644 src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import_dynamic.html.twig create mode 100644 src/Biopen/GeoDirectoryBundle/Resources/views/admin/pages/import/show-data.html.twig diff --git a/app/Resources/SonataAdminBundle/views/layout.html.twig b/app/Resources/SonataAdminBundle/views/layout.html.twig index 8c04727df..e0e7c6eb7 100755 --- a/app/Resources/SonataAdminBundle/views/layout.html.twig +++ b/app/Resources/SonataAdminBundle/views/layout.html.twig @@ -84,6 +84,7 @@ pre { font-size: 11px; line-height: 13px; + text-align: left; } </style> diff --git a/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php b/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php index bf0ba88ec..4d7b8c781 100755 --- a/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php +++ b/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php @@ -2,60 +2,163 @@ 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; +use Biopen\GeoDirectoryBundle\Document\ElementStatus; class ImportAdmin extends ImportAbstractAdmin { + public function getTemplate($name) + { + $isDynamic = $this->getClass() == "Biopen\GeoDirectoryBundle\Document\ImportDynamic"; + switch ($name) { + case 'edit': return '@BiopenAdmin/edit/edit_import.html.twig'; + break; + case 'list': return $isDynamic ? '@BiopenAdmin/list/list_import_dynamic.html.twig' : '@BiopenAdmin/list/list_import.html.twig'; + break; + default : return parent::getTemplate($name); + break; + } + } + protected function configureFormFields(FormMapper $formMapper) { + $dm = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb'); + $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); + + $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"; $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')]) + ->tab('Général') + ->with($title, ['class' => 'col-md-6']) + ->add('sourceName', 'text', array('required' => true, 'label' => 'Nom de la source ')) + ->add('file', 'file', array('label' => 'Fichier CSV à importer', 'required' => false)) + ->add('url', 'text', array('label' => "Ou URL vers un API Json", 'required' => false)); + if ($isDynamic) + $formMapper + ->add('refreshFrequencyInDays', null, array('required' => false, 'label' => "Fréquence de mise à jours des données en jours (laisser vide pour ne jamais mettre à jour automatiquement")) + ->add('idsToIgnore', 'text', array('required' => false, 'attr' => ['class' => 'gogo-display-array'], 'label' => "Liste des IDs qui seront ignorées lors de l'import", 'label_attr' => ['title' => "Pour ignorer un élément, supprimer le (définitivement) et il ne sera plus jamais importé. Si vous supprimez un élément dynamiquement importé juste en changeant son status (soft delete), l'élément sera quand meme importé mais conservera son status supprimé. Vous pourrez donc à tout moment restaurer cet élement pour le voir apparaitre de nouveau"])); + $formMapper + ->end() + ->with("Autres options", ['box_class' => 'box box-default', 'class' => 'col-md-6']) + ->add('geocodeIfNecessary', null, array('required' => false, 'label' => 'Géocoder les élements sans latitude ni longitude à partir de leur adresse')) + ->add('createMissingOptions', null, array('required' => false, 'label' => 'Créer les catégories manquantes', 'label_attr' => ['title' => "Si un élément importé a une catégorie qui n'existe pas encore sur votre carte, elle sera automatiquement crée"])) + ->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')) + ->add('needToHaveOptionsOtherThanTheOnesAddedToEachElements', null, array('required' => false, 'label' => 'Les éléments importés doivent contenir au moins une catégorie en dehors de celles ajoutées manuellement ci-dessus', 'label_attr' => ['title' => "Sans prendre en compte les catégories ajoutés via le champs \"Catégories à ajouter à chaque élément importé\", si les éléments importés n'ont pas de catégories, ils seront marqués comme \"Modération aucune catégorie renseignée\""])); + if ($isDynamic) + $formMapper + ->add('fieldToCheckElementHaveBeenUpdated', null, array('required' => false, 'label' => "Nom de l'attribut à comparer pour la mise à jour", 'label_attr' => ['title' => "Lorsqu'on met à jour une source, certains des éléments à importer existent déjà dans notre base de donnée. Vous pouvez renseigner ici un champs qui permettra de comparer si l'élément à été mis à jour au sein de la source depuis le dernier import. Exple de champ: updatedAt, date_maj etc... (laisser vide pour mettre à jour les éléments à chaque fois)"])); + $formMapper->end(); + if ($this->getSubject()->getId()) + { + $formMapper->with('Historique', array('class' => 'col-sm-12')) + ->add('logs', 'hidden', array('attr' => ['class' => 'gogo-display-logs'], 'mapped' => false)) + ->end(); + } + $formMapper->end(); + $formMapper->tab('Modifier les données en exécutant du code') + ->with('Entrez du code qui sera exécuté à la reception des données, avant leur traitement par GoGoCarto', ["description" => "La variable <b>\$data</b> représente le tableau PHP créé à partir des données Csv ou Json. Quelques examples de transformations simple: +<pre><?php</br>\$data = \$data['elements']</pre> +<pre><?php</br>foreach(\$data as \$key => \$row) { + \$data[\$key]['source'] = \"MySource\"; +}</pre> +<pre><?php</br>foreach(\$data as \$key => \$row) { + \$data[\$key]['latitude'] = \$row['geo']['latitude']); + \$data[\$key]['longitude'] = \$row['geo']['longitude']); +}</pre> +<pre><?php</br>foreach(\$data as \$key => \$row) { + \$data[\$key]['categories'] = array_map(function(\$cat) { return \$cat[0]; }, \$row['categories']); +}</pre>"]) + ->add('customCode', 'text', array('label' => 'Code PHP qui sera exécuté', 'attr' => ['class' => 'gogo-code-editor', 'format' => 'php', 'height' => '500'], 'required' => false)) ->end() - ; + ->end(); + + if ($this->getSubject()->getId()) + { + $formMapper + ->tab('Table de correspondance des champs') + ->with('Transformer les données à importer') + ->add('ontologyMapping', 'hidden', array('attr' => ['class' => 'gogo-mapping-ontology', 'data-form-props' => $formProperties, 'data-props' => $elementProperties])) + ->end() + ->end(); + if (count($this->getSubject()->getOntologyMapping()) > 0) + { + $formMapper->tab('Table de correspondance des catégories') + ->with('Faites correspondre les catégories') + ->add('taxonomyMapping', 'hidden', array('attr' => ['class' => 'gogo-mapping-taxonomy', 'data-options' => $optionsList])) + ->end() + ->end(); + } + + } + + $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) - { - $collection->add('execute', $this->getRouterIdParameter().'/execute'); - } + + protected function configureRoutes(RouteCollection $collection) + { + $collection->add('refresh', $this->getRouterIdParameter().'/refresh'); + $collection->add('collect', $this->getRouterIdParameter().'/collect'); + $collection->add('showData', $this->getRouterIdParameter().'/show-data'); + } + protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper - ->add('id') + ->add('sourceName') ; } + + public function createQuery($context = 'list') + { + $isDynamic = $this->getClass() == "Biopen\GeoDirectoryBundle\Document\ImportDynamic"; + $query = parent::createQuery($context); + if (!$isDynamic) $query->field('type')->equals('normal'); + $query->sort('updatedAt', 'DESC'); + return $query; + } + protected function configureListFields(ListMapper $listMapper) { + $dm = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb'); + $deletedElementsCount = $dm->getRepository('BiopenGeoDirectoryBundle:Element')->findDeletedElementsByImportIdCount(); + $isDynamic = $this->getClass() == "Biopen\GeoDirectoryBundle\Document\ImportDynamic"; + + $listMapper + ->addIdentifier('sourceName', null, array('label' => 'Nom de la source')) + // Total count + ->add('logs', null, array('label' => "Nombre d'éléments", 'template' => '@BiopenAdmin/partials/import/list_total_count.html.twig')) + // non visibles count + ; + if ($isDynamic) + $listMapper + ->add('idsToIgnore', null, array('label' => "Infos", 'template' => '@BiopenAdmin/partials/import/list_non_visibles_count.html.twig', 'choices' => $deletedElementsCount)) + ->add('refreshFrequencyInDays', null, array('label' => 'Mise à jour', 'template' => '@BiopenAdmin/partials/import/list_refresh_frequency.html.twig')); + $listMapper - ->addIdentifier('fileName') + ->add('lastRefresh', null, array('label' => 'Dernier import', 'template' => '@BiopenAdmin/partials/import/list_last_refresh.html.twig')) ->add('_action', 'actions', array( 'actions' => array( 'edit' => array(), + 'delete' => array(), + 'refresh' => array('template' => '@BiopenAdmin/partials/list__action_refresh.html.twig'), ) )) ; diff --git a/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin_old.php b/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin_old.php new file mode 100755 index 000000000..bf0ba88ec --- /dev/null +++ b/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin_old.php @@ -0,0 +1,63 @@ +<?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 diff --git a/src/Biopen/GeoDirectoryBundle/Admin/ImportDynamicAdmin.php b/src/Biopen/GeoDirectoryBundle/Admin/ImportDynamicAdmin.php deleted file mode 100755 index 6395e42b9..000000000 --- a/src/Biopen/GeoDirectoryBundle/Admin/ImportDynamicAdmin.php +++ /dev/null @@ -1,138 +0,0 @@ -<?php - -namespace Biopen\GeoDirectoryBundle\Admin; - -use Sonata\AdminBundle\Datagrid\ListMapper; -use Sonata\AdminBundle\Datagrid\DatagridMapper; -use Sonata\AdminBundle\Form\FormMapper; -use Sonata\AdminBundle\Route\RouteCollection; -use Biopen\GeoDirectoryBundle\Document\ElementStatus; - -class ImportDynamicAdmin extends ImportAbstractAdmin -{ - public function getTemplate($name) - { - switch ($name) { - case 'edit': return '@BiopenAdmin/edit/edit_import_dynamic.html.twig'; - break; - default : return parent::getTemplate($name); - break; - } - } - - protected function configureFormFields(FormMapper $formMapper) - { - $dm = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb'); - $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); - - $formMapper - ->tab('Général') - ->with("Import Dynamique, pour afficher des données gérées par quelqu'un d'autre", ['class' => 'col-md-6']) - ->add('sourceName', 'text', array('required' => true, 'label' => 'Nom de la source ')) - ->add('url', 'text', array('label' => "Url de l'api Json", 'required' => true)) - ->add('refreshFrequencyInDays', null, array('required' => false, 'label' => "Fréquence de mise à jours des données en jours (laisser vide pour ne jamais mettre à jour automatiquement")) - ->add('idsToIgnore', 'text', array('required' => false, 'attr' => ['class' => 'gogo-display-array'], 'label' => "Liste des IDs qui seront ignorées lors de l'import", 'label_attr' => ['title' => "Pour ignorer un élément, supprimer le (définitivement) et il ne sera plus jamais importé. Si vous supprimez un élément dynamiquement importé juste en changeant son status (soft delete), l'élément sera quand meme importé mais conservera son status supprimé. Vous pourrez donc à tout moment restaurer cet élement pour le voir apparaitre de nouveau"])) - ->end() - ->with("Autres options", ['box_class' => 'box box-default', 'class' => 'col-md-6']) - ->add('geocodeIfNecessary', null, array('required' => false, 'label' => 'Géocoder les élements sans latitude ni longitude à partir de leur adresse')) - ->add('createMissingOptions', null, array('required' => false, 'label' => 'Créer les catégories manquantes', 'label_attr' => ['title' => "Si un élément importé a une catégorie qui n'existe pas encore sur votre carte, elle sera automatiquement crée"])) - ->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')) - ->add('needToHaveOptionsOtherThanTheOnesAddedToEachElements', null, array('required' => false, 'label' => 'Les éléments importés doivent contenir au moins une catégorie en dehors de celles ajoutées manuellement ci-dessus', 'label_attr' => ['title' => "Sans prendre en compte les catégories ajoutés via le champs \"Catégories à ajouter à chaque élément importé\", si les éléments importés n'ont pas de catégories, ils seront marqués comme \"Modération aucune catégorie renseignée\""])) - ->add('fieldToCheckElementHaveBeenUpdated', null, array('required' => false, 'label' => "Nom de l'attribut à comparer pour la mise à jour", 'label_attr' => ['title' => "Lorsqu'on met à jour une source, certains des éléments à importer existent déjà dans notre base de donnée. Vous pouvez renseigner ici un champs qui permettra de comparer si l'élément à été mis à jour au sein de la source depuis le dernier import. Exple de champ: updatedAt, date_maj etc... (laisser vide pour mettre à jour les éléments à chaque fois)"])) - ->end(); - if ($this->getSubject()->getId()) - { - $formMapper->with('Historique', array('class' => 'col-sm-12')) - ->add('logs', 'hidden', array('attr' => ['class' => 'gogo-display-logs'], 'mapped' => false)) - ->end(); - } - $formMapper->end(); - $formMapper->tab('Modifier les données en exécutant du code') - ->with('Entrez du code qui sera exécuté à la reception des données, avant leur traitement par GoGoCarto', ["description" => "La variable <b>\$data</b> représente le tableau PHP créé à partir des données Csv ou Json. Quelques examples de transformations simple: -<pre><?php</br>\$data = \$data['elements']</pre> -<pre><?php</br>foreach(\$data as \$key => \$row) { - \$data[\$key]['source'] = \"MySource\"; -}</pre> -<pre><?php</br>foreach(\$data as \$key => \$row) { - \$data[\$key]['latitude'] = \$row['geo']['latitude']); - \$data[\$key]['longitude'] = \$row['geo']['longitude']); -}</pre> -<pre><?php</br>foreach(\$data as \$key => \$row) { - \$data[\$key]['categories'] = array_map(function(\$cat) { return \$cat[0]; }, \$row['categories']); -}</pre>"]) - ->add('customCode', 'text', array('label' => 'Code PHP qui sera exécuté', 'attr' => ['class' => 'gogo-code-editor', 'format' => 'php', 'height' => '500'], 'required' => false)) - ->end() - ->end(); - - if ($this->getSubject()->getId()) - { - $formMapper - ->tab('Table de correspondance des champs') - ->with('Transformer les données à importer') - ->add('ontologyMapping', 'hidden', array('attr' => ['class' => 'gogo-mapping-ontology', 'data-form-props' => $formProperties, 'data-props' => $elementProperties])) - ->end() - ->end(); - if (count($this->getSubject()->getOntologyMapping()) > 0) - { - $formMapper->tab('Table de correspondance des catégories') - ->with('Faites correspondre les catégories') - ->add('taxonomyMapping', 'hidden', array('attr' => ['class' => 'gogo-mapping-taxonomy', 'data-options' => $optionsList])) - ->end() - ->end(); - } - - } - - $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) - { - $collection->add('refresh', $this->getRouterIdParameter().'/refresh'); - $collection->add('collect', $this->getRouterIdParameter().'/collect'); - } - - protected function configureDatagridFilters(DatagridMapper $datagridMapper) - { - $datagridMapper - ->add('sourceName') - ; - } - protected function configureListFields(ListMapper $listMapper) - { - $dm = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb'); - $deletedElementsCount = $dm->getRepository('BiopenGeoDirectoryBundle:Element')->findDeletedElementsByImportIdCount(); - - $listMapper - ->addIdentifier('sourceName', null, array('label' => 'Nom de la source')) - // Total count - ->add('logs', null, array('label' => "Nombre d'éléments", 'template' => '@BiopenAdmin/partials/import/list_total_count.html.twig')) - // non visibles count - ->add('idsToIgnore', null, array('label' => "Infos", 'template' => '@BiopenAdmin/partials/import/list_non_visibles_count.html.twig', 'choices' => $deletedElementsCount)) - ->add('refreshFrequencyInDays', null, array('label' => 'Mise à jour', 'template' => '@BiopenAdmin/partials/import/list_refresh_frequency.html.twig')) - ->add('lastRefresh', null, array('label' => 'Derniere mise à jour', 'template' => '@BiopenAdmin/partials/import/list_last_refresh.html.twig')) - ->add('_action', 'actions', array( - 'actions' => array( - 'edit' => array(), - 'delete' => array(), - 'refresh' => array('template' => '@BiopenAdmin/partials/list__action_refresh.html.twig'), - ) - )) - ; - } -} \ No newline at end of file diff --git a/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController.php b/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController.php index c1abe542d..bb3750406 100755 --- a/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController.php +++ b/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController.php @@ -3,89 +3,227 @@ 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')); - } + public function collectAction() + { + $object = $this->admin->getSubject(); + $result = $this->get('biopen.element_import')->collectData($object); - protected function configureRoutes(RouteCollection $collection) - { - $collection->remove('edit'); - } + $showUrl = $this->admin->generateUrl('showData', ['id' => $object->getId()]); + $this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès.</br>Voici le résultat obtenu pour le premier élément à importer :<pre>" . print_r($result[0], true) . '</pre>' . "<a href='$showUrl'>Voir toutes les données</a>"); + $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]); + return $this->redirect($url); + } + + public function showDataAction() + { + $object = $this->admin->getSubject(); + $result = $this->get('biopen.element_import')->collectData($object); + + $dataDisplay = print_r($result, true); + $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]); + return $this->render('@BiopenAdmin/pages/import/show-data.html.twig', [ + 'dataDisplay' => $dataDisplay, + 'redirectUrl' => $url, + 'import' => $object + ]); + } + + public function refreshAction() + { + $object = $this->admin->getSubject(); + + $object->setCurrState(ImportState::Started); + $object->setCurrMessage("En attente..."); + $em = $this->get('doctrine_mongodb')->getManager(); + $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('edit', ['id' => $object->getId()]); + $stateUrl = $this->generateUrl('biopen_import_state', ['id' => $object->getId()]); + + return $this->render('@BiopenAdmin/pages/import/import-progress.html.twig', [ + 'import' => $object, + 'redirectUrl' => $redirectionUrl, + 'redirectListUrl' => $redirectionUrl = $this->admin->generateUrl('list'), + 'stateUrl' => $stateUrl + ]); + } + + /** + * Overite Sonata CRud Controller + */ + public function editAction($id = null) + { + $request = $this->getRequest(); + $id = $request->get($this->admin->getIdParameter()); + $object = $this->admin->getObject($id); + + if (!$object) throw $this->createNotFoundException(sprintf('unable to find the object with id : %s', $id)); + + $this->admin->checkAccess('edit', $object); + $this->admin->setSubject($object); + + $form = $this->admin->getForm(); + $form->setData($object); + $form->handleRequest($request); + + if ($form->isSubmitted()) { + //TODO: remove this check for 4.0 + if (method_exists($this->admin, 'preValidate')) { + $this->admin->preValidate($object); + } + $isFormValid = $form->isValid(); + + // persist if the form was valid and if in preview mode the preview was approved + if ($isFormValid) { + try { - private function executeImport($import) - { - $object = $import; - $em = $this->get('doctrine_mongodb')->getManager(); - $em->flush(); + // ----- CUSTOM ------- + $object->setOntologyMapping($request->get('ontology')); + $object->setTaxonomyMapping($request->get('taxonomy')); - $object->setCurrState(ImportState::Started); - $object->setCurrMessage("En attente..."); - - $em->persist($object); - $em->flush(); + // ---- END CUSTOM ------ - $this->get('biopen.async')->callCommand('app:elements:importSource', [$object->getId()]); + $object = $this->admin->update($object); - // $result = $this->get('biopen.element_import')->startImport($object); + $this->addFlash( + 'sonata_flash_success', + $this->trans( + 'flash_edit_success', + array('%name%' => $this->escapeHtml($this->admin->toString($object))), + 'SonataAdminBundle' + ) + ); - $redirectionUrl = $this->admin->generateUrl('create'); - $stateUrl = $this->generateUrl('biopen_import_state', ['id' => $object->getId()]); + if ($request->get('collect')) { + $url = $this->admin->generateUrl('collect', ['id' => $object->getId()]); + } else if ($request->get('import')) { + $url = $this->admin->generateUrl('refresh', ['id' => $object->getId()]); + } else { + $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]); + } + return $this->redirect($url); - return $this->render('@BiopenAdmin/pages/import/import-progress.html.twig', [ - 'import' => $object, - 'redirectUrl' => $redirectionUrl, - 'stateUrl' => $stateUrl - ]); + } catch (ModelManagerException $e) { + $this->handleModelManagerException($e); + $isFormValid = false; + } catch (LockException $e) { + $this->addFlash('sonata_flash_error', $this->trans('flash_lock_error', array( + '%name%' => $this->escapeHtml($this->admin->toString($object)), + '%link_start%' => '<a href="'.$this->admin->generateObjectUrl('edit', $object).'">', + '%link_end%' => '</a>', + ), 'SonataAdminBundle')); + } + } + + // show an error message if the form failed validation + if (!$isFormValid) { + if (!$this->isXmlHttpRequest()) { + $this->addFlash( + 'sonata_flash_error', + $this->trans( + 'flash_edit_error', + array('%name%' => $this->escapeHtml($this->admin->toString($object))), + 'SonataAdminBundle' + ) + ); + } + } } - // This method is just an overwrite of the SonataAdminCRUDController for calling the executeImport once the document is created + $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($this->admin->getTemplate('edit'), array( + 'action' => 'edit', + 'form' => $view, + 'object' => $object, + ), null); + } + + + /** + * Overwrite Sonata CRud Controller + */ 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); - } - } + $request = $this->getRequest(); + // the key used to lookup the template + $templateKey = 'edit'; + $this->admin->checkAccess('create'); + $class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass()); + + $object = $this->admin->getNewInstance(); + + $this->admin->setSubject($object); + + $form = $this->admin->getForm(); + $form->setData($object); + $form->handleRequest($request); + + if ($form->isSubmitted()) { + //TODO: remove this check for 4.0 + if (method_exists($this->admin, 'preValidate')) { + $this->admin->preValidate($object); + } + $isFormValid = $form->isValid(); + + // persist if the form was valid and if in preview mode the preview was approved + if ($isFormValid && (!$this->isInPreviewMode($request) || $this->isPreviewApproved($request))) { + try { + $object = $this->admin->create($object); + + $result = $this->get('biopen.element_import')->collectData($object); + + $this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès. Vous pouvez maintenant compléter les tables de correspondances, puis importer les données."); + + $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]) . "#tab_3"; + return $this->redirect($url); + } catch (ModelManagerException $e) { + $this->handleModelManagerException($e); + $isFormValid = false; + } + } + + // show an error message if the form failed validation + if (!$isFormValid) { + if (!$this->isXmlHttpRequest()) { + $this->addFlash( + 'sonata_flash_error', + $this->trans( + 'flash_create_error', + array('%name%' => $this->escapeHtml($this->admin->toString($object))), + 'SonataAdminBundle' + ) + ); + } + } elseif ($this->isPreviewRequested()) { + // pick the preview template if the form was valid and preview was requested + $templateKey = 'preview'; + $this->admin->getShow(); + } + } + + $view = $form->createView(); - $view = $form->createView(); - // set the theme for the current Admin Form - $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme()); + // 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); + return $this->render($this->admin->getTemplate($templateKey), array( + 'action' => 'create', + 'form' => $view, + 'object' => $object, + ), null); } } \ No newline at end of file diff --git a/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController_old.php b/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController_old.php new file mode 100755 index 000000000..c1abe542d --- /dev/null +++ b/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportAdminController_old.php @@ -0,0 +1,91 @@ +<?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 diff --git a/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportDynamicAdminController.php b/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportDynamicAdminController.php deleted file mode 100755 index c6dba8bd6..000000000 --- a/src/Biopen/GeoDirectoryBundle/Controller/Admin/ImportDynamicAdminController.php +++ /dev/null @@ -1,214 +0,0 @@ -<?php - -namespace Biopen\GeoDirectoryBundle\Controller\Admin; - -use Sonata\AdminBundle\Controller\CRUDController as Controller; -use Symfony\Component\HttpFoundation\RedirectResponse; -use Symfony\Component\HttpFoundation\Request; -use Biopen\GeoDirectoryBundle\Document\ImportState; - -class ImportDynamicAdminController extends Controller -{ - public function collectAction() - { - $object = $this->admin->getSubject(); - $result = $this->get('biopen.element_import')->collectData($object); - - $this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès.</br>Voici le résultat obtenu pour le premier élément à importer :<pre>" . print_r($result, true) . '</pre>'); - $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]); - return $this->redirect($url); - } - - public function refreshAction() - { - $object = $this->admin->getSubject(); - - $object->setCurrState(ImportState::Started); - $object->setCurrMessage("En attente..."); - $em = $this->get('doctrine_mongodb')->getManager(); - $em->persist($object); - $em->flush(); - - // $this->get('biopen.async')->callCommand('app:elements:importSource', [$object->getId()]); - - $result = $this->get('biopen.element_import')->importJson($object); - - $redirectionUrl = $this->admin->generateUrl('edit', ['id' => $object->getId()]); - $stateUrl = $this->generateUrl('biopen_import_state', ['id' => $object->getId()]); - - return $this->render('@BiopenAdmin/pages/import/import-progress.html.twig', [ - 'import' => $object, - 'redirectUrl' => $redirectionUrl, - 'redirectListUrl' => $redirectionUrl = $this->admin->generateUrl('list'), - 'stateUrl' => $stateUrl - ]); - } - - /** - * Overite Sonata CRud Controller - */ - public function editAction($id = null) - { - $request = $this->getRequest(); - $id = $request->get($this->admin->getIdParameter()); - $object = $this->admin->getObject($id); - - if (!$object) throw $this->createNotFoundException(sprintf('unable to find the object with id : %s', $id)); - - $this->admin->checkAccess('edit', $object); - $this->admin->setSubject($object); - - $form = $this->admin->getForm(); - $form->setData($object); - $form->handleRequest($request); - - if ($form->isSubmitted()) { - //TODO: remove this check for 4.0 - if (method_exists($this->admin, 'preValidate')) { - $this->admin->preValidate($object); - } - $isFormValid = $form->isValid(); - - // persist if the form was valid and if in preview mode the preview was approved - if ($isFormValid) { - try { - - // ----- CUSTOM ------- - $object->setOntologyMapping($request->get('ontology')); - $object->setTaxonomyMapping($request->get('taxonomy')); - - // ---- END CUSTOM ------ - - $object = $this->admin->update($object); - - $this->addFlash( - 'sonata_flash_success', - $this->trans( - 'flash_edit_success', - array('%name%' => $this->escapeHtml($this->admin->toString($object))), - 'SonataAdminBundle' - ) - ); - - if ($request->get('collect')) { - $url = $this->admin->generateUrl('collect', ['id' => $object->getId()]); - } else if ($request->get('import')) { - $url = $this->admin->generateUrl('refresh', ['id' => $object->getId()]); - } else { - $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]); - } - return $this->redirect($url); - - } catch (ModelManagerException $e) { - $this->handleModelManagerException($e); - $isFormValid = false; - } catch (LockException $e) { - $this->addFlash('sonata_flash_error', $this->trans('flash_lock_error', array( - '%name%' => $this->escapeHtml($this->admin->toString($object)), - '%link_start%' => '<a href="'.$this->admin->generateObjectUrl('edit', $object).'">', - '%link_end%' => '</a>', - ), 'SonataAdminBundle')); - } - } - - // show an error message if the form failed validation - if (!$isFormValid) { - if (!$this->isXmlHttpRequest()) { - $this->addFlash( - 'sonata_flash_error', - $this->trans( - 'flash_edit_error', - array('%name%' => $this->escapeHtml($this->admin->toString($object))), - 'SonataAdminBundle' - ) - ); - } - } - } - - $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($this->admin->getTemplate('edit'), array( - 'action' => 'edit', - 'form' => $view, - 'object' => $object, - ), null); - } - - - /** - * Overwrite Sonata CRud Controller - */ - public function createAction() - { - $request = $this->getRequest(); - // the key used to lookup the template - $templateKey = 'edit'; - $this->admin->checkAccess('create'); - $class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass()); - - $object = $this->admin->getNewInstance(); - - $this->admin->setSubject($object); - - $form = $this->admin->getForm(); - $form->setData($object); - $form->handleRequest($request); - - if ($form->isSubmitted()) { - //TODO: remove this check for 4.0 - if (method_exists($this->admin, 'preValidate')) { - $this->admin->preValidate($object); - } - $isFormValid = $form->isValid(); - - // persist if the form was valid and if in preview mode the preview was approved - if ($isFormValid && (!$this->isInPreviewMode($request) || $this->isPreviewApproved($request))) { - try { - $object = $this->admin->create($object); - - $result = $this->get('biopen.element_import')->collectData($object); - - $this->addFlash('sonata_flash_success', "Les données ont été chargées avec succès. Vous pouvez maintenant compléter les tables de correspondances, puis importer les données."); - - $url = $this->admin->generateUrl('edit', ['id' => $object->getId()]) . "#tab_3"; - return $this->redirect($url); - } catch (ModelManagerException $e) { - $this->handleModelManagerException($e); - $isFormValid = false; - } - } - - // show an error message if the form failed validation - if (!$isFormValid) { - if (!$this->isXmlHttpRequest()) { - $this->addFlash( - 'sonata_flash_error', - $this->trans( - 'flash_create_error', - array('%name%' => $this->escapeHtml($this->admin->toString($object))), - 'SonataAdminBundle' - ) - ); - } - } elseif ($this->isPreviewRequested()) { - // pick the preview template if the form was valid and preview was requested - $templateKey = 'preview'; - $this->admin->getShow(); - } - } - - $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($this->admin->getTemplate($templateKey), array( - 'action' => 'create', - 'form' => $view, - 'object' => $object, - ), null); - } -} \ No newline at end of file diff --git a/src/Biopen/GeoDirectoryBundle/Document/Import.php b/src/Biopen/GeoDirectoryBundle/Document/Import.php index 1502d2016..41e901c9b 100644 --- a/src/Biopen/GeoDirectoryBundle/Document/Import.php +++ b/src/Biopen/GeoDirectoryBundle/Document/Import.php @@ -128,6 +128,13 @@ class Import extends AbstractFile */ private $customCode = "<?php"; + /** + * @var date $lastRefresh + * + * @MongoDB\Field(type="date") + */ + private $lastRefresh = null; + public function __construct() { $this->logs = new \Doctrine\Common\Collections\ArrayCollection();; @@ -516,4 +523,26 @@ class Import extends AbstractFile { return $this->customCode; } + + /** + * Set lastRefresh + * + * @param date $lastRefresh + * @return $this + */ + public function setLastRefresh($lastRefresh) + { + $this->lastRefresh = $lastRefresh; + return $this; + } + + /** + * Get lastRefresh + * + * @return date $lastRefresh + */ + public function getLastRefresh() + { + return $this->lastRefresh; + } } diff --git a/src/Biopen/GeoDirectoryBundle/Document/ImportDynamic.php b/src/Biopen/GeoDirectoryBundle/Document/ImportDynamic.php index b9981b2a0..1bfe72dc5 100644 --- a/src/Biopen/GeoDirectoryBundle/Document/ImportDynamic.php +++ b/src/Biopen/GeoDirectoryBundle/Document/ImportDynamic.php @@ -13,20 +13,14 @@ use \Datetime; class ImportDynamic extends Import { /** - * @var string + * @var string * @MongoDB\Field(type="int") */ private $refreshFrequencyInDays; - /** - * @var date $lastRefresh - * - * @MongoDB\Field(type="date") - */ - private $lastRefresh = null; /** - * @var date $lastRefresh + * @var date $nextRefresh * * @MongoDB\Field(type="date") */ @@ -35,17 +29,17 @@ class ImportDynamic extends Import public function isDynamicImport() { return true; } - public function updateNextRefreshDate() + public function updateNextRefreshDate() { if ($this->getRefreshFrequencyInDays() == 0) $this->setNextRefresh(null); - else - { + else + { $interval = new \DateInterval('P' . $this->getRefreshFrequencyInDays() .'D'); $date = new DateTime(); $date->setTimestamp(time()); $this->setNextRefresh($date->add($interval)); } - } + } /** * Set refreshFrequencyInDays @@ -70,28 +64,6 @@ class ImportDynamic extends Import return $this->refreshFrequencyInDays; } - /** - * Set lastRefresh - * - * @param date $lastRefresh - * @return $this - */ - public function setLastRefresh($lastRefresh) - { - $this->lastRefresh = $lastRefresh; - return $this; - } - - /** - * Get lastRefresh - * - * @return date $lastRefresh - */ - public function getLastRefresh() - { - return $this->lastRefresh; - } - /** * Set nextRefresh * diff --git a/src/Biopen/GeoDirectoryBundle/EventListener/DatabaseIntegrityWatcher.php b/src/Biopen/GeoDirectoryBundle/EventListener/DatabaseIntegrityWatcher.php index efac283df..db2c469db 100755 --- a/src/Biopen/GeoDirectoryBundle/EventListener/DatabaseIntegrityWatcher.php +++ b/src/Biopen/GeoDirectoryBundle/EventListener/DatabaseIntegrityWatcher.php @@ -11,6 +11,7 @@ use Biopen\GeoDirectoryBundle\Document\Element; use Application\Sonata\UserBundle\Document\Group; use Biopen\GeoDirectoryBundle\Document\Option; use Biopen\GeoDirectoryBundle\Document\ImportDynamic; +use Biopen\GeoDirectoryBundle\Document\Import; use Biopen\GeoDirectoryBundle\Document\Webhook; use Doctrine\ODM\MongoDB\DocumentManager; @@ -42,7 +43,7 @@ class DatabaseIntegrityWatcher } } } - else if ($document instanceof ImportDynamic) + else if ($document instanceof Import || $document instanceof ImportDynamic) { $import = $document; $qb = $dm->getRepository('BiopenGeoDirectoryBundle:Element')->createQueryBuilder(); diff --git a/src/Biopen/GeoDirectoryBundle/Resources/config/services_admin.yml b/src/Biopen/GeoDirectoryBundle/Resources/config/services_admin.yml index 3519f3772..8eed9aa93 100755 --- a/src/Biopen/GeoDirectoryBundle/Resources/config/services_admin.yml +++ b/src/Biopen/GeoDirectoryBundle/Resources/config/services_admin.yml @@ -3,17 +3,17 @@ services: class: Biopen\GeoDirectoryBundle\Admin\Element\ElementAdmin arguments: [~, Biopen\GeoDirectoryBundle\Document\Element, 'BiopenGeoDirectoryBundle:Admin/ElementAdmin'] tags: - - { name: sonata.admin, manager_type: doctrine_mongodb, group: "data", label: Éléments } + - { name: sonata.admin, manager_type: doctrine_mongodb, group: "data", label: Éléments } admin.import: class: Biopen\GeoDirectoryBundle\Admin\ImportAdmin arguments: [~, Biopen\GeoDirectoryBundle\Document\Import, 'BiopenGeoDirectoryBundle:Admin/ImportAdmin'] tags: - - { name: sonata.admin, manager_type: doctrine_mongodb, group: "data", label: Import } + - { name: sonata.admin, manager_type: doctrine_mongodb, group: "data", label: Import } admin.source_external: - class: Biopen\GeoDirectoryBundle\Admin\ImportDynamicAdmin - arguments: [~, Biopen\GeoDirectoryBundle\Document\ImportDynamic, 'BiopenGeoDirectoryBundle:Admin/ImportDynamicAdmin' ] + class: Biopen\GeoDirectoryBundle\Admin\ImportAdmin + arguments: [~, Biopen\GeoDirectoryBundle\Document\ImportDynamic, 'BiopenGeoDirectoryBundle:Admin/ImportAdmin' ] tags: - { name: sonata.admin, manager_type: doctrine_mongodb, group: "data", label: Import dynamique } @@ -24,19 +24,19 @@ services: class: Biopen\CoreBundle\Admin\ImageAdmin arguments: [~, Biopen\GeoDirectoryBundle\Document\ElementImage, '' ] tags: - - { name: sonata.admin, manager_type: doctrine_mongodb, group: "Hide" } + - { name: sonata.admin, manager_type: doctrine_mongodb, group: "Hide" } admin.option: class: Biopen\GeoDirectoryBundle\Admin\OptionAdmin arguments: [~, Biopen\GeoDirectoryBundle\Document\Option, 'BiopenGeoDirectoryBundle:Admin/OptionAdmin'] tags: - - { name: sonata.admin, manager_type: doctrine_mongodb, group: "Hide" } + - { name: sonata.admin, manager_type: doctrine_mongodb, group: "Hide" } admin.option.lite: class: Biopen\GeoDirectoryBundle\Admin\OptionLiteAdmin arguments: [~, Biopen\GeoDirectoryBundle\Document\Option, ''] tags: - - { name: sonata.admin, manager_type: doctrine_mongodb, group: "Hide" } + - { name: sonata.admin, manager_type: doctrine_mongodb, group: "Hide" } admin.category.lite: class: Biopen\GeoDirectoryBundle\Admin\CategoryLiteAdmin diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import.html.twig index 624947423..43e0990ab 100755 --- a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import.html.twig +++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import.html.twig @@ -3,12 +3,39 @@ {% block actions %} {% endblock %} -{% block sonata_form_actions %} - {% if admin.hasroute('edit') and admin.isGranted('EDIT') %} - <button class="btn btn-primary" type="submit" name="btn_create_and_edit"><i class="fa fa-upload" aria-hidden="true"></i> Importer</button> +{% block sonata_form_actions %} + {% if object.lastRefresh %} + <p>Dernier import : {{ object.lastRefresh|date('d/m/Y') }}</p> {% endif %} -{% endblock %} + <button type="submit" class="btn btn-success" name="submit"><i class="fa fa-save" aria-hidden="true"></i>Sauvegarder</button> + + {% if object.id != "" and object.ontologyMapping|length > 0 %} + <button type="submit" class="btn btn-primary" name="import" value="import"> + {% if object.lastRefresh %} + <i class="fa fa-refresh" aria-hidden="true"></i> + Importer de nouveau + {% else %} + <i class="fa fa-download" aria-hidden="true"></i> + Importer les données + {% endif %} + </button> + {% endif %} + {% set class = object.ontologyMapping|length > 0 ? 'btn-default' : 'btn-primary' %} + {% if object.id != "" %} + <button type="submit" class="btn btn-default" name="collect" value="collect" title="Met à jour les tables de correspondance sans réellement importer les données"> + <i class="fa fa-eye" aria-hidden="true"></i>Lire les données + </button> + {% endif %} + <a href="{{ admin.generateObjectUrl('list', object) }}" class="btn btn-default view_link"> + <i class="fa fa-list" aria-hidden="true"></i>Retour à la liste + </a> + {% if object.id != "" %} + <a href="{{ admin.generateObjectUrl('delete', object) }}" class="btn btn-danger view_link"> + <i class="fa fa-trash" aria-hidden="true"></i>Supprimer (ainsi que tous les éléments importés) + </a> + {% endif %} +{% endblock %} diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import_dynamic.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import_dynamic.html.twig deleted file mode 100755 index b207ffcfd..000000000 --- a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/edit/edit_import_dynamic.html.twig +++ /dev/null @@ -1,35 +0,0 @@ -{% extends '@BiopenAdmin/core_custom/base_edit.html.twig' %} - -{% block actions %} -{% endblock %} - -{% block sonata_form_actions %} - {% if object.lastRefresh %} - <p>Dernière mise à jour : {{ object.lastRefresh|date('d/m/Y') }}</p> - {% endif %} - - <button type="submit" class="btn btn-success" name="submit"><i class="fa fa-save" aria-hidden="true"></i>Sauvegarder</button> - - {% if object.id != "" and object.ontologyMapping|length > 0 %} - <button type="submit" class="btn btn-primary" name="import" value="import"> - <i class="fa fa-download" aria-hidden="true"></i>Importer les données - </button> - {% endif %} - - {% set class = object.ontologyMapping|length > 0 ? 'btn-default' : 'btn-primary' %} - {% if object.id != "" %} - <button type="submit" class="btn btn-default" name="collect" value="collect" title="Met à jour les tables de correspondance sans réellement importer les données"> - <i class="fa fa-eye" aria-hidden="true"></i>Lire les données - </button> - {% endif %} - <a href="{{ admin.generateObjectUrl('list', object) }}" class="btn btn-default view_link"> - <i class="fa fa-list" aria-hidden="true"></i>Retour à la liste - </a> - {% if object.id != "" %} - <a href="{{ admin.generateObjectUrl('delete', object) }}" class="btn btn-danger view_link"> - <i class="fa fa-trash" aria-hidden="true"></i>Supprimer (ainsi que tous les éléments importés) - </a> - {% endif %} - -{% endblock %} - diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import.html.twig new file mode 100644 index 000000000..a2d74805d --- /dev/null +++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import.html.twig @@ -0,0 +1,7 @@ +{% extends "@BiopenAdmin/list/base_list_custom_batch.html.twig" %} + +{% block list_header %} + <p> + <h4 style="margin: 0 0 20px 10px">Liste des imports "en dur" (les données importées seront gérées par ce site)</h3> + </p> +{% endblock %} \ No newline at end of file diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import_dynamic.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import_dynamic.html.twig new file mode 100644 index 000000000..43fde2bcd --- /dev/null +++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/list/list_import_dynamic.html.twig @@ -0,0 +1,7 @@ +{% extends "@BiopenAdmin/list/base_list_custom_batch.html.twig" %} + +{% block list_header %} + <p> + <h4 style="margin: 0 0 20px 10px">Liste des imports dynamiques (les données importées seront en lecture seule car elles sont gérées par un autre site)</h3> + </p> +{% endblock %} \ No newline at end of file diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/pages/import/show-data.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/pages/import/show-data.html.twig new file mode 100644 index 000000000..64bacafe6 --- /dev/null +++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/pages/import/show-data.html.twig @@ -0,0 +1,18 @@ +{% extends '@BiopenAdmin/pages/page_layout.html.twig' %} + +{% block page_title %}Lecture des données issue de {{ import.sourceName }}{% endblock %} + +{% block page_content %} + <p> + <a href="{{redirectUrl}}"><button class="btn btn-primary"><i class="fa fa-pencil" aria-hidden="true"></i>Retourner à l'import</button></a> + </p> + <pre>{{ dataDisplay }}</pre> + <p> + <a href="{{redirectUrl}}"><button class="btn btn-primary"><i class="fa fa-pencil" aria-hidden="true"></i>Retourner à l'import</button></a> + </p> +{% endblock %} + + + + + diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_last_refresh.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_last_refresh.html.twig index 809a436fa..b0aa91365 100644 --- a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_last_refresh.html.twig +++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_last_refresh.html.twig @@ -2,8 +2,8 @@ {% block field %} {% spaceless %} - <span style="margin-right: 8px">{{ object.lastRefresh|date('d/m/Y - H:i') }}</span> - {% if object.logs|length > 0 %} + <span style="margin-right: 8px">{{ object.lastRefresh ? object.lastRefresh|date('d/m/Y - H:i') : '' }}</span> + {% if object.logs|length > 0 and object.isDynamicImport %} {% set log = object.logs[0] %} {% if log.getDataProp('elementsCreatedCount') > 0 %} <span class="label label-success log-label">+{{ log.getDataProp('elementsCreatedCount') }}</span> @@ -16,7 +16,7 @@ {% endif %} {% if log.getDataProp('elementsNothingToDoCount') > 0 %} <span class="label label-default log-label">{{ log.getDataProp('elementsNothingToDoCount') }} inchangés</span> - {% endif %} + {% endif %} {% if log.getDataProp('elementsErrorsCount') > 0 %} <span class="label label-danger log-label">{{ log.getDataProp('elementsErrorsCount') }} erreurs</span> {% endif %} diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/list__action_refresh.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/list__action_refresh.html.twig index af257c906..1f189d281 100755 --- a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/list__action_refresh.html.twig +++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/list__action_refresh.html.twig @@ -15,7 +15,7 @@ file that was distributed with this source code. <a href="{{ admin.generateObjectUrl('refresh', object) }}" class="btn {{ class|default('btn-sm btn-primary') }} view_link"> {% if object.lastRefresh %} <i class="fa fa-refresh" aria-hidden="true"></i> - Synchroniser + Importer de nouveau {% else %} <i class="fa fa-download" aria-hidden="true"></i> Importer les données diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementImportMappingService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementImportMappingService.php index adc192e4f..8e580f01f 100644 --- a/src/Biopen/GeoDirectoryBundle/Services/ElementImportMappingService.php +++ b/src/Biopen/GeoDirectoryBundle/Services/ElementImportMappingService.php @@ -93,7 +93,6 @@ class ElementImportMappingService // Ontology $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); }); @@ -134,7 +133,7 @@ class ElementImportMappingService $keyName = $parentKey ? $parentKey . '/' . $key : $key; if (!in_array($keyName, $this->allNewFields)) $this->allNewFields[] = $keyName; if (!array_key_exists($keyName, $this->ontologyMapping)) { - $value = in_array($key, $this->coreFields) ? $key : ""; + $value = in_array($keyName, $this->coreFields) ? $key : ""; if (!$value && array_key_exists($key, $this->mappedCoreFields) && in_array($this->mappedCoreFields[$key], $this->coreFields)) $value = $this->mappedCoreFields[$key]; if (!$value || !in_array($value, array_values($this->ontologyMapping))) $this->ontologyMapping[$keyName] = $value; @@ -189,7 +188,6 @@ class ElementImportMappingService private function mapOntology($data) { $mapping = $this->import->getOntologyMapping(); - foreach ($data as $key => $row) { // First map nested fields diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php index de019baa7..742a0fda4 100755 --- a/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php +++ b/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php @@ -51,7 +51,7 @@ class ElementImportOneService { $updateExisting = false; // if we create a new element or update an existing one $realUpdate = false; // if we are sure that the external has been edited with 'FieldToCheckElementHaveBeenUpdated' - if ($row['id']) + if (isset($row['id'])) { if (in_array($row['id'], $import->getIdsToIgnore())) return; $qb = $this->em->createQueryBuilder('BiopenGeoDirectoryBundle:Element'); @@ -94,7 +94,7 @@ class ElementImportOneService // adds missings fields instead of checking if each field is set before accessing $missingFields = array_diff($this->coreFields, array_keys($row)); foreach ($missingFields as $missingField) { - $data[$key][$missingField] = ""; + $row[$missingField] = ""; } $element->setOldId($row['id']); diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php index 11f831ee4..3bea924f1 100755 --- a/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php +++ b/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php @@ -113,24 +113,26 @@ class ElementImportService // do the mapping $data = $this->mappingService->transform($data, $import); - if ($import->isDynamicImport()) - { - $import->setLastRefresh(time()); - $import->updateNextRefreshDate(); - } + $import->setLastRefresh(time()); + $import->setCurrState(ImportState::InProgress); + $qb = $this->em->createQueryBuilder('BiopenGeoDirectoryBundle:Element'); if ($import->isDynamicImport()) { - // before updating the source, we put all elements into DynamicImportTemp status - $qb = $this->em->createQueryBuilder('BiopenGeoDirectoryBundle:Element'); + $import->updateNextRefreshDate(); + + // before updating the source, we put all elements into DynamicImportTemp status $qb->updateMany() ->field('source')->references($import) ->field('status')->gt(ElementStatus::Deleted) // leave the deleted one as they are, so we know we do not need to import them ->field('status')->set(ElementStatus::DynamicImportTemp) ->getQuery()->execute(); } - - $import->setCurrState(ImportState::InProgress); + else + { + // before re importing a static source, we delete all previous items + $qb->remove()->field('source')->references($import)->getQuery()->execute();; + } $this->importOneService->initialize($import); @@ -144,7 +146,7 @@ class ElementImportService } catch (\Exception $e) { $this->countElementErrors++; - if (!is_array($row['id'])) $this->elementIdsErrors[] = "" . $row['id']; + if (isset($row['id']) && !is_array($row['id'])) $this->elementIdsErrors[] = "" . $row['id']; if (!array_key_exists($e->getMessage(), $this->errorsCount)) $this->errorsCount[$e->getMessage()] = 1; else $this->errorsCount[$e->getMessage()]++; @@ -173,7 +175,7 @@ class ElementImportService { if ($this->countElementErrors > 0) { - // If there was an error whil retrieving an already existing element + // If there was an error while retrieving an already existing element // we set back the status to DynamicImport otherwise it will be deleted just after $qb = $this->em->createQueryBuilder('BiopenGeoDirectoryBundle:Element'); $result = $qb->updateMany() -- GitLab