From a5e1e9a3eae6f4d4022bc0cf75da4f645d577fde Mon Sep 17 00:00:00 2001
From: pfleu <fleutotp@gmail.com>
Date: Thu, 8 Jun 2023 23:31:41 +0200
Subject: [PATCH] =?UTF-8?q?R=C3=A9ordonnement=20des=20blocs=20d'une=20entr?=
 =?UTF-8?q?=C3=A9e:=20ajout=20d'un=20log?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Controller/EntryController.php    | 41 +++++++++------------------
 src/Entity/Log.php                    |  2 +-
 templates/log/_formattedLog.html.twig |  8 ++++--
 3 files changed, 20 insertions(+), 31 deletions(-)

diff --git a/src/Controller/EntryController.php b/src/Controller/EntryController.php
index 5be32ee..36f464c 100644
--- a/src/Controller/EntryController.php
+++ b/src/Controller/EntryController.php
@@ -308,14 +308,14 @@ class EntryController extends AppBaseController
         return $this->redirectToRoute('app_entry_show', ['id' => $entry->getId()]);
     }
 
-    public function addBlockLog(Entry $entry, $type, $blockCategory)
+    public function addBlockLog(Entry $entry, $type, $blockCategory = null)
     {
-        if (in_array($blockCategory, [
-            Entry::ATTR_DEFINITION,
-            Entry::ATTR_EXAMPLE,
-            Entry::ATTR_PRONUNCIATION,
-            Entry::ATTR_PART_OF_SPEECH,
-        ])) {
+//        if (in_array($blockCategory, [
+//            Entry::ATTR_DEFINITION,
+//            Entry::ATTR_EXAMPLE,
+//            Entry::ATTR_PRONUNCIATION,
+//            Entry::ATTR_PART_OF_SPEECH,
+//        ])) {
 
             $log = new Log();
             $log->setCreatedBy($this->getUser());
@@ -324,7 +324,7 @@ class EntryController extends AppBaseController
             $log->setBlockCategory($blockCategory);
 
             $entry->addLog($log);
-        }
+//        }
     }
 
     /**
@@ -334,36 +334,21 @@ class EntryController extends AppBaseController
     {
         $movedId = $request->get('movedId');
         $position = $request->get('position');
-
 //        dump($movedId, $position);die();
+
+        $oldAttributes = $entry->getAttributes();
         $attributes = $entry->getAttributes();
 
         $result = $this->moveBlock($attributes, $movedId, $position);
-
+        if ($oldAttributes != $attributes) {
+            $this->addBlockLog($entry, 'order');
+        }
         $entry->setAttributes($attributes);
         $this->em->flush();
 
         return $this->render('entry/show.html.twig', ['entry' => $entry]);
     }
 
-//    public function getBlockByIdAndParent($attrArray, $id)
-//    {
-//        foreach ($attrArray as $item) {
-//            if (isset($item['id']) && $item['id'] == $id) {
-//                return [
-//                    'block' => $item,
-//                    'parent' => $attrArray,
-//                ];
-//            } elseif (is_array($item)) {
-//                $result =  $this->getBlockByIdAndParent($item, $id);
-//                if ($result) {
-//                    return $result;
-//                }
-//            }
-//        }
-//        return false;
-//    }
-
     public function moveBlock(&$attrArray, $id, $newKey)
     {
         foreach ($attrArray as $key => &$item) {
diff --git a/src/Entity/Log.php b/src/Entity/Log.php
index f8aa3ee..615829a 100644
--- a/src/Entity/Log.php
+++ b/src/Entity/Log.php
@@ -168,7 +168,7 @@ class Log
         return $this->blockCategory;
     }
 
-    public function setBlockCategory(string $blockCategory): self
+    public function setBlockCategory(?string $blockCategory): self
     {
         $this->blockCategory = $blockCategory;
 
diff --git a/templates/log/_formattedLog.html.twig b/templates/log/_formattedLog.html.twig
index 593ccaf..c72cf31 100644
--- a/templates/log/_formattedLog.html.twig
+++ b/templates/log/_formattedLog.html.twig
@@ -5,8 +5,12 @@
     {% if log.content == 'add' %}{{ 'a ajouté'|trans }}{% endif %}
     {% if log.content == 'edit' %}{{ 'a modifié'|trans }}{% endif %}
     {% if log.content == 'delete' %}{{ 'a supprimé'|trans }}{% endif %}
-    {{ 'un bloc de type'|trans }}
-    {{ log.blockCategory }}
+    {% if log.content == 'order' %}
+        {{ 'a réordonné les blocs'|trans }}
+    {% else %}
+        {{ 'un bloc de type'|trans }}
+        {{ log.blockCategory }}
+    {% endif %}
 
 {% elseif log.category in [constant('App\\Entity\\Log::CATEGORY_ADD_ENTRY'), constant('App\\Entity\\Log::CATEGORY_DELETE_ENTRY')] %}
 
-- 
GitLab