<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
@extract($_REQUEST);

if (empty($catID)) {
	$heading="Add New Name";
}
else {
	$heading="Edit Category";
}

$record_type="Standard Meta Tags";
$script_name="addeditstandardmetatags.php";
$redirect_script_name="manage_standardmetatags.php";
$table_name="standard_meta_tags_pages";
#################### Add Record ##################

if($_REQUEST['action']=="add_record" && $catName!="") {
	$catDesc=$_POST['catDesc'];
$catDesc1=$_POST['catDesc1'];
	$numCnt=getCount($table_name," where catName='$catName' and parentID='$parentID' and catID!='$catID' and catgType='$catgType' ");
	if($numCnt>0) {
		$_SESSION['session_message']=$record_type.' already exist';
	}
	else{
		if($_FILES['image']['name']!=""){
			$file_extension=strtolower(getExtension($_FILES['image']['name']));
			if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png"){
				$_SESSION['session_message']='Please upload image (.jpg,.gif,.png Format Only)';
				header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
				exit;
			}
			if($_FILES['image']['name']!=""){
			$cat=str_replace(" ", "-", $catName);
			$img_name=$cat.date("d_m_Y_H_i_s");
				
				$img=upload_file($img_name,"image","uploaded_files/category_images");	
				
				$width="100";
				$height="100";
				$source_folder=SITE_DIR_PATH."/uploaded_files/category_images/";
				$destination_folder=SITE_DIR_PATH."/uploaded_files/category_images/thumbs/";
				saveResizedImage($img, $width, $height, $source_folder, $destination_folder);			
			}
		}
		
		if($_FILES['image1']['name']!=""){
			$cat=str_replace(" ", "-", $catName);
			$img_name1=$cat.date("d_m_Y_H_i_s");
				
				$img1=upload_file($img_name1,"image1","uploaded_files/feature_images");	
				
				$width="100";
				$height="100";
				$source_folder=SITE_DIR_PATH."/uploaded_files/feature_images/";
				$destination_folder=SITE_DIR_PATH."/uploaded_files/feature_images/thumbs/";
				saveResizedImage($img1, $width, $height, $source_folder, $destination_folder);			
			}
			
		$maxRes=mysqli_fetch_array(mysqli_query($dbconn,"select max(catOrder) from $table_name where parentID='$parentID'"));
		$catOrder=$maxRes[0]+1;
		$sql=db_query("insert into $table_name set parentID='$parentID', catName='$catName', catgType='$catgType', catOrder='$catOrder', pageTitle='$pageTitle', pageKeywords='$pageKeywords', pageDescription='$pageDescription'");
		$_SESSION['session_message']=$record_type.' has been added successfully';
		header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
		exit;
	}
}

################ Update Record ##################

if($_REQUEST['action']=="update_record" && $catName!="" ) {
	$catDesc=mysqli_real_escape_string($_POST['catDesc'],true);
$catDesc1=mysqli_real_escape_string($_POST['catDesc1'],true);
	$numCnt=getCount($table_name," where catName='$catName' and parentID='$parentID' and catID!='$catID' and catgType='$catgType'");
	if($numCnt>0) {
		$_SESSION['session_message']=$record_type.' already exist';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
		exit;
	}
	
	if($_FILES['image']['name']!=""){
		$file_extension=strtolower(getExtension($_FILES['image']['name']));
		if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png"){
			$_SESSION['session_message']='Please upload image (.jpg,.gif,.png Format Only)';
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
			exit;
		}
		if($_FILES['image']['name']!=""){
			$categoryRes=getResult($table_name," where catID='$catID'","catImage");
			$filename=$categoryRes['catImage'];
			if($filename!="") {
				$file=SITE_DIR_PATH."/uploaded_files/category_images/".$filename;
				if(file_exists($file)) {
					unlink($file);
				}
				
				$file_thumb=SITE_DIR_PATH."/uploaded_files/category_images/thumbs/".$filename;
				if(file_exists($file_thumb)) {
					unlink($file_thumb);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$img_name=$cat.date("d_m_Y_H_i_s");
			$img=upload_file($img_name,"image","uploaded_files/category_images");
			updateTable($table_name," catImage='$img' where catID='$catID'");
			
			$width="100";
			$height="100";
			$source_folder=SITE_DIR_PATH."/uploaded_files/category_images/";
			$destination_folder=SITE_DIR_PATH."/uploaded_files/category_images/thumbs/";
			saveResizedImage($img, $width, $height, $source_folder, $destination_folder);
		}
	}
	
	
	
	
	if($_FILES['image1']['name']!=""){
			$categoryRes=getResult($table_name," where catID='$catID'","featureimage");
			$filename=$categoryRes['featureimage'];
			if($filename!="") {
				$file=SITE_DIR_PATH."/uploaded_files/feature_images/".$filename;
				if(file_exists($file)) {
					unlink($file);
				}
				
				$file_thumb=SITE_DIR_PATH."/uploaded_files/feature_images/thumbs/".$filename;
				if(file_exists($file_thumb)) {
					unlink($file_thumb);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$img_name1=$cat.date("d_m_Y_H_i_s");
			$img1=upload_file($img_name1,"image1","uploaded_files/feature_images");
			updateTable($table_name," featureimage='$img1' where catID='$catID'");
			
			$width="100";
			$height="100";
			$source_folder=SITE_DIR_PATH."/uploaded_files/feature_images/";
			$destination_folder=SITE_DIR_PATH."/uploaded_files/feature_images/thumbs/";
			saveResizedImage($img, $width, $height, $source_folder, $destination_folder);
		}
	
	
		
	updateTable($table_name," catName='$catName', catgType='$catgType', pageTitle='$pageTitle', pageKeywords='$pageKeywords', pageDescription='$pageDescription' where catID='$catID'");
  $_SESSION['session_message']=$record_type.' has been updated successfully';
	header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}
########################################################
admin_header();
?>

<div align="left" class="myTitle"><?=$heading;?></div>
<hr size="2" width="100%" color="#92B2D6">
<?php
print_message();
$res=getResult($table_name," where catID='$catID'");

	$catName=$res['catName'];
	$catDesc=$res['catDesc'];
	$catDesc1=$res['catDesc1'];
	$catgType=$res['catgType'];
	$pageTitle=$res['pageTitle'];
	$pageKeywords=$res['pageKeywords'];
	$pageDescription=$res['pageDescription'];

    $catgType;

?>

<div class="text-right">
    <input type="button" class="btn btn-primary" value="Go Back To Manage <?=$record_type;?>" onclick="window.location.href='<?=SITE_ADMIN_URL?>/<?=$redirect_script_name;?>?parentID=<?=$parentID;?>&catID=<?=$catID;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>';">
</div>
<br />

<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
<?=$msg;?>
    
<div class="form-group">
      <label>Add Name <span>*</span></label>
    <input name="catName" class="form-control"  value="<?=$catName?>" placeholder="Add Name">
</div>  
    
<div class="form-group">
    <label>Mata Title <span>*</span></label>
     <input type="text"  name="pageTitle"   class="form-control" value="<?=$pageTitle?>">
</div>  
<div class="form-group">
    <label>Mata Keyword <span>*</span></label>
   <input type="text"  name="pageKeywords"   class="form-control" value="<?=$pageKeywords?>">
</div>   
<div class="form-group">
    <label>Mata Description <span>*</span></label>
   <textarea name="pageDescription" rows="5" cols="30" class="form-control" ><?=$pageDescription?></textarea>
</div> 
<div class="form-group">
    <?php
   if($res['catID']=="") {
	   ?>
	   <input type="submit" name="add_record" class="btn btn-primary" value="Add <?=$record_type;?>"><input type="hidden" name="action" value="add_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" name="update_record" class="btn btn-primary" value="Update <?=$record_type;?>"><input type="hidden" name="action" value="update_record">
	   <?php	   
   }
   ?>
   &nbsp;&nbsp;<input type="button" name="cancel" class="btn btn-warning" value="Cancel" onclick="javascript: window.location.href='<?=SITE_ADMIN_URL;?>/<?=$redirect_script_name;?>?parentID=<?=$parentID;?>&catID=<?=$catID;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>'">   
</div>  

</form>

<?php
admin_footer();
exit;
?>