<?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 Groups"));

include_once($path_to_root . "/assets/includes/db/asset_groups_db.inc");
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(!($selected_id>0)){
        if (strlen($_POST['code']) == 0) 
        {
            display_error(_("Code cannot be empty."));
            set_focus('code');
            return false;
        }
    }
    
	if (strlen($_POST['description']) == 0) 
	{
        display_error(_("Description cannot be empty."));
        set_focus('description');
		return false;		
	}
    
    return true;
}


function save_asset_type($selected_id)
{    

    if ($selected_id > 0) 
    {
        update_asset_type($selected_id, $_POST['description']);
        display_notification(_('Selected asset type has been updated'));
    } 
    else 
    {
        add_asset_type($_POST['code'], $_POST['description']);
        display_notification(_('New Asset type has been added'));
    }

    
	
} 


    


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

function can_delete($selected_id)
{
	if (asset_groups_in_assets($selected_id))
	{
		display_error(_("Cannot delete this asset type because assets have been created referring to it."));
		return false;
	}
        
        $as_gp_result = get_asset_subgroups_by_asset_group_id($selected_id);
        if (db_num_rows($as_gp_result) > 0) {
            display_error(_("Cannot delete this asset type because asset subgroups have been created referring to it."));
            return false;
        }
	
	return true;
}



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

function display_all_asset_groups($show_inactive)
{
    $result = get_all_asset_groups($show_inactive);

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

    $th = array(_("id"),_("Code"),_("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["code"]);
        label_cell($myrow["description"]);

        inactive_control_cell($myrow["id"], $myrow["inactive"], 'asset_groups', '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_type_edit_controls($selected_id, $Mode)
{
    start_table(TABLESTYLE2);

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

            $myrow = get_asset_type($selected_id);

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

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

    if($selected_id > 0 && $Mode == 'Edit'){
        label_row("Code:", $_POST['code']);
    }
    else{
        text_row(_("Code:"), 'code', null, 20, 50);
    }
    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_type($selected_id);
        $Mode = 'RESET';
    }
}

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

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



display_all_asset_groups(check_value('show_inactive'));

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



end_form(2);

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

end_page();

?>
