diff --git a/modules/notifications/batch/basket_event_stack.php b/modules/notifications/batch/basket_event_stack.php
index 252ee2bae1731ed420648c65602fe54c1ea98400..7d98eef7678916239339025d4de3eea683d1a7cb 100755
--- a/modules/notifications/batch/basket_event_stack.php
+++ b/modules/notifications/batch/basket_event_stack.php
@@ -3,13 +3,13 @@
  BATCH BASKET EVENT STACK
 
  Processes events from table event_stack
- 
- 1 - Add events for each basket with notif enabled 
+
+ 1 - Add events for each basket with notif enabled
 
  2 - Scan event
- 
+
  3 - Prepare e-mail and add to e-mail stack
- 
+
 ******************************************************************************/
 
 /* begin */
@@ -26,10 +26,10 @@ while ($state <> 'END') {
     /*                          LOAD_NOTIFICATIONS                        */
     /* Load notification defsidentified with notification id              */
     /**********************************************************************/
-    case 'LOAD_NOTIFICATIONS' :
+    case 'LOAD_NOTIFICATIONS':
         $logger->write("Loading configuration for notification id " . $notificationId, 'INFO');
         $notification = $notifications_controler->getByNotificationId($notificationId);
-        if ($notification === FALSE) {
+        if ($notification === false) {
             Bt_exitBatch(1, "Notification '".$notificationId."' not found");
         }
         if ($notification->is_enabled === 'N') {
@@ -42,67 +42,76 @@ while ($state <> 'END') {
     /*                          LOAD_EVENTS                               */
     /* Checking if the stack has notifications to proceed                 */
     /**********************************************************************/
-    case 'ADD_EVENTS' :
-        $db = new Database();
-        $secCtrl = new SecurityControler();
+    case 'ADD_EVENTS':
+        $db       = new Database();
+        $secCtrl  = new SecurityControler();
         $entities = new entities();
 
-        $stmt = $db->query("select * from baskets WHERE flag_notif = 'Y'"
-        );
+        $stmt = $db->query("SELECT basket_id, except_notif, basket_clause FROM baskets WHERE flag_notif = 'Y'");
 
         while ($line = $stmt->fetchObject()) {
-	    $logger->write("BASKET: " . $line->basket_id . " in progess ...", 'INFO');
+            $logger->write("BASKET: " . $line->basket_id . " in progess ...", 'INFO');
             $exceptUsers[$line->basket_id] = array();
-            if($line->except_notif != '' || $line->except_notif != null){
+            if ($line->except_notif != '' || $line->except_notif != null) {
                 $arrayExceptNotif = explode(',', $line->except_notif);
                 $exceptUsers[$line->basket_id]=$arrayExceptNotif;
             }
-            $stmt2 = $db->query("select group_id from groupbasket WHERE basket_id = '".$line->basket_id."'");
-            //echo $line->basket_id."\n";
+            $stmt2 = $db->query("SELECT group_id FROM groupbasket WHERE basket_id = ?", array($line->basket_id));
+
             $u=1;
             while ($line2 = $stmt2->fetchObject()) {
-                //echo "_".$line2->group_id."\n";
-                $stmt3 = $db->query("select usergroup_content.user_id,users.status from usergroup_content,users WHERE group_id = '".$line2->group_id."' and usergroup_content.user_id=users.user_id");                
+                $recipients = array();
+                $recipients = $diffusion_type_controler->getRecipients($notification, '');
+                $aRecipients = [];
+                foreach ($recipients as $itemRecipient) {
+                    array_push($aRecipients, $itemRecipient->user_id);
+                }
+                if(empty($aRecipients)){
+                    $aRecipients = "0=1";
+                }
+                $stmt3 = $db->query("SELECT usergroup_content.user_id,users.status FROM usergroup_content, users WHERE group_id = ? and usergroup_content.user_id=users.user_id and users.user_id in (?)", array($line2->group_id, $aRecipients));
                 $baskets_notif = array();
-		          $logger->write("GROUP: " . $line2->group_id . " ... " . $stmt3->rowCount() . " user(s) to notify", 'INFO');
+                $rowCount3 = $stmt3->rowCount();
+                $logger->write("GROUP: " . $line2->group_id . " ... " . $rowCount3 . " user(s) to notify", 'INFO');
                 $z=1;
                 while ($line3 = $stmt3->fetchObject()) {
-                    //echo "__".$line3->user_id."\n";
-                    $whereClause = $secCtrl->process_security_where_clause(
-                            $line->basket_clause, $line3->user_id
-                        );
-                    $whereClause = $entities->process_where_clause(
-                            $whereClause, $line3->user_id
-                        );
-                    
-                    $user_id =$line3->user_id;
-                    
-                    if($line3->status == 'ABS'){
-                        $query= "SELECT * FROM user_abs WHERE user_abs = ?";
-                        $db2 = new Database();
-                        $testStmt = $db2->query($query,array($line3->user_id));
+                    $whereClause = $secCtrl->process_security_where_clause($line->basket_clause, $line3->user_id);
+                    $whereClause = $entities->process_where_clause($whereClause, $line3->user_id);
+                    $user_id = $line3->user_id;
+                    if ($line3->status == 'ABS') {
+                        $query    = "SELECT new_user FROM user_abs WHERE user_abs = ?";
+                        $testStmt = $db->query($query, array($line3->user_id));
                         $abs_user = $testStmt->fetchObject();
-                        $user_id = $abs_user->new_user;
+                        $user_id  = $abs_user->new_user;
                     }
-                    $stmt4 = $db->query("select * from res_view_letterbox ".$whereClause);
-        		    $logger->write($stmt4->rowCount() . " document(s) to process for ".$line3->user_id, 'INFO');
-        		    $i=1;
-                    while ($line4 = $stmt4->fetchObject()) {
-			             echo "DOCUMENT " . $i . "/" . $stmt4->rowCount() . " for USER " . $z . "/" . $stmt3->rowCount()." and GROUP ".$u."/".$stmt2->rowCount()."\n";
-                        $stmt6 = $db->query("SELECT user_id FROM notif_event_stack WHERE record_id = '".$line4->res_id."' and  event_info like '%".$line->basket_id."%' and user_id = '".$line3->user_id."'");
-                        $line6 = $stmt6->fetchObject();
-                        if ($line6) {
-                            # code...
-                        }else{
-                            $info = "Notification [".$line->basket_id."] pour ".$line3->user_id;
-                            $stmt5 = $db->query("INSERT INTO notif_event_stack(table_name,notification_sid,record_id,user_id,event_info,event_date) VALUES('res_letterbox','500','".$line4->res_id."','".$user_id."','".$info."',CURRENT_DATE)");                            
-                            preg_match_all( '#\[(\w+)]#', $info, $result );
-                            $basket_id = $result[1];
-                            if(!in_array($basket_id[0], $baskets_notif)){
-                                $baskets_notif[] = $basket_id[0];
+                        
+                    $stmt4 = $db->query("SELECT res_id FROM res_view_letterbox ".$whereClause);
+                    if(!empty($stmt4)){
+                        $userNbDoc = $stmt4->rowCount();
+                        $logger->write($userNbDoc . " document(s) to process for ".$line3->user_id, 'INFO');
+                        $i=1;
+                        $info = "Notification [".$line->basket_id."] pour ".$line3->user_id;
+                        $stmt6 = $db->query("SELECT record_id FROM notif_event_stack WHERE event_info = ? and user_id = ?", array($info, $line3->user_id));
+                        $aRecordId = [];
+                        while($line6 = $stmt6->fetchObject()){
+                            $aRecordId[$line6->record_id] = $line6->record_id;
+                        }
+                        $queryValues = "";
+                        while ($line4 = $stmt4->fetchObject()) {
+                            echo "DOCUMENT " . $i . "/" . $userNbDoc . " for USER " . $z . "/" . $rowCount3." and GROUP ".$u."/".$stmt2->rowCount()."\n";
+                            if (empty($aRecordId[$line4->res_id])) {
+                                $queryValues .= "('res_letterbox','500','".$line4->res_id."','".$user_id."','".$info."',CURRENT_DATE),";
+                                preg_match_all('#\[(\w+)]#', $info, $result);
+                                $basket_id = $result[1];
+                                if (!in_array($basket_id[0], $baskets_notif)) {
+                                    $baskets_notif[] = $basket_id[0];
+                                }
                             }
+                            $i++;
+                        }
+                        if(!empty($queryValues)){
+                            $db->query("INSERT INTO notif_event_stack (table_name, notification_sid, record_id, user_id, event_info, event_date) VALUES " . substr($queryValues, 0, -1));
                         }
-			         $i++;
                     }
                     $z++;
                 }
@@ -125,44 +134,41 @@ while ($state <> 'END') {
     /*                  MERGE_EVENT                                       */
     /* Process event stack to get recipients                              */
     /**********************************************************************/
-    case 'SCAN_EVENT' :
+    case 'SCAN_EVENT':
         $i = 1;
-        foreach($events as $event) {
+        
+        foreach ($events as $event) {
             $logger->write("scanning EVENT : " .$i."/".$totalEventsToProcess." (BASKET => ".$basket_id[0].", DOCUMENT => ".$res_id.", RECIPIENT => ".$user_id.")", 'INFO');
-            preg_match_all( '#\[(\w+)]#', $event->event_info, $result );
+            preg_match_all('#\[(\w+)]#', $event->event_info, $result);
             $basket_id = $result[1];
             //$logger->write("Basket => " .$basket_id[0], 'INFO');
 
             // Diffusion type specific res_id
             $res_id = false;
-            if($event->table_name == $coll_table || $event->table_name == $coll_view) {
+            if ($event->table_name == $coll_table || $event->table_name == $coll_view) {
                 $res_id = $event->record_id;
             } else {
                 $res_id = $diffusion_type_controler->getResId($notification, $event);
-            } 
+            }
             $event->res_id = $res_id;
         
-                    //$logger->write('Document => ' . $res_id, 'INFO');
-                    $user_id = $event->user_id;
-                    //$logger->write('Recipient => ' . $user_id, 'INFO');
+            //$logger->write('Document => ' . $res_id, 'INFO');
+            $user_id = $event->user_id;
+            //$logger->write('Recipient => ' . $user_id, 'INFO');
 
+            if (!isset($tmpNotifs[$user_id])) {
+                $query    = "SELECT * FROM users WHERE user_id = ?";
+                $arrayPDO = array($user_id);
+                $stmt     = $db->query($query, $arrayPDO);
+                $tmpNotifs[$user_id]['recipient'] = $stmt->fetchObject();
+                //$tmpNotifs[$user_id]['recipient'] = $user_id;
+                $tmpNotifs[$user_id]['attach'] = $diffusion_type_controler->getAttachFor($notification, $user_id);
+                //$logger->write('Checking if attachment required for ' . $user_id . ': ' . $tmpNotifs[$user_id]['attach'], 'INFO');
+            }
+            preg_match_all('#\[(\w+)]#', $event->event_info, $result);
+            $basket_id = $result[1];
+            $tmpNotifs[$user_id]['baskets'][$basket_id[0]]['events'][] = $event;
 
-                    if(!isset($tmpNotifs[$user_id])) {
-                        $query = "SELECT * from users where user_id = ?";
-                        $arrayPDO = array($user_id);
-                        $db2 = new Database();
-                        $stmt=$db2->query($query, $arrayPDO);
-                        $tmpNotifs[$user_id]['recipient'] = $stmt->fetchObject();
-                        //$tmpNotifs[$user_id]['recipient'] = $user_id;
-                        $tmpNotifs[$user_id]['attach'] = $diffusion_type_controler->getAttachFor($notification, $user_id);
-                        //$logger->write('Checking if attachment required for ' . $user_id . ': ' . $tmpNotifs[$user_id]['attach'], 'INFO');
-                    }
-                    preg_match_all( '#\[(\w+)]#', $event->event_info, $result );
-                    $basket_id = $result[1];
-                    //print_r($basket_id);
-                    $tmpNotifs[$user_id]['baskets'][$basket_id[0]]['events'][] = $event;
-
-                    //print_r($tmpNotifs[$user_id]);
         $i++;
         }
         $totalNotificationsToProcess = count($tmpNotifs);
@@ -174,76 +180,76 @@ while ($state <> 'END') {
     /**********************************************************************/
         $logger->write('STATE:MERGE NOTIF', 'INFO');
         $i=1;
-        foreach($tmpNotifs as $user_id => $tmpNotif) {
+        foreach ($tmpNotifs as $user_id => $tmpNotif) {
             foreach ($tmpNotif['baskets'] as $key => $basket_list) {
                 $basketId = $key;
-                $stmt6 = $db->query("SELECT basket_name FROM baskets WHERE basket_id = '".$key."'");
-                $line6 = $stmt6->fetchObject();
-                $subject = $line6->basket_name;
+                $stmt6    = $db->query("SELECT basket_name FROM baskets WHERE basket_id = ?", array($key));
+                $line6    = $stmt6->fetchObject();
+                $subject  = $line6->basket_name;
             
-            // Merge template with data and style
-            $logger->write('generate e-mail '.$i.'/'.$totalNotificationsToProcess.' (TEMPLATE =>' . $notification->template_id .', SUBJECT => '.$subject.', RECIPIENT => '.$user_id.', DOCUMENT(S) => '.count($basket_list['events']), 'INFO');
+                // Merge template with data and style
+                $logger->write('generate e-mail '.$i.'/'.$totalNotificationsToProcess.' (TEMPLATE =>' . $notification->template_id .', SUBJECT => '.$subject.', RECIPIENT => '.$user_id.', DOCUMENT(S) => '.count($basket_list['events']), 'INFO');
 
-            //$logger->write('Merging template #' . $notification->template_id 
-            //    . ' to basket '.$subject.' for user ' . $user_id . ' ('.count($basket_list['events']).' documents)', 'INFO');
-            
-            $params = array(
-                'recipient' => $tmpNotif['recipient'],
-                'events' => $basket_list['events'],
-                'notification' => $notification,
-                'maarchUrl' => $maarchUrl,
-                'maarchApps' => $maarchApps,
-                'coll_id' => $coll_id,
-                'res_table' => $coll_table,
-                'res_view' => $coll_view
-            );
-            $html = $templates_controler->merge($notification->template_id, $params, 'content');
+                //$logger->write('Merging template #' . $notification->template_id
+                //    . ' to basket '.$subject.' for user ' . $user_id . ' ('.count($basket_list['events']).' documents)', 'INFO');
+                
+                $params = array(
+                    'recipient'    => $tmpNotif['recipient'],
+                    'events'       => $basket_list['events'],
+                    'notification' => $notification,
+                    'maarchUrl'    => $maarchUrl,
+                    'maarchApps'   => $maarchApps,
+                    'coll_id'      => $coll_id,
+                    'res_table'    => $coll_table,
+                    'res_view'     => $coll_view
+                );
+                $html = $templates_controler->merge($notification->template_id, $params, 'content');
            
-            if(strlen($html) === 0) {
-                foreach($tmpNotif['events'] as $event) {
-                    $events_controler->commitEvent($event->event_stack_sid, "FAILED: Error when merging template");
+                if (strlen($html) === 0) {
+                    foreach ($tmpNotif['events'] as $event) {
+                        $events_controler->commitEvent($event->event_stack_sid, "FAILED: Error when merging template");
+                    }
+                    Bt_exitBatch(8, "Could not merge template with the data");
                 }
-                Bt_exitBatch(8, "Could not merge template with the data");
-            }
             
-            // Prepare e-mail for stack
-            $sender = (string)$mailerParams->mailfrom;
-            $recipient_mail = $tmpNotif['recipient']->mail;
-            //$subject = $notification->description;
-            $html = $func->protect_string_db($html, '', 'no');
-            $html = str_replace('&amp;', '&', $html);
-            $html = str_replace('&', '#and#', $html);
-            
-            // Attachments
-            $attachments = array();
-            if($tmpNotif['attach']) {   
-                $logger->write('Adding attachments', 'INFO');
-                foreach($tmpNotif['events'] as $event) {
-                    // Check if event is related to document in collection
-                    if($event->res_id != '') {
-                        $query = "SELECT "
-                            . "ds.path_template ,"
-                            . "mlb.path, "
-                            . "mlb.filename " 
-                            . "FROM ".$coll_view." mlb LEFT JOIN docservers ds ON mlb.docserver_id = ds.docserver_id "
-                            . "WHERE mlb.res_id = ?";
-                        $stmt = Bt_doQuery($db, $query, array($event->res_id));
-                        $path_parts = $stmt->fetchObject();
-                        $path = $path_parts->path_template . str_replace('#', '/', $path_parts->path) . $path_parts->filename;
-                        $path = str_replace('//', '/', $path);
-                        $path = str_replace('\\', '/', $path);
-                        $attachments[] = $path;
+                // Prepare e-mail for stack
+                $sender         = (string)$mailerParams->mailfrom;
+                $recipient_mail = $tmpNotif['recipient']->mail;
+
+                //$subject = $notification->description;
+                $html = $func->protect_string_db($html, '', 'no');
+                $html = str_replace('&amp;', '&', $html);
+                $html = str_replace('&', '#and#', $html);
+                
+                // Attachments
+                $attachments = array();
+                if ($tmpNotif['attach']) {
+                    $logger->write('Adding attachments', 'INFO');
+                    foreach ($tmpNotif['events'] as $event) {
+                        // Check if event is related to document in collection
+                        if ($event->res_id != '') {
+                            $query = "SELECT "
+                                . "ds.path_template ,"
+                                . "mlb.path, "
+                                . "mlb.filename "
+                                . "FROM ".$coll_view." mlb LEFT JOIN docservers ds ON mlb.docserver_id = ds.docserver_id "
+                                . "WHERE mlb.res_id = ?";
+                            $stmt          = Bt_doQuery($db, $query, array($event->res_id));
+                            $path_parts    = $stmt->fetchObject();
+                            $path          = $path_parts->path_template . str_replace('#', '/', $path_parts->path) . $path_parts->filename;
+                            $path          = str_replace('//', '/', $path);
+                            $path          = str_replace('\\', '/', $path);
+                            $attachments[] = $path;
+                        }
                     }
+                    $logger->write(count($attachments) . ' attachment(s) added', 'INFO');
                 }
-                $logger->write(count($attachments) . ' attachment(s) added', 'INFO');
-            }
-            if(in_array($user_id, $exceptUsers[$basketId])){
-                $logger->write('Notification disabled for '.$user_id, 'WARNING');
-            }else{
-
-                $logger->write('... adding e-mail to email stack', 'INFO');
-                if ($_SESSION['config']['databasetype'] == 'ORACLE') {
-                    $query = "DECLARE
+                if (in_array($user_id, $exceptUsers[$basketId])) {
+                    $logger->write('Notification disabled for '.$user_id, 'WARNING');
+                } else {
+                    $logger->write('... adding e-mail to email stack', 'INFO');
+                    if ($_SESSION['config']['databasetype'] == 'ORACLE') {
+                        $query = "DECLARE
                                   vString notif_email_stack.html_body%type;
                                 BEGIN
                                   vString := '" . $html ."';
@@ -251,36 +257,32 @@ while ($state <> 'END') {
                                   (sender, recipient, subject, html_body, charset, attachments, module) 
                                   VALUES (?, ?, ?, vString, ?, '".implode(',', $attachments)."', 'notifications');
                                 END;";
-                    $arrayPDO = array($sender, $recipient_mail, $subject, $mailerParams->charset);
-                } else {
-
-                    if(count($attachments) > 0) {
-                        $query = "INSERT INTO " . _NOTIF_EMAIL_STACK_TABLE_NAME 
+                        $arrayPDO = array($sender, $recipient_mail, $subject, $mailerParams->charset);
+                    } else {
+                        if (count($attachments) > 0) {
+                            $query = "INSERT INTO " . _NOTIF_EMAIL_STACK_TABLE_NAME
                             . " (sender, recipient, subject, html_body, charset, attachments, module) "
                             . "VALUES (?, ?, ?, ?, ?, '".implode(',', $attachments)."', 'notifications')";
-                    } else {
-                        $query = "INSERT INTO " . _NOTIF_EMAIL_STACK_TABLE_NAME 
+                        } else {
+                            $query = "INSERT INTO " . _NOTIF_EMAIL_STACK_TABLE_NAME
                             . " (sender, recipient, subject, html_body, charset, module) "
-                            . "VALUES (?, ?, ?, ?, ?, 'notifications')";  
+                            . "VALUES (?, ?, ?, ?, ?, 'notifications')";
+                        }
+                        $arrayPDO = array($sender, $recipient_mail, $subject, $html, $mailerParams->charset);
                     }
-                    $arrayPDO = array($sender, $recipient_mail, $subject, $html, $mailerParams->charset);
-                    
-                }
-                //$logger->write('SQL query:' . $query, 'DEBUG');
-                $db2 = new Database();
-                $db2->query($query, $arrayPDO);
-            }
-            foreach($basket_list['events'] as $event) {
-                if(in_array($event->user_id, $exceptUsers[$basketId])){
-                    $events_controler->commitEvent($event->event_stack_sid, "WARNING : Notification disabled for ".$event->user_id);  
 
-                }else{
-                    $events_controler->commitEvent($event->event_stack_sid, "SUCCESS");
+                    $db->query($query, $arrayPDO);
+                }
+                foreach ($basket_list['events'] as $event) {
+                    if (in_array($event->user_id, $exceptUsers[$basketId])) {
+                        $events_controler->commitEvent($event->event_stack_sid, "WARNING : Notification disabled for ".$event->user_id);
+                    } else {
+                        $events_controler->commitEvent($event->event_stack_sid, "SUCCESS");
+                    }
                 }
             }
-            }
-        $i++;
-        } 
+            $i++;
+        }
         $state = 'END';
     }
 }