From 1a09eb55e7f4b467c65992daf4d8e24552e7f16c Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 21 Mar 2019 14:46:42 +0100
Subject: [PATCH] FEAT #9685 Redirect listInstances + getUsersByEntity

---
 rest/index.php                                |  1 +
 sql/data_en.sql                               |  2 +-
 .../controllers/ActionMethodController.php    |  9 +++++++-
 .../entity/controllers/EntityController.php   | 22 +++++++++++++++++++
 .../controllers/ListInstanceController.php    | 12 +++++++++-
 src/app/user/models/UserModelAbstract.php     |  6 +++--
 6 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 43840896540..c6e9cb5bf6b 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -163,6 +163,7 @@ $app->get('/entities/{id}', \Entity\controllers\EntityController::class . ':getB
 $app->put('/entities/{id}', \Entity\controllers\EntityController::class . ':update');
 $app->delete('/entities/{id}', \Entity\controllers\EntityController::class . ':delete');
 $app->get('/entities/{id}/details', \Entity\controllers\EntityController::class . ':getDetailledById');
+$app->get('/entities/{id}/users', \Entity\controllers\EntityController::class . ':getUsersById');
 $app->put('/entities/{id}/reassign/{newEntityId}', \Entity\controllers\EntityController::class . ':reassignEntity');
 $app->put('/entities/{id}/status', \Entity\controllers\EntityController::class . ':updateStatus');
 $app->get('/entityTypes', \Entity\controllers\EntityController::class . ':getTypes');
diff --git a/sql/data_en.sql b/sql/data_en.sql
index 615884e81c7..94b76b689f1 100644
--- a/sql/data_en.sql
+++ b/sql/data_en.sql
@@ -936,7 +936,7 @@ TRUNCATE TABLE contact_addresses;
 INSERT INTO contact_addresses (id, contact_id, contact_purpose_id, departement, firstname, lastname, title, function, occupancy, address_num, address_street, address_complement, address_town, address_postal_code, address_country, phone, email, website, salutation_header, salutation_footer, other_data, user_id, entity_id, is_private, enabled) VALUES (1, 1, 1, '', 'Jean-Louis', 'ERCOLANI', 'title1', 'Directeur Général', '', '11', 'Boulevard du Sud-Est', '', 'MAARCH LES BAINS', '99000', 'France', '', 'jeanlouis.ercolani@maarch.org', 'http://www.maarch.com', '', '', '', 'bblier', 'VILLE', 'N', 'Y');
 INSERT INTO contact_addresses (id, contact_id, contact_purpose_id, departement, firstname, lastname, title, function, occupancy, address_num, address_street, address_complement, address_town, address_postal_code, address_country, phone, email, website, salutation_header, salutation_footer, other_data, user_id, entity_id, is_private, enabled) VALUES (2, 1, 2, '', 'Karim', 'SY', 'title1', 'Administrateur', '', '', 'Sacré Coeur 3', 'Villa 9653 4ème phase', 'DAKAR', '', 'SENEGAL', '', 'karim.sy@maarch.org', 'http://www.maarch.com', '', '', '', 'bblier', 'VILLE', 'N', 'Y');
 INSERT INTO contact_addresses (id, contact_id, contact_purpose_id, departement, firstname, lastname, title, function, occupancy, address_num, address_street, address_complement, address_town, address_postal_code, address_country, phone, email, website, salutation_header, salutation_footer, other_data, user_id, entity_id, is_private, enabled) VALUES (3, 1, 1, '', 'Laurent', 'GIOVANNONI', 'title1', 'Directeur Général Adjoint', NULL, '11', 'Boulevard du Sud-Est', '', 'MAARCH LES BAINS', '99000', 'FRANCE', '', 'laurent.giovannoni@maarch.org', 'http://www.maarch.com', '', '', '', 'bblier', 'COU', 'N', 'Y');
-INSERT INTO contact_addresses (id, contact_id, contact_purpose_id, departement, firstname, lastname, title, function, occupancy, address_num, address_street, address_complement, address_town, address_postal_code, address_country, phone, email, website, salutation_header, salutation_footer, other_data, user_id, entity_id, is_private, enabled) VALUES (4, 2, 1, '', 'Nicolas', 'MARTIN', 'title1', '', NULL, '13', 'RUE LA PREFECTURE', '', 'MAARCH LES BAINS', '', '', '', '', 'http://www.maarch.com', '', '', '', 'bblier', 'COU', 'N', 'Y');
+INSERT INTO contact_addresses (id, contact_id, contact_purpose_id, departement, firstname, lastname, title, function, occupancy, address_num, address_street, address_complement, address_town, address_postal_code, address_country, phone, email, website, salutation_header, salutation_footer, other_data, user_id, entity_id, is_private, enabled, external_id) VALUES (4, 2, 1, '', 'Nicolas', 'MARTIN', 'title1', '', NULL, '13', 'RUE LA PREFECTURE', '', 'MAARCH LES BAINS', '', '', '', '', 'http://www.maarch.com', '', '', '', 'bblier', 'COU', 'N', 'Y', '{"m2m":"org_987654321_DGS_SF"}');
 INSERT INTO contact_addresses (id, contact_id, contact_purpose_id, departement, firstname, lastname, title, function, occupancy, address_num, address_street, address_complement, address_town, address_postal_code, address_country, phone, email, website, salutation_header, salutation_footer, other_data, user_id, entity_id, is_private, enabled) VALUES (5, 3, 1, '', 'Brigitte', 'BERGER', 'title1', 'Directrice Générale', NULL, '25', 'PLACE DES MIMOSAS', '', 'MAARCH LES BAINS', '99000', 'FRANCE', '', 'info@maarch.org', 'http://www.maarch.com', '', '', '', 'bblier', 'COU', 'N', 'Y');
 Select setval('contact_addresses_id_seq', (select max(id)+1 from contact_addresses), false);
 -- Default contact_communication
diff --git a/src/app/action/controllers/ActionMethodController.php b/src/app/action/controllers/ActionMethodController.php
index ebb2ebe3d22..8b58a3a1f95 100644
--- a/src/app/action/controllers/ActionMethodController.php
+++ b/src/app/action/controllers/ActionMethodController.php
@@ -16,6 +16,7 @@ use Attachment\models\AttachmentModel;
 use Convert\controllers\ConvertPdfController;
 use Docserver\models\DocserverModel;
 use Entity\controllers\ListInstanceController;
+use Entity\models\ListInstanceModel;
 use History\controllers\HistoryController;
 use Note\models\NoteModel;
 use Resource\models\ResModel;
@@ -242,7 +243,13 @@ class ActionMethodController
 
         $currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]);
 
-        $controller = ListInstanceController::updateListInstance(['data' => [['resId' => $args['resId'], 'listInstances' => $args['data']]], 'userId' => $currentUser['id']]);
+        $listInstances = [];
+        if (!empty($args['data']['onlyRedirectDest'])) {
+            $listInstances = ListInstanceModel::get(['select' => ['*'], 'where' => ['res_id = ?', 'difflist_type = ?', 'item_mode != ?'], 'data' => [$args['resId'], 'entity_id', 'dest']]);
+        }
+
+        $listInstances = array_merge($listInstances, $args['data']['listInstances']);
+        $controller = ListInstanceController::updateListInstance(['data' => [['resId' => $args['resId'], 'listInstances' => $listInstances]], 'userId' => $currentUser['id']]);
         if (!empty($controller['errors'])) {
             return ['errors' => [$controller['errors']]];
         }
diff --git a/src/app/entity/controllers/EntityController.php b/src/app/entity/controllers/EntityController.php
index 9202f238e98..ad52dfdfdbe 100755
--- a/src/app/entity/controllers/EntityController.php
+++ b/src/app/entity/controllers/EntityController.php
@@ -458,6 +458,28 @@ class EntityController
         return $response->withJson(['success' => 'success']);
     }
 
+    public function getUsersById(Request $request, Response $response, array $aArgs)
+    {
+        $entity = entitymodel::getById(['id' => $aArgs['id'], 'select' => ['entity_id']]);
+        if (empty($entity)) {
+            return $response->withStatus(400)->withJson(['errors' => 'Entity not found']);
+        }
+
+        $users = UserEntityModel::getWithUsers([
+            'select'    => ['DISTINCT users.id', 'users.user_id', 'firstname', 'lastname'],
+            'where'     => ['users_entities.entity_id = ?', 'status not in (?)'],
+            'data'      => [$entity['entity_id'], ['DEL', 'ABS']],
+            'orderBy'   => ['lastname', 'firstname']
+        ]);
+
+        foreach ($users as $key => $user) {
+            $users[$key]['labelToDisplay'] = "{$user['firstname']} {$user['lastname']}";
+            $users[$key]['descriptionToDisplay'] = UserModel::getPrimaryEntityByUserId(['userId' => $user['user_id']])['entity_label'];
+        }
+
+        return $response->withJson(['users' => $users]);
+    }
+
     public function getTypes(Request $request, Response $response)
     {
         return $response->withJson(['types' => EntityModel::getTypes()]);
diff --git a/src/app/entity/controllers/ListInstanceController.php b/src/app/entity/controllers/ListInstanceController.php
index 369d55039a2..83901d4ca40 100755
--- a/src/app/entity/controllers/ListInstanceController.php
+++ b/src/app/entity/controllers/ListInstanceController.php
@@ -130,11 +130,21 @@ class ListInstanceController
                 'data'  => [$ListInstanceByRes['resId'], $ListInstanceByRes['listInstances'][0]['difflist_type']]
             ]);
 
+            $recipientFound = false;
+            foreach ($ListInstanceByRes['listInstances'] as $instance) {
+                if ($instance['item_mode'] == 'dest') {
+                    $recipientFound = true;
+                }
+            }
+            if (!$recipientFound) {
+                return ['errors' => 'Dest is missing', 'code' => 403];
+            }
+
             foreach ($ListInstanceByRes['listInstances'] as $instance) {
                 $listControl = ['item_id', 'item_type', 'item_mode', 'difflist_type'];
                 foreach ($listControl as $itemControl) {
                     if (empty($instance[$itemControl])) {
-                        return ['errors' => $itemControl . ' are empty', 'code' => 400];
+                        return ['errors' => "ListInstance {$itemControl} is not set or empty", 'code' => 400];
                     }
                 }
 
diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php
index 2d35ef8278f..2ea008aa33c 100755
--- a/src/app/user/models/UserModelAbstract.php
+++ b/src/app/user/models/UserModelAbstract.php
@@ -140,9 +140,11 @@ abstract class UserModelAbstract
         if (empty($user)) {
             return [];
         }
-        $users[$args['login']] = $user[0];
+        if (empty($args['select']) || in_array('id', $args['select'])) {
+            $users[$args['login']] = $user[0];
+        }
 
-        return $users[$args['login']];
+        return $user[0];
     }
     
     public static function getByLowerLogin(array $aArgs)
-- 
GitLab