<?php
header('Content-type: application/json');
include '../db/db.php';
session_start();
$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$log_lang = htmlspecialchars(trim($_SESSION['lang']), ENT_QUOTES, 'UTF-8');

$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), ENT_QUOTES, 'UTF-8');

if (empty($log)) {
	$redirectUrl = "index.php";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

} else {

	$sqlu = "SELECT * FROM user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = date("Y-m-d");

		/****************************** Insert Eng *************************************/

		if(isset($_FILES['upload_pdf'])) {	
			if($_FILES["upload_pdf"]["name"] != '') {
				$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
				$date = htmlspecialchars(strip_tags(trim($_POST['date'])), ENT_QUOTES, 'UTF-8');
				$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');	
				
				$group_no = mysqli_fetch_assoc(mysqli_query($conn, "SELECT press_release FROM group_id_tbl"));
				$g_no = $group_no['press_release'];

				$valid_extensions = array('pdf', 'png','jpg','jpeg'); // valid extensions
				$path = '../upload/PDF/press_release/'; // upload directory	

				// get uploaded file's extension
				$ext = strtolower(pathinfo($_FILES["upload_pdf"]["name"], PATHINFO_EXTENSION));
				$tmp = $_FILES['upload_pdf']['tmp_name'];

				// check valid format
				if(in_array($ext, $valid_extensions)) 
				{
					
					if($ext == 'pdf') {
						$img_name = '';
						$pdf_name = rand(100000, 999999).'.'.$ext;
						$path = $path.$pdf_name;	
					
					} else {
						$pdf_name = '';
						$img_name = rand(100000, 999999).'.'.$ext;
						$path = $path.$img_name;	
					}
						
					if (file_exists($path))
					{			
						$response['status'] = '2'; // File Exists
					
					} else {		
						if(move_uploaded_file($tmp,$path)) 
						{  
							if($log_lang == 1) {	
								$sql = "INSERT INTO press_release VALUES (NULL, '$title', '$des','$date', '$pdf_name', '$img_name', '$user_id', '$g_no', '1', '0')";

								if(mysqli_query($conn, $sql)) {
									$g_no2 = $g_no+1;
									$group_no_up = mysqli_query($conn, "UPDATE group_id_tbl SET press_release='$g_no2'");
									
									$response['status'] = '1'; // Success
								
								} else {
									$response['status'] = '3'; // Sql Error
								}
							}
						
						} else{				
						  //exit("Error While uploading image on the server");
						  $response['status'] = '4'; // Uploading Error
						}
					}
				} 
				else 
				{
					$response['status'] = '5'; // Invaild File
				}
			 
			}
			echo json_encode($response);	
		}
		
		/****************************** Insert Sinhala/Tamil *************************************/
		
		if (array_key_exists('add_press_lang', $_POST)) {
			$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
			$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
			$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
			$lang = htmlspecialchars(strip_tags(trim($_POST['lang'])), ENT_QUOTES, 'UTF-8');

			$sqlg = mysqli_query($conn, "SELECT * FROM press_release WHERE group_id='$group_id' AND lang='$lang'");
			
			if(mysqli_num_rows($sqlg) == 0) {
				$sql_eng = mysqli_fetch_assoc(mysqli_query($conn, "SELECT date, pdf, img FROM press_release WHERE group_id='$group_id' AND lang=1"));
				$date = $sql_eng['date'];	
				$pdf = $sql_eng['pdf'];	
				$img = $sql_eng['img'];	
				
				$sql = "INSERT INTO press_release VALUES (NULL, '$title', '$des','$date', '$pdf', '$img', '$user_id', '$group_id', '$lang', '0')";
			
				if(mysqli_query($conn, $sql)) {
					echo json_encode(array("msgType" => 1, "msg" => "Successfully Added"));
				
				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save Data"));
				}			
			
			} else {
				echo json_encode(array("msgType" => 3, "msg" => "Data Already Exist"));
			}
		}
		
		/****************************** Remove *************************************/
		
		if (array_key_exists('remove_press', $_POST)) {

			if($log_lang == 1) {
			
				$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');		
				$sql = "UPDATE press_release SET del=1 WHERE group_id='$group_id'";
				
				if(!empty($group_id)) {
				
					if(mysqli_query($conn, $sql)) {
						echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
					
					} else {
						echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
					}
				
				} else {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! empty id"));
				}
			
			}

		}		
		
		/****************************** Load Form Data *************************************/
		
		if (array_key_exists('get_dataset', $_POST)) {

			$group_id = htmlspecialchars(strip_tags(trim($_POST['gid'])), ENT_QUOTES, 'UTF-8');
			$lang = htmlspecialchars(strip_tags(trim($_POST['lang'])), ENT_QUOTES, 'UTF-8');
			
			$data = array();
			$sql = mysqli_query($conn, "SELECT title, description, date FROM press_release WHERE group_id=$group_id AND lang='$lang'");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}
		
		/****************************** Update *************************************/
		
		if (array_key_exists('update_press_lang', $_POST)) {
			$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
			$lang = htmlspecialchars(strip_tags(trim($_POST['lang'])), ENT_QUOTES, 'UTF-8');
			
			$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
			$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
			$date = htmlspecialchars(strip_tags(trim($_POST['date'])), ENT_QUOTES, 'UTF-8');			

			if($log_lang == 1) {
				$sql = mysqli_query($conn, "UPDATE press_release SET date='$date' WHERE group_id='$group_id'");
			}

			$sql_up = mysqli_query($conn, "UPDATE press_release SET title='$title', description='$des', user='$user_id' WHERE group_id='$group_id' AND lang='$lang'");

			if($sql_up) {
				echo json_encode(array("msgType" => 1, "msg" => "Successfully Added"));
			
			} else {
				echo json_encode(array("msgType" => 2, "msg" => "Sorry ! Could not be Save Data"));
			}
		}

		/****************************** Update Eng *************************************/

		if(isset($_FILES['pdf_update'])) {	
			if($_FILES["pdf_update"]["name"] != '') {
				$group_id = htmlspecialchars(strip_tags(trim($_POST['group_id'])), ENT_QUOTES, 'UTF-8');
				$des = htmlspecialchars(strip_tags(trim($_POST['des'])), ENT_QUOTES, 'UTF-8');
				$title = htmlspecialchars(strip_tags(trim($_POST['title'])), ENT_QUOTES, 'UTF-8');
				$date = htmlspecialchars(strip_tags(trim($_POST['date'])), ENT_QUOTES, 'UTF-8');
			
				$valid_extensions = array('pdf', 'png','jpg','jpeg'); // valid extensions
				$path = '../upload/PDF/press_release/'; // upload directory	

				// get uploaded file's extension
				$ext = strtolower(pathinfo($_FILES["pdf_update"]["name"], PATHINFO_EXTENSION));
				$tmp = $_FILES['pdf_update']['tmp_name'];

				// check valid format
				if(in_array($ext, $valid_extensions)) 
				{					
					if($ext == 'pdf') {
						$img_name = '';
						$pdf_name = rand(100000, 999999).'.'.$ext;
						$path = $path.$pdf_name;	
					
					} else {
						$pdf_name = '';
						$img_name = rand(100000, 999999).'.'.$ext;
						$path = $path.$img_name;	
					}	
						
					if (file_exists($path))
					{			
						$response['status'] = '2'; // File Exists
					
					} else {
						
						$sql_eng = mysqli_fetch_assoc(mysqli_query($conn, "SELECT pdf, img FROM press_release WHERE group_id='$group_id' AND lang=1"));	

						if(!empty($sql_eng['pdf'])) {
							$path_old = "../upload/PDF/press_release/".$sql_eng['pdf'];
							$files = glob($path_old);
							
							foreach($files as $file) {
								unlink($file);
							}
						}
						
						if(!empty($sql_eng['img'])) {
							$path_old = "../upload/PDF/press_release/".$sql_eng['img'];
							$files = glob($path_old);
							
							foreach($files as $file) {
								unlink($file);
							}
						}
						
						if(move_uploaded_file($tmp,$path)) 
						{  
							if($log_lang == 1) {					
								$sql = "UPDATE press_release SET pdf='$pdf_name', img='$img_name', date='$date' WHERE group_id='$group_id'";
								$sql_up = "UPDATE press_release SET title='$title', description='$des', user='$user_id' WHERE group_id='$group_id' AND lang='1'";
								
								if(mysqli_query($conn, $sql)&& mysqli_query($conn, $sql_up)) {
									$response['status'] = '1'; // Success
								
								} else {
									$response['status'] = '3'; // Sql Error
								}
							}
						
						} else{				
						  //exit("Error While uploading image on the server");
						  $response['status'] = '4'; // Uploading Error
						}
					}
				} 
				else 
				{
					$response['status'] = '5'; // Invaild File
				}
			 
			}
			echo json_encode($response);	
		}		

		/******************************************************************/		

	} else{
		$redirectUrl = "index.php";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}
}
?>