From b934beec5270ad4447947db3460caffff8fa4b88 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 18 Jul 2019 10:07:27 +0200
Subject: [PATCH] FEAT #11266 TIME 2:00 Get indexing informations

---
 rest/index.php                                |  1 +
 sql/develop.sql                               |  6 +++
 src/app/group/controllers/GroupController.php | 49 ++++++++++++++++++-
 3 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/rest/index.php b/rest/index.php
index c1cec2107ab..593f32bd18a 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -179,6 +179,7 @@ $app->get('/groups/{id}', \Group\controllers\GroupController::class . ':getById'
 $app->put('/groups/{id}', \Group\controllers\GroupController::class . ':update');
 $app->delete('/groups/{id}', \Group\controllers\GroupController::class . ':delete');
 $app->get('/groups/{id}/details', \Group\controllers\GroupController::class . ':getDetailledById');
+$app->get('/groups/{id}/indexing', \Group\controllers\GroupController::class . ':getIndexingDetailsById');
 $app->put('/groups/{id}/services/{serviceId}', \Group\controllers\GroupController::class . ':updateService');
 $app->put('/groups/{id}/reassign/{newGroupId}', \Group\controllers\GroupController::class . ':reassignUsers');
 
diff --git a/sql/develop.sql b/sql/develop.sql
index 301348594cf..a9dcea81e40 100755
--- a/sql/develop.sql
+++ b/sql/develop.sql
@@ -32,6 +32,12 @@ UPDATE res_attachments SET fulltext_result = 'ERROR' WHERE fulltext_result = '-1
 UPDATE res_version_attachments SET fulltext_result = 'SUCCESS' WHERE fulltext_result = '1' OR fulltext_result = '2';
 UPDATE res_version_attachments SET fulltext_result = 'ERROR' WHERE fulltext_result = '-1' OR fulltext_result = '-2';
 
+/* GROUPS INDEXING */
+ALTER TABLE usergroups DROP COLUMN IF EXISTS can_index;
+ALTER TABLE usergroups ADD COLUMN can_index boolean NOT NULL DEFAULT FALSE;
+ALTER TABLE usergroups DROP COLUMN IF EXISTS indexation_parameters;
+ALTER TABLE usergroups ADD COLUMN indexation_parameters jsonb NOT NULL DEFAULT '{"actions" : {}, "entities" : [], "keywords" : []}';
+
 
 /* REFACTORING */
 ALTER TABLE res_letterbox DROP COLUMN IF EXISTS converter_result;
diff --git a/src/app/group/controllers/GroupController.php b/src/app/group/controllers/GroupController.php
index f94897651b7..500d53ed29b 100755
--- a/src/app/group/controllers/GroupController.php
+++ b/src/app/group/controllers/GroupController.php
@@ -2,7 +2,9 @@
 
 namespace Group\controllers;
 
+use Action\models\ActionModel;
 use Basket\models\GroupBasketModel;
+use Entity\models\EntityModel;
 use Group\models\ServiceModel;
 use Group\models\GroupModel;
 use Respect\Validation\Validator;
@@ -130,7 +132,7 @@ class GroupController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $group = GroupModel::getById(['id' => $aArgs['id']]);
+        $group = GroupModel::getById(['id' => $aArgs['id'], 'select' => ['id', 'group_id', 'group_desc', 'enabled']]);
         if (empty($group)) {
             return $response->withStatus(400)->withJson(['errors' => 'Group not found']);
         }
@@ -205,6 +207,51 @@ class GroupController
         return $response->withJson(['success' => 'success']);
     }
 
+    public function getIndexingDetailsById(Request $request, Response $response, array $args)
+    {
+        if (!ServiceModel::hasService(['id' => 'admin_groups', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
+            return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
+        }
+
+        $group = GroupModel::getById(['id' => $args['id'], 'select' => ['can_index', 'indexation_parameters']]);
+        if (empty($group)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Group not found']);
+        }
+
+        $group['indexation_parameters'] = json_decode($group['indexation_parameters'], true);
+        $allActions = ActionModel::get(['select' => ['id', 'label_action'], 'where' => ['component in (?)'], 'data' => [['confirmAction', 'closeMailAction']]]);
+
+        $allEntities = EntityModel::get([
+            'select'    => ['e1.id', 'e1.entity_id', 'e1.entity_label', 'e2.id as parent_id'],
+            'table'     => ['entities e1', 'entities e2'],
+            'left_join' => ['e1.parent_entity_id = e2.entity_id'],
+            'where'     => ['e1.enabled = ?'],
+            'data'      => ['Y']
+        ]);
+
+        foreach ($allEntities as $key => $value) {
+            $allEntities[$key]['id'] = $value['id'];
+            if (empty($value['parent_id'])) {
+                $allEntities[$key]['parent'] = '#';
+                $allEntities[$key]['icon']   = "fa fa-building";
+            } else {
+                $allEntities[$key]['parent'] = $value['parent_id'];
+                $allEntities[$key]['icon']   = "fa fa-sitemap";
+            }
+            $allEntities[$key]['state']['opened'] = false;
+            $allEntities[$key]['allowed']         = true;
+            if (in_array($value['id'], $group['indexation_parameters']['entities'])) {
+                $allEntities[$key]['state']['opened']   = true;
+                $allEntities[$key]['state']['selected'] = true;
+            }
+
+            $allEntities[$key]['text'] = $value['entity_label'];
+        }
+
+
+        return $response->withJson(['group' => $group, 'actions' => $allActions, 'entities' => $allEntities]);
+    }
+
     public static function getGroupsClause(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['userId']);
-- 
GitLab