From ada3ca718b55909e97bd3173596f7f99c8e8e94b Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 9 Apr 2020 13:58:48 +0100
Subject: [PATCH] FEAT #13679 TIE 3:30 continue refactoring notification
 scripts diffusionTypesController

---
 .../batch/process_event_stack.php             |   4 +-
 .../controllers/DiffusionTypesController.php  | 930 ++++++++----------
 2 files changed, 437 insertions(+), 497 deletions(-)

diff --git a/modules/notifications/batch/process_event_stack.php b/modules/notifications/batch/process_event_stack.php
index 1d276a93e20..cbeecb140e1 100755
--- a/modules/notifications/batch/process_event_stack.php
+++ b/modules/notifications/batch/process_event_stack.php
@@ -62,14 +62,14 @@ while ($state != 'END') {
             foreach ($events as $event) {
                 Bt_writeLog(['level' => 'INFO', 'message' => "Getting recipients using diffusion type '".$notification['diffusion_type']."'"]);
                 // Diffusion type specific recipients
-                $recipients = \Notification\controllers\DiffusionTypesController::getRecipients(['request' => 'recipients', 'notification' => $notification, 'event' => $event]);
+                $recipients = \Notification\controllers\DiffusionTypesController::getItemsToNotify(['request' => 'recipients', 'notification' => $notification, 'event' => $event]);
                 // Diffusion type specific res_id
                 Bt_writeLog(['level' => 'INFO', 'message' => "Getting document ids using diffusion type '".$notification['diffusion_type']."'"]);
                 $res_id = false;
                 if ($event['table_name'] == $coll_table || $event['table_name'] == $coll_view) {
                     $res_id = $event['record_id'];
                 } else {
-                    $res_id = \Notification\controllers\DiffusionTypesController::getRecipients(['request' => 'res_id', 'notification' => $notification, 'event' => $event]);
+                    $res_id = \Notification\controllers\DiffusionTypesController::getItemsToNotify(['request' => 'res_id', 'notification' => $notification, 'event' => $event]);
                 }
                 $event['res_id'] = $res_id;
 
diff --git a/src/app/notification/controllers/DiffusionTypesController.php b/src/app/notification/controllers/DiffusionTypesController.php
index cbb5f9370f8..539fb1ec5f3 100644
--- a/src/app/notification/controllers/DiffusionTypesController.php
+++ b/src/app/notification/controllers/DiffusionTypesController.php
@@ -21,7 +21,7 @@ use User\models\UserModel;
 
 class DiffusionTypesController
 {
-    public static function getRecipients($args = [])
+    public static function getItemsToNotify($args = [])
     {
         $diffusionTypes = NotificationModel::getDiffusionType();
         foreach ($diffusionTypes as $diffusionType) {
@@ -30,15 +30,20 @@ class DiffusionTypesController
                 break;
             }
         }
-        $recipients = DiffusionTypesController::$function(['request' => $args['request'], 'notification' => $args['notification']]);
-        return $recipients;
+
+        $items = [];
+        if (!empty($function)) {
+            $items = DiffusionTypesController::$function(['request' => $args['request'], 'notification' => $args['notification'], 'event' => $args['event']]);
+        }
+
+        return $items;
     }
 
     public static function getRecipientsByContact($args = [])
     {
         if ($args['request'] == 'recipients') {
             $contactsMatch = DatabaseModel::select([
-                'select'    => ['contact.id as user_id', 'contact.email as mail'],
+                'select'    => ['contacts.id as user_id', 'contacts.email as mail'],
                 'table'     => ['resource_contacts', 'contacts'],
                 'left_join' => ['resource_contacts.item_id = contacts.id'],
                 'where'     => ['res_id = ?', 'type = ?', 'mode = ?'],
@@ -52,528 +57,463 @@ class DiffusionTypesController
 
     public static function getRecipientsByCopie($args = [])
     {
-        switch ($request) {
-            case 'recipients':
-                $recipients = array();
-                $dbRecipients = new Database();
-        
-                // Copy to users
-                $select = 'SELECT distinct us.*';
-                $from = ' FROM listinstance li '
-                    .' JOIN users us ON li.item_id = us.user_id';
-                $where = " WHERE li.coll_id = 'letterbox_coll'   AND li.item_mode = 'cc'"
-                    ." AND item_type='user_id'";
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-        
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id'
-                            .' AND ('
-                                .' notes.id not in (SELECT DISTINCT note_id FROM note_entities) '
-                                .' OR us.user_id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid)'
-                            .')'
-                        ;
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= " AND listinstance_id = :recordid AND lb.status not in ('INIT', 'AVAL') AND li.item_id <> :userid";
-                        $arrayPDO = array_merge($arrayPDO, array(':userid' => $event->user_id));
-                }
-        
-                $query = $select.$from.$where;
-        
-                $stmt = $dbRecipients->query($query, $arrayPDO);
-        
-                while ($recipient = $stmt->fetchObject()) {
-                    $recipients[] = $recipient;
-                }
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                // Copy to entities
-                $select = 'SELECT distinct us.*';
-                $from = ' FROM listinstance li '
-                    .' LEFT JOIN users_entities ue ON li.item_id = ue.entity_id '
-                    .' JOIN users us ON ue.user_id = us.user_id';
-                $where = " WHERE li.coll_id = 'letterbox_coll'   AND li.item_mode = 'cc'"
-                    ." AND item_type='entity_id'";
-        
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id'
-                            .' AND ('
-                                .' notes.id not in (SELECT DISTINCT note_id FROM note_entities) '
-                                .' OR us.user_id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid)'
-                            .')'
-                        ;
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $where .= ' AND listinstance_id = :recordid';
-                }
-        
-                $query = $select.$from.$where;
-        
-                $stmt = $dbRecipients->query($query, $arrayPDO);
-        
-                while ($recipient = $stmt->fetchObject()) {
-                    $recipients[] = $recipient;
-                }
-                break;
-        
-            case 'res_id':
-                $arrayPDO = array(':recordid' => $event->record_id);
-                $select = 'SELECT li.res_id';
-                $from = ' FROM listinstance li';
-                $where = " WHERE li.coll_id = 'letterbox_coll'   ";
-        
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id';
-                        break;
+        if ($args['request'] == 'recipients') {
+            $table    = ['listinstance li', 'users us'];
+            $leftJoin = ['li.item_id = us.id'];
+            $where    = ["li.item_mode = 'cc' AND item_type='user_id'"];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    $where[]    = '( notes.id not in (SELECT DISTINCT note_id FROM note_entities) OR us.id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid))';
+                    break;
+        
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid AND lb.status not in ('INIT', 'AVAL') AND li.item_id <> :userid";
+                    $arrayPDO[':userid'] = $args['event']['user_id'];
+            }
         
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        break;
+            // Main document resource from view
+            $recipientsUser = DatabaseModel::select([
+                'select'    => ['distinct us.*'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
         
-                    case 'listinstance':
-                    default:
-                        $where .= ' AND listinstance_id = :recordid';
-                }
+            // Copy to entities
+            $table    = ['listinstance li', 'entities e', 'users_entities ue', 'users us'];
+            $leftJoin = ['li.item_id = e.id', 'e.entity_id = ue.entity_id', 'ue.user_id = us.id'];
+            $where    = ["li.item_mode = 'cc' AND item_type='entity_id'"];
+
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    $where[]    = '( notes.id not in (SELECT DISTINCT note_id FROM note_entities) OR us.id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid))';
+                    break;
+
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $where[] = "listinstance_id = :recordid";
+            }
         
-                $query = $query = $select.$from.$where;
+            // Main document resource from view
+            $recipientsEntities = DatabaseModel::select([
+                'select'    => ['distinct us.*'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => [':recordid' => $args['event']['record_id']],
+            ]);
+
+            $recipients = array_merge($recipientsUser, $recipientsEntities);
+
+            return $recipients;
+        } elseif ($args['request'] == 'res_id') {
+            $table    = ['listinstance li'];
+            $leftJoin = [];
+            $where    = [];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    break;
+
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $where[] = "listinstance_id = :recordid";
+            }
         
-                $dbResId = new Database();
-                $stmt = $dbResId->query($query, $arrayPDO);
-                $res_id_record = $stmt->fetchObject();
-                $res_id = $res_id_record->res_id;
-                break;
+            // Main document resource from view
+            $resId = DatabaseModel::select([
+                'select'    => ['li.res_id'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => [':recordid' => $args['event']['record_id']],
+            ]);
+
+            return $resId[0]['res_id'];
         }
     }
 
     public static function getRecipientsByDestEntity($args = [])
     {
-        switch ($request) {
-            case 'recipients':
-                $recipients = array();
-                $dbRecipients = new Database();
-        
-                $select = 'SELECT distinct en.entity_id, en.enabled, en.email AS mail';
-                $from = ' FROM res_view_letterbox rvl JOIN entities en ON rvl.destination = en.entity_id';
-                $where = ' WHERE rvl.res_id = :recordid';
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-        
-                $query = $select.$from.$where;
-        
-                $stmt = $dbRecipients->query($query, $arrayPDO);
-        
-                while ($recipient = $stmt->fetchObject()) {
-                    $recipients[] = $recipient;
-                }
-                break;
-        
-            case 'res_id':
-                $select = 'SELECT li.res_id';
-                $from = ' FROM listinstance li';
-                $where = " WHERE li.coll_id = 'letterbox_coll'   ";
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND listinstance_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                }
-        
-                $query = $query = $select.$from.$where;
+        if ($args['request'] == 'recipients') {
+            $recipients = DatabaseModel::select([
+                'select'    => ['distinct en.entity_id', 'en.enabled', 'en.email AS mail'],
+                'table'     => ['res_view_letterbox rvl', 'entities en'],
+                'left_join' => ['rvl.destination = en.entity_id'],
+                'where'     => ['rvl.res_id = ?'],
+                'data'      => [$args['event']['record_id']]
+            ]);
+
+            return $recipients;
+        } elseif ($args['request'] == 'res_id') {
+            $table    = ['listinstance li'];
+            $leftJoin = [];
+            $where    = [];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = notes.identifier';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    break;
+
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid";
+            }
+
+            if ($args['notification']['diffusion_properties'] != '') {
+                $aStatus    = explode(',', $args['notification']['diffusion_properties']);
+                $where[]    = 'lb.status in (:statustab)';
+                $arrayPDO[] = [':statustab' => $aStatus];
+            }
         
-                $dbResId = new Database();
-                $stmt = $dbResId->query($query, $arrayPDO);
-                $res_id_record = $stmt->fetchObject();
-                $res_id = $res_id_record->res_id;
-                break;
+            // Main document resource from view
+            $resId = DatabaseModel::select([
+                'select'    => ['li.res_id'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
+
+            return $resId[0]['res_id'];
         }
     }
 
     public static function getRecipientsByDestUserSign($args = [])
     {
-        switch ($request) {
-            case 'recipients':
-                $recipients = array();
-                $dbRecipients = new Database();
-        
-                $select = 'SELECT distinct us.*';
-                $from = ' FROM listinstance li JOIN users us ON li.item_id = us.user_id';
-                $where = " WHERE li.coll_id = 'letterbox_coll' AND li.item_mode = 'sign' "
-                    .'and process_date IS NULL ';
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id'
-                            .' AND ('
-                                .' notes.id not in (SELECT DISTINCT note_id FROM note_entities) '
-                                .' OR us.user_id IN (SELECT ue.user_id FROM note_entities ne JOIN '
-                                .' users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid)'
-                            .')';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-        
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND listinstance_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                }
-        
-                $query = $select.$from.$where;
-        
-                $stmt = $dbRecipients->query($query, $arrayPDO);
-        
-                while ($recipient = $stmt->fetchObject()) {
-                    $recipients[] = $recipient;
-                }
-                break;
-        
-            case 'res_id':
-                $select = 'SELECT li.res_id';
-                $from = ' FROM listinstance li';
-                $where = " WHERE li.coll_id = 'letterbox_coll'   ";
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND listinstance_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                }
+        if ($args['request'] == 'recipients') {
+            $table    = ['listinstance li', 'users us'];
+            $leftJoin = ['li.item_id = us.id'];
+            $where    = ["li.item_mode = 'sign' and process_date IS NULL"];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = notes.identifier';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    $where[]    = '(notes.id not in (SELECT DISTINCT note_id FROM note_entities) OR us.id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid))';
+                    break;
+        
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid";
+            }
+
+            if ($args['notification']['diffusion_properties'] != '') {
+                $aStatus    = explode(',', $args['notification']['diffusion_properties']);
+                $where[]    = 'lb.status in (:statustab)';
+                $arrayPDO[] = [':statustab' => $aStatus];
+            }
         
-                $query = $query = $select.$from.$where;
+            // Main document resource from view
+            $recipients = DatabaseModel::select([
+                'select'    => ['distinct us.*'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
+
+            return $recipients;
+        } elseif ($args['request'] == 'res_id') {
+            $table    = ['listinstance li'];
+            $leftJoin = [];
+            $where    = [];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = notes.identifier';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    break;
+
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid";
+            }
+
+            if ($args['notification']['diffusion_properties'] != '') {
+                $aStatus    = explode(',', $args['notification']['diffusion_properties']);
+                $where[]    = 'lb.status in (:statustab)';
+                $arrayPDO[] = [':statustab' => $aStatus];
+            }
         
-                $dbResId = new Database();
-                $stmt = $dbResId->query($query, $arrayPDO);
-                $res_id_record = $stmt->fetchObject();
-                $res_id = $res_id_record->res_id;
-                break;
+            // Main document resource from view
+            $resId = DatabaseModel::select([
+                'select'    => ['li.res_id'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
+
+            return $resId[0]['res_id'];
         }
     }
 
     public static function getRecipientsByDestUserVisa($args = [])
     {
-        switch ($request) {
-            case 'recipients':
-                $recipients = array();
-                $dbRecipients = new Database();
-        
-                $select = 'SELECT distinct us.*';
-                $from = ' FROM listinstance li JOIN users us ON li.item_id = us.user_id';
-                $where = " WHERE li.coll_id = 'letterbox_coll' AND li.item_mode = 'visa' "
-                    .'and process_date IS NULL ';
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id'
-                            .' AND ('
-                                .' notes.id not in (SELECT DISTINCT note_id FROM note_entities) '
-                                .' OR us.user_id IN (SELECT ue.user_id FROM note_entities ne JOIN '
-                                .' users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid)'
-                            .')';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-        
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND listinstance_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                }
-        
-                $query = $select.$from.$where;
-        
-                $stmt = $dbRecipients->query($query, $arrayPDO);
-        
-                while ($recipient = $stmt->fetchObject()) {
-                    $recipients[] = $recipient;
-                }
-                break;
-        
-            case 'res_id':
-                $select = 'SELECT li.res_id';
-                $from = ' FROM listinstance li';
-                $where = " WHERE li.coll_id = 'letterbox_coll'   ";
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
-                        $where .= ' AND notes.id = :recordid AND li.item_id != notes.user_id';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND listinstance_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                }
+        if ($args['request'] == 'recipients') {
+            $table    = ['listinstance li', 'users us'];
+            $leftJoin = ['li.item_id = us.id'];
+            $where    = ["li.item_mode = 'visa' and process_date IS NULL"];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = notes.identifier';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    $where[]    = '(notes.id not in (SELECT DISTINCT note_id FROM note_entities) OR us.id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid))';
+                    break;
+        
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid";
+            }
+
+            if ($args['notification']['diffusion_properties'] != '') {
+                $aStatus    = explode(',', $args['notification']['diffusion_properties']);
+                $where[]    = 'lb.status in (:statustab)';
+                $arrayPDO[] = [':statustab' => $aStatus];
+            }
         
-                $query = $query = $select.$from.$where;
+            // Main document resource from view
+            $recipients = DatabaseModel::select([
+                'select'    => ['distinct us.*'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
+
+            return $recipients;
+        } elseif ($args['request'] == 'res_id') {
+            $table    = ['listinstance li'];
+            $leftJoin = [];
+            $where    = [];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = notes.identifier';
+                    $where[]    = 'notes.id = :recordid AND li.item_id != notes.user_id';
+                    break;
+
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid";
+            }
+
+            if ($args['notification']['diffusion_properties'] != '') {
+                $aStatus    = explode(',', $args['notification']['diffusion_properties']);
+                $where[]    = 'lb.status in (:statustab)';
+                $arrayPDO[] = [':statustab' => $aStatus];
+            }
         
-                $dbResId = new Database();
-                $stmt = $dbResId->query($query, $arrayPDO);
-                $res_id_record = $stmt->fetchObject();
-                $res_id = $res_id_record->res_id;
-                break;
+            // Main document resource from view
+            $resId = DatabaseModel::select([
+                'select'    => ['li.res_id'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
+
+            return $resId[0]['res_id'];
         }
     }
 
     public static function getRecipientsByDestUser($args = [])
     {
-        switch ($request) {
-            case 'recipients':
-                $recipients = array();
-                $dbRecipients = new Database();
-        
-                $select = 'SELECT distinct us.*';
-                $from = ' FROM listinstance li JOIN users us ON li.item_id = us.user_id';
-                $where = " WHERE li.item_mode = 'dest'";
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
-                        $where .= ' AND notes.id = :recordid AND us.id != notes.user_id'
-                            .' AND ('
-                                .' notes.id not in (SELECT DISTINCT note_id FROM note_entities) '
-                                .' OR us.user_id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid)'
-                            .')';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-        
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND lb.res_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' AND listinstance_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                }
-        
-                $query = $select.$from.$where;
-        
-                $stmt = $dbRecipients->query($query, $arrayPDO);
-        
-                while ($recipient = $stmt->fetchObject()) {
-                    $recipients[] = $recipient;
-                }
-                break;
-        
-            case 'res_id':
-                $select = 'SELECT li.res_id';
-                $from = ' FROM listinstance li JOIN users us ON li.item_id = us.user_id';
-                $where = " WHERE ";
-        
-                $arrayPDO = array(':recordid' => $event->record_id);
-                switch ($event->table_name) {
-                    case 'notes':
-                        $from .= ' JOIN notes ON notes.identifier = li.res_id';
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = notes.identifier';
-                        $where .= ' notes.id = :recordid AND us.id != notes.user_id';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'res_letterbox':
-                    case 'res_view_letterbox':
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' lb.res_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                        break;
-        
-                    case 'listinstance':
-                    default:
-                        $from .= ' JOIN res_letterbox lb ON lb.res_id = li.res_id';
-                        $where .= ' listinstance_id = :recordid';
-                        if ($notification->diffusion_properties != '') {
-                            $status_tab = explode(',', $notification->diffusion_properties);
-                            $where .= ' AND lb.status in (:statustab)';
-                            $arrayPDO = array_merge($arrayPDO, array(':statustab' => $status_tab));
-                        }
-                }
+        if ($args['request'] == 'recipients') {
+            $table    = ['listinstance li', 'users us'];
+            $leftJoin = ['li.item_id = us.id'];
+            $where    = ["li.item_mode = 'dest'"];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = notes.identifier';
+                    $where[]    = 'notes.id = :recordid AND us.id != notes.user_id';
+                    $where[]    = '(notes.id not in (SELECT DISTINCT note_id FROM note_entities) OR us.id IN (SELECT ue.user_id FROM note_entities ne JOIN users_entities ue ON ne.item_id = ue.entity_id WHERE ne.note_id = :recordid))';
+                    break;
+        
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid";
+            }
+
+            if ($args['notification']['diffusion_properties'] != '') {
+                $aStatus    = explode(',', $args['notification']['diffusion_properties']);
+                $where[]    = 'lb.status in (:statustab)';
+                $arrayPDO[] = [':statustab' => $aStatus];
+            }
         
-                $query = $query = $select.$from.$where;
+            // Main document resource from view
+            $recipients = DatabaseModel::select([
+                'select'    => ['distinct us.*'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
+
+            return $recipients;
+        } elseif ($args['request'] == 'res_id') {
+            $table    = ['listinstance li','users us'];
+            $leftJoin = ['li.item_id = us.id'];
+            $where    = [];
+            $arrayPDO = [':recordid' => $args['event']['record_id']];
+        
+            switch ($args['event']['table_name']) {
+                case 'notes':
+                    $table[]    = 'notes';
+                    $leftJoin[] = 'notes.identifier = li.res_id';
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = notes.identifier';
+                    $where[]    = 'notes.id = :recordid AND us.id != notes.user_id';
+                    break;
+
+                case 'res_letterbox':
+                case 'res_view_letterbox':
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = 'lb.res_id = :recordid';
+                    break;
+
+                case 'listinstance':
+                default:
+                    $table[]    = 'res_letterbox lb';
+                    $leftJoin[] = 'lb.res_id = li.res_id';
+                    $where[]    = "listinstance_id = :recordid";
+            }
+
+            if ($args['notification']['diffusion_properties'] != '') {
+                $aStatus    = explode(',', $args['notification']['diffusion_properties']);
+                $where[]    = 'lb.status in (:statustab)';
+                $arrayPDO[] = [':statustab' => $aStatus];
+            }
         
-                $dbResId = new Database();
-                $stmt = $dbResId->query($query, $arrayPDO);
-                $res_id_record = $stmt->fetchObject();
-                $res_id = $res_id_record->res_id;
-                break;
+            // Main document resource from view
+            $resId = DatabaseModel::select([
+                'select'    => ['li.res_id'],
+                'table'     => $table,
+                'left_join' => $leftJoin,
+                'where'     => $where,
+                'data'      => $arrayPDO,
+            ]);
+
+            return $resId[0]['res_id'];
         }
     }
 
-- 
GitLab