From f07a753e4ba132e485370f3fcf411a5ed68c7c04 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Thu, 21 Nov 2019 11:45:11 +0100
Subject: [PATCH] FEAT #11861 TIME 0:15 added get contacts by resId route

---
 .../resource/controllers/ResController.php    | 206 ++++++++++++++++++
 src/app/user/models/UserModelAbstract.php     |  19 ++
 2 files changed, 225 insertions(+)

diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index 94b6f7c9c45..b837db4c9fd 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -17,6 +17,8 @@ namespace Resource\controllers;
 use AcknowledgementReceipt\models\AcknowledgementReceiptModel;
 use Basket\models\BasketModel;
 use Basket\models\RedirectBasketModel;
+use Contact\controllers\ContactController;
+use Contact\models\ContactModel;
 use Convert\controllers\ConvertPdfController;
 use Convert\controllers\ConvertThumbnailController;
 use Convert\models\AdrModel;
@@ -45,6 +47,7 @@ use Slim\Http\Request;
 use Slim\Http\Response;
 use SrcCore\controllers\PreparedClauseController;
 use SrcCore\models\CoreConfigModel;
+use SrcCore\models\DatabaseModel;
 use SrcCore\models\ValidatorModel;
 use Status\models\StatusModel;
 use Tag\models\TagModel;
@@ -617,6 +620,34 @@ class ResController
         return $response->withJson(['isAllowed' => true]);
     }
 
+    public function getContacts(Request $request, Response $response, array $args)
+    {
+        $resource = ResModel::getById(['select', ['*'], 'resId' => $args['resId']]);
+
+        if (empty($resource)) {
+            return $response->withStatus(404)->withJson(['errors' => 'Document does not exist']);
+        }
+
+        $queryParams = $request->getQueryParams();
+
+        $contacts = [];
+        if ($queryParams['type'] == 'senders') {
+            if ($resource['category_id'] == 'outgoing') {
+                $contacts = ResController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'resource_contacts', 'columnRes' => 'dest_user_id']);
+            } else {
+                $contacts = ResController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'contacts_res', 'columnRes' => 'exp_user_id']);
+            }
+        } elseif ($queryParams['type'] == 'recipients') {
+            if ($resource['category_id'] == 'outgoing') {
+                $contacts = ResController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'contact_res', 'columnRes' => 'exp_user_id']);
+            } else {
+                $contacts = ResController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'resource_contacts', 'columnRes' => null]);
+            }
+        }
+
+        return $response->withJson(['contacts' => $contacts]);
+    }
+
     public static function getEncodedDocument(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['resId']);
@@ -1307,4 +1338,179 @@ class ResController
 
         return $response->withJson(['resources' => $resources, 'count' => count($resources)]);
     }
+
+    public static function getFormattedContacts(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['resource', 'tableMulti']);
+        ValidatorModel::arrayType($args, ['resource']);
+        ValidatorModel::stringType($args, ['tableMulti', 'columnRes']);
+
+        $resource = $args['resource'];
+
+        $rawContacts = [];
+        if ($resource['is_multicontacts'] == 'Y' || !isset($args['columnRes'])) {
+            if ($args['tableMulti'] == 'contacts_res') {
+                $multiContacts = DatabaseModel::select([
+                    'select' => ['contact_id', 'address_id', 'mode'],
+                    'table' => ['contacts_res'],
+                    'where' => ['res_id = ?'],
+                    'data' => [$resource['res_id']]
+                ]);
+
+                foreach ($multiContacts as $multiContact) {
+                    $rawContacts[] = [
+                        'login'         => $multiContact['contact_id'],
+                        'address_id'    => $multiContact['address_id'],
+                        'mode'          => $multiContact['mode']
+                    ];
+                }
+            } elseif ($args['tableMulti'] == 'resource_contacts') {
+                $multiContacts = DatabaseModel::select([
+                    'select' => ['item_id', 'type', 'mode'],
+                    'table' => ['resource_contacts'],
+                    'where' => ['res_id = ?'],
+                    'data' => [$resource['res_id']]
+                ]);
+
+                foreach ($multiContacts as $multiContact) {
+                    $rawContacts[] = [
+                        'user_id' => $multiContact['type'] == 'user' ? $multiContact['item_id'] : null,
+                        'address_id' => $multiContact['type'] == 'contact' ? $multiContact['item_id'] : null,
+                        'entity_id' => $multiContact['type'] == 'entity' ? $multiContact['item_id'] : null,
+                    ];
+                }
+            }
+        } else {
+            $rawContacts[] = [
+                'login'         => $resource[$args['columnRes']],
+                'address_id'    => $resource['address_id'],
+            ];
+        }
+
+        $contacts = [];
+
+        foreach ($rawContacts as $rawContact) {
+            if (!empty($rawContact['address_id'])) {
+                $contactView = ContactModel::getOnView([
+                    'select' => [
+                        'is_corporate_person', 'lastname', 'firstname', 'address_num', 'address_street', 'address_complement',
+                        'address_town', 'address_postal_code', 'address_country', 'ca_id', 'society', 'website', 'phone',
+                        'contact_firstname', 'contact_lastname', 'address_country', 'email', 'function', 'contact_other_data',
+                        'occupancy'
+                    ],
+                    'where'     => ['ca_id = ?'],
+                    'data'      => [$rawContact['address_id']]
+                ]);
+
+                $contactView = $contactView[0];
+
+                if (!empty($rawContact['mode']) && $rawContact['mode'] == 'third') {
+                    $mode = 'third';
+                } else {
+                    $mode = $contactView['is_corporate_person'] == 'Y' ? 'corporate' : 'physical';
+                }
+
+                $contact = [
+                    'mode'      => $mode,
+                    'firstname' => $contactView['firstname'] ?? '',
+                    'lastname'  => $contactView['lastname'] ?? '',
+                    'email'     => $contactView['email'] ?? '',
+                    'phone'     => $contactView['phone'] ?? '',
+                    'society'   => $contactView['society'] ?? '',
+                    'function'  => $contactView['function'] ?? '',
+                    'num'       => $contactView['address_num'] ?? '',
+                    'street'    => $contactView['address_street'] ?? '',
+                    'complement'=> $contactView['address_complement'] ?? '',
+                    'town'      => $contactView['address_town'] ?? '',
+                    'postalCode'=> $contactView['address_postal_code'] ?? '',
+                    'country'   => $contactView['address_country'] ?? '',
+                    'otherData' => $contactView['contact_other_data'] ?? '',
+                    'website'   => $contactView['website'] ?? '',
+                    'occupancy' => $contactView['occupancy'] ?? '',
+                    'department' => $contactView['departement'] ?? ''
+                ];
+
+                $filling = ContactController::getFillingRate(['contact' => $contact]);
+
+                $contact['filling'] = $filling['color'];
+
+                $contacts[] = $contact;
+            } elseif (!empty($rawContact['login'] || !empty($rawContact['user_id']))) {
+                if (!empty($rawContact['login'])) {
+                    $user = UserModel::getByLowerLogin(['login' => $rawContact['login']]);
+                } else {
+                    $user = UserModel::getById(['id' => $rawContact['user_id']]);
+                }
+
+                $phone = '';
+                if (!empty($phone) || ($user['id'] != $GLOBALS['id']
+                        && PrivilegeController::hasPrivilege(['privilegeId' => 'view_personal_data', 'userId' => $GLOBALS['id']]))) {
+                    $phone = $user['phone'];
+                }
+
+                $primaryEntity = UserModel::getPrimaryEntityById(['id' => $user['id']]);
+
+                $userEntities = UserModel::getNonPrimaryEntitiesById(['id' => $user['id']]);
+                $userEntities = array_column($userEntities, 'entity_label');
+
+                $nonPrimaryEntities = implode(', ', $userEntities);
+
+                $contact = [
+                    'mode'      => 'internal',
+                    'firstname' => $user['firstname'],
+                    'lastname'  => $user['lastname'],
+                    'email'     => $user['mail'],
+                    'phone'     => $phone,
+                    'society'   => '',
+                    'function'  => '',
+                    'num'       => '',
+                    'street'    => '',
+                    'complement'=> '',
+                    'town'      => '',
+                    'postalCode'=> '',
+                    'country'   => '',
+                    'otherData' => '',
+                    'website'   => '',
+                    'occupancy' => $nonPrimaryEntities,
+                    'department' => $primaryEntity['entity_label']
+                ];
+
+                $filling = ContactController::getFillingRate(['contact' => $contact]);
+
+                $contact['filling'] = $filling['color'];
+
+                $contacts[] = $contact;
+            } elseif (!empty($rawContact['entity_id'])) {
+                $entity = EntityModel::getById(['id' => $rawContact['entity_id']]);
+
+                $contact = [
+                    'mode'      => 'entity',
+                    'firstname' => '',
+                    'lastname'  => $entity['entity_label'],
+                    'email'     => $entity['email'],
+                    'phone'     => '',
+                    'society'   => '',
+                    'function'  => '',
+                    'num'       => '',
+                    'street'    => '',
+                    'complement'=> '',
+                    'town'      => '',
+                    'postalCode'=> '',
+                    'country'   => '',
+                    'otherData' => '',
+                    'website'   => '',
+                    'occupancy' => '',
+                    'department' => ''
+                ];
+
+                $filling = ContactController::getFillingRate(['contact' => $contact]);
+
+                $contact['filling'] = $filling['color'];
+
+                $contacts[] = $contact;
+            }
+        }
+
+        return $contacts;
+    }
 }
diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php
index 235ec7f0777..28faec35d37 100755
--- a/src/app/user/models/UserModelAbstract.php
+++ b/src/app/user/models/UserModelAbstract.php
@@ -426,6 +426,25 @@ abstract class UserModelAbstract
         return $entity[0];
     }
 
+    public static function getNonPrimaryEntitiesById(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['id']);
+        ValidatorModel::intVal($args, ['id']);
+
+        $entities = DatabaseModel::select([
+            'select'    => ['users_entities.entity_id', 'entities.entity_label', 'users_entities.user_role', 'users_entities.primary_entity'],
+            'table'     => ['users, users_entities, entities'],
+            'where'     => ['users.user_id = users_entities.user_id', 'users_entities.entity_id = entities.entity_id', 'users.id = ?', 'users_entities.primary_entity = ?'],
+            'data'      => [$args['id'], 'N']
+        ]);
+
+        if (empty($entities)) {
+            return [];
+        }
+
+        return $entities;
+    }
+
     public static function getGroupsByLogin(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['login']);
-- 
GitLab