Skip to content
Snippets Groups Projects
manage_listinstance.php 52.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • * File : manage_listinstance.php.
    
    SNA's avatar
    SNA committed
    *
    * Pop up used to create and modify diffusion lists instances
    *
    * @version 1.0
    
    SNA's avatar
    SNA committed
    * @since 06/2006
    
    SNA's avatar
    SNA committed
    * @license GPL
    * @author  Claire Figueras  <dev@maarch.org>
    
    * @author  Cyril Vazquez  <dev@maarch.org>
    
    SNA's avatar
    SNA committed
    */
    
    require_once 'core/class/usergroups_controler.php';
    
    require_once 'modules/entities/class/class_manage_listdiff.php';
    require_once 'modules/entities/entities_tables.php';
    require_once 'core/core_tables.php';
    
    SNA's avatar
    SNA committed
    
    
    $core_tools = new core_tools();
    $core_tools->load_lang();
    
    SNA's avatar
    SNA committed
    $func = new functions();
    
    
    $db = new Database();
    
    $difflist = new diffusion_list();
    
    $usergroups_controler = new usergroups_controler();
    
    
    // *****************************************************************************
    // Manage request paramaters
    // *****************************************************************************
    
    // Origin
    $origin = $_REQUEST['origin'];
    
    // Action ?
    
    if (isset($_GET['action'])) {
    
    if (isset($_GET['id'])) {
    
    if (isset($_GET['cat'])) {
    
    if (isset($_GET['rank'])) {
    
    
    // Mode (dest/copy or custom copy mode)
    
    if (isset($_GET['role']) && !empty($_GET['role'])) {
    
    if (isset($_GET['specific_role'])) {
    
        $specific_role = $_GET['specific_role'];
    
        $specific_role = false;
    
    }
    
    // *****************************************************************************
    // Manage SESSION paramaters
    // *****************************************************************************
    
    //$objectType = $_SESSION[$origin]['diff_list']['difflist_type'];
    
    // Load roles
    
    $difflistType = $difflist->get_difflist_type($objectType);
    $roles = $difflist->get_difflist_type_roles($difflistType);
    
    $available_roles = $difflist->list_difflist_roles();
    
    if ($difflistType->allow_entities == 'Y') {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    // Entity_id by default
    
    if ($difflistType == '') {
        $roles = array();
        $difflistType = $difflist->get_difflist_type('entity_id');
        $roles = $difflist->get_difflist_type_roles($difflistType);
        $allow_entities = true;
    
    // Dest user
    if (isset($_SESSION[$origin]['diff_list']['dest']['users'][0])
        && !empty($_SESSION[$origin]['diff_list']['dest']['users'][0])) {
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
    // 1.4 create indexed array of existing diffusion to search for users/entities easily
    $user_roles = array();
    $entity_roles = array();
    
    foreach ($roles as $role_id_local => $role_label) {
    
    Florian Azizian's avatar
    Florian Azizian committed
        if (!empty($_SESSION[$origin]['diff_list'][$role_id_local]['users'])) {
            for ($i = 0, $l = count($_SESSION[$origin]['diff_list'][$role_id_local]['users']); $i < $l; ++$i) {
                $user_id = $_SESSION[$origin]['diff_list'][$role_id_local]['users'][$i]['user_id'];
                $user_roles[$user_id][] = $role_id_local;
            }
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
        }
    
    Florian Azizian's avatar
    Florian Azizian committed
        if (!empty($_SESSION[$origin]['diff_list'][$role_id_local]['entities'])) {
            for ($i = 0, $l = count($_SESSION[$origin]['diff_list'][$role_id_local]['entities']); $i < $l; ++$i) {
                $entity_id = $_SESSION[$origin]['diff_list'][$role_id_local]['entities'][$i]['entity_id'];
                $entity_roles[$entity_id][] = $role_id_local;
            }
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
        }
    
    }
    // *****************************************************************************
    // Search functions / filter users and entities avilable for list composition
    // *****************************************************************************
    
    if (isset($_POST['what_users']) && !empty($_POST['what_users'])) {
    
        $_GET['what_users'] = $_POST['what_users'];
    
    SNA's avatar
    SNA committed
    }
    
    if (isset($_POST['what_services']) && !empty($_POST['what_services'])) {
    
        $_GET['what_services'] = $_POST['what_services'];
    
    SNA's avatar
    SNA committed
    }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    // by default, if difflist for entities, load users and entity of the selectionned entity
    
    $primaryEntityLabel = '';
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    if ($_GET['what_services'] == '' && $_GET['what_users'] == '') {
    
        if ($_SESSION[$origin]['difflist_object']['object_label'] != null) {
    
            $_GET['what_services'] = $_SESSION[$origin]['difflist_object']['object_label'];
    
            $_SESSION['user']['primaryentity']['id'];
            $_GET['what_services'] = $_SESSION['user']['primaryentity']['id'];
    
            foreach ($_SESSION['user']['entities'] as $key => $value) {
                if ($_SESSION['user']['entities'][$key]['ENTITY_ID'] == $_SESSION['user']['primaryentity']['id']) {
    
                    $primaryEntityLabel = $_SESSION['user']['entities'][$key]['ENTITY_LABEL'];
    
                    $_GET['what_services'] = $primaryEntityLabel;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    }
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    if (isset($_REQUEST['no_filter'])) {
        $_GET['what_users'] = '%';
        $_GET['what_services'] = '%';
    }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    
    SNA's avatar
    SNA committed
    $users = array();
    $entities = array();
    
    SNA's avatar
    SNA committed
    $whereUsers = '';
    $whereEntities = '';
    $orderByUsers = '';
    $orderByEntities = '';
    $whereEntitiesUsers = '';
    
    $what = '';
    
    SNA's avatar
    SNA committed
    $whatUsers = '';
    $whatServices = '';
    $onlyCc = false;
    
    $noDelete = false;
    
    if (isset($_SESSION['current_basket']) && count($_SESSION['current_basket']) > 0) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        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'] = '';
                //$redirect_groupbasket['entities'] = $db->empty_list();
    
            if (empty($redirect_groupbasket['users_entities'])) {
    
                $redirect_groupbasket['users_entities'] = '';
                //$redirect_groupbasket['users_entities'] = $db->empty_list();
    
    SNA's avatar
    SNA committed
    if (isset($_REQUEST['only_cc'])) {
        $onlyCc = true;
    
    SNA's avatar
    SNA committed
    }
    
    if (isset($_REQUEST['no_delete'])) {
        $noDelete = true;
    }
    
    $PDOarray = array();
    
    if (isset($_GET['what_users'])
        && !empty($_GET['what_users'])
    
        $what_users = $func->wash($_GET['what_users'], 'no', '', 'no');
    
        $user_expr =
            ' and ( '
                .'lower(u.lastname) like lower(:whatUser) '
                .' or lower(u.firstname) like lower(:whatUser) '
                .' or lower(u.user_id) like lower(:whatUser)'
            .')';
        $PDOarray = array_merge($PDOarray, array(':whatUser' => '%'.$what_users.'%'));
    
    SNA's avatar
    SNA committed
    }
    
    if (isset($_GET['what_services'])
        && !empty($_GET['what_services'])
    
    ) {
        $what_services = addslashes(
    
            functions::wash($_GET['what_services'], 'no', '', 'no')
    
    SNA's avatar
    SNA committed
        );
    
        //$what_services = $what_services;
    
        $entity_expr =
            ' and ('
                .' lower(e.entity_label) like lower(:whatEntity) '
                .' or lower(e.entity_id) like lower(:whatEntity)'
            .')';
        $PDOarray = array_merge($PDOarray, array(':whatEntity' => '%'.$what_services.'%'));
    
    /*$users_query =
    
        "select u.user_id, u.firstname, u.lastname, e.entity_id, e.entity_label "
        . "FROM " . $_SESSION['tablename']['users'] . " u, " . ENT_ENTITIES . " e, "
        . ENT_USERS_ENTITIES . " ue WHERE u.status <> 'DEL' and u.enabled = 'Y' and"
        . " e.entity_id = ue.entity_id and u.user_id = ue.user_id and"
    
        . " e.enabled = 'Y' and ue.primary_entity='Y' " . $user_expr . $entity_expr
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        . " order by u.lastname asc, u.firstname asc, u.user_id asc, e.entity_label asc limit 50";*/
    
    
    $where = "u.status <> 'DEL' and u.status != 'SPD' and"
    
        .' e.entity_id = ue.entity_id and u.user_id = ue.user_id and'
        ." e.enabled = 'Y' and ue.primary_entity='Y' ".$user_expr.$entity_expr;
    $order = 'order by u.lastname asc, u.firstname asc, u.user_id asc, e.entity_label asc';
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    $users_query = $db->limit_select(
    
        0,
        50,
        'u.user_id, u.firstname, u.lastname, e.entity_id, e.entity_label',
        $_SESSION['tablename']['users'].' u, '.ENT_ENTITIES.' e, '.ENT_USERS_ENTITIES.' ue',
        $where,
        '',
        '',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        $order
    );
    //echo $users_query;exit;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    if ($user_expr == '' && $entity_expr == '') {
        //no query
    } else {
    
        $stmt = $db->query($users_query, $PDOarray);
    
        while ($line = $stmt->fetchObject()) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            array_push(
                $users,
                array(
    
                    'ID' => functions::show_string($line->user_id),
    
                    'PRENOM' => functions::show_string($line->firstname),
    
                    'NOM' => functions::show_string($line->lastname),
    
                    'DEP_ID' => functions::show_string($line->entity_id),
    
                    'DEP' => functions::show_string($line->entity_label),
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                )
            );
        }
    
    SNA's avatar
    SNA committed
    }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    
        'select e.entity_id,  e.entity_label FROM '
            .$_SESSION['tablename']['users'].' u, '.ENT_ENTITIES.' e, '
            .ENT_USERS_ENTITIES." ue WHERE u.status <> 'DEL' and u.enabled = 'Y'"
            .'and  e.entity_id = ue.entity_id and u.user_id = ue.user_id and '
            ."e.enabled = 'Y' ".$user_expr.$entity_expr
            .' group by e.entity_id, e.entity_label order by e.entity_label asc limit 50';
    
    
    $where = "u.status <> 'DEL' and u.status != 'SPD'".'and  e.entity_id = ue.entity_id and u.user_id = ue.user_id and '
    
            ."e.enabled = 'Y' ".$user_expr.$entity_expr;
    $order = 'ORDER by entity_label ASC';
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    $entity_query = $db->limit_select(
    
        0,
        50,
        'e.entity_id,  e.entity_label',
        $_SESSION['tablename']['users'].' u, '.ENT_ENTITIES.' e, '.ENT_USERS_ENTITIES.' ue',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        $where,
    
        'group by e.entity_id, e.entity_label',
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        '',
        $order
    );
    //echo $entity_query;exit;
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    if ($user_expr == '' && $entity_expr == '') {
        //no query
    } else {
    
        $stmt = $db->query($entity_query, $PDOarray);
    
        while ($line = $stmt->fetchObject()) {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            array_push(
                $entities,
                array(
    
                    'ID' => functions::show_string($line->entity_id),
    
                    'DEP' => functions::show_string($line->entity_label),
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                )
            );
        }
    
    SNA's avatar
    SNA committed
    }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
    $_REQUEST['dest_is_set'] = $dest_is_set;
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
    
    
    //****************************************************************************************
    // RELOAD PARENT ID VALIDATION OF LIST
    //****************************************************************************************
    //****************************************************************************************
    // SWITCH ON ACTION REQUEST
    //****************************************************************************************
    switch ($action) {
    
    // ADDS
    //***************************************************************************************
    // Add USER AS dest/copy/custom mode
    
    case 'add_user':
    
        $stmt = $db->query(
    
            'SELECT u.firstname, u.lastname, e.entity_id, e.entity_label '
            .' FROM '.USERS_TABLE.' u '
            .' LEFT JOIN '.ENT_USERS_ENTITIES.' ue ON u.user_id = ue.user_id '
            .' LEFT JOIN '.ENT_ENTITIES.' e ON ue.entity_id = e.entity_id'
    
    Florian Azizian's avatar
    Florian Azizian committed
            ." WHERE u.user_id= ? and ue.primary_entity = 'Y'",
            array($id)
    
        $line = $stmt->fetchObject();
    
        if (!isset($_SESSION[$origin]['diff_list'][$role_id]['users'])) {
            $_SESSION[$origin]['diff_list'][$role_id]['users'] = array();
    
            if ($lastUser = end($_SESSION[$origin]['diff_list'][$role_id]['users'])) {
    
    
        // If dest is set && role is dest, move current dest to copy (legacy)
    
        if ($role_id == 'dest' && $dest_is_set) {
    
            if (!isset($_SESSION[$origin]['diff_list']['copy']['users'])) {
    
                $_SESSION[$origin]['diff_list']['copy']['users'] = array();
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
            $old_dest = $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'];
    
            if (!in_array('copy', $user_roles[$old_dest])) {
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
                array_push(
                    $_SESSION[$origin]['diff_list']['copy']['users'],
                    array(
                        'user_id' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'],
                        'firstname' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['firstname'],
                        'lastname' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['lastname'],
                        'entity_id' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'],
                        'entity_label' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_label'],
                        'visible' => 'Y',
                    )
                );
            }
    
            unset($_SESSION[$origin]['diff_list']['dest']['users'][0]);
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
            $_SESSION[$origin]['diff_list']['dest']['users'] = array_values(
                $_SESSION[$origin]['diff_list']['dest']['users']
            );
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
        array_push(
            $_SESSION[$origin]['diff_list'][$role_id]['users'],
            array(
    
                'user_id' => functions::show_string($id),
                'firstname' => functions::show_string($line->firstname),
                'lastname' => functions::show_string($line->lastname),
                'entity_id' => functions::show_string($line->entity_id),
                'entity_label' => functions::show_string($line->entity_label),
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
                'visible' => $visible,
            )
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
        $_SESSION[$origin]['diff_list'][$role_id]['users'] = array_values(
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
             $_SESSION[$origin]['diff_list'][$role_id]['users']
    
        break;
    
    // ADD ENTITY AS copy/custom mode
    case 'add_entity':
    
        $stmt = $db->query(
    
            'SELECT entity_id, entity_label FROM '.ENT_ENTITIES
    
    Florian Azizian's avatar
    Florian Azizian committed
            .' WHERE entity_id = ?',
            array($id)
    
        $line = $stmt->fetchObject();
    
        if (!isset($_SESSION[$origin]['diff_list'][$role_id]['entities'])) {
            $_SESSION[$origin]['diff_list'][$role_id]['entities'] = array();
    
            if ($lastEntity = end($_SESSION[$origin]['diff_list'][$role_id]['entities'])) {
    
                $visible = $lastEntity['visible'];
    
            $_SESSION[$origin]['diff_list'][$role_id]['entities'],
    
                'entity_id' => functions::show_string($id),
    
                'entity_label' => functions::show_string($line->entity_label),
    
                'visible' => $visible,
    
    
    // REMOVE
    //***************************************************************************************
    // Remove USER
    case 'remove_user':
    
        if ($rank !== false && $id && $role_id
    
            && $_SESSION[$origin]['diff_list'][$role_id]['users'][$rank]['user_id'] == $id
    
            $visible = $_SESSION[$origin]['diff_list'][$role_id]['users'][$rank]['visible'];
    
            unset($_SESSION[$origin]['diff_list'][$role_id]['users'][$rank]);
    
            $_SESSION[$origin]['diff_list'][$role_id]['users'] = array_values(
                $_SESSION[$origin]['diff_list'][$role_id]['users']
            );
    
            // Set next user (replacing removed one) visible
            if (isset($_SESSION[$origin]['diff_list'][$role_id]['users'][$rank])) {
    
                $_SESSION[$origin]['diff_list'][$role_id]['users'][$rank]['visible'] = $visible;
    
            }
            if ($role_id == 'dest') {
                $dest_is_set = false;
            }
    
        break;
    
    // Remove ENTITY
    case 'remove_entity':
    
        if ($rank !== false && $id && $role_id
    
            && $_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank]['entity_id'] == $id
    
    SNA's avatar
    SNA committed
        ) {
    
            $visible = $_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank]['visible'];
    
            unset($_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank]);
    
            $_SESSION[$origin]['diff_list'][$role_id]['entities'] = array_values(
                $_SESSION[$origin]['diff_list'][$role_id]['entities']
            );
    
            if (isset($_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank])) {
    
                $_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank]['visible'] = $visible;
    
    //***************************************************************************************
    
    case 'dest_to_copy':
    
        if ($dest_is_set) {
            if (!isset($_SESSION[$origin]['diff_list']['copy']['users'])) {
    
                $_SESSION[$origin]['diff_list']['copy']['users'] = array();
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
            $old_dest = $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'];
    
    
            if ($_SESSION['collection_id_choice'] != 'letterbox_coll') {
                if (!in_array('copy', $user_roles[$old_dest])) {
                    array_push(
    
                            $_SESSION[$origin]['diff_list']['copy']['users'],
                            array(
                                'user_id' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'],
                                'firstname' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['firstname'],
                                'lastname' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['lastname'],
                                'entity_id' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'],
                                'entity_label' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_label'],
    
                                'visible' => 'Y',
    
                }
                unset($_SESSION[$origin]['diff_list']['dest']['users'][0]);
                $_SESSION[$origin]['diff_list']['dest']['users'] = array_values(
    
                        $_SESSION[$origin]['diff_list']['dest']['users']
                    );
    
                $old_dest_array = $_SESSION[$origin]['diff_list']['dest']['users'][0];
                $old_copy_array = $_SESSION[$origin]['diff_list']['copy']['users'][0];
    
    
                if (!in_array('copy', $user_roles[$old_dest])) {
    
                    $_SESSION[$origin]['diff_list']['copy']['users'][0]['user_id'] = $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'];
                    $_SESSION[$origin]['diff_list']['copy']['users'][0]['firstname'] = $_SESSION[$origin]['diff_list']['dest']['users'][0]['firstname'];
                    $_SESSION[$origin]['diff_list']['copy']['users'][0]['lastname'] = $_SESSION[$origin]['diff_list']['dest']['users'][0]['lastname'];
                    $_SESSION[$origin]['diff_list']['copy']['users'][0]['entity_id'] = $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'];
                    $_SESSION[$origin]['diff_list']['copy']['users'][0]['entity_label'] = $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_label'];
                    $_SESSION[$origin]['diff_list']['copy']['users'][0]['visible'] = 'Y';
                }
    
                $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'] = $old_copy_array['user_id'];
                $_SESSION[$origin]['diff_list']['dest']['users'][0]['firstname'] = $old_copy_array['firstname'];
                $_SESSION[$origin]['diff_list']['dest']['users'][0]['lastname'] = $old_copy_array['lastname'];
                $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'] = $old_copy_array['entity_id'];
                $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_label'] = $old_copy_array['entity_label'];
    
                $_SESSION[$origin]['diff_list']['dest']['users'][0]['visible'] = 'Y';
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
            }
    
            $dest_is_set = false;
    
        break;
    
    case 'copy_to_dest':
        if ($dest_is_set) {
    
            if (!isset($_SESSION[$origin]['diff_list'][$role_id]['users'])) {
    
                $_SESSION[$origin]['diff_list'][$role_id]['users'] = array();
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
            $old_dest = $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'];
    
            if (!in_array('copy', $user_roles[$old_dest])) {
    
    Cyril Vazquez's avatar
    Cyril Vazquez committed
                array_push(
                    $_SESSION[$origin]['diff_list']['copy']['users'],
                    array(
                        'user_id' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'],
                        'firstname' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['firstname'],
                        'lastname' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['lastname'],
                        'entity_id' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'],
                        'entity_label' => $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_label'],
    
                        'visible' => 'Y',
    
            unset($_SESSION[$origin]['diff_list']['dest']['users'][0]);
            $_SESSION[$origin]['diff_list']['dest']['users'] = array_values(
                $_SESSION[$origin]['diff_list']['dest']['users']
            );
    
        if (isset($_SESSION[$origin]['diff_list']['copy']['users'][$rank]['user_id'])
            && !empty($_SESSION[$origin]['diff_list']['copy']['users'][$rank]['user_id'])
    
    SNA's avatar
    SNA committed
        ) {
    
            $_SESSION[$origin]['diff_list']['dest']['users'][0]['user_id'] = $_SESSION[$origin]['diff_list']['copy']['users'][$rank]['user_id'];
            $_SESSION[$origin]['diff_list']['dest']['users'][0]['firstname'] = $_SESSION[$origin]['diff_list']['copy']['users'][$rank]['firstname'];
            $_SESSION[$origin]['diff_list']['dest']['users'][0]['lastname'] = $_SESSION[$origin]['diff_list']['copy']['users'][$rank]['lastname'];
            $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'] = $_SESSION[$origin]['diff_list']['copy']['users'][$rank]['entity_id'];
            $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_label'] = $_SESSION[$origin]['diff_list']['copy']['users'][$rank]['entity_label'];
    
            $_SESSION[$origin]['diff_list']['dest']['users'][0]['visible'] = 'Y';
            unset($_SESSION[$origin]['diff_list']['copy']['users'][$rank]);
    
            $_SESSION[$origin]['diff_list']['copy']['users'] = array_values(
                $_SESSION[$origin]['diff_list']['copy']['users']
    
    SNA's avatar
    SNA committed
            );
    
            $dest_is_set = false;
    
        $downUser =
    
                $_SESSION[$origin]['diff_list'][$role_id]['users'],
    
                $rank,
                1,
                $preserve_keys = true
            );
    
        $newRank = $rank + 1;
        $upUser =
    
                $_SESSION[$origin]['diff_list'][$role_id]['users'],
    
                $newRank,
                1,
                $preserve_keys = true
            );
    
        if ($upUser[0] && $downUser[0]) {
            // Switch visible values
    
            $downUserVisible = $downUser[0]['visible'];
            $upUserVisible = $upUser[0]['visible'];
            $upUser[0]['visible'] = $downUserVisible;
            $downUser[0]['visible'] = $upUserVisible;
    
            // Switch positions
    
            $_SESSION[$origin]['diff_list'][$role_id]['users'][$rank] = $upUser[0];
            $_SESSION[$origin]['diff_list'][$role_id]['users'][$newRank] = $downUser[0];
        }
        break;
    
    case 'move_entity_down':
    
        $downEntity =
    
                $_SESSION[$origin]['diff_list'][$role_id]['entities'],
    
                $rank,
                1,
                $preserve_keys = true
            );
    
        $newRank = $rank + 1;
        $upEntity =
    
                $_SESSION[$origin]['diff_list'][$role_id]['entities'],
    
                $newRank,
                1,
                $preserve_keys = true
            );
    
        if ($upEntity[0] && $downEntity[0]) {
            // Switch visible values
    
            $downEntityVisible = $downEntity[0]['visible'];
            $upEntityVisible = $upEntity[0]['visible'];
            $upEntity[0]['visible'] = $downEntityVisible;
            $downEntity[0]['visible'] = $upEntityVisible;
    
            // Switch positions
    
            $_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank] = $upEntity[0];
            $_SESSION[$origin]['diff_list'][$role_id]['entities'][$newRank] = $downEntity[0];
        }
    
    case 'move_user_up':
    
                $_SESSION[$origin]['diff_list'][$role_id]['users'],
    
                $rank,
                1,
                $preserve_keys = true
            );
    
        $newRank = $rank - 1;
        $downUser =
    
                $_SESSION[$origin]['diff_list'][$role_id]['users'],
    
                $newRank,
                1,
                $preserve_keys = true
            );
    
        if ($upUser[0] && $downUser[0]) {
            // Switch visible values
    
            $downUserVisible = $downUser[0]['visible'];
            $upUserVisible = $upUser[0]['visible'];
            $upUser[0]['visible'] = $downUserVisible;
            $downUser[0]['visible'] = $upUserVisible;
    
            // Switch positions
            $_SESSION[$origin]['diff_list'][$role_id]['users'][$rank] = $downUser[0];
    
            $_SESSION[$origin]['diff_list'][$role_id]['users'][$newRank] = $upUser[0];
        }
        break;
    
    
    case 'move_entity_up':
    
        $upEntity =
    
            $_SESSION[$origin]['diff_list'][$role_id]['entities'],
    
            $rank,
            1,
            $preserve_keys = true
        );
    
        $newRank = $rank - 1;
        $downEntity =
    
                $_SESSION[$origin]['diff_list'][$role_id]['entities'],
    
                $newRank,
                1,
                $preserve_keys = true
            );
    
    
        if ($upEntity[0] && $downEntity[0]) {
            // Switch visible values
    
            $downEntityVisible = $downEntity[0]['visible'];
            $upEntityVisible = $upEntity[0]['visible'];
            $upEntity[0]['visible'] = $downEntityVisible;
            $downEntity[0]['visible'] = $upEntityVisible;
    
            // Switch positions
    
            $_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank] = $downEntity[0];
            $_SESSION[$origin]['diff_list'][$role_id]['entities'][$newRank] = $upEntity[0];
        }
    
    //***************************************************************************************
    
    case 'make_user_visible':
    
        $_SESSION[$origin]['diff_list'][$role_id]['users'][$rank]['visible'] = 'Y';
    
    case 'make_user_unvisible':
    
        $_SESSION[$origin]['diff_list'][$role_id]['users'][$rank]['visible'] = 'N';
        break;
    
    
    case 'make_entity_visible':
    
        $_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank]['visible'] = 'Y';
    
    case 'make_entity_unvisible':
    
        $_SESSION[$origin]['diff_list'][$role_id]['entities'][$rank]['visible'] = 'N';
        break;
    // END SWITCH ACTION
    
    // 1.4 create indexed array of existing diffusion to search for users/entities easily
    
    $user_roles = array();
    $entity_roles = array();
    
    foreach ($roles as $role_id => $role_label) {
    
    Florian Azizian's avatar
    Florian Azizian committed
        if (!empty($_SESSION[$origin]['diff_list'][$role_id]['users'])) {
            for ($i = 0, $l = count($_SESSION[$origin]['diff_list'][$role_id]['users']); $i < $l; ++$i) {
                $user_id = $_SESSION[$origin]['diff_list'][$role_id]['users'][$i]['user_id'];
                $user_roles[$user_id][] = $role_id;
            }
    
    Florian Azizian's avatar
    Florian Azizian committed
        if (!empty($_SESSION[$origin]['diff_list'][$role_id]['entities'])) {
            for ($i = 0, $l = count($_SESSION[$origin]['diff_list'][$role_id]['entities']); $i < $l; ++$i) {
                $entity_id = $_SESSION[$origin]['diff_list'][$role_id]['entities'][$i]['entity_id'];
                $entity_roles[$entity_id][] = $role_id;
            }
    
    SNA's avatar
    SNA committed
    
    
    $core_tools->load_html();
    $core_tools->load_header(_USER_ENTITIES_TITLE);
    $time = $core_tools->get_session_time_expire();
    
    $displayValue = '';
    $displayValue = ''.$displayValue.'';
    
    $link = $_SESSION['config']['businessappurl'].'index.php?display=true&module=entities&page=manage_listinstance&origin='.$origin;
    if ($onlyCc) {
        $link .= '&only_cc';
    }
    if ($noDelete) {
        $link .= '&no_delete';
    }
    if ($cat) {
        $link .= '&cat='.$cat;
    }
    if ($specific_role) {
        $link .= '&specific_role='.$specific_role;
    }
    
    $linkwithwhat =
        $link
        .'&what_users='.$whatUsers
        .'&what_services='.$whatServices;
    //******************************************************************************
    // DISPLAY EXISTING LIST
    //******************************************************************************
    
    SNA's avatar
    SNA committed
    ?>
    
    <body onload="setTimeout(window.close, <?php echo $time; ?>*60*1000);">
    
        <script type="text/javascript">
            function add_user(id) {
    
                var user_id = $('user_id_' + id).value;
                var role_select = $('user_role_' + id);
    
                var role = role_select.options[role_select.selectedIndex].value;
                goTo('<?php echo $linkwithwhat; ?>&action=add_user&id=' + user_id + '&role=' + role);
    
            function add_entity(id) {
    
                var entity_id = $('entity_id_' + id).value;
                var role_select = $('entity_role_' + id);
    
                var role = role_select.options[role_select.selectedIndex].value;
                goTo('<?php echo $linkwithwhat; ?>&action=add_entity&id=' + entity_id + '&role=' + role);
    
    Florian Azizian's avatar
    Florian Azizian committed
        <br />
    
        /*if ((isset($_GET['what_users']) && ! empty($_GET['what_users']))
    
            || (isset($_GET['what_services']) && !empty($_GET['what_services']))
    
            || ( !empty($user_roles) || !empty($entity_roles))
    
    Florian Azizian's avatar
    Florian Azizian committed
        <div id="diff_list" class="block" align="center">
            <h2>
                <?php
    
                echo _DIFFUSION_LIST.'&nbsp;';
                if ($_SESSION[$origin]['difflist_object']['object_label'] != '') {
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    functions::xecho($_SESSION[$origin]['difflist_object']['object_label']);
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                }
    
                /*if ($difflistType->difflist_type_label != '') {
                    echo "<br /><small><small><small> (" . $difflistType->difflist_type_label . ")</small></small></small>";
                }*/
    
    Florian Azizian's avatar
    Florian Azizian committed
            ?>
            </h2>
            <?php
    
            //**************************************************************************
            // DEST USER
            //**************************************************************************
            if (1 == 2 && isset($_SESSION[$origin]['diff_list']['dest']['user_id'])
                && !empty($_SESSION[$origin]['diff_list']['dest']['user_id'])
                && !$onlyCc
            ) {
                ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
            <h3 class="sstit">
                <?php echo _PRINCIPAL_RECIPIENT; ?>
            </h3>
            <table cellpadding="0" cellspacing="0" border="0" class="listing spec">
                <tr>
                    <td style="width:5%;">
                        <i class="fa fa-user fa-2x" title="<?php echo _USER; ?>"></i>
                    </td>
                    <td style="width:5%;">
                        <?php
    
                    if ($_SESSION[$origin]['diff_list']['dest']['visible'] == 'Y') {
                        ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <i class="fa fa-check fa-2x" title="<?php echo _VISIBLE; ?>"></i>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td>
                        <?php functions::xecho($_SESSION[$origin]['diff_list']['dest']['lastname']).' '.$_SESSION[$origin]['diff_list']['dest']['firstname']; ?>
                    </td>
                    <td>
                        <?php functions::xecho($_SESSION[$origin]['diff_list']['dest']['entity_label']); ?>
                    </td>
                    <td class="action_entities" style="width:5%;">
                        <!-- Remove dest -->
                        <a href="<?php echo $linkwithwhat; ?>&action=remove_dest"><i
                                class="fa fa-times fa-2x"></i></a>
                    </td>
                    <td class="action_entities" style="width:15%;">
                        <!-- Move dest to copy -->
                        <a href="<?php echo $linkwithwhat; ?>&action=dest_to_copy&role=copy"><i
                                class="fa fa-arrow-down fa-2x"></i>
                            <?php echo _TO_CC; ?></a>
                    </td>
                </tr>
            </table>
            <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
            <br />
            <?php
    
            //**************************************************************************
            // OTHER ROLES
            //**************************************************************************
            foreach ($roles as $role_id => $role_label) {
    
                if ($cat == 'outgoing' && $role_id == 'dest') {
                    $role_label = _REDACTOR;
                } elseif ($cat != 'outgoing' && $role_id == 'dest') {
                    $role_label = _ASSIGNEE;
    
                if (!empty($_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'])) {
                    $entity_id_dest = $_SESSION[$origin]['diff_list']['dest']['users'][0]['entity_id'];
                }
    
    Florian Azizian's avatar
    Florian Azizian committed
                if ((!empty($_SESSION[$origin]['diff_list'][$role_id]['users']) && count($_SESSION[$origin]['diff_list'][$role_id]['users']) > 0) || (!empty($_SESSION[$origin]['diff_list'][$role_id]['entities']) && count($_SESSION[$origin]['diff_list'][$role_id]['entities']) > 0)) {
    
                    if (($specific_role == $role_id || $specific_role.'_copy' == $role_id || $specific_role.'_info' == $role_id) || !isset($_REQUEST['specific_role'])) {
                        ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
            <h3 class="sstit" style="font-size:1.5em; text-align:left; margin-left:230px; margin-bottom: -10px">
                <?php functions::xecho($role_label); ?>
            </h3>
            <table cellpadding="0" cellspacing="0" border="0" class="listing liste_diff spec">
                <?php
    
                    //**************************************************************************
                    // OTHER ROLE USERS
                    //**************************************************************************
                    $color = ' class="col"';
    
    kevin.dezaphi's avatar
    kevin.dezaphi committed
                        if (!empty($_SESSION[$origin]['diff_list'][$role_id]['users'])) {
    
    Florian Azizian's avatar
    Florian Azizian committed
                            $nbDiffListUser = count($_SESSION[$origin]['diff_list'][$role_id]['users']);
                        } else {
                            $nbDiffListUser = 0;
                        }
    
    kevin.dezaphi's avatar
    kevin.dezaphi committed
                        for ($i = 0, $l = $nbDiffListUser; $i < $l; ++$i) {
    
                            $user = $_SESSION[$origin]['diff_list'][$role_id]['users'][$i];
    
                            if ($color == ' class="col"') {
                                $color = '';
                            } else {
                                $color = ' class="col"';
                            } ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                <tr <?php echo $color; ?> >
                    <td style="width:5%;">
                        <i class="fa fa-user fa-2x" title="<?php echo _USER.' '.$role_label; ?>"></i>
                    </td>
                    <td style="width:5%;">
                        <?php
    
                            if ($user['visible'] == 'Y') {
                                ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=make_user_unvisible&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>">
                            <i class="fa fa-check fa-2x" title="<?php echo _VISIBLE; ?>"></i>
                        </a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=make_user_visible&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>">
                            <i class="fa fa-times fa-2x" title="<?php echo _NOT_VISIBLE; ?>"></i>
                        </a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td>
                        <?php functions::xecho($user['lastname'].' '.$user['firstname']); ?>
                    </td>
                    <td>
                        <?php functions::xecho($user['entity_label']); ?>
                    </td>
                    <td class="action_entities" style="width:5%;">
                        <?php
    
                                /*if (!$noDelete && ($role_id != 'dest' && !$onlyCc)) { */
                                if (!$noDelete && (!$onlyCc || ($onlyCc && $role_id != 'dest'))) {
                                    ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=remove_user&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>&id=<?php functions::xecho($user['user_id']); ?>"><i
                                class="fa fa-times fa-lg" title="<?php echo _DEL_USER_LISTDIFF; ?>"></i></a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td class="action_entities" style="width:15%;">
                        <!-- Switch copy to dest -->
                        <?php
    
                                //if($role_id == 'dest' && isset($roles['copy']) && ($role_id != 'dest' && $onlyCc)) {
                                if ($role_id == 'dest' && isset($roles['copy']) && !$onlyCc && $_SESSION[$origin]['diff_list']['copy']['users'][0] != '') {
                                    ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php functions::xecho($linkwithwhat); ?>&action=dest_to_copy&role=copy"
                            title="<?php echo _GO_TO_CC; ?>"><i class="fa fa-arrow-down"></i>&nbsp;
                            <?php echo _TO_CC; ?></a>
                        <?php
    
                                } elseif ($role_id == 'copy' && !$onlyCc && isset($roles['dest'])) {
                                    ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=copy_to_dest&role=copy&rank=<?php functions::xecho($i); ?>"
                            title="<?php echo _GO_TO_DEST; ?>"><i class="fa fa-arrow-up"></i>&nbsp;
                            <?php echo _TO_DEST; ?></a>
                        <?php
    
                                } else {
                                    echo '&nbsp;';
                                } ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td class="action_entities" style="width:5%;">
                        <!-- Move up in list -->
                        <?php
    
                                if ($i > 0) {
                                    ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=move_user_up&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>"><i
                                class="fa fa-arrow-up" title="<?php echo _UP_USER_ONE_ROW; ?>"></i></a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td class="action_entities" style="width:5%;">
                        <!-- Move down in list -->
                        <?php
    
                                if ($i < $l - 1) {
                                    ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=move_user_down&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>"><i
                                class="fa fa-arrow-down" title="<?php echo _DOWN_USER_ONE_ROW; ?>"></i></a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                </tr>
                <?php
    
                        }
    
                        //**************************************************************************
                        // OTHER ROLE ENTITIES
                        //**************************************************************************
    
    Florian Azizian's avatar
    Florian Azizian committed
                        $nbEntitiesDiffList = 0;
                        if ($_SESSION[$origin]['diff_list'][$role_id]['entities']) {
                            $nbEntitiesDiffList = count($_SESSION[$origin]['diff_list'][$role_id]['entities']);
                        }
                        for ($i = 0, $l = $nbEntitiesDiffList; $i < $l; ++$i) {
    
                            $entity = $_SESSION[$origin]['diff_list'][$role_id]['entities'][$i];
                            if ($color == ' class="col"') {
                                $color = '';
                            } else {
                                $color = ' class="col"';
                            } ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                <tr <?php echo $color; ?> >
                    <td style="width:5%;">
                        <i class="fa fa-sitemap fa-2x" title="<?php echo _ENTITY.' '.$role_label; ?>"></i>
                    </td>
                    <td style="width:5%;">
                        <?php
    
                            if ($entity['visible'] == 'Y') {
                                ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=make_entity_unvisible&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>">
                            <i class="fa fa-check fa-2x" title="<?php echo _VISIBLE; ?>"></i>
                        </a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=make_entity_visible&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>">
                            <i class="fa fa-times fa-2x" title="<?php echo _NOT_VISIBLE; ?>"></i>
                        </a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td>
                        <?php functions::xecho($entity['entity_id']); ?>
                    </td>
                    <td>
                        <?php functions::xecho($entity['entity_label']); ?>
                    </td>
                    <td class="action_entities" style="width:5%;">
                        <?php
    
                            if (!$noDelete) {
                                ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=remove_entity&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>&id=<?php functions::xecho($entity['entity_id']); ?>">
                            <i class="fa fa-times fa-lg" title="<?php echo _DEL_ENTITY_LISTDIFF; ?>"></i>
                        </a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td class="action_entities" style="width:15%;">&nbsp;</td>
                    <td class="action_entities" style="width:5%;">
                        <!-- Move up in list -->
                        <?php
    
                            if ($i > 0) {
                                ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=move_entity_up&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>"><i
                                class="fa fa-arrow-up" title="<?php echo _UP_ENTITY_ONE_ROW; ?>"></i></a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                    <td class="action_entities" style="width:5%;">
                        <!-- Move down in list -->
                        <?php
    
                            if ($i < $l - 1) {
                                ?>
    
    Florian Azizian's avatar
    Florian Azizian committed
                        <a href="<?php echo $linkwithwhat; ?>&action=move_entity_down&role=<?php functions::xecho($role_id); ?>&rank=<?php functions::xecho($i); ?>"><i
                                class="fa fa-arrow-down" title="<?php echo _DOWN_ENTITY_ONE_ROW; ?>"></i></a>
                        <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
                    </td>
                </tr>
                <?php
    
    Florian Azizian's avatar
    Florian Azizian committed
            </table>
            <br />
            <?php
    
                }
            }
            //******************************************************************************
            // ACTIONS BUTTONS
            //******************************************************************************?>
    
    		<form name="pop_diff" method="post" >
    			<div align="center">