<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_APPROVAL_PATHS';
$path_to_root = "..";
include($path_to_root . "/includes/session.inc");
//include_once ($path_to_root . "/trace.php");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
	
page(_($help_context = "Data Access Groups"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
//include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/admin/db/data_access_groups_db.inc");
//include_once($path_to_root . "/projects/includes/db/approval_path_configuration_db.inc");

$user_comp = user_company();
//$new_item = get_post('approval_path_id')== -1 || get_post('cancel'); 

simple_page_mode(true);
//------------------------------------------------------------------------------------


function can_process($selected_id = -1)
{
    $count = get_existing_data_access_group($_POST['dag_user_id'], $_POST['data_access_group_id'], $selected_id);
    
    if($count > 0)
    {
        display_error("Selected Data Access Group already assigned to the user.");
        set_focus('dag_id');
        return;           
    }
  
    return true;
}

//if (list_updated('category_id') || list_updated('mb_flag')) {
//	$Ajax->activate('details');
//}

//------------------------------------------------------------------------------------------------



function data_access_group_settings(&$dag_user_id) 
{
	global $SysPrefs, $path_to_root, $new_item, $pic_height, $Refs;
        
         
    div_start('code_table');
	start_table(TABLESTYLE, "width=40%");
        
        $result = get_user_data_access_groups($dag_user_id);
        
        $th = array(_("ID"), _("Data Access Groups"),"");
          table_header($th);
                        
        $k = $j = 0;
         
         while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);

            label_cell($myrow["dag_id"], "align=right");
            label_cell($myrow["value_name"]);
            
            edit_button_cell("Delete".$myrow['id'], _("Delete")); 		 	
            end_row();

        } //end of while loop
 
        br();
        end_table();
        
    div_end();
}

//$Mode = 'Delete';
if ($Mode == 'Delete')
{
    delete_user_data_access_group($selected_id);
	
    display_notification(_("Selected Data Access Group has been removed from the user."));
    $Mode = 'RESET';
}




if (isset($_POST['ADD_ITEM']) && can_process() == true)
{
    if(!($_POST['dag_user_id'] > 0)){
        display_error("Please select a user.");
    }
    else{     	       	
            add_data_access_group($_POST['dag_user_id'], $_POST['data_access_group_id']); 			

            display_notification(_("Selected Data Access Group has been added to the user."));
              
            //$Mode = 'RESET';
	}
}

//-------------------------------------------------------------------------------------------

start_form(true);

//if (db_has_approval_paths()) 
{
	start_table(TABLESTYLE_NOBORDER);
	start_row();
    
            system_users_list_cells(_("User:"), 'dag_user_id', null,
                                _('Select a User'), true);

            $new_item = get_post('dag_user_id')=='';

            if(get_post('dag_user_id') == -1 || get_post('dag_user_id') == ''){
                $new_item = TRUE;
            }

            //check_cells(_("Show inactive:"), 'show_inactive', null, true);
	end_row();
	end_table();
        echo "<hr></center>";

}
//else
//{
////	hidden('dag_user_id', get_post('dag_user_id'));
//}

hidden('selected_id', $selected_id);

div_start('details');

$dag_user_id = get_post('dag_user_id');

if ($dag_user_id == -1){
    $dag_user_id = '';
}


data_access_group_settings($dag_user_id); 
br();
div_end();
//end_table();

start_table(TABLESTYLE_NOBORDER);
//start_row();
    value_list_row("Data Access Group:", 'data_access_group_id', 'DATA_ACCESS_GROUPS', null, 'Select a Data Access Group', false);
//end_row();

end_table(1);
submit_center('ADD_ITEM', _("ADD"), true, '', 'default');

//submit_add_or_update_center($selected_id == -1, '', 'both');
hidden('popup', @$_REQUEST['popup']);

end_form();

//------------------------------------------------------------------------------------

end_page(@$_REQUEST['popup']);


?>
