<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ASSET_GROUPS';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
add_access_extensions();
set_ext_domain('/asset_register');

page(_($help_context = "Asset Subgroups"));

include_once($path_to_root . "/assets/includes/db/asset_subgroups_db.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/ui/ui_lists_custom.inc");
simple_page_mode(true);
//-----------------------------------------------------------------------------------


function can_commit($selected_id)
{    
    if (!($_POST['asset_group_id'] > 0))
	{
        display_error(_("Please select an asset type."));
        set_focus('asset_group_id');
		return false;		
	}
    
    
	if (strlen($_POST['description']) == 0) 
	{
        display_error(_("Description cannot be empty."));
        set_focus('description');
		return false;		
	}
    
    return true;
}


function save_asset_group($selected_id)
{    

    if ($selected_id > 0) 
    {
        update_asset_group($selected_id, $_POST['description']);
        display_notification(_('Selected asset group has been updated'));
    } 
    else 
    {
        add_asset_group($_POST['asset_group_id'], $_POST['description']);
        display_notification(_('New Asset group has been added'));
    }

    
	
} 


    


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

function can_delete($selected_id)
{
	if (asset_subgroups_in_assets($selected_id))
	{
		display_error(_("Cannot delete this asset group because assets have been created referring to it."));
		return false;
	}
	
	return true;
}



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

function display_all_asset_subgroups($asset_group_id, $show_inactive)
{
    $result = get_all_asset_subgroups($asset_group_id, $show_inactive);

    
    start_table(TABLESTYLE, "width=50%");

    $th = array(_("id"),_("Description"), "", "");
    inactive_control_column($th);
    table_header($th);
    $k = 0;
    while ($myrow = db_fetch($result)) 
    {
        alt_table_row_color($k);	

        label_cell($myrow["id"]);
        label_cell($myrow["description"]);

        inactive_control_cell($myrow["id"], $myrow["inactive"], 'asset_subgroups', 'id');
        edit_button_cell("Edit".$myrow['id'], _("Edit"));
        delete_button_cell("Delete".$myrow['id'], _("Delete"));
        end_row();
    }
    inactive_control_row($th);
    end_table(1);
}

function display_asset_group_edit_controls($selected_id, $Mode)
{
    start_table(TABLESTYLE2);

    if ($selected_id != -1) 
    {
        if ($Mode == 'Edit') {
            //editing an existing status code

            $myrow = get_asset_group($selected_id);

            $_POST['id']  = $myrow["id"];
            $_POST['description']  = $myrow["description"];

        }
        hidden('selected_id', $selected_id);
    } 

    text_row(_("Description:"), 'description', null, 50, 200);

    end_table(1);

    submit_add_or_update_center($selected_id == -1, '', 'both');
    

}

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

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
    if(can_commit($selected_id)){
        save_asset_group($selected_id);
        $Mode = 'RESET';
    }
}

if ($Mode == 'Delete')
{
	if (can_delete($selected_id))
	{
		delete_asset_group($selected_id);
		display_notification(_('Selected asset group has been deleted'));
	}
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

start_form();

//$_POST['asset_group_id'] = get_global_asset_type();

if(!isset($_POST['asset_group_id'])){
    $_POST['asset_group_id'] = get_global_asset_group();
}

start_table(TABLESTYLE_NOBORDER);
    start_row();
        asset_groups_list_row(_("Asset Group:"), 'asset_group_id', null, 'Select an asset type', true);
    end_row();
end_table();

set_global_asset_group($_POST['asset_group_id']);
    
display_all_asset_subgroups($_POST['asset_group_id'], check_value('show_inactive'));

display_asset_group_edit_controls($selected_id, $Mode);
//-----------------------------------------------------------------------------------



end_form();

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

end_page();

?>
