From 090a5b8fb14a31102aae54997a84f6b9a695c410 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Thu, 21 Nov 2019 17:22:46 +0100
Subject: [PATCH] FEAT #11861 TIME 0:10 moved getContacts to ContactController

---
 rest/index.php                                |   1 +
 .../contact/controllers/ContactController.php | 207 ++++++++++++++++++
 .../resource/controllers/ResController.php    | 206 -----------------
 3 files changed, 208 insertions(+), 206 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index e5004cb230e..ba089e975d7 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -334,6 +334,7 @@ $app->get('/res/{resId}/notes/count', \Resource\controllers\ResController::class
 $app->put('/res/externalInfos', \Resource\controllers\ResController::class . ':updateExternalInfos');
 $app->get('/categories', \Resource\controllers\ResController::class . ':getCategories');
 $app->get('/resources/{resId}/users/{userId}/isDestinationChanging', \Action\controllers\PreProcessActionController::class . ':isDestinationChanging');
+$app->get('/resources/{resId}/contacts', \Contact\controllers\ContactController::class . ':getContacts');
 
 //ResourcesList
 $app->get('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}', \Resource\controllers\ResourceListController::class . ':get');
diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php
index 3e4f5a28399..75021d35e5a 100755
--- a/src/app/contact/controllers/ContactController.php
+++ b/src/app/contact/controllers/ContactController.php
@@ -16,13 +16,17 @@ namespace Contact\controllers;
 
 use Contact\models\ContactFillingModel;
 use Contact\models\ContactModel;
+use Entity\models\EntityModel;
 use Group\controllers\PrivilegeController;
+use Resource\models\ResModel;
 use SrcCore\models\CoreConfigModel;
 use Respect\Validation\Validator;
 use Slim\Http\Request;
 use Slim\Http\Response;
+use SrcCore\models\DatabaseModel;
 use SrcCore\models\TextFormatModel;
 use SrcCore\models\ValidatorModel;
+use User\models\UserModel;
 
 class ContactController
 {
@@ -229,6 +233,34 @@ class ContactController
         return $response->withJson(['success' => 'success']);
     }
 
+    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 = ContactController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'resource_contacts', 'columnRes' => null]);
+            } else {
+                $contacts = ContactController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'contacts_res', 'columnRes' => 'exp_user_id']);
+            }
+        } elseif ($queryParams['type'] == 'recipients') {
+            if ($resource['category_id'] == 'outgoing') {
+                $contacts = ContactController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'contact_res', 'columnRes' => 'exp_user_id']);
+            } else {
+                $contacts = ContactController::getFormattedContacts(['resource' => $resource, 'tableMulti' => 'resource_contacts', 'columnRes' => null]);
+            }
+        }
+
+        return $response->withJson(['contacts' => $contacts]);
+    }
+
     public static function getFillingRate(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['contact']);
@@ -442,4 +474,179 @@ class ContactController
             return false;
         }
     }
+
+    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/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index 2994adbe37d..faa82ebd725 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -17,8 +17,6 @@ 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;
@@ -47,7 +45,6 @@ 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;
@@ -620,34 +617,6 @@ 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' => null]);
-            } 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']);
@@ -1365,179 +1334,4 @@ 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;
-    }
 }
-- 
GitLab