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

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

$record_type="blog";
$script_name="addeditblog.php";
$redirect_script_name="manage_blog.php";
$table_name="blog_tbl";
#################### Add Record ##################
if($id=='remove_image'){
	db_query("UPDATE $table_name SET $field='' where catID = '$catID'");
	$_SESSION['session_message']='<div class"btn btn-success">'.$record_type.' has been updated successfully</div>';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}
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']='<div class="btn btn-success">Please upload image (.jpg,.gif,.png Format Only)</div>';
				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("s");
				$img=upload_file($img_name,"image","uploaded-files/blog/images");	
			}
		}		
		if($_FILES['image1']['name']!=""){
			$cat=str_replace(" ", "-", $catName);
			$img_name1="b1".date("s");				
			$img1=upload_file($img_name1,"image1","uploaded-files/blog/banners");	
		}
		if($_FILES['image2']['name']!=""){
			$cat=str_replace(" ", "-", $catName);
			$img_name2="b2".date("s");
			$img2=upload_file($img_name1,"image2","uploaded-files/blog/banners");	
		}
		
		$maxRes=db_fetch_array(db_query("select max(catOrder) from $table_name where parentID='$parentID'"));
		$catOrder=$maxRes[0]+1;
		if($blogDname == ''){
			$blogDname = $catName;
		}
		$sql=db_query("insert into $table_name set parentID='$parentID', catName='$catName',blogDname='$blogDname',blog_written='$blog_written', catgType='$catgType', catDesc='$catDesc', catDesc1='$catDesc1', catImage='$img', banner1='$img1', catOrder='$catOrder', pageTitle='$pageTitle', pageKeywords='$pageKeywords', pageDescription='$pageDescription', blogDate='$blog_date'");
		$_SESSION['session_message']='<div class"btn btn-success">'.$record_type.' has been updated successfully</div>';
		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=$_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']='<div class="btn btn-warning">'.$record_type.' already exist</div>';
		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']='<div class="btn btn-warning">Please upload image (.jpg,.gif,.png Format Only)</div>';
			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/blog/images/".$filename;
				if(file_exists($file)) {
					unlink($file);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$img_name=$cat.date("s");
			$img=upload_file($img_name,"image","uploaded-files/blog/images");
			updateTable($table_name," catImage='$img' where catID='$catID'");
		}
	}
	if($_FILES['image1']['name']!=""){
			$categoryRes=getResult($table_name," where catID='$catID'","banner1");
			$filename=$categoryRes['banner1'];
			if($filename!="") {
				$file=SITE_DIR_PATH."/uploaded-files/blog/banners/".$filename;
				if(file_exists($file)) {
					unlink($file);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$img_name1="b1".date("his");
			$img1=upload_file($img_name1,"image1","uploaded-files/blog/banners");
			updateTable($table_name," banner1='$img1' where catID='$catID'");
		}

if($_FILES['image2']['name']!=""){
			$categoryRes=getResult($table_name," where catID='$catID'","banner2");
			$filename=$categoryRes['banner2'];
			if($filename!="") {
				$file=SITE_DIR_PATH."/uploaded-files/blog/banners/".$filename;
				if(file_exists($file)) {
					unlink($file);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$img_name2="b2".date("s");
			$img2=upload_file($img_name2,"image2","uploaded-files/blog/banners");
			updateTable($table_name," banner2='$img2' where catID='$catID'");
		}
		if($blogDname == ''){
			$blogDname = $catName;
		}
	updateTable($table_name," catName='$catName',blogDname='$blogDname',blog_written='$blog_written', catgType='$catgType', catDesc='$catDesc', catDesc1='$catDesc1', pageTitle='$pageTitle', pageKeywords='$pageKeywords', pageDescription='$pageDescription', blogDate='$blog_date' where catID='$catID'");
  $_SESSION['session_message']='<div class="btn btn-success">'.$record_type.' has been updated successfully</div>';
	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'];
    $blog_written=$res['blog_written'];
    $blog_date=$res['blogDate'];
    $blogDname=$res['blogDname'];

    $catgType=$res['catgType'];
	$pageTitle=$res['pageTitle'];
	$pageKeywords=$res['pageKeywords'];
	$pageDescription=$res['pageDescription'];


?>

<div class="text-right">
    <input type="button" class="button" 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>



<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
<?=$msg;?>
    
   <div class="form-group">
       <label>Blog Name <span>*</span></label>
        <input type="text" name="catName" class="form-control" value="<?=$catName?>" placeholder="Blog category Name">
    </div> 
     <div class="form-group">
       <label>Blog Display Name <span>*</span></label>
        <input type="text" name="blogDname" class="form-control" value="<?=$blogDname?>" placeholder="Blog Display Name">
    </div> 
     <div class="form-group">
       <label>Blog Written By <span>*</span></label>
        <input type="text" name="blog_written" class="form-control" value="<?=$blog_written?>" placeholder="Blog Written by">
    </div> 
     <div class="form-group">
       <label>Blog Post Date <span>*</span></label>
        <input type="date"  name="blog_date" class="form-control" value="<?=$blog_date?>" placeholder="Blog category Name">
    </div> 

    <div class="form-group">
       <label>Blog Meta Title <span>*</span></label>
        <textarea name="pageTitle"  rows="5" cols="30" class="form-control" ><?=$pageTitle?></textarea>
    </div> 
    <div class="form-group">
       <label>Blog Meta Keyword <span>*</span></label>
       <textarea name="pageKeywords"  rows="5" cols="30" class="form-control" ><?=$pageKeywords?></textarea>
    </div>
        <div class="form-group">
       <label>Blog Meta Description <span>*</span></label>
      <textarea name="pageDescription"  rows="5" cols="30" class="form-control" ><?=$pageDescription?></textarea>
    </div> 
        <div class="form-group">
       <label>Blog Short Description <span>*</span></label>
       <textarea name="catDesc"  rows="5" cols="30" class="form-control" ><?= $catDesc?></textarea>
    </div> 
    <div class="form-group">
       <label>Blog Content <span>*</span></label>
      <textarea name="catDesc1" id="catDesc1" rows="10" cols="70" class="form-control" ><?=$catDesc1?></textarea>
    </div> 
    <div class="form-group">
       <label>Blog Image <span>*</span></label>
       <input type="file" name="image" type="file" class="form-control button" >
    <strong>Image Size: Please upload square image for better view.</strong>
    </div> 
    <div class="form-group">
   <?php
   if($res['catImage']!=""){
	   $file1=SITE_DIR_PATH."/uploaded-files/blog/images/".$res['catImage'];
	   if(file_exists($file1)){
		   $dimens=GetImageSize($file1);
		   if($dimens[0]>130){
			   $dimens[0]=$dimens[0]+30;
			   $dimens[1]=$dimens[1]+80;
		   }
		   else{
			   $dimens[0]=200;
			   $dimens[1]=200;
		   }
		   ?>
        <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup_new.php?img=blog/images/<?=$res['catImage'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/<?= $script_name?>?catID=<?= $catID?>&id=remove_image&field=catImage">Remove</a></strong>
		   <?php
	   }
   }
                ?>
    </div>
    <div class="form-group">
       <label>Banner1 </label>
        <input name="image1" type="file" class="form-control">
         <strong>Image Size: Please upload square image for better view.</strong>
    </div> 
      <div class="form-group">
        <?php
   if($res['banner1']!=""){
	   $file1=SITE_DIR_PATH."/uploaded-files/blog/banners/".$res['banner1'];
	   if(file_exists($file1)){
		   $dimens=GetImageSize($file1);
		   if($dimens[0]>130){
			   $dimens[0]=$dimens[0]+30;
			   $dimens[1]=$dimens[1]+80;
		   }
		   else{
			   $dimens[0]=200;
			   $dimens[1]=200;
		   }
		   ?>
		   <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup_new.php?img=blog/banners/<?=$res['banner1'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/<?= $script_name?>?catID=<?= $catID?>&id=remove_image&field=banner1">Remove</a></strong>
		   <?php
	   }
   }
   ?>
    </div> 
    <div class="form-group">
       <label>Banner2</label>
      <input name="image2" type="file" class="form-control">
         <strong>Image Size: Please upload square image for better view.</strong>
    </div>
    <div class="form-group">
          <?php
   if($res['banner2']!=""){
	   $file1=SITE_DIR_PATH."/uploaded-files/blog/banners/".$res['banner2'];
	   if(file_exists($file1)){
		   $dimens=GetImageSize($file1);
		   if($dimens[0]>130){
			   $dimens[0]=$dimens[0]+30;
			   $dimens[1]=$dimens[1]+80;
		   }
		   else{
			   $dimens[0]=200;
			   $dimens[1]=200;
		   }
		   ?>
		   <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup_new.php?img=blog/banners/<?=$res['banner2'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/<?=$script_name?>?catID=<?= $catID?>&id=remove_image&field=banner2">Remove</a></strong>
		   <?php
	   }
   }
   ?>
    </div> 
    <div class="form-group">
           <?php
   if($res['catID']=="") {
	   ?>
	   <input type="submit" name="add_record" class="button btn btn-success" value="Add <?=$record_type;?>">
        <input type="hidden" name="action" value="add_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" name="update_record" class="btn btn-primary btn-lg" value="Update <?=$record_type;?>">
        <input type="hidden" name="action" value="update_record">
	   <?php	   
   }
   ?>
        &nbsp;&nbsp;<input type="button" name="cancel" class="button 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;
?>