From 69951aee65751a57b3b745b578f3eb0630d671ee Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 5 Jun 2017 13:37:29 +0100
Subject: [PATCH] =?UTF-8?q?FIX=20#5607=20r=C3=A9cup=C3=A9rer=20automatique?=
 =?UTF-8?q?ment=20les=20roles=20persistent=20lorsqu'on=20redirige=20en=20m?=
 =?UTF-8?q?asse?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 modules/entities/load_listinstance.php | 12 +++++++--
 modules/entities/redirect.php          | 35 ++++++++++++++------------
 2 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/modules/entities/load_listinstance.php b/modules/entities/load_listinstance.php
index 9d25f3b5574..e0985a8989f 100644
--- a/modules/entities/load_listinstance.php
+++ b/modules/entities/load_listinstance.php
@@ -103,8 +103,16 @@ if(($specific_role <> null || $specific_role <> '') && empty($_SESSION[$origin][
 $content = '';
 
 if(!empty($_SESSION[$origin]['diff_list'])) {
-
-    $_SESSION[$origin]['diff_list'] = $diffList->list_difflist_roles_to_keep($_SESSION['doc_id'], $_REQUEST['collId'], $objectType, $_SESSION[$origin]['diff_list']);
+    // Si on redirige en masse plusieurs courriers, on ne récupère pas les roles persistent
+    if(empty($_SESSION['stockCheckbox']) || count($_SESSION['stockCheckbox']) == 1){
+        if(!empty($_SESSION['stockCheckbox']) && count($_SESSION['stockCheckbox']) == 1){
+            // Cas où on redirige en masse un courrier
+            $res_id = $_SESSION['stockCheckbox'][0];
+        } else {
+            $res_id = $_SESSION['doc_id'];
+        }
+        $_SESSION[$origin]['diff_list'] = $diffList->list_difflist_roles_to_keep($res_id, $_REQUEST['collId'], $objectType, $_SESSION[$origin]['diff_list']);
+    }
 
     $roles    = $diffList->list_difflist_roles();
     $difflist = $_SESSION[$origin]['diff_list'];
diff --git a/modules/entities/redirect.php b/modules/entities/redirect.php
index a4931fefd26..6ece1fa2bf8 100644
--- a/modules/entities/redirect.php
+++ b/modules/entities/redirect.php
@@ -64,10 +64,9 @@ require_once('apps/' . $_SESSION['config']['app_id'] . '/class/class_chrono.php'
             	$values_str .= $values[$i].', ';
 
 		    } else if (_ID_TO_DISPLAY == 'chrono_number'){
-				$values_str .= $values[$i].', ';
-				$chrono_number = $cr7->get_chrono_number($values[$i], 'res_view_letterbox');
-				$chrono_number_str .= $chrono_number.', ';
-				$values_str_chrn .= $chrono_number_str;
+                $values_str      .= $values[$i].', ';
+                $chrono_number   = $cr7->get_chrono_number($values[$i], 'res_view_letterbox');
+                $values_str_chrn .= $chrono_number.', ';
 		    }
         }
     }else{ 
@@ -81,8 +80,7 @@ require_once('apps/' . $_SESSION['config']['app_id'] . '/class/class_chrono.php'
 		    	$values_str .= $_SESSION['stockCheckbox'][$i].', ';
 
 		         $chrono_number = $cr7->get_chrono_number($_SESSION['stockCheckbox'][$i], 'res_view_letterbox');
-		         $chrono_number_str .= $chrono_number.', ';
-		         $values_str_chrn .= $chrono_number_str;
+		         $values_str_chrn .= $chrono_number.', ';
 		    }
         }
     }
@@ -104,8 +102,8 @@ require_once('apps/' . $_SESSION['config']['app_id'] . '/class/class_chrono.php'
     {
       
         $EntitiesIdExclusion = array();
-        $entities = $entity_ctrl->getAllEntities();
-        $countEntities = count($entities);
+        $entities            = $entity_ctrl->getAllEntities();
+        $countEntities       = count($entities);
 
         for ($cptAllEnt = 0;$cptAllEnt<$countEntities;$cptAllEnt++) {
             if (!is_integer(array_search($entities[$cptAllEnt]->__get('entity_id'), $servicesCompare))) {
@@ -113,7 +111,7 @@ require_once('apps/' . $_SESSION['config']['app_id'] . '/class/class_chrono.php'
             }
         }
         
-        $allEntitiesTree= array();
+        $allEntitiesTree = array();
         $allEntitiesTree = $ent->getShortEntityTreeAdvanced(
             $allEntitiesTree, 'all', '', $EntitiesIdExclusion, 'all'
         );
@@ -395,10 +393,15 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
                 
                 $stmt = $db->query(
                     "INSERT INTO notes (identifier, tablename, user_id, "
-                            . "date_note, note_text, coll_id ) VALUES (?,?,?,CURRENT_TIMESTAMP,?,?)",array($res_id,$table,$userIdTypist,$content_note,$coll_id)
+                            . "date_note, note_text, coll_id ) VALUES (?,?,?,CURRENT_TIMESTAMP,?,?)",array($res_id, $table, $userIdTypist, $content_note, $coll_id)
                 );
             }
-            $stmt = $db->query("update ".$table." set destination = ? where res_id = ?",array($entityId,$res_id)); 
+            $stmt = $db->query("update ".$table." set destination = ? where res_id = ?",array($entityId, $res_id)); 
+        }
+
+        // Si on redirige en masse plusieurs courriers, on récupère automatiquement les roles persistent
+        if(count($arr_id) > 1){
+            $new_difflist = $diffList->list_difflist_roles_to_keep($res_id, $coll_id, $new_difflist['difflist_type'], $new_difflist);
         }
         
         # If feature activated, put old dest in copy
@@ -407,7 +410,7 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
             $stmt = $db->query(
                 "select * "
                 . " from " . $_SESSION['tablename']['ent_listinstance'] 
-                . " where coll_id = ? and res_id = ? and item_type = 'user_id' and item_mode = 'dest'",array($coll_id,$res_id)
+                . " where coll_id = ? and res_id = ? and item_type = 'user_id' and item_mode = 'dest'",array($coll_id, $res_id)
             );
 
             $old_dest = $stmt->fetchObject();
@@ -447,10 +450,10 @@ function manage_form($arr_id, $history, $id_action, $label_action, $status, $col
                     array_push(
                         $new_difflist['copy']['users'], 
                         array(
-						'user_id' => $old_dest->item_id, 
-						'viewed' => (integer)$old_dest->viewed,
-						'visible' => 'Y',
-						'difflist_type' => $new_difflist['difflist_type']
+                        'user_id'       => $old_dest->item_id, 
+                        'viewed'        => (integer)$old_dest->viewed,
+                        'visible'       => 'Y',
+                        'difflist_type' => $new_difflist['difflist_type']
                         )
                     );
                 }
-- 
GitLab