From 75a41c3c39a88eddd2bc5d70ef529e313b5b56da Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 2 Oct 2020 15:22:58 +0200
Subject: [PATCH] FEAT #13271 TIME 2:20 New search fields + groups route

---
 src/app/group/controllers/GroupController.php | 14 +++---
 .../search/controllers/SearchController.php   | 43 +++++++++++++++++++
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/src/app/group/controllers/GroupController.php b/src/app/group/controllers/GroupController.php
index d13d5dead36..9f8942c18d2 100755
--- a/src/app/group/controllers/GroupController.php
+++ b/src/app/group/controllers/GroupController.php
@@ -24,13 +24,15 @@ class GroupController
 
     public function get(Request $request, Response $response)
     {
-        if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_groups', 'userId' => $GLOBALS['id']])) {
-            return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
-        }
+        $hasPrivilege = PrivilegeController::hasPrivilege(['privilegeId' => 'admin_groups', 'userId' => $GLOBALS['id']]);
 
-        $groups = GroupModel::get(['orderBy' => ['group_desc']]);
-        foreach ($groups as $key => $value) {
-            $groups[$key]['users'] = GroupModel::getUsersById(['id' => $value['id'], 'select' => ['users.user_id', 'users.firstname', 'users.lastname']]);
+        $select = $hasPrivilege ? ['*'] : ['id', 'group_desc'];
+        $groups = GroupModel::get(['select' => $select, 'orderBy' => ['group_desc']]);
+
+        if ($hasPrivilege) {
+            foreach ($groups as $key => $value) {
+                $groups[$key]['users'] = GroupModel::getUsersById(['id' => $value['id'], 'select' => ['users.user_id', 'users.firstname', 'users.lastname']]);
+            }
         }
 
         return $response->withJson(['groups' => $groups]);
diff --git a/src/app/search/controllers/SearchController.php b/src/app/search/controllers/SearchController.php
index e116e298ca3..1bbdf7825ce 100644
--- a/src/app/search/controllers/SearchController.php
+++ b/src/app/search/controllers/SearchController.php
@@ -410,6 +410,10 @@ class SearchController
             $args['searchData'][] = "%{$body['chrono']['values']}%";
             $args['searchData'][] = "%{$body['chrono']['values']}%";
         }
+        if (!empty($body['barcode']) && !empty($body['barcode']['values']) && is_string($body['barcode']['values'])) {
+            $args['searchWhere'][] = 'barcode ilike ?';
+            $args['searchData'][] = "%{$body['barcode']['values']}%";
+        }
         if (!empty($body['resId']) && !empty($body['resId']['values']) && is_array($body['resId']['values'])) {
             if (Validator::intVal()->notEmpty()->validate($body['resId']['values']['start'])) {
                 $args['searchWhere'][] = 'res_id >= ?';
@@ -514,6 +518,16 @@ class SearchController
                 $args['searchData'][] = SearchController::getEndDayDate(['date' => $body['processLimitDate']['values']['end']]);
             }
         }
+        if (!empty($body['closingDate']) && !empty($body['closingDate']['values']) && is_array($body['closingDate']['values'])) {
+            if (Validator::date()->notEmpty()->validate($body['closingDate']['values']['start'])) {
+                $args['searchWhere'][] = 'closing_date >= ?';
+                $args['searchData'][] = $body['closingDate']['values']['start'];
+            }
+            if (Validator::date()->notEmpty()->validate($body['closingDate']['values']['end'])) {
+                $args['searchWhere'][] = 'closing_date <= ?';
+                $args['searchData'][] = SearchController::getEndDayDate(['date' => $body['closingDate']['values']['end']]);
+            }
+        }
         if (!empty($body['senders']) && !empty($body['senders']['values']) && is_array($body['senders']['values']) && is_array($body['senders']['values'][0])) {
             $where = '';
             $data = [];
@@ -664,6 +678,35 @@ class SearchController
                 $args['searchData'][] = $foldersMatch;
             }
         }
+        if (!empty($body['notes']) && !empty($body['notes']['values']) && is_string($body['notes']['values'])) {
+            $notesMatch = NoteModel::get(['select' => ['identifier'], 'where' => ['note_text ilike ?'], 'data' => ["%{$body['notes']['values']}%"]]);
+            if (empty($notesMatch)) {
+                return null;
+            }
+
+            $args['searchWhere'][] = 'res_id in (?)';
+            $notesMatch = array_column($notesMatch, 'identifier');
+            $args['searchData'][] = $notesMatch;
+        }
+        if (!empty($body['attachment_type']) && !empty($body['attachment_type']['values']) && is_array($body['attachment_type']['values'])) {
+            $args['searchWhere'][] = 'res_id in (select DISTINCT res_id_master from res_attachments where attachment_type in (?))';
+            $args['searchData'][] = $body['attachment_type']['values'];
+        }
+        if (!empty($body['attachment_creationDate']) && !empty($body['attachment_creationDate']['values']) && is_array($body['attachment_creationDate']['values'])) {
+            if (Validator::date()->notEmpty()->validate($body['attachment_creationDate']['values']['start'])) {
+                $args['searchWhere'][] = 'res_id in (select DISTINCT res_id_master from res_attachments where creation_date >= ?)';
+                $args['searchData'][] = $body['attachment_creationDate']['values']['start'];
+            }
+            if (Validator::date()->notEmpty()->validate($body['attachment_creationDate']['values']['end'])) {
+                $args['searchWhere'][] = 'res_id in (select DISTINCT res_id_master from res_attachments where creation_date <= ?)';
+                $args['searchData'][] = SearchController::getEndDayDate(['date' => $body['attachment_creationDate']['values']['end']]);
+            }
+        }
+        if (!empty($body['groupSign']) && !empty($body['groupSign']['values']) && is_array($body['groupSign']['values'])) {
+            $args['searchWhere'][] = 'res_id in (select DISTINCT res_id from listinstance where item_mode = ? AND item_id in (select DISTINCT user_id from usergroup_content where group_id in (?)))';
+            $args['searchData'][] = 'sign';
+            $args['searchData'][] = $body['groupSign']['values'];
+        }
 
         return ['searchWhere' => $args['searchWhere'], 'searchData' => $args['searchData']];
     }
-- 
GitLab