<?php
/****************************************
/*  Author 	: Kvvaradha
/*  Module 	: Enhanced HRM
/*  E-mail 	: admin@kvcodes.com
/*  Version : 1.0
/*  Http 	: www.kvcodes.com
*****************************************/
$page_security = 'SA_OPEN';
$path_to_root="../../..";
include_once($path_to_root . "/includes/db_pager.inc");
include($path_to_root . "/includes/session.inc");
$version_id = get_company_prefs('version_id');

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include($path_to_root . "/modules/ExtendedHRM/includes/Payroll.inc" );

page(_("Candidates"), @$_REQUEST['popup'], false, "");
 
simple_page_mode(true);
if (isset($_GET['dl']))
	$download_id = $_GET['dl'];
else
	$download_id = find_submit('download');

if ($download_id != -1){
	$row = GetRow('kv_empl_candidates', array('id' => $download_id));
	if ($row['unique_name'] != ""){
		if(in_ajax()) {
			$Ajax->redirect($_SERVER['PHP_SELF'].'?dl='.$download_id);
		} else {
			
    		header("Content-type: 'application/octet-stream' ");
	    	//header('Content-Length: '.$row['filesize']);
    		header('Content-Disposition: attachment; filename='.$row['filename']);
    		echo file_get_contents(company_path()."/attachments/empldocs/".$row['unique_name']);
	    	exit();
		}
	}	
}

function edit_link($row){
  	return button('Edit'.$row["id"], _("Edit"), _("Edit"), ICON_EDIT);
}

function download_link($row){
  	return button('download'.$row["id"], _("Download"), _("Download"), ICON_DOWN);
}

function delete_link($row){
  	return button('Delete'.$row["id"], _("Delete"), _("Delete"), ICON_DELETE);
}

function can_process(){
	if (strlen($_POST['fname']) == 0) {
			display_error(_("First Name cannot be empty."));
			set_focus('fname');
			return false;
		}
		if (strlen($_POST['lname']) == 0) {
			display_error(_("Last Name cannot be empty."));
			set_focus('lname');
			return false;
		}
		if (strlen($_POST['email']) == 0) {
			display_error(_("Email cannot be empty."));
			set_focus('email');
			return false;
		}

		if (strlen($_POST['qualification']) == 0) {
			display_error(_("Qualification cannot be empty."));
			set_focus('Qualification');
			return false;
		}

		if (strlen($_POST['position']) == 0) {
			display_error(_("Position cannot be empty."));
			set_focus('position');
			return false;
		}

		if (strlen($_POST['salary']) == 0) {
			display_error(_("Salary cannot be empty."));
			set_focus('salary');
			return false;
		}

		if (strlen($_POST['phone']) == 0) {
			display_error(_("Phone cannot be empty."));
			set_focus('phone');
			return false;
		}

		if (strlen($_POST['status']) == 0) {
			display_error(_("Status cannot be empty."));
			set_focus('status');
			return false;
		}

		if (strlen($_POST['last_company']) == 0) {
			display_error(_("Last company worked cannot be empty."));
			set_focus('last_company');
			return false;
		}

		// if(empty($_FILES["kv_attach_name"]))
  //       {
  //           display_error(_("* Upload Your Resume"));
  //           return false;
  //       }

	return true;
}

if (($Mode=='ADD_ITEM' && can_process() ) || ($Mode=='UPDATE_ITEM' && can_process())){

	if(!isset($max_image_size))
			$max_image_size = 5000;
		$upload_file = "";
		if (isset($_FILES['kv_attach_name']) && $_FILES['kv_attach_name']['name'] != '') {

			$result = $_FILES['kv_attach_name']['error'];
			$upload_file = 'Yes'; 
			$attr_dir = company_path().'/attachments' ; 
			if (!file_exists($attr_dir)){				
				mkdir($attr_dir);
			}
			$attach_dir = $attr_dir.'/empldocs';
			if (!file_exists($attach_dir)){
				mkdir($attach_dir);
			}	
			$filename = basename($_FILES['kv_attach_name']['name']);
			$tmp = explode('.', $filename);
			$ext = strtolower(end($tmp));
			
			if(in_array($ext, array('docx','doc', 'pdf', 'jpg', 'jpeg', 'gif', 'bmp', 'png', 'rtf', 'txt'))){		
				if(isset($_POST['unique_name']) && $_POST['unique_name'] == '')
					$kv_file_name = uniqid();			
				else
					$kv_file_name = $_POST['unique_name']; 
					
				$filename = $attach_dir."/".$kv_file_name; 
				if ( $_FILES['kv_attach_name']['size'] > ($max_image_size * 1024)) { //File Size Check
					display_warning(_("The file size is over the maximum allowed. The maximum size allowed in KB is") . ' ' . $max_image_size);
					$upload_file ='No';
				} 
				elseif (file_exists($filename)){
					$result = unlink($filename);
					if (!$result) 	{
						display_error(_("The existing Docs could not be removed"));
						$upload_file ='No';
					}
				}
			}
			$Mode = 'RESET';
			$Ajax->activate('_page_body');

			$input_error = 0;
			if ($input_error != 1)	{
		    	if ($selected_id != -1)     	{
		    		Update('kv_empl_candidates', array('id' => $selected_id), array('fname' => $_POST['fname'],'lname' => $_POST['lname'], 'email' => $_POST['email'], 'qualification' => $_POST['qualification'], 'position' => $_POST['position'], 'salary' => $_POST['salary'], 'experience' => $_POST['experience'], 'phone' => $_POST['phone'], 'status' => $_POST['status'], 'last_company' => $_POST['last_company'], 'comments' => $_POST['comments'])); 
				display_notification(_("Employee Docs has been updated."));   		
			
		    	}   else  	{
		    				$actual_file_nam = $_FILES['kv_attach_name']['name'];
							$result  =  move_uploaded_file($_FILES['kv_attach_name']['tmp_name'], $filename);
		    		Insert('kv_empl_candidates', array('fname' => $_POST['fname'],'lname' => $_POST['lname'], 'email' => $_POST['email'], 'qualification' => $_POST['qualification'], 'position' => $_POST['position'], 'salary' => $_POST['salary'], 'experience' => $_POST['experience'], 'phone' => $_POST['phone'], 'status' => $_POST['status'], 'last_company' => $_POST['last_company'], 'comments' => $_POST['comments'], 'filename' => $actual_file_nam, 'unique_name' => $kv_file_name,)); 
					display_notification(_("Employee Docs has been attached!."));
		    	}      	
				$Mode = 'RESET';
			}
	}else
		display_error(_("* Upload Your Resume"));	
} 

if ($Mode == 'Delete'){

	$cancel_delete = 0;

	if ($cancel_delete == 0) {
		Delete('kv_empl_candidates', array('id' => $selected_id));
		display_notification(_('Selected Candidates has been deleted'));
	} //end if Delete department
	$Mode = 'RESET';
} 

if ($Mode == 'RESET'){
	$selected_id = -1;
	$sav = get_post('show_inactive');
	unset($_POST);
	if ($sav) $_POST['show_inactive'] = 1;
}
//-------------------------------------------------------------------------------------------------
function viewing_controls(){
    start_table(TABLESTYLE_NOBORDER);
	start_row();
    text_cells(_('Search'),'search_text');
    submit_cells('ProcessSearch', _("Search"), '', '', 'default');

	end_row();
    end_table(1);

}
start_form(false);
	viewing_controls();
	handle_search();
end_form(2);

function handle_search(){
 $sql ="SELECT * FROM ".TB_PREF."kv_empl_candidates As empl WHERE 1";

if(isset($_POST['search_text']) && strlen($_POST['search_text']) >0){
		$sql.=" AND (empl.fname LIKE '%".trim($_POST['search_text'])."%' OR empl.lname LIKE '%".trim($_POST['search_text'])."%'  OR empl.email LIKE '%".trim($_POST['search_text'])."%'  OR empl.comments LIKE '%".trim($_POST['search_text'])."%'  OR empl.status LIKE '%".trim($_POST['search_text'])."%')";
	}

 $cols = array(
			_("id") => 'skip',
			_("Name") => array('name'=>'fname'.'lname'),
			_("lname") => array('name'=>'lname'),
			_("Email") => array('name'=>'email'),
			_("Qualification") => array('name'=>'qualification'),	    	    
			_("Position") => array('name'=>'position'),	   
			_("Salary") => array('name'=>'salary'),	   
			_("Experience") => array('name'=>'experience'),	   
			_("Phone") => array('name'=>'phone'),	   
			_("Status") => array('name'=>'status'), 	 
			_("Last company") => array('name'=>'last_company'), 	 
			_("Comments") => array('name'=>'comments'),  
		  array('insert'=>true, 'fun'=>'edit_link'),
		   array('insert'=>true, 'fun'=>'download_link'),
		  array('insert'=>true, 'fun'=>'delete_link'),
		    );
 $table =& new_db_pager('kv_empl_candidates', $sql, $cols);
		$table->width = "80%";
		display_db_pager($table);

// $result = GetAll('kv_empl_candidates');
}
$action = $_SERVER['PHP_SELF'];
start_form(true, false, $action);
start_table(TABLESTYLE, "width=30%");

// $th = array(_("ID"), _("Name"), "Email", "Qualification", "Position", "Salary", "Experience", "Phone", "Status", "Last company", "Comments", "", '', '');
// inactive_control_column($th);

// table_header($th);
// $k = 0; 

// foreach($result as $myrow) {
	
// 	alt_table_row_color($k);
		
// 	label_cell($myrow["id"]);
// 	label_cell($myrow["fname"]);
// 	label_cell($myrow["email"]);
// 	label_cell($myrow["qualification"]);
// 	label_cell($myrow["position"]);
// 	label_cell($myrow["salary"]);
// 	label_cell($myrow["experience"]);
// 	label_cell($myrow["phone"]);
// 	label_cell($myrow["status"]);
// 	label_cell($myrow["last_company"]);
// 	label_cell($myrow["comments"]);
// 	// inactive_control_cell($myrow["id"], $myrow["inactive"], 'fname', 'id');
//  	// edit_button_cell("Edit".$myrow["id"], _("Edit"));
//  	// delete_button_cell("Delete".$myrow["id"], _("Delete"));
//  	if(basename($_SERVER['PHP_SELF']) != 'employees.php')
//     		echo edit_link($myrow);
//     	echo download_link($myrow);
//     	echo delete_link($myrow);
// 	end_row();
// }

// inactive_control_row($th);
// end_table(1);


if (isset($_POST['ProcessSearch'])){
		unset($_POST['ProcessSearch']);
	$Ajax->activate('kv_empl_candidates');
}

//-------------------------------------------------------------------------------------------------
start_table(TABLESTYLE2);

if ($selected_id != -1) {
 	if ($Mode == 'Edit') {
		//editing an existing department
		$myrow = GetRow('kv_empl_candidates', array('id' =>$selected_id));

		$_POST['fname']  = $myrow["fname"];
		$_POST['lname']  = $myrow["lname"];
		$_POST['email']  = $myrow["email"];
		$_POST['qualification']  = $myrow["qualification"];
		$_POST['position']  = $myrow["position"];
		$_POST['salary']  = $myrow["salary"];
		$_POST['experience']  = $myrow["experience"];
		$_POST['phone']  = $myrow["phone"];
		$_POST['status']  = $myrow["status"];
		$_POST['last_company']  = $myrow["last_company"];
		$_POST['comments']  = $myrow["comments"];	
		hidden('unique_name', $myrow['unique_name']);	
	}
	
} else {	
			hidden('unique_name', '');
			$_POST['cv_title'] = ''; 
		}
hidden("selected_id", $selected_id);
text_row_ex(_("First Name")." :", 'fname', 30); 
text_row_ex(_("Last Name")." :", 'lname', 30); 
text_row_ex(_("Email")." :", 'email', 40); 
text_row_ex(_("Qualification")." :", 'qualification', 40); 
text_row_ex(_("Position")." * :", 'position', 40); 
text_row_ex(_("Salary requirements")." :", 'salary', 40); 
text_row_ex(_("Working Experience")." * :", 'experience', 40); 
text_row_ex(_("Phone")." * :", 'phone', 20); 
echo "<td><label >"._("Employment status")."* </label>";
echo "<td>".kv_radio(_('Employed'), 'status','Employed',null,true),kv_radio(_('Un Employed'), 'status','Un Employed',null,true),kv_radio(_('Self Employed'), 'status','Self Employed',null,true),kv_radio(_('Student'), 'status','Student',null,true)."</td>";
text_row_ex(_("Last worked company")." :", 'last_company', 40); 
textarea_row(_("Reference / Comments / Questions")." :", 'comments', null, 22, 4); 
kv_doc_row(_("Resume") . ":", 'kv_attach_name', 'kv_attach_name');
end_table(1);
submit_add_or_update_center($selected_id == -1, '', 'both');
end_form();
end_page();
?>