From ed3ad8715e79c28883a745987dd6659749abc830 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 14 Mar 2019 11:24:14 +0100
Subject: [PATCH] FEAT #9685 get listinstance by res id

---
 rest/index.php                                |  2 +-
 .../controllers/ListInstanceController.php    | 23 +++++++-------
 .../controllers/ListTemplateController.php    |  2 +-
 .../models/ListInstanceModelAbstract.php      | 30 -------------------
 4 files changed, 13 insertions(+), 44 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 8577150059f..45c4fc9b242 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -199,7 +199,7 @@ $app->get('/links/resId/{resId}', \Link\controllers\LinkController::class . ':ge
 
 //Listinstance
 $app->get('/listinstance/{id}', \Entity\controllers\ListInstanceController::class . ':getById');
-$app->get('/res/{resId}/listinstance', \Entity\controllers\ListInstanceController::class . ':getListByResId');
+$app->get('/resources/{resId}/listInstance', \Entity\controllers\ListInstanceController::class . ':getByResId');
 $app->get('/res/{resId}/visaCircuit', \Entity\controllers\ListInstanceController::class . ':getVisaCircuitByResId');
 $app->get('/res/{resId}/avisCircuit', \Entity\controllers\ListInstanceController::class . ':getAvisCircuitByResId');
 $app->put('/listinstances', \Entity\controllers\ListInstanceController::class . ':update');
diff --git a/src/app/entity/controllers/ListInstanceController.php b/src/app/entity/controllers/ListInstanceController.php
index cd8a8bc9340..c477873ffad 100755
--- a/src/app/entity/controllers/ListInstanceController.php
+++ b/src/app/entity/controllers/ListInstanceController.php
@@ -33,25 +33,24 @@ class ListInstanceController
         return $response->withJson($listinstance);
     }
 
-    public function getListByResId(Request $request, Response $response, array $aArgs)
+    public function getByResId(Request $request, Response $response, array $args)
     {
-        if (!Validator::intVal()->validate($aArgs['resId']) || !ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) {
+        if (!Validator::intVal()->validate($args['resId']) || !ResController::hasRightByResId(['resId' => $args['resId'], 'userId' => $GLOBALS['userId']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
         }
-        $listinstances = ListInstanceModel::getListByResId(['select' => ['listinstance_id', 'sequence', 'CASE WHEN item_mode=\'cc\' THEN \'copy\' ELSE item_mode END', 'item_id', 'item_type', 'firstname as item_firstname', 'lastname as item_lastname', 'entity_label as item_entity', 'viewed', 'process_date', 'process_comment', 'signatory', 'requested_signature'], 'id' => $aArgs['resId']]);
-        
-        $roles = EntityModel::getRoles();
 
-        $listinstancesFormat = [];
-        foreach ($listinstances as $key2 => $listinstance) {
-            foreach ($roles as $key => $role) {
-                if ($role['id'] == $listinstance['item_mode']) {
-                    $listinstancesFormat[$role['label']][] = $listinstance;
-                }
+        $listinstances = ListInstanceModel::get(['select' => ['*'], 'where' => ['res_id = ?', 'difflist_type = ?'], 'data' => [$args['resId'], 'entity_id']]);
+        foreach ($listinstances as $key => $value) {
+            if ($value['item_type'] == 'entity_id') {
+                $listTemplates[$key]['labelToDisplay'] = Entitymodel::getByEntityId(['entityId' => $value['item_id'], 'select' => ['entity_label']])['entity_label'];
+                $listTemplates[$key]['descriptionToDisplay'] = '';
+            } else {
+                $listTemplates[$key]['labelToDisplay'] = UserModel::getLabelledUserById(['login' => $value['item_id']]);
+                $listTemplates[$key]['descriptionToDisplay'] = UserModel::getPrimaryEntityByUserId(['userId' => $value['item_id']])['entity_label'];
             }
         }
 
-        return $response->withJson($listinstancesFormat);
+        return $response->withJson(['listInstance' => $listinstances]);
     }
 
     public function getVisaCircuitByResId(Request $request, Response $response, array $aArgs)
diff --git a/src/app/entity/controllers/ListTemplateController.php b/src/app/entity/controllers/ListTemplateController.php
index 4f93a100b3c..d31ea32cf22 100755
--- a/src/app/entity/controllers/ListTemplateController.php
+++ b/src/app/entity/controllers/ListTemplateController.php
@@ -268,7 +268,7 @@ class ListTemplateController
 
         foreach ($listTemplates as $key => $value) {
             if ($value['item_type'] == 'entity_id') {
-                $listTemplates[$key]['labelToDisplay'] = entitymodel::getByEntityId(['entityId' => $value['item_id'], 'select' => ['entity_label']])['entity_label'];
+                $listTemplates[$key]['labelToDisplay'] = Entitymodel::getByEntityId(['entityId' => $value['item_id'], 'select' => ['entity_label']])['entity_label'];
                 $listTemplates[$key]['descriptionToDisplay'] = '';
             } else {
                 $listTemplates[$key]['labelToDisplay'] = UserModel::getLabelledUserById(['login' => $value['item_id']]);
diff --git a/src/app/entity/models/ListInstanceModelAbstract.php b/src/app/entity/models/ListInstanceModelAbstract.php
index 73712f4ac91..3b8842167a9 100755
--- a/src/app/entity/models/ListInstanceModelAbstract.php
+++ b/src/app/entity/models/ListInstanceModelAbstract.php
@@ -88,36 +88,6 @@ abstract class ListInstanceModelAbstract
         return true;
     }
 
-    public static function getListByResId(array $aArgs)
-    {
-        ValidatorModel::notEmpty($aArgs, ['id']);
-        ValidatorModel::intVal($aArgs, ['id']);
-        ValidatorModel::arrayType($aArgs, ['select']);
-
-        $aListinstance = DatabaseModel::select([
-            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
-            'table'     => ['listinstance', 'users', 'users_entities', 'entities'],
-            'left_join' => ['listinstance.item_id = users.user_id', 'users_entities.user_id = users.user_id', 'entities.entity_id = users_entities.entity_id'],
-            'where'     => ['res_id = ?', 'item_type = ?', 'difflist_type = ?', 'primary_entity = ?'],
-            'data'      => [$aArgs['id'], 'user_id', 'entity_id', 'Y'],
-            'order_by'  => ['listinstance_id ASC'],
-        ]);
-
-        unset($aArgs['select'][5]);
-        unset($aArgs['select'][6]);
-
-        $aListinstance2 = DatabaseModel::select([
-            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
-            'table'     => ['listinstance', 'entities'],
-            'left_join' => ['listinstance.item_id = entities.entity_id'],
-            'where'     => ['res_id = ?', 'item_type = ?', 'difflist_type = ?'],
-            'data'      => [$aArgs['id'], 'entity_id', 'entity_id'],
-            'order_by'  => ['listinstance_id ASC'],
-        ]);
-
-        return array_merge($aListinstance, $aListinstance2);
-    }
-
     public static function getVisaCircuitByResId(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['id']);
-- 
GitLab