<?php 

// Include main file
include_once('../libs/main.php');

// Check admin Login
include_once('checkLogin.php');

$successMsg = '';

if(isset($_REQUEST['successMsg'])){
	$successMsg = $_REQUEST['successMsg'];
}

if(isset($_REQUEST['failedMsg'])){
	$failedMsg = $_REQUEST['failedMsg'];
}

// Logical section of news
$newsObj =  new  news();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['news_id'])){
	$newsDetail = $newsObj->fromId($_REQUEST['news_id']);
}


if(isset($_POST['hiddenSubmit'])){
	
	
	$newsData->news_title 		= $_POST['news_title'];	
	$newsData->news_slug 		= $_POST['news_slug'];	
	$newsData->news_desc 		= addslashes($_POST['news_desc']);
	$newsData->news_date 		= date('Y-m-d',strtotime($_POST['news_date']));
	$newsData->news_meta_title 	= $_POST['news_meta_title'];	
	$newsData->news_meta_keywords 	= $_POST['news_meta_keywords'];	
	$newsData->news_meta_desc 		= $_POST['news_meta_desc'];
	$newsData->news_status 		= $_POST['news_status'];	
	//print_r($newsData); exit;
	// edit news details 
	if(isset($_POST['news_id'])){

		$newsData->news_id = $_POST['news_id'];
		
		$newsD = $newsObj->fromId($newsData->news_id);
		
		if(isset($_FILES['news_image']) && $_FILES['news_image']['name']!=''){
			if($newsD->news_image!='' && file_exists(DIR_UPLOAD_BANNER.$newsD->news_image)){
				unlink(DIR_UPLOAD_BANNER.$newsD->news_image);
			}
		}	
		
		$rs = $newsObj->updateRecords($newsData);
		if($rs){
			$successMsg ="news Details Updated successfully";			
			header('Location:newslist.php?&successMsg='.$successMsg);
			exit;
		}
		
	// add news details 	
	}else{
		
		$lastOrder = $newsObj->getLastOrder();
		$newsData->news_order = $lastOrder+1;
		
		$rs = $newsObj->addRecords($newsData);
		if($rs){
			$successMsg ="news Details Added successfully";			
			header('Location:newslist.php?successMsg='.$successMsg);
			exit;
		}
	}
}

//$allnews = $newsObj->getAllRecords();

// Include Template 
include_once('../libs/routs.php')

?>