<?php  

$page_security = 'SA_OPEN';
$path_to_root = "../../..";
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/modules/ExtendedHRM/includes/db/custom_fields_db.inc");
include_once($path_to_root . "/modules/ExtendedHRM/includes/ui/custom_fields_ui.inc");

page(_($help_context = "Custom header setup"));

include_once($path_to_root . "/includes/ui.inc");
simple_page_mode(true);

function can_process(){
	if (strlen($_POST['header_label']) == 0)	{
		display_error(_("The field label cannot be empty."));
		set_focus('field_label');
		return false;
	}
	// if($_POST['field_name'] =='dropdown' || $_POST['field_name'] =='radiobutton'){

	// 	for ($i=1; $i <=$_POST['field_item_count'] ; $i++) { 

	// 		if(strlen($_POST['field_item_'.$i]) ==0 ){
	//			sprintf(_("The field item label %s cannot be empty.")$i);
	// 			set_focus('field_item_'.$i);
	// 			return false;
	// 		}
	// 	}
	// }
	return true;
}
if ($Mode=='ADD_ITEM' && can_process()){	
	add_custom_header($_POST['header_label'],$_POST['module'],$_POST['position']);	
	display_notification(_('New custom field has been added'));
	$Mode = 'RESET';
}
if ($Mode=='UPDATE_ITEM' && can_process()){
	update_custom_header($selected_id, $_POST['header_label'],$_POST['position']);
	display_notification(_('Selected custom field has been updated'));
	$Mode = 'RESET';
}

if ($Mode == 'Delete'){
	if(!has_custom_fields($selected_id)){
		delete_custom_header	($selected_id,$_POST['module']);
		display_notification(_('Selected custom field has been deleted'));
		$Mode = 'RESET';
	}else{
		display_warning(_('Could not delete this CUSTOM HEADER! First you should remove CUSTOM FIELD(S) under this CUSTOM HEADER !'));
		$Mode = 'RESET';
	}
}
$module ='employees';	
if ($Mode == 'RESET'){
	$module=$_POST['module'];
	$selected_id = -1;
	unset($_POST);
	$_POST['module']=$module;
	$Ajax->activate('main_table');
}	
if(list_updated('selected_module')){
	$module =$_POST['selected_module'];
	unset($_POST);
	$selected_id = -1;
	$Ajax->activate('main_table');
}

if(list_updated('field_name')){
	$Ajax->activate('main_table');
}
if(list_updated('field_item_count')){
	$Ajax->activate('main_table');
}

if(isset($_POST['selected_module'])){
	$module =$_POST['selected_module'];
}else{
	// $module ='customer';	
}

if(!isset($_POST['field_name'])){
	$_POST['field_name']='text';
}

if(!isset($_POST['field_item_count'])){
	$_POST['field_item_count']='1';
}

start_form(true);
div_start('main_table');
start_table(TABLESTYLE_NOBORDER);
start_row();
label_row('Select a module',module_list('selected_module',$module,true));
end_row();
end_table();
// display_error("selected module -->>".$_POST['module']);

start_table(TABLESTYLE, "width='30%'");
$th = array (_('Header Label'), _('Position'),' ',' ');
table_header($th);
$k = 0;
$result = get_custom_headers($module);
// while ($myrow = db_fetch($result))
if(!empty($result)){
	foreach ($result as $myrow) {
	    alt_table_row_color($k);
		label_cell($myrow["header_label"]);
		label_cell($myrow["position"]);
	 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
	 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
		end_row();
	}
}else{
	label_cell( _('No records'), "colspan=4 class='navibar'");
}
end_table(1);
div_start('fields_table');
start_table(TABLESTYLE2);

if ($selected_id != -1){
 	if ($Mode == 'Edit') {
		$myrow = get_custom_header($selected_id);

		$_POST['header_label']  = $myrow["header_label"];
		$_POST['position']  = $myrow["position"];
	}
	hidden('selected_id', $selected_id);
} else {
	$_POST['position']  = 'left';
}

text_row_ex(_("Header Label").':', 'header_label', 20);	
label_row(_('Position'),draw_position_radiobutton('position',$_POST['position']));
start_row();
end_table(1);

hidden('module',$module);
submit_add_or_update_center($selected_id == -1, '', 'both');
div_end();
div_end();
end_form();	
end_page();
?>