<?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");
include_once($path_to_root . "/modules/ExtendedHRM/includes/Payroll.inc" );
include($path_to_root . "/modules/ExtendedHRM/includes/db/picklist.inc" );
page(_($help_context = "Custom field setup"));

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

function can_process(){
	if (strlen($_POST['field_label']) == 0)	{
		display_error(_("The field label cannot be empty."));
		set_focus('field_label');
		return false;
	}
	if($_POST['field_name'] =='function'){
		if (strlen($_POST['fun_name']) == 0){
			display_error(_("The function name cannot be empty."));
			set_focus('fun_name');
			return false;
		}
	}
	return true;
}
if ($Mode=='ADD_ITEM' && can_process()){
	if($_POST['field_name'] =='dropdown' || $_POST['field_name'] =='radiobutton'){
		add_custom_field($_POST['field_name'],$_POST['field_label'],$_POST['module'],uniqid(),$_POST['pick_type'],$_POST['selceted_header']);
	}elseif($_POST['field_name'] =='function'){
		add_custom_field($_POST['field_name'],$_POST['field_label'],$_POST['module'],uniqid(),$_POST['fun_name'],$_POST['selceted_header']);
	}else{
		add_custom_field($_POST['field_name'],$_POST['field_label'],$_POST['module'],uniqid(),'0',$_POST['selceted_header']);
	}
	display_notification(_('New custom field has been added'));
	$Mode = 'RESET';
}

if ($Mode=='UPDATE_ITEM' && can_process()){
	$update =1;
	if(has_custom_values($_POST['module'],$selected_id)){
		if($_POST['field_name'] ==GetSingleValue('custom_fields','field',array('id'=>$selected_id))){
			$update =1;
		}else{
			$update =0;
		}
	}
	if($update){
		if($_POST['field_name'] =='dropdown' || $_POST['field_name'] =='radiobutton'){
			update_custom_field($selected_id, $_POST['field_name'],$_POST['field_label'],$_POST['pick_type'],$_POST['selceted_header']);
		}elseif($_POST['field_name'] =='function'){
			update_custom_field($selected_id, $_POST['field_name'],$_POST['field_label'],$_POST['fun_name'],$_POST['selceted_header']);
		}else{
			update_custom_field($selected_id, $_POST['field_name'],$_POST['field_label'],'0',$_POST['selceted_header']);
		}
		display_notification(_('Selected custom field has been updated'));
	}else{
		display_warning(_('Could not change FIELD TYPE !'));
	}	
	$Mode = 'RESET';
}

if ($Mode == 'Delete'){
	if(! has_custom_values($_POST['module'],$selected_id)){
		delete_custom_field($selected_id,$_POST['module']);
		display_notification(_('Selected custom field has been deleted'));
	}else{
		display_warning(_('Could not delete this CUSTOM FIELD !'));
	}
	$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;
	// $Mode = 'RESET';
	$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'),_('Field type'), _('Field Label'),' ',' ');
table_header($th);
$k = 0;
$result = get_custom_fields_with_header($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(ucfirst($myrow["field"]));
		label_cell($myrow["label"]);
	 	edit_button_cell("Edit".$myrow['id'], _("Edit"));
	 	delete_button_cell("Delete".$myrow['id'], _("Delete"));
		end_row();
	}
}else{
	label_cell( _('No records'), "colspan=5 class='navibar'");
}
end_table(1);
div_start('fields_table');
start_table(TABLESTYLE2);

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

		$_POST['field_name']  = $myrow["field"];
		$_POST['field_label']  = $myrow["label"];
		$_POST['selceted_header']  = $myrow["header_id"];
		if($_POST['field_name'] =='dropdown' || $_POST['field_name'] =='radiobutton'){
			$_POST['pick_type']=$myrow['pick_type'];
		}
		if($_POST['field_name'] =='function'){
			$_POST['fun_name']=$myrow['pick_type'];
		}
	}
	hidden('selected_id', $selected_id);
} else {
		$_POST['factor']  = number_format2(1,4);
		if(!isset($_POST['selceted_header'])){
			$_POST['selceted_header']  = 0;
		}
}
custom_header_list_cells(_("Select Header"),"selceted_header",$_POST['selceted_header'],'Default header',false,$module);
label_row(_('Select Field Type'),custom_filed_list('field_name',$_POST['field_name'],true));
text_row_ex(_("Field Label"), 'field_label', 20);	
if($_POST['field_name'] =='dropdown' || $_POST['field_name'] =='radiobutton'){
	start_row();
	hrm_pick_list_type_row(_("Pick Type"). ' :', 'pick_type', null, _("Select Pick"), true, false);
	end_row();
}
if($_POST['field_name'] =='function'){	
	text_row_ex(_("Function Name"), 'fun_name', 20);
}

end_table(1);

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