<?php
require_once("class/class.phpmailer.php");
require_once("class/class.smtp.php");

$rs_type = mysql_query("SELECT * FROM tbl_doctype");

function doctype(){
	return mysql_query("SELECT * FROM tbl_doctype ORDER BY doctype_name ASC");
}
function doc_io_type(){
	return mysql_query("SELECT * FROM tbl_doc_io_type ORDER BY io_name ASC");
}
/*--------------------------------SELECT TO EDIT ------------------------------*/
function doc(){
	$doc_id = mysql_real_escape_string(stripslashes($_GET['doc_id']));
	$sql = "SELECT d.*,dt.doctype_name, dt_r.doctype_name as refer_docname, in_t.io_name, m.m_id, 
	u.first_name, org.orgunitname,ptn.position_id, if(ptn.position_name IS NULL OR ptn.position_name ='', 'ວິຊາການ', ptn.position_name) as position_name, m.orgunitid 
	From tbl_in_out_document d INNER JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id)
	LEFT JOIN sys_user u ON(u.orgunitid=m.orgunitid)
	INNER JOIN tbl_doctype dt ON (dt.doctype_id=d.doctype_id)
	LEFT JOIN tbl_doctype dt_r ON(dt_r.doctype_id=d.refer_type)
	LEFT JOIN tbl_doc_io_type in_t ON (in_t.doc_io_id=d.doc_io_id)
	LEFT JOIN emp_position ptn ON(ptn.position_id=u.position_id)
	LEFT JOIN orgunit org ON (org.orgunitid = u.orgunitid) WHERE d.doc_id='$doc_id'";
	return mysql_query($sql);
}
/*--------------------------------SELECT TO EDIT ------------------------------

/*============ list the emp that in the current action section ===========*/
function list_org(){
	$org_id = (int)$_GET['orgunitid'];
	return mysql_query("SELECT * FROM orgunit WHERE isdeleted=0 AND inactive=0 AND unittypeid=5 ORDER BY orgunitid ASC");
}

/*============== function send mail after save docin // on localhost ========================*/
function send_mail($doc_id, $orgunitid){
	$mail=new PHPMailer();

	$mail->IsSMTP();
	$mail->IsHTML(true);

	$mail->SMTPAuth = true;
	$mail->SMTPSecure = "ssl";
	$mail->Host = "smtp.gmail.com";
	$mail->Port = 465;

	$mail->Username = "ict.followupwork@gmail.com";
	$mail->Password = "cunning@494";
	$mail->From = "ICT document in";
	$mail->FromName = "ICT document in";
	$text= $part1." ICT document in";
	$mail->Subject = $text;
	$sql = "SELECT emp.name, emp.email_address, d.* FROM tbl_in_out_document d INNER JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id) 
		INNER JOIN emp_personal emp ON (emp.emp_id = m.receive_person)
		INNER JOIN  emp_employment epp ON(epp.emp_id=emp.emp_id)
		where m.doc_id = $doc_id AND m.orgunitid=$orgunitid  AND m.receive_status = 1 AND epp.workingstatus=1";
	$sql_doc = mysql_query($sql); 
	$rs_doc = mysql_fetch_array($sql_doc);			
	$mail->Body = JB_utf8_to_html("ເອກະສານເຂົ້າສົ່ງຈາກຄະນະພາກສ່ວນ: ")." <a href='http://192.168.20.48/ictweb/document/show_filein.php?filein=".$rs_doc['doc_file']."&type=".$rs_doc['doctype_id']."'>".JB_utf8_to_html($rs_doc['doc_title'])."</a><br>
		".JB_utf8_to_html("ເລກທີເອກະສານເຂົ້າ: ").JB_utf8_to_html($rs_doc['doc_code'])."<br />
		".JB_utf8_to_html("ວັນທີເຂົ້າ: ").$rs_doc['doc_date']."<br />
		".JB_utf8_to_html("ຄໍາເຫັນຂອງພະແນກ: ").JB_utf8_to_html($rs_doc['doc_comment'])."<br />";
		
		
	//======== declare array variable to get 
	$sql_doc_2 = mysql_query($sql); 
	while ($rso = mysql_fetch_array($sql_doc_2)) {
		// get employee info 
		$emp_name = $rso['name'];
		//$email_address = $rso['email_address'];
		$email_address = "phetmany.mhing@gmail.com";
		$split = stripos($email_address, ",");
		$split2 = stripos($email_address, ";");
		if(($split != false) && $split2 != false){
			continue; // --use continue to next loop and don't send the current address, it is more then two email address or incorrect
		}
		$email = array();
		if(($split == true) && ($split2 == false)){
			$email = explode(',', $email_address);
		}elseif(($split == false) && ($split2 == true)){
			$email = explode(';', $email_address);
		}else{
			$email[] = $email_address;
		} 
		// send mail here
		for($s = 0; $s<count($email); $s++){
			$mail->AddAddress($email[$s],"ICT web admin");
			//$mail->AddAddress("kongxiong.12@gmail.com","ICT web admin"); 
		}
		
	}
	if(mysql_num_rows($sql_doc_2) !=0){
		if(!$mail->Send()) {
			echo "Mailer Error: " . $mail->ErrorInfo;
		} else {
		 //$msg =  "<SCRIPT language='JavaScript'>alert('Mailer has been sent to $num people ..!')</SCRIPT>";			
		}
	}
}
/*============== function send mail after save docin // on server side ========================*/
function send_mail_server($doc_id, $orgunitid){
	
	$sql = "SELECT emp.name, emp.email_address, d.* FROM tbl_in_out_document d INNER JOIN tbl_manage_doc m ON(m.doc_id=d.doc_id) 
		INNER JOIN emp_personal emp ON (emp.emp_id = m.receive_person)
		INNER JOIN  emp_employment epp ON(epp.emp_id=emp.emp_id)
		where m.doc_id = $doc_id AND m.orgunitid=$orgunitid  AND m.receive_status = 1 AND epp.workingstatus=1";
	$sql_doc = mysql_query($sql); 
	$rs_doc = mysql_fetch_array($sql_doc);			
	$body = JB_utf8_to_html("ເຂົ້າຮັບເອົາເອກະສານ: ")." <a href='http://ict.etllao.com/new_ict/document/show_filein.php?filein=".$rs_doc['doc_file']."&type=".$rs_doc['doctype_id']."'>".JB_utf8_to_html($rs_doc['doc_title'])."</a><br>
		".JB_utf8_to_html("ເລກທີເອກະສານເຂົ້າ: ").JB_utf8_to_html($rs_doc['doc_code'])."<br />
		".JB_utf8_to_html("ວັນທີເຂົ້າ: ").$rs_doc['doc_date']."<br />
		".JB_utf8_to_html("ຄໍາເຫັນຂອງພະແນກ: ").JB_utf8_to_html($rs_doc['doc_comment'])."<br />";
		
		
	//======== declare array variable to get 
	$sql_doc_2 = mysql_query($sql); 
	while ($rso = mysql_fetch_array($sql_doc_2)) {
		// get employee info 
		$emp_name = $rso['name'];
		$email_address_get = "";
		$email_address = $rso['email_address'];
		//$email_address = "kongxiong.12@gmail.com, xxiong42@yahoo.com, kongxiong143@yahoo.com, kongx2010@hotmail.co.uk, konginter.13@gmail.com";
		// get mail as string 
		$email_address_get = str_replace(";",",",mysql_real_escape_string(stripslashes($email_address))); // 
		// get id to guest
		if($email_address_get != "") $email_address  = $email_address.", ".$email_address_get;
	}
	// pre to send mail 
	$strTo = ltrim($email_address,","); 
	$strSubject = "ICT document in";
	$strHeader = 'From: ict.followupwork@gmail.com' . "\r\n";
	$strHeader.= "MIME-version: 1.0\n";
	$strHeader.= "Content-type: text/html; charset= utf-8\n";
	$strMessage = $body;
	$flgSend = @mail($strTo,$strSubject,$strMessage,$strHeader); // @ = No Show Error //
	
	if(mysql_num_rows($sql_doc_2) !=0){	
		if($flgSend){
			//echo "Email Sent.<br />".$strTo."<br />";
		}else{
			echo "not send or err";
			exit();
		}
	}
}
