<?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 page
$pagesObj =  new  pages();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['page_id'])){
	$pagesDetail = $pagesObj->fromId($_REQUEST['page_id']);
}
$parentDetails = $pagesObj->getAllRecords('*',' page_parent=0'," ORDER BY page_order");


if(isset($_POST['hiddenSubmit'])){
	
	if(isset($_FILES['page_banner']) && $_FILES['page_banner']['name']!=''){
		$image=$_FILES['page_banner']['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['page_banner']['tmp_name'], DIR_UPLOAD_BANNER.$image);		
		$pagesData->page_banner	= $image;	
	}else{
		$pagesData->page_banner	= $_POST['uploaded_file'];	
	}
	
	$pagesData->page_title 		= $_POST['page_title'];	
	$pagesData->page_slug 		= $_POST['page_slug'];	
	$pagesData->page_desc 		= $_POST['page_desc'];
	
	$pagesData->page_meta_title 	= $_POST['page_meta_title'];	
	$pagesData->page_meta_keywords 	= $_POST['page_meta_keywords'];	
	$pagesData->page_meta_desc 		= $_POST['page_meta_desc'];
	$pagesData->on_footer 		= $_POST['on_footer'];	
	$pagesData->on_header 		= $_POST['on_header'];	
	$pagesData->on_home 		= $_POST['on_home'];	
	$pagesData->page_parent = $_POST['page_parent'];
	$pagesData->page_status 		= $_POST['page_status'];	
	
	// edit pages details 
	if(isset($_POST['page_id'])){

		$pagesData->page_id = $_POST['page_id'];
		
		$pageD = $pagesObj->fromId($pagesData->page_id);
		
		if(isset($_FILES['page_banner']) && $_FILES['page_banner']['name']!=''){
			if($pageD->page_banner!='' && file_exists(DIR_UPLOAD_BANNER.$pageD->page_banner)){
				unlink(DIR_UPLOAD_BANNER.$pageD->page_banner);
			}
		}	
		
		$rs = $pagesObj->updateRecords($pagesData);
		if($rs){
			$successMsg ="page Details Updated successfully";			
			header('Location:pagelist.php?&successMsg='.$successMsg);
			exit;
		}
		
	// add pages details 	
	}else{
		
		$lastOrder = $pagesObj->getLastOrder();
		$pagesData->page_order = $lastOrder+1;
		
		$rs = $pagesObj->addRecords($pagesData);
		if($rs){
			$successMsg ="page Details Added successfully";			
			header('Location:pagelist.php?successMsg='.$successMsg);
			exit;
		}
	}
}

//$allpages = $pagesObj->getAllRecords();

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

?>