Path : /var/www/html/smart_kpp2_v2/modules/idocument/
File Upload :
Current File : /var/www/html/smart_kpp2_v2/modules/idocument/save.php

<?php 
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
?>
<?php
$predoc_id = $_GET['predoc_id'];
//print_r($_REQUEST);
//print_r($_FILES);
//print_r($_SESSION);
foreach($_REQUEST as $key=>$value)
{
  $$key=$value;
}

//$predoc_id ="";
$workgroup = "";//$_SESSION['workgroup'];
/*$content1 = htmlspecialchars($content1);
$content2 = htmlspecialchars($content2);
$content3 = htmlspecialchars($content3);*/

//เช็คบุคลากร
$sql_person = "select * from person_main where person_id='$officer'";
$dbquery_person = mysqli_query($connect,$sql_person);
$result_person = mysqli_fetch_array($dbquery_person);
$total_pid = mysqli_num_rows($dbquery_person);
$position_code = $result_person['position_code'];
$department = $result_person['department'];
$fullname=$result_person['prename'].$result_person['name']." ".$result_person['surname'];

	//เช็คค่าว่าง
	if($predoc_id == 1 and $bookto_id == ''){	//เมนอทันที แต่ไม่เลือกผู้ผ่านเรื่อง		
		echo" <br><br><center>กรุณาเลือก ผอ.กลุ่ม หรือ รอง ผอ.สพท. หรือ ผอ.สพท. ในช่อง <b>ส่งบันทึกข้อความถึง</b> ก่อนบันทึกเสนอ - <br>";
		?>
		
		 <form><br> <input type="button" class="btn btn-danger" value="กลับไปแก้ไข" onclick="history.back()"></form></center>
		<?php
		exit();
	} //จบเช็คค่าว่าง

//Get book no.
/* 
$sqlBookNo = "Select MAX(book_number)+1 as MaxBookNo From idocument_main Where book_year = (".$_SESSION['bookregister_year'].")";

$query_BookNo=mysqli_query($connect,$sqlBookNo);
$result_BookNo=mysqli_fetch_array($query_BookNo);

$book_number = ($result_BookNo['MaxBookNo'] == NULL)?1:$result_BookNo['MaxBookNo'];
$book_year = $_SESSION['bookregister_year'];
$book_no = $book_number ."/". $book_year;
$book_date = date("Y-m-d");
*/

$sqlBookNo = "Select COUNT(book_number) as MaxBookNo From idocument_main Where book_year = (".$_SESSION['bookregister_year'].")";

$query_BookNo=mysqli_query($connect,$sqlBookNo);
$result_BookNo=mysqli_fetch_array($query_BookNo);

$book_number = $result_BookNo['MaxBookNo']+1;
//$book_number = ($result_BookNo['MaxBookNo'] == NULL)?1:$result_BookNo['MaxBookNo'];
$book_year = $_SESSION['bookregister_year'];
$book_no = $book_number ."/". $book_year;
$book_date = date("Y-m-d");
			
/*//ไฟล์ที่อัพมา
				$target_file = $target_dir . basename($_FILES["UploadedFile"]["name"][$j]);
				$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//เช็คนามสกุล
//$allow_lastname = ("doc","docx","xlsx","xls","pdf","zip","rar","ppt","pptx");
if(in_array($allow_lastname, $imageFileType)) { //เริ่มเช็คนามสกุล
*/

$book_status = 1;
  $sql = "INSERT INTO `idocument_main` (
	`id`, 
	`workgroup`, 
	`workgroup_txt`, 
	`book_year`, 
	`book_number`, 
	`book_no`, 
	`book_date`, 
	`subject`, 
	`pre_doc_id`,
	`book_to`, 
	`content1`, 
	`content2`, 
	`content3`, 
	`officer`, 
	`officer_name`, 
	`officer_position`,
	`book_status`, 
	`book_type`)
	 VALUES 
	 (NULL, 
	 	'$department', 
	 	'$workgroup_txt', 
	 	'$book_year', 
	 	'$book_number', 
	 	'$book_no', 
	 	'$book_date', 
	 	'$subject', 
	 	'$predoc_id', 
	 	'$book_to', 
	 	'$content1', 
	 	'$content2', 
	 	'$content3', 
	 	'$officer', 
	 	'$officer_name', 
	 	'$officer_position',
	 	'$book_status',
	 	'$book_type');";
//echo $bookto_id;
//echo $sql;
$dbquery = mysqli_query($connect,$sql);

/*}else{ //ถ้าไม่ใช้ไฟล์ที่อนุญาตไม่ให้ผ่าน
				echo "<br><br><center><font color=red>-รูปแบบไฟล์ไม่ถูกต้อง-</font></center>";
			} //จบการเช็คนามสกุล
*/
if ($dbquery){
	$last_id = mysqli_insert_id($connect);
	$up_time = date("Y-m-d H:i:s");

	//บันทึกเฉพาะเรื่องจริงและเลือกคนผ่านเรื่อง
	//if($predoc_id == 1 and $bookto_id > ''){	
   // $sqlSento = "Insert INTO idocument_sendto(document_id,rec_id,person_id,send_time,open_time,document_from,status) 
	//Values('$last_id', md5('$last_id'), '$bookto_id', '$up_time', '0000-00-00 00:00:00', '$officer','1');";
	//mysqli_query($connect, $sqlSento);

	//บันทึกเฉพาะเรื่องจริงและเลือกคนผ่านเรื่อง
		//if($predoc_id == 1 and $bookto_id > ''){	

		$sqlSento = "INSERT INTO idocument_sendto
		(document_id,rec_id,person_id,send_time,open_time,document_from,status)
		VALUES
		('$last_id', md5('$last_id'), '$bookto_id', '$up_time', NULL, '$officer','1')";

		mysqli_query($connect, $sqlSento);
	//} //จบเช็คเรื่องจริงและเลือกคนผ่าน
//exit();
	$id_work = $_POST['id_work'];
	if($id_work > ''){
		$sql_update_work = "update work_main set idoc_id='$last_id' where id='$id_work'";
		$query_update_work=mysqli_query($connect,$sql_update_work);
		$result_update_work=mysqli_fetch_array($query_update_work);
	}


if($predoc_id == 1 and $bookto_id > ''){ //ถ้าบันทึกเสนอเรื่องจริงและเลือกคนผ่าน ให้ส่ง line
//แจ้งเตือน line ///
/*
$officer_name;	//เจ้าของเรื่อง
$officer_position //ตำแหน่งเจ้าของเรื่อง
$predoc_id	//สถานะบันทึก 0=ร่าง 1=เสนอ
$subject  //ชื่อเรื่อง
$book_type  //ความเร่งด่วน 0=ปกติ 1=ด่วน	2=ด่วนที่สุด 3=ลับ
$book_no	//เลขที่
$book_date	//ลงวันที่
$workgroup_txt	//กลุ่ม
*/
$person_id=$bookto_id; 	//เลข ปชช. ของ ผอ.กลุ่มหรือรอง หรือผู้ที่เราจะเสนอบันทึก

		
if($book_type==0){
	$book_type_name ="ปกติ";
}elseif($book_type==1){
	$book_type_name ="ด่วน";
}elseif($book_type==2){
	$book_type_name ="ด่วนที่สุด";
}elseif($book_type==3){
	$book_type_name ="ลับ";
}

	//สร้าง url สั้น
	//index.php?option=idocument&task=book_pass&action=comment&id=376
	$link1 = "/index.php?option=idocument";
    $link2 = "&task=book_pass";
    $link3 = "&id=$last_id";
    $link4 = "&action=comment";
    $url_permis = "$url"."$folder"."$link1"."$link2"."$link3"."$link4";
    //สุ่ม ref_id
	function generateRandomString($length = 20) {
	    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    $charactersLength = strlen($characters);
	    $randomString = '';
	    for ($i = 0; $i < $length; $i++) {
	        $randomString .= $characters[rand(0, $charactersLength - 1)];
	    }
	    return $randomString;
	}
	$url_id = generateRandomString();
	$url_id2 = generateRandomString();
	
	//
    //บันทึก url ของบันทึกข้อความ
    $sql = "insert into urlshort (person_id, url_id, url_name) values ('$officer', '$url_id','$url_permis')";
	$dbquery = mysqli_query($connect,$sql);
	$result = mysqli_fetch_array($dbquery);     
	$urlshort = "$url$folder/shortlink.php?url_id=$url_id"; // //ลิ้งค์สั้น
	$url_id_data = $result['url_id'];
	//จบสร้าง url ของบันทึกข้อความ

	//บันทึก url ของหน้า login จาก line
	$link11 = "/?p=$person_id";
    $link21 = "&url_id=$url_id";
    $url_permis = "$url"."$folder"."$link11"."$link21";
    $sql = "insert into urlshort (person_id, url_id, url_name) values ('$officer', '$url_id2','$url_permis')";
	$dbquery = mysqli_query($connect,$sql);
	$result = mysqli_fetch_array($dbquery); 
	$urlshort = "$url$folder/shortlink.php?url_id=$url_id2"; // //ลิ้งค์สั้น	
	//จบสร้าง url ของหน้า login จาก line
    
 	$message = "มีบันทึกข้อความ ($book_type_name) เสนอท่าน: \n"; 
 	//   $subject = "$subject"." [$fullname"." $posi_sch]";
	$subject = "เรื่อง: $subject \nเลขที่: $book_no \nลงวันที่: $book_date \nเสนอโดย: $officer_name $officer_position \n$workgroup_txt\n\nตรวจสอบ/อนุมัติ: $urlshort";
	//require_once "linenotify.php";	 //นำเข้าคำสั่งส่งไลน์

//จบแจ้งเตือน line ///
} //จบการเช็คสถานะบันทึก



$target_dir = "./modules/".$_GET['option']."/upload_files/";
$allow_lastname = array("doc","docx","xlsx","xls","pdf","zip","rar","ppt","pptx");

// เอกสารอ้างอิง
$file_no = 0;
if (isset($_POST['UploadedFileComment']) && is_array($_POST['UploadedFileComment'])) {
    $UploadedFileComment = $_POST['UploadedFileComment'];
    for($j = 0; $j < count($UploadedFileComment); $j++) {
        $commentFile = $UploadedFileComment[$j];
        if (!empty($_FILES['UploadedFile']['tmp_name'][$j]) && is_uploaded_file($_FILES['UploadedFile']['tmp_name'][$j])) {
            ++$file_no;
            $imageFileType = pathinfo($_FILES["UploadedFile"]["name"][$j], PATHINFO_EXTENSION);
            $imageFileType = strtolower($imageFileType);
            if (in_array($imageFileType, $allow_lastname)) {
                $rename_file = $target_dir . $last_id . '-ref-' . round(microtime(true)) . '_' . $file_no . '.' . $imageFileType;
                if (move_uploaded_file($_FILES["UploadedFile"]["tmp_name"][$j], $rename_file)) {
                    $sql = "INSERT INTO idocument_files(document_id, file_name, file_des, filetype, docType) 
                            VALUES('$last_id', '$rename_file', '$commentFile', '$imageFileType', 'ref')";
                    mysqli_query($connect, $sql);
                }
            }
        }
    }
}

// เอกสารแนบ
$file_no = 0;
if (isset($_POST['UploadedFileComment2']) && is_array($_POST['UploadedFileComment2'])) {
    $UploadedFileComment2 = $_POST['UploadedFileComment2'];
    for($j = 0; $j < count($UploadedFileComment2); $j++) {
        $commentFile = $UploadedFileComment2[$j];
        if (!empty($_FILES['UploadedFile2']['tmp_name'][$j]) && is_uploaded_file($_FILES['UploadedFile2']['tmp_name'][$j])) {
            ++$file_no;
            $imageFileType = pathinfo($_FILES["UploadedFile2"]["name"][$j], PATHINFO_EXTENSION);
            $imageFileType = strtolower($imageFileType);
            if (in_array($imageFileType, $allow_lastname)) {
                $rename_file = $target_dir . $last_id . '-attach-' . round(microtime(true)) . '_' . $file_no . '.' . $imageFileType;
                if (move_uploaded_file($_FILES["UploadedFile2"]["tmp_name"][$j], $rename_file)) {
                    $sql = "INSERT INTO idocument_files(document_id, file_name, file_des, filetype, docType) 
                            VALUES('$last_id', '$rename_file', '$commentFile', '$imageFileType', 'attach')";
                    mysqli_query($connect, $sql);
                }
            }
        }
    }
}

}
//exit();
echo "<script language='javascript'>window.location.href ='?option=".$option."&task=view'</script>";
?>