<?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 gallery
$gallerysObj =  new  gallerys();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['gallery_id'])){
	$gallerysDetail = $gallerysObj->fromId($_REQUEST['gallery_id']);
}


if(isset($_POST['hiddenSubmit'])){
	
	if(isset($_FILES['gallery_banner']) && $_FILES['gallery_banner']['name']!='' || $_POST['uploaded_file'] != ''){
		if (isset($_FILES['gallery_banner']) && $_FILES['gallery_banner']['name']!=''){
		$image=$_FILES['gallery_banner']['name'];
		$image=time().'_'.$image;
		$extension = strtolower(getExtension($image));
		
		if($extension!='jpg' and $extension!='gif' and $extension!='jpeg' and $extension!='png'){
			header('location:addgallery.php?failedMsg=Invalid image extension.');
			exit;
		}
		
		move_uploaded_file($_FILES['gallery_banner']['tmp_name'], DIR_UPLOAD_BANNER.$image);		
		$gallerysData->gallery_banner	= $image;
		}else{
			$gallerysData->gallery_banner	= $_POST['uploaded_file'];
		}
	}else{
		
		header('location:addgallery.php?failedMsg=Image Field is blank.');
		exit;

	}
	
	$gallerysData->gallery_title 		= $_POST['gallery_title'];
	$gallerysData->on_sponcer 		= $_POST['on_sponcer'];
	$gallerysData->on_gallery 		= $_POST['on_gallery'];
	$gallerysData->on_client_logo 		= $_POST['on_client_logo'];
	
	$gallerysData->gallery_status 		= $_POST['gallery_status'];	
	
	// edit gallerys details 
	if(isset($_POST['gallery_id'])){

		$gallerysData->gallery_id = $_POST['gallery_id'];
		
		$galleryD = $gallerysObj->fromId($gallerysData->gallery_id);
		
		if(isset($_FILES['gallery_banner']) && $_FILES['gallery_banner']['name']!=''){
			if($galleryD->gallery_banner!='' && file_exists(DIR_UPLOAD_BANNER.$galleryD->gallery_banner)){
				unlink(DIR_UPLOAD_BANNER.$galleryD->gallery_banner);
			}
		}	
		
		$rs = $gallerysObj->updateRecords($gallerysData);
		if($rs){
			$successMsg ="gallery Details Updated successfully";			
			header('Location:gallerylist.php?&successMsg='.$successMsg);
			exit;
		}
		
	// add gallerys details 	
	}else{
		
		$lastOrder = $gallerysObj->getLastOrder();
		$gallerysData->gallery_order = $lastOrder+1;
		
		$rs = $gallerysObj->addRecords($gallerysData);
		if($rs){
			$successMsg ="gallery Details Added successfully";			
			header('Location:gallerylist.php?successMsg='.$successMsg);
			exit;
		}
	}
}

//$allgallerys = $gallerysObj->getAllRecords();

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

?>