<?php
include_once "../../includes/connectConfig.php";

	if($_POST)
	{	
		if(isset($_POST['addfetures'])):
		
			$featurePost = test_input($_POST['featurePost']);
			
			$gtPost = gtCount("page_selected","status = 'news' AND delete_status = 0");
			if($gtPost == 5)
			{
				$output["runstt"] = "warant";
				$output["errorreason"] = "Limit Reached for Featured News";
				echo json_encode($output);
				exit;
			}
			
			$chkPstdel = getOne('page_selected'," post_id = '$featurePost' AND status = 'news' AND delete_status = 0",'id');
			if(!empty($chkPstdel)){
				$prd = $db_con->prepare("UPDATE page_selected SET delete_status = 1 WHERE post_id =:pstrec ");
				$prd->bindParam(':pstrec',$featurePost);
				$prd->execute();
				
				$output["runstt"] = "grant";
				$output["resv"] = "bg-info";
				$output["errorreason"] = "Featured News Removed.";
				echo json_encode($output);
				exit;
			}
			
			
			
			$chkPstNdel = getOne('page_selected'," post_id = '$featurePost' AND status = 'news' AND delete_status = 1",'id');
			if(!empty($chkPstNdel)){
				$prd = $db_con->prepare("UPDATE page_selected SET delete_status = 0 WHERE post_id =:pstrec ");
				$prd->bindParam(':pstrec',$featurePost);
				$prd->execute();
			
				$output["runstt"] = "grant";
				$output["resv"] = "bg-danger";
				$output["errorreason"] = "News Featured Successfully";
				echo json_encode($output);
				exit;
			}
			
			$prd = $db_con->prepare("INSERT INTO page_selected(post_id,status) VALUES(:pstrec,'news')");
			$prd->bindParam(':pstrec',$featurePost);
			$prd->execute();
			
			$output["runstt"] = "grant";
			$output["resv"] = "bg-danger";
			$output["errorreason"] = "News Featured Successfully";
			echo json_encode($output);
			exit;
		
		else:
		
			$valid_extensions = array('png','jpg','jpeg');

			$namea = $_FILES['pics_a']['name'];
			$sizea = $_FILES['pics_a']['size'];
			$extsa = strtolower(pathinfo($namea, PATHINFO_EXTENSION));

			if(!empty($namea) && !(in_array($extsa, $valid_extensions)) || $sizea > 9000000)
			{
				$output["runstt"] = "warant";
				$output["errorform"] = "a";
				$output["errorreason"] = "Check file type or size";
				echo json_encode($output);
				exit;
			}
			else
			{
				$folder = "../../img/shield-extras/";  
				$num_rn = substr(md5(uniqid(rand())), 0, 7 );
				
				$pstrec = (isset($_POST["pstrec"])) ? test_input($_POST["pstrec"]) : '';
				$post_title = test_input($_POST["posttitle"]);
				$ps_dept = test_input($_POST["psdept"]);
				$ps_tdate = test_input(date('Y-m-d', strtotime($_POST["pstdate"])));
				$ps_post = test_input($_POST["pspost"]);
				$briefpost =  test_input($_POST['postbrief']);
				$de_scpost = $_POST['postdescription'];
				$p_url = php_slug_url($post_title);
				
				$crtss = (isset($_SESSION['prdss']) & !empty($_SESSION['prdss'])) ? $_SESSION['prdss'] : '';
				
				$imga = "";
				if(!empty($namea))
				{
					$tmpa = $_FILES['pics_a']['tmp_name'];
					list($txta, $exta) = explode(".", $namea);
					$imga = strtolower($num_rn.'_'.rand(1000,100000).'.'.$exta);
					$filePatha = $folder.'/'.$imga;
					
					move_uploaded_file($tmpa, $filePatha);
				}
			

				if($_POST['post_info'] == "post-record"){
					
					//INSERT INTO TABLE
					$prd = $db_con->prepare("INSERT INTO  curicular_extraacts (nwtitle, nwimg, nwdept, nwdate, nwby, newsbrief, post_news, nwurl) VALUES(:post_title, :imga, :pdpt, :pdate, :ppost, :briefpost, :pstnt, :p_url)");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imga);
					$prd->bindParam(':pdpt',$ps_dept);
					$prd->bindParam(':pdate',$ps_tdate);
					$prd->bindParam(':ppost',$ps_post);
					$prd->bindParam(':briefpost',$briefpost);
					$prd->bindParam(':pstnt',$de_scpost);
					$prd->bindParam(':p_url',$p_url);
					$prd->execute();
					$prr_id = $db_con->lastInsertId();
					
					$chk_img = (!empty($crtss)) ? getALL("sample_imageload","img_post = '$crtss'","si_id ASC") : '';
					if(!empty($chk_img)){
						foreach($chk_img as $cmk){
						$imgck = $cmk['img_nm'];		
						$ins_img = $db_con->prepare("INSERT INTO curicular_extraimgs(img_nm, cr_id) VALUES (:imne, :imue)");
						$ins_img->bindParam(':imne',$imgck);$ins_img->bindParam(':imue',$prr_id); $ins_img->execute();
						
						$ins_del = $db_con->prepare("DELETE FROM sample_imageload WHERE si_id = '$cmk[si_id]' ");$ins_del->execute();
						}
						unset($_SESSION['prdss']);
					}
					else
					{
						if(!empty($crtss)){
						$output['runstt'] = "warant";
						$output['errorreason'] = "Error processing images, Edit Post to updated images.";
						echo json_encode($output);
						exit;
						}
					}
					
					$output["runstt"] = "grant";
					$output["errorreason"] = "Record Posted Successfully";
					echo json_encode($output);
					exit;
					
				}
				elseif($_POST['post_info'] == "edit-record")
				{
					$imgnm = (!empty($namea) && $imga != "") ? $imga : getOne('curicular_extraacts'," cr_id = '$pstrec' ",'nwimg');
					
					//INSERT INTO TABLE
					$prd = $db_con->prepare("UPDATE curicular_extraacts SET nwtitle=:post_title, nwimg=:imga, nwdept=:pdpt, nwdate=:pdate, nwby=:ppost, newsbrief=:briefpost, post_news=:pstnt, nwurl=:p_url WHERE cr_id=:pstrec");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imgnm);
					$prd->bindParam(':pdpt',$ps_dept);
					$prd->bindParam(':pdate',$ps_tdate);
					$prd->bindParam(':ppost',$ps_post);
					$prd->bindParam(':briefpost',$briefpost);
					$prd->bindParam(':pstnt',$de_scpost);
					$prd->bindParam(':p_url',$p_url);
					$prd->bindParam(':pstrec',$pstrec);
					$prd->execute();
					
					$chk_img = (!empty($crtss)) ? getALL("sample_imageload","img_post = '$crtss'","si_id ASC") : '';
					if(!empty($chk_img)){
						foreach($chk_img as $cmk){
						$imgck = $cmk['img_nm'];		
						$ins_img = $db_con->prepare("INSERT INTO curicular_extraimgs(img_nm, cr_id) VALUES (:imne, :imue)");
						$ins_img->bindParam(':imne',$imgck);$ins_img->bindParam(':imue',$pstrec); $ins_img->execute();
						
						$ins_del = $db_con->prepare("DELETE FROM sample_imageload WHERE si_id = '$cmk[si_id]' ");$ins_del->execute();
						}
						unset($_SESSION['prdss']);
					}
					else
					{
						if(!empty($crtss)){
							$output['runstt'] = "warant";
							$output['errorreason'] = "Error processing images, Edit Post to updated images.";
							echo json_encode($output);
							exit;
						}
					}
					
					$output["runstt"] = "grant";
					$output["errorreason"] = "Record Data Updated";
					echo json_encode($output);
					exit;
				}
				else
				{
					$output["runstt"] = "warant";
					$output["errorreason"] = "";
					echo json_encode($output);
					exit;
				}
			}
		endif;
	}
?>