From 470445776dc7d34f7a9e1a4649178b1fb721b1d9 Mon Sep 17 00:00:00 2001
From: Laurent Giovannoni <laurent.giovannoni@maarch.org>
Date: Wed, 17 Oct 2018 17:50:13 +0200
Subject: [PATCH] FIX #8566

---
 apps/maarch_entreprise/actions/index_mlb.php  |   8 +-
 .../actions/validate_mail.php                 |   4 +-
 core/class/class_security.php                 |   4 +
 .../class/class_modules_tools_Abstract.php    | 164 +++++++++++++++++-
 modules/entities/manage_listinstance.php      |   4 +-
 modules/entities/redirect.php                 |  16 +-
 6 files changed, 177 insertions(+), 23 deletions(-)

diff --git a/apps/maarch_entreprise/actions/index_mlb.php b/apps/maarch_entreprise/actions/index_mlb.php
index 0d987ac371f..cbe8d6cc54a 100755
--- a/apps/maarch_entreprise/actions/index_mlb.php
+++ b/apps/maarch_entreprise/actions/index_mlb.php
@@ -107,15 +107,13 @@ function get_form_txt($values, $pathManageAction, $actionId, $table, $module, $c
 
         $allEntitiesTree = array();
         $EntitiesIdExclusion = array();
-
-        if (!empty($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$actionId]['entities']) &&
-            is_array($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$actionId]['entities']) &&
-            count($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$actionId]['entities']) > 0)
+        if (!empty($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$actionId]['entities']))
         {
+            
             $stmt = $db->query(
                 'SELECT entity_id FROM '
                 .ENT_ENTITIES.' WHERE entity_id not in ('
-                .$_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$actionId]['entities']
+                .$_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$actionId]['entities']
                 .") and enabled= 'Y' order by entity_id"
             );
 
diff --git a/apps/maarch_entreprise/actions/validate_mail.php b/apps/maarch_entreprise/actions/validate_mail.php
index 4839e4c3514..aee93933445 100755
--- a/apps/maarch_entreprise/actions/validate_mail.php
+++ b/apps/maarch_entreprise/actions/validate_mail.php
@@ -168,11 +168,11 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
         $EntitiesIdExclusion = array();
         $load_listmodel = true;
 
-        if (is_array($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities']) && count($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities']) > 0) {
+        if (!empty($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities'])) {
             $stmt = $db->query(
                 'SELECT entity_id FROM '
                 .ENT_ENTITIES.' WHERE entity_id not in ('
-                .$_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities']
+                .$_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities']
                 .") and enabled= 'Y' order by entity_id"
             );
             while ($res = $stmt->fetchObject()) {
diff --git a/core/class/class_security.php b/core/class/class_security.php
index 4cc509d7b76..4063a927949 100755
--- a/core/class/class_security.php
+++ b/core/class/class_security.php
@@ -202,6 +202,10 @@ class security extends Database
                 if (isset($_SESSION['user']['redirect_groupbasket'])) {
                     $array['redirect_groupbasket'] = $_SESSION['user']['redirect_groupbasket'];
                 }
+
+                if (isset($_SESSION['user']['redirect_groupbasket_by_group'])) {
+                    $array['redirect_groupbasket_by_group'] = $_SESSION['user']['redirect_groupbasket_by_group'];
+                }
                 /*************************************/
                 $array['services'] = $serv_controler->loadUserServices(
                     $array['UserId']
diff --git a/modules/entities/class/class_modules_tools_Abstract.php b/modules/entities/class/class_modules_tools_Abstract.php
index 76bc8836b19..20e253650fa 100755
--- a/modules/entities/class/class_modules_tools_Abstract.php
+++ b/modules/entities/class/class_modules_tools_Abstract.php
@@ -144,9 +144,11 @@ abstract class entities_Abstract extends functions
             	'level' => (string) $type -> typelevel,
             );
         }
+        // TODO: keep only by_group session array
         $core = new core_tools;
         if ($core->is_module_loaded('basket')) {
             $_SESSION['user']['redirect_groupbasket'] = array();
+            $_SESSION['user']['redirect_groupbasket_by_group'] = array();
             if (isset($userData)
             	&& isset($userData['primarygroup'])
             	&& isset($userData['UserId'])
@@ -154,37 +156,80 @@ abstract class entities_Abstract extends functions
 	            $arr1 = $this->load_redirect_groupbasket_session(
 	                $userData['primarygroup'],
 	                $userData['UserId']
+                );
+                $arr1ByGroup = $this->load_redirect_groupbasket_session_by_group(
+	                $userData['primarygroup'],
+	                $userData['UserId']
 	            );
 	            $arr2 = $this->load_redirect_groupbasket_session_for_abs(
 	                $userData['UserId']
-	            );
+                );
+                $arr2ByGroup = $this->load_redirect_groupbasket_session_for_abs_by_group(
+	                $userData['UserId']
+                );
+                
                 $arrSecondary = array();
+                $arrSecondaryByGroup = array();
                 for ($cptB=0;$cptB<count($_SESSION['user']['baskets']);$cptB++) {
                     $arrTmp = array();
+                    $arrTmpByGroup = array();
                     if ($_SESSION['user']['baskets'][$cptB]['is_secondary']) {
                         $arrTmp = $this->load_redirect_groupbasket_secondary_session(
                             $_SESSION['user']['baskets'][$cptB]['id'],
                             $_SESSION['user']['baskets'][$cptB]['group_id'],
                             $userData['UserId']
                         );
-                        //$this->show_array($arr3);
                     }
+                    //echo $_SESSION['user']['baskets'][$cptB]['id'] 
+                    //    . ' ' . $_SESSION['user']['baskets'][$cptB]['group_id'] . '<br/>';
+                    $arrTmpByGroup = $this->load_redirect_groupbasket_secondary_session_by_group(
+                                $_SESSION['user']['baskets'][$cptB]['id'],
+                                $_SESSION['user']['baskets'][$cptB]['group_id'],
+                                $userData['UserId']
+                    );
                     if (!empty($arrTmp[$_SESSION['user']['baskets'][$cptB]['id']])) {
                         $arrSecondary = array_merge($arrSecondary, $arrTmp);
                     }
+                    if (!empty($arrTmpByGroup[$_SESSION['user']['baskets'][$cptB]['id']])) {
+                        $arrSecondaryByGroup = array_merge_recursive($arrSecondaryByGroup, $arrTmpByGroup);
+                    }
                 }
                 if (!empty($arrSecondary)) {
-                    $_SESSION['user']['redirect_groupbasket']  = array_merge(
+                    $_SESSION['user']['redirect_groupbasket'] = array_merge(
                         $arr1, $arr2, $arrSecondary
                     );
+                    $_SESSION['user']['redirect_groupbasket_by_group'] = array_merge_recursive(
+                        $arr1ByGroup, $arr2ByGroup, $arrSecondaryByGroup
+                    );
                 } else {
-                    $_SESSION['user']['redirect_groupbasket']  = array_merge(
+                    $_SESSION['user']['redirect_groupbasket'] = array_merge(
                         $arr1, $arr2
                     );
+                    $_SESSION['user']['redirect_groupbasket_by_group'] = array_merge_recursive(
+                        $arr1ByGroup, $arr2ByGroup
+                    );
+                }
+            }
+            //to delete duplicates entries due to array_merge_recursive fonction when digital key in an array
+            foreach($_SESSION['user']['redirect_groupbasket_by_group'] as $aBasketKey => $aBasketContent) {
+                foreach($aBasketContent as $aGroupKey => $aGroupContent) {
+                    $cptCtrl = 1;
+                    foreach ($aGroupContent as $aActionKey => $aActionContent) {
+                        if ($cptCtrl % 2 == 0) {
+                            unset($_SESSION['user']['redirect_groupbasket_by_group'][$aBasketKey][$aGroupKey][$aActionKey]);
+                        }
+                        $cptCtrl++;
+                    }
                 }
             }
+            // echo '<pre>';
+            // print_r($_SESSION['user']['redirect_groupbasket']);
+            // echo '</pre>';
+            // echo '<pre>';
+            // print_r($_SESSION['user']['redirect_groupbasket_by_group']);
+            // echo '</pre>';
+            // exit;
         }
-
     }
 
     public function process_where_clause($whereClause, $userId)
@@ -631,12 +676,43 @@ abstract class entities_Abstract extends functions
         return $arr;
     }
 
+    public function load_redirect_groupbasket_session_by_group($primaryGroup, $userId)
+    {
+        $arr = array();
+        $db = new Database();
+        $stmt = $db->query(
+        	'select distinct basket_id from ' . ENT_GROUPBASKET_REDIRECT
+            . " where group_id = ?",array(trim($primaryGroup))
+        );
+
+     
+        while ($res = $stmt->fetchObject()) {
+            $basketId = $res->basket_id;
+            $arr[$basketId] = array();
+
+            $stmt2 = $db->query(
+            	"select distinct action_id from " . ENT_GROUPBASKET_REDIRECT
+                . " where group_id = ? and basket_id = ?",array(trim($primaryGroup),trim($basketId))
+            );
+            while ($line = $stmt2->fetchObject()) {
+                $actionId = $line->action_id;
+                $arr[$basketId][$primaryGroup][$actionId]['entities'] = '';
+                $arr[$basketId][$primaryGroup][$actionId]['users_entities'] = '';
+                $tmpArr = $this->get_redirect_groupbasket(
+                    $primaryGroup, $basketId, $userId, $actionId
+                );
+                $arr[$basketId][$primaryGroup][$actionId]['entities'] = $tmpArr['entities'];
+                $arr[$basketId][$primaryGroup][$actionId]['users_entities'] = $tmpArr['users'];
+            }
+        }
+        return $arr;
+    }
+
     public function load_redirect_groupbasket_secondary_session($basketId, $groupId, $userId)
     {
         $arr = array();
         $db = new Database();
 
-        
         $arr[$basketId] = array();
 
         $stmt = $db->query(
@@ -657,6 +733,31 @@ abstract class entities_Abstract extends functions
         return $arr;
     }
 
+    public function load_redirect_groupbasket_secondary_session_by_group($basketId, $groupId, $userId)
+    {
+        $arr = array();
+        $db = new Database();
+
+        $arr[$basketId] = array();
+
+        $stmt = $db->query(
+            "select distinct action_id from " . ENT_GROUPBASKET_REDIRECT
+            . " where group_id = ? and basket_id = ?",array(trim($groupId),trim($basketId))
+        );
+        while ($line = $stmt->fetchObject()) {
+            $actionId = $line->action_id;
+            $arr[$basketId][$groupId][$actionId]['entities'] = '';
+            $arr[$basketId][$groupId][$actionId]['users_entities'] = '';
+            $tmpArr = $this->get_redirect_groupbasket(
+                $groupId, $basketId, $userId, $actionId
+            );
+            $arr[$basketId][$groupId][$actionId]['entities'] = $tmpArr['entities'];
+            $arr[$basketId][$groupId][$actionId]['users_entities'] = $tmpArr['users'];
+        }
+
+        return $arr;
+    }
+
     public function load_redirect_groupbasket_session_for_abs($userId)
     {
         $arr = array();
@@ -708,6 +809,57 @@ abstract class entities_Abstract extends functions
         return $arr;
     }
 
+    public function load_redirect_groupbasket_session_for_abs_by_group($userId)
+    {
+        $arr = array();
+        $db = new Database();
+
+        if (! isset($_SESSION['user']['baskets'])) {
+            require_once('modules/basket/class/class_modules_tools.php');
+            $bask = new basket();
+            $baskAbs = $bask->load_basket_abs($userId);
+        } else {
+            $baskAbs = $_SESSION['user']['baskets'];
+        }
+        for ($i = 0; $i < count($baskAbs); $i ++) {
+            if ($baskAbs[$i]['abs_basket']) {
+                $stmt = $db->query(
+                	"select uc.group_id from " . USERGROUP_CONTENT_TABLE
+                    . " uc , " . USERGROUPS_TABLE . " u where uc.user_id = ? and u.group_id = "
+                    . "uc.group_id and u.enabled= 'Y' and "
+                    . "uc.primary_group = 'Y'",array($baskAbs[$i]['basket_owner'])
+                );
+                //$db->show();
+                $res = $stmt->fetchObject();
+                $primaryGroup = $res->group_id;
+                $tmpBasketId = preg_replace(
+                	'/_' . $baskAbs[$i]['basket_owner'] . '$/', '',
+                    $baskAbs[$i]['id']
+                );
+                $stmt = $db->query(
+                	"select distinct action_id from " . ENT_GROUPBASKET_REDIRECT
+                    . " where group_id = ? and basket_id = ?",array(trim($primaryGroup),trim($tmpBasketId))
+                );
+                //$db->show();
+                while ($line = $stmt->fetchObject()) {
+                    $actionId = $line->action_id;
+                    $arr[$baskAbs[$i]['id']][$primaryGroup][$actionId]['entities'] = '';
+                    $arr[$baskAbs[$i]['id']][$primaryGroup][$actionId]['users_entities'] = '';
+
+                    $tmpArr = $this->get_redirect_groupbasket(
+                        $primaryGroup, $tmpBasketId,
+                        $baskAbs[$i]['basket_owner'], $actionId
+                    );
+                    $arr[$baskAbs[$i]['id']][$primaryGroup][$actionId]['entities'] =
+                        $tmpArr['entities'];
+                    $arr[$baskAbs[$i]['id']][$primaryGroup][$actionId]['users_entities'] =
+                        $tmpArr['users'];
+                }
+            }
+        }
+        return $arr;
+    }
+
 
     public function get_redirect_groupbasket($groupId, $basketId, $userId, $actionId)
     {
diff --git a/modules/entities/manage_listinstance.php b/modules/entities/manage_listinstance.php
index bf659b16bc4..ac118c52931 100755
--- a/modules/entities/manage_listinstance.php
+++ b/modules/entities/manage_listinstance.php
@@ -171,8 +171,8 @@ $noDelete = false;
 $redirect_groupbasket = false;
 
 if (isset($_SESSION['current_basket']) && count($_SESSION['current_basket']) > 0) {
-    if (is_array($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']])) {
-        $redirect_groupbasket = current($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']]);
+    if (is_array($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']])) {
+        $redirect_groupbasket = current($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']]);
 
         if (empty($redirect_groupbasket['entities'])) {
             $redirect_groupbasket['entities'] = '';
diff --git a/modules/entities/redirect.php b/modules/entities/redirect.php
index 1a4de6edc3b..5ca53203249 100755
--- a/modules/entities/redirect.php
+++ b/modules/entities/redirect.php
@@ -38,20 +38,20 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
         $labelAction = functions::show_string($resAction->label_action);
     }
     
-    preg_match("'^ ,'", $_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities'], $out);
+    preg_match("'^ ,'", $_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities'], $out);
     if (is_array($out[0]) && count($out[0]) == 1) {
-        $_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities'] = substr($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities'], 2, strlen($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities']));
+        $_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities'] = substr($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities'], 2, strlen($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities']));
     }
-    if (!empty($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities'])) {
-        $stmt = $db->query("select entity_id, entity_label from ".ENT_ENTITIES." where entity_id in (".$_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities'].") and enabled= 'Y' order by entity_label");
+    if (!empty($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities'])) {
+        $stmt = $db->query("select entity_id, entity_label from ".ENT_ENTITIES." where entity_id in (".$_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities'].") and enabled= 'Y' order by entity_label");
         while ($res = $stmt->fetchObject()) {
             array_push($services, array( 'ID' => $res->entity_id, 'LABEL' => $db->show_string($res->entity_label)));
             array_push($servicesCompare, $res->entity_id);
         }
     }
     $users = array();
-    if (!empty($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['users_entities'])) {
-        $stmt = $db->query("select distinct ue.user_id, u.lastname, u.firstname from ".ENT_USERS_ENTITIES." ue, ".$_SESSION['tablename']['users']." u where ue.entity_id in (".$_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['users_entities'].") and u.user_id = ue.user_id and (u.status = 'OK' or u.status = 'ABS') and enabled = 'Y' order by u.lastname asc");
+    if (!empty($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['users_entities'])) {
+        $stmt = $db->query("select distinct ue.user_id, u.lastname, u.firstname from ".ENT_USERS_ENTITIES." ue, ".$_SESSION['tablename']['users']." u where ue.entity_id in (".$_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['users_entities'].") and u.user_id = ue.user_id and (u.status = 'OK' or u.status = 'ABS') and enabled = 'Y' order by u.lastname asc");
         while ($res = $stmt->fetchObject()) {
             array_push($users, array( 'ID' => $res->user_id, 'NOM' => functions::show_string($res->lastname), "PRENOM" => functions::show_string($res->firstname)));
         }
@@ -100,7 +100,7 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
     $templatesControler = new templates_controler();
     $templates = array();
 
-    if (!empty($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['entities'])) {
+    if (!empty($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['entities'])) {
         $EntitiesIdExclusion = array();
         $entities            = $entity_ctrl->getAllEntities();
         $countEntities       = count($entities);
@@ -190,7 +190,7 @@ function get_form_txt($values, $path_manage_action, $id_action, $table, $module,
         $frm_str .='</form>';
         $frm_str .='</div>';
     }
-    if (!empty($_SESSION['user']['redirect_groupbasket'][$_SESSION['current_basket']['id']][$id_action]['users_entities'])) {
+    if (!empty($_SESSION['user']['redirect_groupbasket_by_group'][$_SESSION['current_basket']['id']][$_SESSION['current_basket']['group_id']][$id_action]['users_entities'])) {
         $frm_str .='<hr />';
         $frm_str .='<div id="form3">';
         $frm_str .= '<form name="frm_redirect_user" id="frm_redirect_user" method="post" class="forms" action="#">';
-- 
GitLab