<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_APPROVAL_PATHS';
$path_to_root = "../..";

//include($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/projects/includes/db/approval_path_configuration_db.inc");
include_once($path_to_root . "/projects/includes/db/approval_group_db.inc");
simple_page_mode(false);


//----------------------------------------------------------------------------------
if (isset($_GET['approval_path_id']))
{
	$approval_path_id = $_POST['approval_path_id'] = $_GET['approval_path_id'];
}

function can_commit($selected_id)
{
	if (isset($_POST['position_seq_no']) == FALSE)
	{
		display_error(_("The position sequence number cannot be empty."));
		set_focus('position_seq_no');
        return false;
	}
    
//	if (strlen($_POST['position_code']) == 0)
//	{
//		display_error(_("The position code cannot be empty."));
//		set_focus('position_code');
//        return false;
//	}
    
    if (strlen($_POST['position_description']) == 0)
	{
		display_error(_("The position description cannot be empty."));
		set_focus('position_description');
        return false;
	}

    if (strlen($_POST['position_code']) > 0)
    {
        $code = get_existing_approval_path_configuration_code($_POST['position_code'], $selected_id);
        if(isset($code['position_code'])){
            display_error(_("The entered position code already exists."));
            set_focus('position_code');
            return false;
        }
    }
    
	return true;
    
}

//$stock_id = $_POST['approval_path_id'];
if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{
    if(can_commit($selected_id)){
    	write_approval_path_configuration($selected_id, $approval_path_id, $_POST['position_seq_no'],
                $_POST['position_code'], $_POST['position_description'], $_POST['approval_group_id'], 
                $_POST['next_position_message'], $_POST['next_position_email_template_id'], $_POST['previous_position_email_template_id'],
                $_POST['rejection_email_template_id'], $_POST['display_status']);
        
		if($selected_id != ''){
			display_notification(_('Selected approval path configuration detail has been updated'));
        }
		else{
			display_notification(_('New approval path configuration detail has been added'));
        }
        
		$Mode = 'RESET';
        clear_config_data();
	}
}

//----------------------------------------------------------------------------------
function clear_config_data()
{
	unset($_POST['position_seq_no']);
    unset($_POST['position_code']);
	unset($_POST['position_description']);
	unset($_POST['approval_group_id']);
    unset($_POST['next_position_email_template_id']);
	unset($_POST['previous_position_email_template_id']);
	unset($_POST['rejection_email_template_id']);

}
if ($Mode == 'Delete')
{
        delete_approval_path_configuration($selected_id);
        display_notification(_('Selected approval path has been deleted'));
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}
if (list_updated('approval_path_id')) {
	$Ajax->activate('details');
}
//----------------------------------------------------------------------------------
start_form();

//approval_path_list_row(_("Select a path:"), 'approval_path_id', null,false,true, check_value('show_inactive'));
br(2);

$result = get_approval_path_configuration($approval_path_id);

div_start('details');

start_table(TABLESTYLE, "width=80%");
$th = array(_('Position Seq. No'),_('Position Code'), _('Position Description'),
    _('Approval Group'),_('Display Status'),_('Next Position Message'),_('Next Position Email'),
    _('Previous Position Email'),_('Rejection Email'), "", "");
//inactive_control_column($th);

table_header($th);
$k = 0; //row colour counter

while ($myrow = db_fetch($result))
{

	alt_table_row_color($k);

	//label_cell($myrow["approval_path_name"]);
	
	label_cell($myrow["position_seq_no"]);
    label_cell($myrow["position_code"]);
    label_cell($myrow["position_description"]);
    label_cell($myrow["approval_group_description"]);
    label_cell($myrow["display_status"]);
    label_cell($myrow["next_position_message"]);
    label_cell($myrow["next_email_template_description"]);
	label_cell($myrow["previous_email_template_description"]);
	label_cell($myrow["rejection_email_template_description"]);

 	edit_button_cell("Edit".$myrow["id"], _("Edit"));
 	delete_button_cell("Delete".$myrow["id"], _("Delete"));
	end_row();
}
end_table(1);

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

start_table(TABLESTYLE2);

if ($selected_id != '') 
{
 	if ($Mode == 'Edit') {
		//editing an existing item category

		$myrow = get_approval_configure_details($selected_id);

        $_POST['approval_group_id']  = $myrow["approval_group_id"];
		$_POST['approval_group_description']  = $myrow["approval_group_description"];
        $_POST['position_description'] = $myrow["position_description"];
		$_POST['position_seq_no']  = $myrow["position_seq_no"];
        $_POST['position_code']  = $myrow["position_code"];
        $_POST['display_status']  = $myrow["display_status"];
        $_POST['next_position_message']  = $myrow["next_position_message"];
		$_POST['next_position_email_template_id']  = $myrow["next_position_email_template_id"];
        $_POST['previous_position_email_template_id']  = $myrow["previous_position_email_template_id"];
        $_POST['rejection_email_template_id']  = $myrow["rejection_email_template_id"];
        $_POST['next_email_template_description']  = $myrow["next_email_template_description"];
        $_POST['previous_email_template_description'] = $myrow["previous_email_template_description"];
		$_POST['rejection_email_template_description']  = $myrow["rejection_email_template_description"];
	}
	hidden('selected_id', $myrow["id"]);
}

amount_row(_("Position Sequence No:"), 'position_seq_no', null, null, null, 0);

text_row(_("Position Code:"), 'position_code', null, 35, 50);

text_row(_("Position Description:"), 'position_description', null, 35, 200);

text_row(_("Dispaly Status:"), 'display_status', null, 35, 50);

textarea_row(_("NExt Position Message:"), 'next_position_message', null, 25, 3);

approval_group_list_row(_("Approval group:"), 'approval_group_id', null, false, false, check_value('show_inactive'));

email_template_list_row(_("Next Position Email Template:"), 'next_position_email_template_id', null, '');

email_template_list_row(_("Previous Position Email Template:"), 'previous_position_email_template_id', null, '');

email_template_list_row(_("Rejection Position Email Template:"), 'rejection_email_template_id', null, '');

end_table(1);

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

div_end();

end_form();



?>
