From d3e606da851328514cfcf6c47d95e3058e8e53bd Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 26 Dec 2019 15:28:33 +0100
Subject: [PATCH] FEAT #12635 TIME 0:25 Contacts list filling order

---
 src/app/contact/controllers/ContactController.php | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php
index 1da58505e24..b7782956fa1 100755
--- a/src/app/contact/controllers/ContactController.php
+++ b/src/app/contact/controllers/ContactController.php
@@ -65,7 +65,7 @@ class ContactController
         $queryParams['offset'] = (empty($queryParams['offset']) || !is_numeric($queryParams['offset']) ? 0 : (int)$queryParams['offset']);
         $queryParams['limit'] = (empty($queryParams['limit']) || !is_numeric($queryParams['limit']) ? 25 : (int)$queryParams['limit']);
         $order = !in_array($queryParams['order'], ['asc', 'desc']) ? '' : $queryParams['order'];
-        $queryParams['orderBy'] = !in_array($queryParams['orderBy'], ['firstname', 'lastname', 'company']) ? ['id'] : ["{$queryParams['orderBy']} {$order}", 'id'];
+        $orderBy = !in_array($queryParams['orderBy'], ['firstname', 'lastname', 'company']) ? ['id'] : ["{$queryParams['orderBy']} {$order}", 'id'];
 
         if (!empty($queryParams['search'])) {
             $fields = ['firstname', 'lastname', 'company', 'address_number', 'address_street', 'address_additional1', 'address_additional2', 'address_postcode', 'address_town', 'address_country'];
@@ -89,7 +89,7 @@ class ContactController
             ],
             'where'     => $requestData['where'] ?? null,
             'data'      => $requestData['data'] ?? null,
-            'orderBy'   => $queryParams['orderBy'],
+            'orderBy'   => $orderBy,
             'offset'    => $queryParams['offset'],
             'limit'     => $queryParams['limit']
         ]);
@@ -100,6 +100,14 @@ class ContactController
             $filling = ContactController::getFillingRate(['contactId' => $contact['id']]);
             $contacts[$key]['filling'] = $filling;
         }
+        if ($queryParams['orderBy'] == 'filling') {
+            usort($contacts, function($a, $b) {
+                return $a['filling']['rate'] <=> $b['filling']['rate'];
+            });
+            if ($queryParams['order'] == 'desc') {
+                $contacts = array_reverse($contacts);
+            }
+        }
 
         return $response->withJson(['contacts' => $contacts, 'count' => $count]);
     }
-- 
GitLab