From 2d47552d18550b3f26a6ec5b98cdbb7c7f8ce2d1 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Wed, 7 Oct 2020 15:10:50 +0200
Subject: [PATCH] FEAT #13271 TIME 1:30 Senders department search

---
 .../search/controllers/SearchController.php   | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/src/app/search/controllers/SearchController.php b/src/app/search/controllers/SearchController.php
index 26a48c9aa70..f975fbc5179 100644
--- a/src/app/search/controllers/SearchController.php
+++ b/src/app/search/controllers/SearchController.php
@@ -673,6 +673,39 @@ class SearchController
             $args['searchData'][] = 'true';
             $args['searchData'][] = $body['groupSign']['values'];
         }
+        if (!empty($body['senderDepartment']) && !empty($body['senderDepartment']['values']) && is_array($body['senderDepartment']['values'])) {
+            $departments = '';
+            foreach ($body['senderDepartment']['values'] as $value) {
+                if (!is_numeric($value)) {
+                    continue;
+                }
+                if (!empty($departments)) {
+                    $departments .= ', ';
+                }
+                $departments .= "'{$value}%'";
+            }
+            $contacts = ContactModel::get([
+                'select' => ['id'],
+                'where'  => ["address_postcode like any (array[{$departments}])"]
+            ]);
+            $contactIds = array_column($contacts, 'id');
+            if (empty($contactIds)) {
+                return null;
+            } else {
+                $sendersMatch = ResourceContactModel::get([
+                    'select'    => ['res_id'],
+                    'where'     => ['item_id in (?)', 'type = ?', 'mode = ?'],
+                    'data'      => [$contactIds, 'contact', 'sender']
+                ]);
+                $sendersMatch = array_column($sendersMatch, 'res_id');
+                if (empty($sendersMatch)) {
+                    return null;
+                } else {
+                    $args['searchWhere'][] = 'res_id in (?)';
+                    $args['searchData'][] = $sendersMatch;
+                }
+            }
+        }
 
         return ['searchWhere' => $args['searchWhere'], 'searchData' => $args['searchData']];
     }
-- 
GitLab