<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;

if ( $_GET['iswebview'] == 'yes' ){
	include 'requires/session_api.php' ;
}else{
	include 'requires/session.php' ;
}

$page 						= escapeString($_GET['page']) ;
$type 						= escapeString($_GET['type']) ;
$page_type 					= escapeString($_GET['page_type']) ;
$search_query 				= '' ;

switch($type){
	case 'pending' :
		$print_filename 	= 'Employment' ;
		$title 				= 'Employment Application Form' ;
	break ;
	case 'offer' :
		$print_filename 	= 'Offer' ;
		$search_query 	   .= " AND employment_status = 'Offer'" ;
		$title 				= 'Offer Letter' ;
	break ;	
	case 'iea' :
		$print_filename 	= 'IEA' ;
		$search_query 	   .= " AND employment_status = 'Offer'" ;
		$title 				= 'Individual Employment Agreement between an Employer and an Employee' ;
	break ;
	case 'confirmation' :
		$print_filename 	= 'Confirmation' ;
		$search_query 	   .= " AND employment_status = 'Confirmation'" ;
		$title 				= 'Confirmation of Employment' ;
	break ;	
	case 'terminate' :
		$print_filename 	= 'Terminate' ;
		$search_query 	   .= " AND employment_status = 'Terminate'" ;
		$title 				= 'Termination Letter' ;
	break ;	
}

$mysqli_page 				= $mysqli->query("SELECT * FROM staff_employment
							   			  	  WHERE employment_id = '".$page."' AND employment_trash = '0' ".$search_query." LIMIT 1");
// check quotation
if ($mysqli_page->num_rows > 0){
	
	// set query in array
	$row_page 			= $mysqli_page->fetch_array(MYSQLI_ASSOC) ;
	
	// employment position
	$mysqli_position 	= $mysqli->query("SELECT post_title FROM system_post
									   	  WHERE post_id = '".$row_page['employment_position']."' AND post_type = 'hr-position' AND post_categories = 'hr-position' LIMIT 1") ; 
	if ($mysqli_position->num_rows > 0){
		$row_position 	= $mysqli_position->fetch_array(MYSQLI_ASSOC) ;
		$position 		= dataFilter($row_position['post_title']) ;	
	}
	
	// incharge person
	$mysqli_query = "SELECT * FROM system_user
					 WHERE user_id = '".$row_page['employment_user_id']."' AND (user_permission = 'admin' OR user_permission = 'hr') AND user_trash = '0' LIMIT 1" ;
	$mysqli_incharge_by = $mysqli->query($mysqli_query) ;
	if ($mysqli_incharge_by->num_rows > 0){
		$row_incharge_by = $mysqli_incharge_by->fetch_array(MYSQLI_ASSOC) ;
		$incharge_by = dataFilter($row_incharge_by['user_call']).' . '.dataFilter($row_incharge_by['user_fullname']) ;
	}
	
	// assigned by
	$mysqli_query = "SELECT * FROM system_user a
					 LEFT JOIN system_post b ON (a.user_position = b.post_id)
					 WHERE user_id = '".$row_page['employment_assign_by']."' AND (user_permission = 'admin' OR user_permission = 'hr') AND user_trash = '0' ORDER BY user_name" ;
	$mysqli_assign_by = $mysqli->query($mysqli_query) ;
	if ($mysqli_assign_by->num_rows > 0){
		$row_assign_by = $mysqli_assign_by->fetch_array(MYSQLI_ASSOC) ;
		$assign_by = dataFilter($row_assign_by['user_call']).' . '.dataFilter($row_assign_by['user_fullname']) ;
		$assign_by_position = dataFilter($row_assign_by['post_title']) ;
	}
	
	switch($type){
		case 'offer' :
		case 'iea' :
			$status_text = jsonEncodeDecode('decode', $row_page['employment_status_text']) ;
			$offer_status = $status_text['offer_status'] ;
		break ;
		case 'confirmation' :
			$employment_confirmation_date = $row_page['employment_confirmation_date'] ;
			$employment_confirmation_date = ($employment_confirmation_date != '0000-00-00 00:00:00' ? date('d.m.Y', strtotime($employment_confirmation_date)) : '') ;
		break ;	
		case 'terminate' :
			$employment_terminate_date = $row_page['employment_terminate_date'] ;
			$employment_terminate_date = ($employment_terminate_date != '0000-00-00 00:00:00' ? date('d.m.Y', strtotime($employment_terminate_date)) : '') ;
		break ;
	}
	
	$new_worker  = dataFilter($row_page['employment_call']).' . '.dataFilter($row_page['employment_name']) ;
	
	$letter_head = getOwnerCompanyLetterHead($row_page['employment_branch']) ;
	$html2       = ''; 

	// set body content
	$html = $letter_head['header'].'
	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="employment_table" >
		<tr><td colspan="4">&nbsp;</td></tr>
		<tr>
			<td colspan="4" width="100%" style="font-size:24px; padding:10px 0; text-align:center; border:1px solid #000; background:#000; color:white;text-transform:uppercase;text-align:center;">
				<strong>'.$title.'</strong>
			</td>
		</tr>
		<tr><td colspan="4">&nbsp;</td></tr>' ;
		switch($type){
			case 'pending' :
				include_once 'HR/letter-pending.php' ;
			break ;
			case 'offer' :
				include_once 'HR/letter-offer.php' ;
			break ;	
			case 'iea' :
				// include_once 'HR/letter-iea.php' ;
				include_once 'HR/letter-iea-temp.php' ;
				
				// page footer
				$footer = '
				<table border="0" cellpadding="0" cellspacing="0" width="960" style="font-size:18px;">
					<tr>
						<td align="center">{PAGENO}</td>
					</tr>
				</table>' ;
				
			break ;
			case 'confirmation' :
				include_once 'HR/letter-confirmation.php' ;
			break ;
			case 'terminate' :
				include_once 'HR/letter-terminate.php' ;
			break ;
		}
	$html .= '
	</table>'.$html2.$html_offer ;

	
	// page header
	$header = '' ;

	// keep footer html content in variable
	include_once 'MPDF/mpdf.php' ;

	$mpdf = new mPDF('utf-8', 'A4', '', 'freesans', 15, 15, 15, 15, 5, 5) ;
	ini_set("memory_limit","999999999999999999999999999999999999999999M");

	// Use different Odd/Even headers and footers and mirror margins
	$mpdf->mirrorMargins = 1 ;

	//allow special character
	$mpdf->useAdobeCJK = true;
	
	// set mpdf header
	$mpdf->SetHTMLHeader($header) ;
	$mpdf->SetHTMLHeader($header,'E') ;
	
	// set mpdf footer
	$mpdf->SetHTMLFooter($footer) ;
	$mpdf->SetHTMLFooter($footer,'E') ;
	
	// write in html
	$mpdf->WriteHTML($html) ;
	
	// set filename
	$filename = $print_filename.'-'.strPad(3, $page) ; // Your Filename whit local date and time
	$filename_save = $filename.'.pdf' ;
	$filename_jpeg = $filename.'.jpg' ;
	$filename_temp = $filename ;
	
	// turns all headers/footers off from new page onwards
	$mpdf->useAdobeCJK = true;
	
	// check output type
	$page_type = ($_GET['page_type']) ;
	$page_type_output = ($page_type == 'jpeg' ? 'F' : 'I') ;
	
	//$mpdf->SetAutoFont(AUTOFONT_ALL);
	$mpdf->Output($filename_save, $page_type_output);
	
	// check page type
	if ($page_type == 'jpeg'){
		//*******************************************************************
		//******************************************************************* save pdf file to jpeg file
		//*******************************************************************
		$img = new imagick() ;
		//this must be called before reading the image, otherwise has no effect - "-density {$x_resolution}x{$y_resolution}"
		$img->setResolution(300,300) ; //this is important to give good quality output, otherwise text might be unclear
		$img->readImage("{$filename_save}") ; //read the pdf
		$total_page = $img->getNumberImages() ;
		$img->setImageFormat('jpg') ; //set new format
		$img->writeImages($filename_jpeg, true) ; //save image file
		//*******************************************************************
		//******************************************************************* force download jpeg file
		//*******************************************************************
		$file_name = $filename_jpeg ; // grab the requested file's name
		// make sure it's a file before doing anything!
		if ($total_page > 1){
			for ($i = 0; $i < $total_page; $i++){
				$file_name = $filename_temp.'-'.$i.'.jpg' ;
				if(is_file($file_name)) {
					$files_to_zip[] = $file_name ;		
				}
			}
			create_zip($files_to_zip, $filename_temp.'.zip') ;
			for ($i = 0; $i < $total_page; $i++){ unlink($filename_temp.'-'.$i.'.jpg') ; }
			$file_name = $filename_jpeg = $filename_temp.'.zip' ;
		}
		if(is_file($file_name)) {			
			// required for IE
			if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off') ;	}
			// get the file mime type using the file extension
			switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
				case 'pdf': $mime = 'application/pdf'; break ;
				case 'zip': $mime = 'application/zip'; break ;
				case 'jpeg':
				case 'jpg': $mime = 'image/jpg'; break ;
				default: $mime = 'application/force-download' ;
			}
			header('Pragma: public') ; 	// required
			header('Expires: 0') ;		// no cache
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0') ;
			header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT') ;
			header('Cache-Control: private',false) ;
			header('Content-Type: '.$mime) ;
			header('Content-Disposition: attachment; filename="'.basename($file_name).'"') ;
			header('Content-Transfer-Encoding: binary') ;
			header('Content-Length: '.filesize($file_name)) ;	// provide file size
			header('Connection: close') ;
			readfile($file_name); // push it out			
		}
		unlink($filename_jpeg) ;
		unlink($filename_save) ;
		echo '<script type="text/javascript">setTimeout("window.close();", 0);</script>' ;
	}
	exit ;
}
// redirect page
header("Location: main.php") ;
exit ;
?>
