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

	if($_POST)
	{
		if(isset($_POST['setimport'])):
		
			$bestPost = test_input($_POST['bestPost']);
			
			$gtPost = getOne('blog_post'," bestpost = 1",'id');
			
			if($gtPost != "")
			{
				$prd = $db_con->prepare("UPDATE blog_post SET bestpost = 0 WHERE id=:pstrec");
				$prd->bindParam(':pstrec',$gtPost);
				$prd->execute();
			}
			
			$prd = $db_con->prepare("UPDATE blog_post SET bestpost = 1 WHERE id=:pstrec");
			$prd->bindParam(':pstrec',$bestPost);
			$prd->execute();
			
			$output["runstt"] = "grant";
			$output["errorreason"] = "Post Marked";
			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 > 2000000)
			{
				$output["runstt"] = "warant";
				$output["errorform"] = "a";
				$output["errorreason"] = "Check file type or size";
				echo json_encode($output);
				exit;
			}
			else
			{
				$folder = "../../post_files/";  
				$num_rn = substr(md5(uniqid(rand())), 0, 7 );
				
				$pstrec = (isset($_POST['pstrec']) && !empty($_POST['pstrec'])) ? test_input($_POST["pstrec"]) : '';
				
				$post_title = test_input($_POST["jewtitle"]);
				$description =  $_POST['jewbrief'];
				$briefpost =  test_input($_POST['dsbrief']);
				$p_url = php_slug_url($post_title);
				
				
				$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'] == "create")
				{
					//INSERT INTO TABLE
					$prd = $db_con->prepare("INSERT INTO newspost(nwtitle, nwimg, newsbrief, post_news, nwurl) VALUES(:post_title, :imga, :briefpost, :post_info, :p_url)");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imga);
					$prd->bindParam(':briefpost',$briefpost);
					$prd->bindParam(':post_info',$description);
					$prd->bindParam(':p_url',$p_url);
					$prd->execute();
                
					$output["runstt"] = "grant";
					$output["errorreason"] = "Post Submited Sucessfully";
					echo json_encode($output);
					exit;
				}
				elseif($_POST['post_info'] == "edit")
				{
					$chkjw = getOne("newspost"," nw_id  = '$pstrec' AND delete_status = 0 ","nw_id");
					if(!empty($chkjw)){
					$imgnm = (!empty($namea) && $imga != "") ? $imga : getOne('newspost'," nw_id = '$pstrec' ",'nwimg');
					
					//INSERT INTO TABLE
					$prd = $db_con->prepare("UPDATE newspost SET nwtitle=:post_title, nwimg=:imga, newsbrief=:briefpost, post_news=:post_info, nwurl=:p_url WHERE nw_id=:pstrec");
					
					$prd->bindParam(':post_title',$post_title);
					$prd->bindParam(':imga',$imgnm);
					$prd->bindParam(':briefpost',$briefpost);
					$prd->bindParam(':post_info',$description);
					$prd->bindParam(':p_url',$p_url);
					$prd->bindParam(':pstrec',$pstrec);
					$prd->execute();
					
					$output["runstt"] = "grant";
					$output["errorreason"] = "Post Updated Sucessfully";
					echo json_encode($output);
					exit;
					}
					else{
						$output["runstt"] = "warant";
						$output["errorreason"] = "Invalid Record, Try Again.";
						echo json_encode($output);
						exit;
					}
				}
				else
				{
					$output["runstt"] = "warant";
					$output["errorreason"] = "";
					echo json_encode($output);
					exit;
				}
			}
		endif;
	}
?>