From 18f99ea498a5fd175fdb931c287e8c393c7b405a Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Fri, 25 Sep 2020 16:10:39 +0200
Subject: [PATCH] FEAT #13271 TIME 1:00 refactor admin search configuration +
 added history

---
 rest/index.php                                |  3 +-
 .../controllers/ConfigurationController.php   | 33 +++++++++
 .../SearchAdministrationController.php        | 72 -------------------
 .../search/controllers/SearchController.php   |  8 +++
 src/core/lang/lang-en.php                     |  2 +
 src/core/lang/lang-fr.php                     |  2 +
 6 files changed, 46 insertions(+), 74 deletions(-)
 delete mode 100644 src/app/search/controllers/SearchAdministrationController.php

diff --git a/rest/index.php b/rest/index.php
index 242366ef593..1c2ea1bc882 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -455,8 +455,7 @@ $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/ac
 
 //Search
 $app->post('/search', \Search\controllers\SearchController::class . ':get');
-$app->get('/search/configuration', \Search\controllers\SearchAdministrationController::class . ':get');
-$app->put('/search/configuration', \Search\controllers\SearchAdministrationController::class . ':update');
+$app->get('/search/configuration', \Search\controllers\SearchController::class . ':getConfiguration');
 
 $app->get('/searchTemplates', \Search\controllers\SearchTemplateController::class . ':get');
 $app->post('/searchTemplates', \Search\controllers\SearchTemplateController::class . ':create');
diff --git a/src/app/configuration/controllers/ConfigurationController.php b/src/app/configuration/controllers/ConfigurationController.php
index b8466a158f6..faa4baf991e 100755
--- a/src/app/configuration/controllers/ConfigurationController.php
+++ b/src/app/configuration/controllers/ConfigurationController.php
@@ -16,6 +16,7 @@ namespace Configuration\controllers;
 
 use Configuration\models\ConfigurationModel;
 use Group\controllers\PrivilegeController;
+use History\controllers\HistoryController;
 use Respect\Validation\Validator;
 use Slim\Http\Request;
 use Slim\Http\Response;
@@ -69,11 +70,43 @@ class ConfigurationController
             }
             $data['charset'] = empty($data['charset']) ? 'utf-8' : $data['charset'];
             unset($data['passwordAlreadyExists']);
+        } elseif ($args['privilege'] == 'admin_search') {
+            if (!Validator::notEmpty()->arrayType()->validate($data['listDisplay'])) {
+                return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay is empty or not an array']);
+            }
+            if (isset($data['listDisplay']['subInfos']) && !Validator::arrayType()->validate($data['listDisplay']['subInfos'])) {
+                return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[subInfos] is not set or not an array']);
+            }
+            if (!Validator::intVal()->validate($data['listDisplay']['templateColumns'])) {
+                return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[templateColumns] is not set or not an array']);
+            }
+            foreach ($data['listDisplay']['subInfos'] as $value) {
+                if (!Validator::stringType()->notEmpty()->validate($value['value'])) {
+                    return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[subInfos][value] is empty or not a string']);
+                } elseif (!isset($value['cssClasses']) || !is_array($value['cssClasses'])) {
+                    return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[subInfos][cssClasses] is not set or not an array']);
+                }
+            }
+
+            if (empty($data['listEvent']['defaultTab'])) {
+                $data['listEvent']['defaultTab'] = 'dashboard';
+            }
+
+            $data = ['listDisplay' => $data['listDisplay'], 'listEvent' => $data['listEvent']];
+
         }
 
         $data = json_encode($data);
         ConfigurationModel::update(['set' => ['value' => $data], 'where' => ['privilege = ?'], 'data' => [$args['privilege']]]);
 
+        HistoryController::add([
+            'tableName' => 'configurations',
+            'recordId'  => $args['privilege'],
+            'eventType' => 'UP',
+            'eventId'   => 'configurationUp',
+            'info'       => _CONFIGURATION_UPDATED . ' : ' . $args['privilege']
+        ]);
+
         return $response->withJson(['success' => 'success']);
     }
 
diff --git a/src/app/search/controllers/SearchAdministrationController.php b/src/app/search/controllers/SearchAdministrationController.php
deleted file mode 100644
index e5ab7739e5b..00000000000
--- a/src/app/search/controllers/SearchAdministrationController.php
+++ /dev/null
@@ -1,72 +0,0 @@
-<?php
-
-/**
-* Copyright Maarch since 2008 under licence GPLv3.
-* See LICENCE.txt file at the root folder for more details.
-* This file is part of Maarch software.
-*
-*/
-
-/**
-* @brief Search Administration Controller
-* @author dev@maarch.org
-*/
-
-namespace Search\controllers;
-
-use Configuration\models\ConfigurationModel;
-use Group\controllers\PrivilegeController;
-use Respect\Validation\Validator;
-use Slim\Http\Request;
-use Slim\Http\Response;
-
-class SearchAdministrationController
-{
-    public function get(Request $request, Response $response) {
-        $configuration = ConfigurationModel::getByPrivilege(['privilege' => 'admin_search']);
-        $configuration = json_decode($configuration, true);
-
-        return $response->withJson(['configuration' => $configuration]);
-    }
-
-    public function update(Request $request, Response $response)
-    {
-        if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_search', 'userId' => $GLOBALS['id']])) {
-            return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
-        }
-
-        $body = $request->getParsedBody();
-
-        if (!Validator::notEmpty()->arrayType()->validate($body['listDisplay'])) {
-            return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay is empty or not an array']);
-        }
-        if (isset($body['listDisplay']['subInfos']) && !Validator::arrayType()->validate($body['listDisplay']['subInfos'])) {
-            return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[subInfos] is not set or not an array']);
-        }
-        if (!Validator::intVal()->validate($body['listDisplay']['templateColumns'])) {
-            return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[templateColumns] is not set or not an array']);
-        }
-        foreach ($body['listDisplay']['subInfos'] as $value) {
-            if (!Validator::stringType()->notEmpty()->validate($value['value'])) {
-                return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[subInfos][value] is empty or not a string']);
-            } elseif (!isset($value['cssClasses']) || !is_array($value['cssClasses'])) {
-                return $response->withStatus(400)->withJson(['errors' => 'Body listDisplay[subInfos][cssClasses] is not set or not an array']);
-            }
-        }
-
-        if (empty($body['listEvent']['defaultTab'])) {
-            $body['listEvent']['defaultTab'] = 'dashboard';
-        }
-
-        $configuration = ['listDisplay' => $body['listDisplay'], 'listEvent' => $body['listEvent']];
-        $configuration = json_encode($configuration);
-
-        ConfigurationModel::update([
-            'set'   => ['value' => $configuration],
-            'where' => ['privilege = ?'],
-            'data'  => ['admin_search']
-        ]);
-
-        return $response->withStatus(204);
-    }
-}
diff --git a/src/app/search/controllers/SearchController.php b/src/app/search/controllers/SearchController.php
index 9de17d6193e..c7a95c00af2 100644
--- a/src/app/search/controllers/SearchController.php
+++ b/src/app/search/controllers/SearchController.php
@@ -17,6 +17,7 @@ namespace Search\controllers;
 use Attachment\models\AttachmentModel;
 use Basket\models\BasketModel;
 use Basket\models\RedirectBasketModel;
+use Configuration\models\ConfigurationModel;
 use Contact\controllers\ContactController;
 use Contact\models\ContactModel;
 use CustomField\models\CustomFieldModel;
@@ -245,6 +246,13 @@ class SearchController
         return $response->withJson(['resources' => $resources, 'count' => count($allResources), 'allResources' => $allResources]);
     }
 
+    public function getConfiguration(Request $request, Response $response) {
+        $configuration = ConfigurationModel::getByPrivilege(['privilege' => 'admin_search']);
+        $configuration = json_decode($configuration['value'], true);
+
+        return $response->withJson(['configuration' => $configuration]);
+    }
+
     private static function getUserDataClause(array $args)
     {
         ValidatorModel::notEmpty($args, ['userId', 'login']);
diff --git a/src/core/lang/lang-en.php b/src/core/lang/lang-en.php
index 616a2db957c..bbd40788796 100755
--- a/src/core/lang/lang-en.php
+++ b/src/core/lang/lang-en.php
@@ -482,3 +482,5 @@ define('_NOT_GENERATED', 'Not generated');
 
 define('_REGISTERED_MAIL_DISTRIBUTED', 'Acknowledgement receipt received : registered mail distributed');
 define('_REGISTERED_MAIL_NOT_DISTRIBUTED', 'Acknowledgement receipt received : registered mail not distributed');
+
+define('_CONFIGURATION_UPDATED', 'Configuration updated');
diff --git a/src/core/lang/lang-fr.php b/src/core/lang/lang-fr.php
index 735e9ae4da0..954853b176f 100755
--- a/src/core/lang/lang-fr.php
+++ b/src/core/lang/lang-fr.php
@@ -482,3 +482,5 @@ define('_NOT_GENERATED', 'Non généré');
 
 define('_REGISTERED_MAIL_DISTRIBUTED', 'Accusé de réception reçu : recommandé distribué');
 define('_REGISTERED_MAIL_NOT_DISTRIBUTED', 'Accusé de réception reçu : recommandé non distribué');
+
+define('_CONFIGURATION_UPDATED', 'Configuration modifiée');
-- 
GitLab