From 71486ee43703dccc55dff1096900370a5cffdbba Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Thu, 5 Dec 2019 12:04:34 +0100
Subject: [PATCH] FEAT #12510 TIME 1:00 moved formatted contacts to
 ContactController

---
 .../contact/controllers/ContactController.php | 140 +++++++++++++-----
 .../resource/controllers/ExportController.php |   8 +-
 .../controllers/ResourceListController.php    |   4 +-
 .../controllers/SummarySheetController.php    |   4 +-
 .../controllers/AutoCompleteController.php    | 101 +------------
 5 files changed, 117 insertions(+), 140 deletions(-)

diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php
index 149c95d6eb8..099f6568a3d 100755
--- a/src/app/contact/controllers/ContactController.php
+++ b/src/app/contact/controllers/ContactController.php
@@ -361,9 +361,9 @@ class ContactController
 
         $contacts = [];
         if ($queryParams['type'] == 'senders') {
-            $contacts = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
+            $contacts = ContactController::getParsedContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
         } elseif ($queryParams['type'] == 'recipients') {
-            $contacts = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
+            $contacts = ContactController::getParsedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
         }
 
         return $response->withJson(['contacts' => $contacts]);
@@ -599,7 +599,7 @@ class ContactController
         }
     }
 
-    public static function getFormattedContacts(array $args)
+    public static function getParsedContacts(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId', 'mode']);
         ValidatorModel::intVal($args, ['resId']);
@@ -708,11 +708,12 @@ class ContactController
         return $contacts;
     }
 
-    public static function getFormattedExportContacts(array $args)
+    public static function getFormattedContacts(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId', 'mode']);
         ValidatorModel::intVal($args, ['resId']);
         ValidatorModel::stringType($args, ['mode']);
+        ValidatorModel::boolType($args, ['onlyContact']);
 
         $contacts = [];
 
@@ -729,37 +730,13 @@ class ContactController
                     'id'        => $resourceContact['item_id']
                 ]);
 
-                $address = '';
-                if (!empty($contactRaw['address_number'])) {
-                    $address .= $contactRaw['address_number'] . ' ';
-                }
-                if (!empty($contactRaw['address_street'])) {
-                    $address .= $contactRaw['address_street'] . ' ';
-                }
-                if (!empty($contactRaw['address_postcode'])) {
-                    $address .= $contactRaw['address_postcode'] . ' ';
-                }
-                if (!empty($contactRaw['address_town'])) {
-                    $address .= $contactRaw['address_town'] . ' ';
-                }
-                if (!empty($contactRaw['address_country'])) {
-                    $address .= $contactRaw['address_country'];
+                if (isset($args['onlyContact']) && $args['onlyContact']) {
+                    $contactToDisplay = ContactController::getFormattedOnlyContact(['contact' => $contactRaw]);
+                } else {
+                    $contactToDisplay = ContactController::getFormattedContactWithAddress(['contact' => $contactRaw]);
                 }
 
-                $contactToDisplay = '';
-                if (!empty($contactRaw['firstname'])) {
-                    $contactToDisplay .= $contactRaw['firstname'] . ' ';
-                }
-                if (!empty($contactRaw['lastname'])) {
-                    $contactToDisplay .= $contactRaw['lastname'];
-                }
-                if (!empty($contactRaw['company'])) {
-                    $contactToDisplay .= " ({$contactRaw['company']})";
-                }
-
-                if (!empty($address)) {
-                    $contactToDisplay .= ' - ' . $address;
-                }
+                $contactToDisplay = $contactToDisplay['contact']['otherInfo'];
 
                 $contact = $contactToDisplay;
             } elseif ($resourceContact['type'] == 'user') {
@@ -840,4 +817,101 @@ class ContactController
 
         return true;
     }
+
+    public static function getFormattedOnlyContact(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['contact']);
+        ValidatorModel::arrayType($args, ['contact']);
+
+        $contactName = '';
+        if (!empty($args['contact']['firstname'])) {
+            $contactName .= $args['contact']['firstname'] . ' ';
+        }
+        if (!empty($args['contact']['lastname'])) {
+            $contactName .= $args['contact']['lastname'] . ' ';
+        }
+
+        $company = '';
+        if (!empty($args['contact']['company'])) {
+            $company = $args['contact']['company'];
+
+            if (!empty($contactName)) {
+                $company = '(' . $company . ') ';
+            }
+        }
+
+        $contactToDisplay = $contactName . $company;
+
+        $contact = [
+            'type'          => 'onlyContact',
+            'id'            => $args['contact']['id'],
+            'idToDisplay'   => $contactToDisplay,
+            'otherInfo'     => $contactToDisplay,
+            'rateColor'     => ''
+        ];
+
+        return ['contact' => $contact];
+    }
+
+    public static function getFormattedContactWithAddress(array $args)
+    {
+        ValidatorModel::notEmpty($args, ['contact']);
+        ValidatorModel::arrayType($args, ['contact']);
+        ValidatorModel::boolType($args, ['color']);
+
+        if (!empty($args['color'])) {
+            $rate = ContactController::getFillingRate(['contact' => $args['contact']]);
+        }
+        $rateColor = empty($rate['color']) ? '' : $rate['color'];
+
+        $address = '';
+
+        if (!empty($args['contact']['address_number'])) {
+            $address.= $args['contact']['address_number'] . ' ';
+        }
+        if (!empty($args['contact']['address_street'])) {
+            $address.= $args['contact']['address_street'] . ' ';
+        }
+        if (!empty($args['contact']['address_postcode'])) {
+            $address.= $args['contact']['address_postcode'] . ' ';
+        }
+        if (!empty($args['contact']['address_town'])) {
+            $address.= $args['contact']['address_town'] . ' ';
+        }
+        if (!empty($args['contact']['address_country'])) {
+            $address.= $args['contact']['address_country'];
+        }
+
+        $contactName = '';
+        if (!empty($args['contact']['firstname'])) {
+            $contactName .= $args['contact']['firstname'] . ' ';
+        }
+        if (!empty($args['contact']['lastname'])) {
+            $contactName .= $args['contact']['lastname'] . ' ';
+        }
+
+        $company = '';
+        if (!empty($args['contact']['company'])) {
+            $company = $args['contact']['company'];
+
+            if (!empty($contactName)) {
+                $company = '(' . $company . ') ';
+            }
+        }
+
+        $contactToDisplay = $contactName . $company;
+
+        $otherInfo = empty($address) ? "{$contactToDisplay}" : "{$contactToDisplay} - {$address}";
+        $contact = [
+            'type'          => 'contact',
+            'id'            => $args['contact']['id'],
+            'contact'       => $contactToDisplay,
+            'address'       => $address,
+            'idToDisplay'   => "{$contactToDisplay}<br/>{$address}",
+            'otherInfo'     => $otherInfo,
+            'rateColor'     => $rateColor
+        ];
+
+        return ['contact' => $contact];
+    }
 }
diff --git a/src/app/resource/controllers/ExportController.php b/src/app/resource/controllers/ExportController.php
index 832119196c0..da84eb09f10 100644
--- a/src/app/resource/controllers/ExportController.php
+++ b/src/app/resource/controllers/ExportController.php
@@ -255,10 +255,10 @@ class ExportController
                     } elseif ($value['value'] == 'getDestinationEntityType') {
                         $csvContent[] = $resource['enthree.entity_type'];
                     } elseif ($value['value'] == 'getSenders') {
-                        $senders = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
+                        $senders = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
                         $csvContent[] = implode("\n", $senders);
                     } elseif ($value['value'] == 'getRecipients') {
-                        $recipients = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
+                        $recipients = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
                         $csvContent[] = implode("\n", $recipients);
                     } elseif ($value['value'] == 'getTypist') {
                         $csvContent[] = UserModel::getLabelledUserById(['id' => $resource['typist']]);
@@ -367,10 +367,10 @@ class ExportController
                     } elseif ($value['value'] == 'getDestinationEntityType') {
                         $content[] = $resource['enthree.entity_type'];
                     } elseif ($value['value'] == 'getSenders') {
-                        $senders = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
+                        $senders = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
                         $content[] = implode("\n", $senders);
                     } elseif ($value['value'] == 'getRecipients') {
-                        $recipients = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
+                        $recipients = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
                         $content[] = implode("\n", $recipients);
                     } elseif ($value['value'] == 'getTypist') {
                         $content[] = UserModel::getLabelledUserById(['id' => $resource['typist']]);
diff --git a/src/app/resource/controllers/ResourceListController.php b/src/app/resource/controllers/ResourceListController.php
index 4927647e512..a85764979c9 100644
--- a/src/app/resource/controllers/ResourceListController.php
+++ b/src/app/resource/controllers/ResourceListController.php
@@ -812,10 +812,10 @@ class ResourceListController
                         $value['displayValue'] = ResourceListController::getAssignee(['resId' => $resource['res_id']]);
                         $display[] = $value;
                     } elseif ($value['value'] == 'getSenders') {
-                        $value['displayValue'] = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
+                        $value['displayValue'] = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'sender', 'onlyContact' => true]);
                         $display[] = $value;
                     } elseif ($value['value'] == 'getRecipients') {
-                        $value['displayValue'] = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
+                        $value['displayValue'] = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient', 'onlyContact' => true]);
                         $display[] = $value;
                     } elseif ($value['value'] == 'getVisaWorkflow') {
                         $value['displayValue'] = ResourceListController::getVisaWorkflow(['resId' => $resource['res_id']]);
diff --git a/src/app/resource/controllers/SummarySheetController.php b/src/app/resource/controllers/SummarySheetController.php
index 90cd7d924ed..1e18a232599 100755
--- a/src/app/resource/controllers/SummarySheetController.php
+++ b/src/app/resource/controllers/SummarySheetController.php
@@ -284,8 +284,8 @@ class SummarySheetController
                 $pdf->MultiCell($widthNotes, 30, _PRIORITY . " : {$priority}", 1, 'L', false, 0, '', '', true, 0, true);
                 $pdf->MultiCell($widthNotes, 30, _PROCESS_LIMIT_DATE . " : {$processLimitDate}", 1, 'L', false, 1, '', '', true, 0, true);
             } elseif ($unit['unit'] == 'senderRecipientInformations') {
-                $senders = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
-                $recipients = ContactController::getFormattedExportContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
+                $senders = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'sender']);
+                $recipients = ContactController::getFormattedContacts(['resId' => $resource['res_id'], 'mode' => 'recipient']);
 
                 if (!empty($senders) && count($senders) > 2) {
                     $nbSenders = count($senders);
diff --git a/src/core/controllers/AutoCompleteController.php b/src/core/controllers/AutoCompleteController.php
index 4b1bbcf47c8..5b6ace605ec 100755
--- a/src/core/controllers/AutoCompleteController.php
+++ b/src/core/controllers/AutoCompleteController.php
@@ -226,10 +226,10 @@ class AutoCompleteController
         $autocompleteData = [];
         foreach ($contacts as $contact) {
             if (!empty($data['onlyContacts']) && $data['onlyContacts'] == 'true' && !in_array($contact['contact_id'], $onlyContacts)) {
-                $autocompleteData[] = AutoCompleteController::getFormattedOnlyContact(['contact' => $contact])['contact'];
+                $autocompleteData[] = ContactController::getFormattedOnlyContact(['contact' => $contact])['contact'];
                 $onlyContacts[] = $contact['contact_id'];
             }
-            $autocompleteData[] = AutoCompleteController::getFormattedContactV2(['contact' => $contact, 'color' => $color])['contact'];
+            $autocompleteData[] = ContactController::getFormattedContactWithAddress(['contact' => $contact, 'color' => $color])['contact'];
         }
 
         $excludedUsers = ['superadmin'];
@@ -759,101 +759,4 @@ class AutoCompleteController
 
         return ['contact' => $contact];
     }
-
-    public static function getFormattedContactV2(array $args)
-    {
-        ValidatorModel::notEmpty($args, ['contact']);
-        ValidatorModel::arrayType($args, ['contact']);
-        ValidatorModel::boolType($args, ['color']);
-
-        if (!empty($args['color'])) {
-            $rate = ContactController::getFillingRate(['contact' => $args['contact']]);
-        }
-        $rateColor = empty($rate['color']) ? '' : $rate['color'];
-
-        $address = '';
-
-        if (!empty($args['contact']['address_number'])) {
-            $address.= $args['contact']['address_number'] . ' ';
-        }
-        if (!empty($args['contact']['address_street'])) {
-            $address.= $args['contact']['address_street'] . ' ';
-        }
-        if (!empty($args['contact']['address_postcode'])) {
-            $address.= $args['contact']['address_postcode'] . ' ';
-        }
-        if (!empty($args['contact']['address_town'])) {
-            $address.= $args['contact']['address_town'] . ' ';
-        }
-        if (!empty($args['contact']['address_country'])) {
-            $address.= $args['contact']['address_country'];
-        }
-
-        $contactName = '';
-        if (!empty($args['contact']['firstname'])) {
-            $contactName .= $args['contact']['firstname'] . ' ';
-        }
-        if (!empty($args['contact']['lastname'])) {
-            $contactName .= $args['contact']['lastname'] . ' ';
-        }
-
-        $company = '';
-        if (!empty($args['contact']['company'])) {
-            $company = $args['contact']['company'];
-
-            if (!empty($contactName)) {
-                $company = '(' . $company . ') ';
-            }
-        }
-
-        $contactToDisplay = $contactName . $company;
-
-        $otherInfo = empty($address) ? "{$contactToDisplay}" : "{$contactToDisplay} - {$address}";
-        $contact = [
-            'type'          => 'contact',
-            'id'            => $args['contact']['id'],
-            'contact'       => $contactToDisplay,
-            'address'       => $address,
-            'idToDisplay'   => "{$contactToDisplay}<br/>{$address}",
-            'otherInfo'     => $otherInfo,
-            'rateColor'     => $rateColor
-        ];
-
-        return ['contact' => $contact];
-    }
-
-    public static function getFormattedOnlyContact(array $args)
-    {
-        ValidatorModel::notEmpty($args, ['contact']);
-        ValidatorModel::arrayType($args, ['contact']);
-
-        $contactName = '';
-        if (!empty($args['contact']['firstname'])) {
-            $contactName .= $args['contact']['firstname'] . ' ';
-        }
-        if (!empty($args['contact']['lastname'])) {
-            $contactName .= $args['contact']['lastname'] . ' ';
-        }
-
-        $company = '';
-        if (!empty($args['contact']['company'])) {
-            $company = $args['contact']['company'];
-
-            if (!empty($contactName)) {
-                $company = '(' . $company . ') ';
-            }
-        }
-
-        $contactToDisplay = $contactName . $company;
-
-        $contact = [
-            'type'          => 'onlyContact',
-            'id'            => $args['contact']['id'],
-            'idToDisplay'   => $contactToDisplay,
-            'otherInfo'     => $contactToDisplay,
-            'rateColor'     => ''
-        ];
-
-        return ['contact' => $contact];
-    }
 }
-- 
GitLab