<?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 testimonial
$testimonialObj =  new  testimonial();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['testimonial_id'])){
	$testimonialDetail = $testimonialObj->fromId($_REQUEST['testimonial_id']);
}


if(isset($_POST['hiddenSubmit'])){
	if(isset($_FILES['testimonial_image']) && $_FILES['testimonial_image']['name']!=''){
		$image=$_FILES['testimonial_image']['name'];
		$image=time().'_'.$image;
		$extension = strtolower(getExtension($image));
		
		if($extension!='jpg' and $extension!='gif' and $extension!='jpeg' and $extension!='png'){
			header('location:addpage.php?$failedMsg=Invalid image extension.');
			exit;
		}
		
		move_uploaded_file($_FILES['testimonial_image']['tmp_name'], DIR_UPLOAD_BANNER.$image);		
		$testimonialData->testimonial_image	= $image;	
	}else{
		$testimonialData->testimonial_image	= $_POST['uploaded_file'];	
	}
	
	$testimonialData->testimonial_title 		= $_POST['testimonial_title'];	
	$testimonialData->testimonial_desc 		= $_POST['testimonial_desc'];
	$testimonialData->testimonial_company 		= $_POST['testimonial_company'];
	$testimonialData->testimonial_meta_title 	= '';	
	$testimonialData->testimonial_meta_keywords 	= '';	
	$testimonialData->testimonial_meta_desc 		= '';
	$testimonialData->testimonial_status 		= $_POST['testimonial_status'];	
	//print_r($testimonialData); exit;
	// edit testimonial details 
	if(isset($_POST['testimonial_id'])){

		$testimonialData->testimonial_id = $_POST['testimonial_id'];
		
		$testimonialD = $testimonialObj->fromId($testimonialData->testimonial_id);
		
		if(isset($_FILES['testimonial_image']) && $_FILES['testimonial_image']['name']!=''){
			if($testimonialD->testimonial_image!='' && file_exists(DIR_UPLOAD_BANNER.$testimonialD->testimonial_image)){
				unlink(DIR_UPLOAD_BANNER.$testimonialD->testimonial_image);
			}
		}	
		
		$rs = $testimonialObj->updateRecords($testimonialData);
		if($rs){
			$successMsg ="testimonial Details Updated successfully";			
			header('Location:testimoniallist.php?&successMsg='.$successMsg);
			exit;
		}
		
	// add testimonial details 	
	}else{
		
		
		
		$rs = $testimonialObj->addRecords($testimonialData);
		if($rs){
			$successMsg ="testimonial Details Added successfully";			
			header('Location:testimoniallist.php?successMsg='.$successMsg);
			exit;
		}
	}
}

//$alltestimonial = $testimonialObj->getAllRecords();

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

?>