From 406b059b50464698287f88eb837fe5e2f3f1cd96 Mon Sep 17 00:00:00 2001
From: Sebastian Castro <sebastian.castro@protonmail.com>
Date: Sun, 15 Sep 2019 09:23:15 +0200
Subject: [PATCH] Element import add option "prevent import if no categories"

---
 .../CoreBundle/Document/GoGoLogImport.php     | 17 ++++++------
 .../GeoDirectoryBundle/Admin/ImportAdmin.php  |  3 ++-
 .../GeoDirectoryBundle/Document/Import.php    | 27 +++++++++++++++++++
 .../import/list_non_visibles_count.html.twig  |  3 +++
 .../Services/ElementImportOneService.php      |  3 +++
 .../Services/ElementImportService.php         |  4 +++
 6 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/src/Biopen/CoreBundle/Document/GoGoLogImport.php b/src/Biopen/CoreBundle/Document/GoGoLogImport.php
index b6bc73279..1074da93c 100644
--- a/src/Biopen/CoreBundle/Document/GoGoLogImport.php
+++ b/src/Biopen/CoreBundle/Document/GoGoLogImport.php
@@ -11,19 +11,20 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
  */
 class GoGoLogImport extends GoGoLog
 {
-    public function displayMessage() 
+    public function displayMessage()
     {
         $result = $this->getMessage() . " ! <strong>Total: " . $this->getDataProp('elementsCount') . "</strong> ";
 
-        if ($this->getDataProp('elementsCreatedCount') > 0) $result .= " - " . $this->getDataProp('elementsCreatedCount') . " élément.s importé.s";
-        if ($this->getDataProp('elementsUpdatedCount') > 0) $result .= " - " . $this->getDataProp('elementsUpdatedCount') . " élement.s mis à jour";
-        if ($this->getDataProp('elementsNothingToDoCount') > 0) $result .= " - " . $this->getDataProp('elementsNothingToDoCount') . " élement.s laissé.s tel.s quel.s (rien à mettre à jour)";   
-        if ($this->getDataProp('elementsMissingGeoCount') > 0) $result .= " - " . $this->getDataProp('elementsMissingGeoCount') . " élement.s sans geoloc";
-        if ($this->getDataProp('elementsMissingTaxoCount') > 0) $result .= " - " . $this->getDataProp('elementsMissingTaxoCount') . " élement.s sans categories";
-        if ($this->getDataProp('elementsDeletedCount') > 0) $result .= " - " . $this->getDataProp('elementsDeletedCount') . " élement.s supprimé.s";
+        if ($this->getDataProp('elementsCreatedCount') > 0) $result .= " - " . $this->getDataProp('elementsCreatedCount') . " importé.s";
+        if ($this->getDataProp('elementsUpdatedCount') > 0) $result .= " - " . $this->getDataProp('elementsUpdatedCount') . " mis à jour";
+        if ($this->getDataProp('elementsNothingToDoCount') > 0) $result .= " - " . $this->getDataProp('elementsNothingToDoCount') . " laissé.s tel.s quel.s (rien à mettre à jour)";
+        if ($this->getDataProp('elementsMissingGeoCount') > 0) $result .= " - " . $this->getDataProp('elementsMissingGeoCount') . " sans geoloc";
+        if ($this->getDataProp('elementsMissingTaxoCount') > 0) $result .= " - " . $this->getDataProp('elementsMissingTaxoCount') . " sans categories";
+        if ($this->getDataProp('elementsPreventImportedNoTaxo') > 0) $result .= " - " . $this->getDataProp('elementsPreventImportedNoTaxo') . " non importés car sans catégories";
+        if ($this->getDataProp('elementsDeletedCount') > 0) $result .= " - " . $this->getDataProp('elementsDeletedCount') . " supprimé.s";
         if ($this->getDataProp('elementsErrorsCount') > 0) $result .= " - " . $this->getDataProp('elementsErrorsCount') . " erreur.s pendant l'import";
 
         if ($this->getDataProp('errorMessages')) $result .= '</br></br>' . implode('</br>', $this->getDataProp('errorMessages'));
         return $result;
-    }    
+    }
 }
diff --git a/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php b/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php
index fac4f706a..80cd60a20 100755
--- a/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php
+++ b/src/Biopen/GeoDirectoryBundle/Admin/ImportAdmin.php
@@ -59,7 +59,8 @@ class ImportAdmin extends AbstractAdmin
                         'multiple' => true,
                         'btn_add' => false,
                         'label' => 'Catégories à ajouter à chaque élément importé'), array('admin_code' => 'admin.option_hidden'))
-                    ->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('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('preventImportIfNoCategories', null, array('required' => false, 'label' => "Ne pas importer les éléments qui n'ont aucune catégories", 'label_attr' => ['title' => "Lorsqu'on veut importer seulement une partie des éléments d'une base de donnée, il peut être pratique de mapper uniquement les catégories que l'on veut importer. Mais tous les autres élément seront aussi importés mais sans catégories. En cochant cette option, uniquement les éléments avec une catégorie mappée seront importés"]));
          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)"]));
diff --git a/src/Biopen/GeoDirectoryBundle/Document/Import.php b/src/Biopen/GeoDirectoryBundle/Document/Import.php
index f53e079b1..e609a8d39 100644
--- a/src/Biopen/GeoDirectoryBundle/Document/Import.php
+++ b/src/Biopen/GeoDirectoryBundle/Document/Import.php
@@ -75,6 +75,11 @@ class Import extends AbstractFile
      */
     private $needToHaveOptionsOtherThanTheOnesAddedToEachElements = false;
 
+    /**
+     * @MongoDB\Field(type="bool")
+     */
+    private $preventImportIfNoCategories = false;
+
     /**
      * @var string
      * @MongoDB\Field(type="string")
@@ -635,4 +640,26 @@ class Import extends AbstractFile
     {
         return $this->newTaxonomyToMap;
     }
+
+    /**
+     * Set preventImportIfNoCategories
+     *
+     * @param bool $preventImportIfNoCategories
+     * @return $this
+     */
+    public function setPreventImportIfNoCategories($preventImportIfNoCategories)
+    {
+        $this->preventImportIfNoCategories = $preventImportIfNoCategories;
+        return $this;
+    }
+
+    /**
+     * Get preventImportIfNoCategories
+     *
+     * @return bool $preventImportIfNoCategories
+     */
+    public function getPreventImportIfNoCategories()
+    {
+        return $this->preventImportIfNoCategories;
+    }
 }
diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_non_visibles_count.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_non_visibles_count.html.twig
index 4825ff82c..5741c6c3b 100644
--- a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_non_visibles_count.html.twig
+++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/partials/import/list_non_visibles_count.html.twig
@@ -22,6 +22,9 @@
       {% if log.getDataProp('elementsMissingTaxoCount') > 0 %}
         <span class="label label-warning log-label">{{ log.getDataProp('elementsMissingTaxoCount') }} sans categories</span>
       {% endif %}
+      {% if log.getDataProp('elementsPreventImportedNoTaxo') > 0 %}
+        <span class="label label-warning log-label">{{ log.getDataProp('elementsPreventImportedNoTaxo') }} non importés car sans catégories</span>
+      {% endif %}
 
       <style>
         .label.log-label { margin-right: 5px; }
diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php
index c6d37ce8f..b121990c6 100755
--- a/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php
+++ b/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php
@@ -145,6 +145,9 @@ class ElementImportOneService
 		$element->setGeo(new Coordinates($lat, $lng));
 
 		$this->createCategories($element, $row, $import);
+
+		if ($import->getPreventImportIfNoCategories() && $element->getModerationState() == ModerationState::NoOptionProvided) return "no_category";
+
 		$this->createImages($element, $row);
 		$this->createOpenHours($element, $row);
 		$this->saveCustomFields($element, $row);
diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php
index 0e5155766..696f79474 100755
--- a/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php
+++ b/src/Biopen/GeoDirectoryBundle/Services/ElementImportService.php
@@ -18,6 +18,7 @@ class ElementImportService
 	protected $countElementUpdated = 0;
 	protected $countElementNothingToDo = 0;
 	protected $countElementErrors = 0;
+  protected $countNoCategoryPreventImport = 0;
 	protected $elementIdsErrors = [];
 	protected $errorsMessages = [];
 	protected $errorsCount = [];
@@ -38,6 +39,7 @@ class ElementImportService
 		$this->countElementUpdated = 0;
 		$this->countElementNothingToDo = 0;
 		$this->countElementErrors = 0;
+    $this->countNoCategoryPreventImport = 0;
 		$this->elementIdsErrors = [];
 		$this->errorsMessages = [];
 		$this->errorsCount = [];
@@ -147,6 +149,7 @@ class ElementImportService
           case 'nothing_to_do': $this->countElementNothingToDo++; break;
           case 'created': $this->countElementCreated++; break;
           case 'updated': $this->countElementUpdated++; break;
+          case 'no_category': $this->countNoCategoryPreventImport++; break;
         }
 				$i++;
 			}
@@ -219,6 +222,7 @@ class ElementImportService
 			"elementsNothingToDoCount" => $this->countElementNothingToDo,
 			"elementsMissingGeoCount" => $elementsMissingGeoCount,
 			"elementsMissingTaxoCount" => $elementsMissingTaxoCount,
+      "elementsPreventImportedNoTaxo" => $this->countNoCategoryPreventImport,
 			"elementsDeletedCount" => $countElemenDeleted,
 			"elementsErrorsCount" => $this->countElementErrors,
 			"errorMessages" => $this->errorsMessages
-- 
GitLab