<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
@extract($_REQUEST);
if (empty($catID)) {
	$heading="Add New Category";
}
else {
	$heading="Edit Category";
}
$res=getResult("admin_details"," where user_type='admin'");
$record_type="New Category";
$script_name="addeditcategory.php";
$redirect_script_name="manage_categories.php";
$table_name="category_tbl";
#################### Add Record ##################
if($id=='remove_image'){
	mysqli_query($GLOBALS['dbcon'],"UPDATE $table_name SET $field='' where catID = '$catID'");
	$_SESSION[session_message]=$record_type.' has been updated successfully';
	//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);

	$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
        exit;
}

if($_REQUEST[action]=="add_record" && $catName!="") {

	$catDesc=mysqli_real_escape_string($GLOBALS['dbcon'],$_POST[catDesc]);

	$numCnt=getCount($table_name," where catName='$catName' and parentID='$parentID' and catID!='$catID' and catgType='$catgType' ");
	$count_cat = mysqli_query($GLOBALS['dbcon'],"SELECT * FROM $table_name");
	if(mysqli_num_rows($count_cat) >= $res['category_count']){
		$_SESSION[session_message]=$record_type." count exceeds the allowed limit[$res[category_count]]";
	}else{
		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);

				$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
        exit;
				
			}
			if($_FILES[image][name]!=""){
			$cat=str_replace(" ", "-", $catName);
			$img_name=$cat.date("s");
				
				$img=upload_file($img_name,"image","uploaded-files/category/images");		
			}
		}

		if($_FILES[banner][name]!=""){
			$file_extension=strtolower(getExtension($_FILES[banner][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);

				$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
        exit;
				
			}
			if($_FILES[banner][name]!=""){
				$cat=str_replace(" ", "-", $catName);
				$banner_name=$cat."-banner".date("s");
				$banner=upload_file($banner_name,"banner","uploaded-files/category/banners");	
			}
		}
		if($_FILES[icon][name]!=""){
			$file_extension=strtolower(getExtension($_FILES[icon][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);

				$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
        exit;

			}
			if($_FILES[icon][name]!=""){
				$cat=str_replace(" ", "-", $catName);
				$banner_icon_name=$cat."-banner-icon".date("s");
				$banner_icon=upload_file($banner_icon_name,"icon","uploaded-files/category/icons");	
			}
		}
		if($_FILES[faq_icon][name]!=""){
			$file_extension=strtolower(getExtension($_FILES[faq_icon][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);

				$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
				exit;
			}
			if($_FILES[faq_icon][name]!=""){
				$cat=str_replace(" ", "-", $catName);
				$faq_icon_name=$cat."-faq-icon".date("s");
				$faq_icon=upload_file($faq_icon_name,"faq_icon","uploaded-files/category/icons");	
			}
		}
		$maxRes=db_fetch_array(db_query("select max(catOrder) from $table_name where parentID='$parentID'"));
		$catOrder=$maxRes[0]+1;
		$catalogname=$_FILES[catalog][name];
		$cimg=upload_file($catalogname,"catalog","uploaded_files/catalog_images");	
		$sql=db_query("insert into $table_name set parentID='$parentID', catName='$catName', displayName='$displayName', catgType='$catgType', catDesc='$catDesc', catDesc1='$catDesc1', catImage='$img', catBanner='$banner', catIcon='$banner_icon',faq_icon='$faq_icon',catOrder='$catOrder', pageTitle='$pageTitle', pageKeywords='$pageKeywords',pageDescription2='$pageDescription2', pageDescription='$pageDescription',pageDescription1='$pageDescription1', footerContent='$fcontent', catalogImage='$cimg', catalogLink='$catlink'");
		$_SESSION[session_message]=$record_type.' <span style="color:green;">'.$catName.' </span> has been added successfully';
		//header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);

		$redirect_url = SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
        
		exit;
	}
	}
	
}
################ Update Record ##################

if($_REQUEST[action]=="update_record" && $catName!="" ) {
	$catDesc=mysqli_real_escape_string($GLOBALS['dbcon'],$_POST[catDesc]);
	$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);
		$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
		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);
			$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
			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("s");
			$img=upload_file($img_name,"image","uploaded-files/category/images");
			updateTable($table_name," catImage='$img' where catID='$catID'");
		}
	}
	if($_FILES[banner][name]!=""){
			$categoryRes=getResult($table_name," where catID='$catID'","catImage");
			$banner_name=$categoryRes['catBanner'];
			if($banner_name!="") {
				$banner=SITE_DIR_PATH."/uploaded-files/category/banners/".$banner_name;
				if(file_exists($banner)) {
					unlink($banner);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$banner_name=$cat."-banner".date("s");
			$banner=upload_file($banner_name,"banner","uploaded-files/category/banners");
			updateTable($table_name," catBanner='$banner' where catID='$catID'");
		}
		if($_FILES[icon][name]!=""){
			$categoryRes=getResult($table_name," where catID='$catID'","catIcon");
			$icon_name=$categoryRes['catIcon'];
			if($icon_name!="") {
				$banner=SITE_DIR_PATH."/uploaded-files/category/banners/icons/".$icon_name;
				if(file_exists($banner)) {
					unlink($banner);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$icon=$cat."-banner-icon".date("s");
			$cat_icon=upload_file($icon,"icon","uploaded-files/category/icons");
			updateTable($table_name," catIcon='$cat_icon' where catID='$catID'");
		}
		if($_FILES[faq_icon][name]!=""){
			$categoryRes=getResult($table_name," where catID='$catID'","faq_icon");
			$icon_name=$categoryRes['faq_icon'];
			if($icon_name!="") {
				$banner=SITE_DIR_PATH."/uploaded-files/category/banners/icons/".$icon_name;
				if(file_exists($banner)) {
					unlink($banner);
				}
			}
			$cat=str_replace(" ", "-", $catName);
			$faq_icon1=$cat."-faq-icon".date("s");
			$faq_icon=upload_file($faq_icon1,"faq_icon","uploaded-files/category/icons");
			updateTable($table_name," faq_icon='$faq_icon' where catID='$catID'");
		}
	updateTable($table_name," catName='$catName', displayName='$displayName', catgType='$catgType', catDesc='$catDesc', catDesc1='$catDesc1',pageDescription2='$pageDescription2', pageTitle='$pageTitle', pageKeywords='$pageKeywords', pageDescription='$pageDescription', pageDescription1='$pageDescription1', footerContent='$fcontent', catalogLink='$catlink' 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);
	$redirect_url = SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize;
        echo "<script>window.location.href='$redirect_url';</script>";
        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];
	$displayName=$res[displayName];
	$catDesc=$res[catDesc];
	$catDesc1=$res[catDesc1];
	$catDesc2=$res[pageDescription1];
	$catgType=$res[catgType];
	$pageTitle=$res[pageTitle];
	$pageKeywords=$res[pageKeywords];
	$pageDescription=$res[pageDescription];
	$footerContent=$res[footerContent];
	$catalogLink=$res[catalogLink];
$pageDescription2=$res[pageDescription2];

?>

<table width="100%"  border="0" cellpadding="0" cellspacing="2">
 <tr>
  <td align="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;?>';"></td>
 </tr>
</table><br />

<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
<?=$msg;?>
<table width="100%"  border="0" cellpadding="0" cellspacing="2" class="lightBorder">
 <tr>
  <td class="brdrbtm" width="125"><b>Category Name</b>*</td>
  <td class="brdrbtm">
         <div class="form-group">
   <input name="catName" class="txtfieldadmin" size="70" value="<?=$catName?>" id="NOBLANK~Please enter category name~DM~">
      </div>
  </td>
 </tr>
  <tr>
  <td class="brdrbtm" width="125"><b>Display Name</b>*</td>
  <td class="brdrbtm">
         <div class="form-group">
   <input name="displayName" class="txtfieldadmin" size="70" value="<?=$displayName?>" id="NOBLANK~Please enter category name~DM~">
      </div>
  </td>
 </tr>

 <tr>
  <tr>
  <td class="brdrbtm"><b>Product Meta Title</b></td>
      <td class="brdrbtm">   <div class="form-group"><textarea name="pageTitle"  rows="5" cols="30" class="txtfieldadmin" ><?=$pageTitle?></textarea></div></td>

 </tr>
 <tr>
  <td class="brdrbtm"><b>Product Meta Keyword</b></td>
     <td class="brdrbtm">   <div class="form-group"><textarea name="pageKeywords"  rows="5" cols="30" class="txtfieldadmin" ><?=$pageKeywords?></textarea></div></td>

 </tr>
 <tr>
  <td class="brdrbtm"><b>Product Meta Description</b></td>
     <td class="brdrbtm">   <div class="form-group"><textarea name="pageDescription"  rows="5" cols="30" class="txtfieldadmin" ><?=$pageDescription?></textarea></div></td>

 </tr>
    <tr>
  <td class="brdrbtm"><b>Product Short Description</b></td>
    <td class="brdrbtm">   <div class="form-group"><textarea name="catDesc"  rows="5" cols="30" class="txtfieldadmin" ><?=$catDesc?></textarea></div></td>

 </tr>
  <tr>
  <td class="brdrbtm"><b> Companies Category Description 1</b></td>
      <td class="brdrbtm">   <div class="form-group"><textarea name="catDesc1" id="catDesc1" rows="10" cols="70" class="txtfieldadmin" ><?=$catDesc1?></textarea></div></td>
 </tr>
 <tr>
  <td class="brdrbtm"><b> Companies Category Description 2</b></td>
      <td class="brdrbtm">   <div class="form-group"><textarea name="pageDescription1" id="catDesc2" rows="10" cols="70" class="txtfieldadmin" ><?=$catDesc2?></textarea></div></td>
 </tr>
     <tr>
  <td class="brdrbtm"><b> Companies Category Description 3</b></td>
      <td class="brdrbtm">   <div class="form-group"><textarea name="pageDescription2" id="pageDescription2" rows="10" cols="70" class="txtfieldadmin" ><?=$pageDescription2?></textarea></div></td>
 </tr>

 <tr>
  <td class="brdrbtm" width="150"><b>Category Image</b></td>
  <td class="brdrbtm" >
         <div class="form-group">
   <input name="image" type="file" class="button">
   <?php
   if($res[catImage]!=""){
	   $file1=SITE_DIR_PATH."/uploaded-files/category/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=category/images/<?=$res[catImage];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID?>&id=remove_image&field=catImage">Remove</a></strong>
		   <?php
	   }
   }
   ?>
   <br/><strong>Image Size: Please upload square image for better view.</strong>
      </div>
  </td>
 </tr>
 <tr>
  <td class="brdrbtm" width="150"><b>Category Banner</b></td>
  <td class="brdrbtm" >
         <div class="form-group">
   <input name="banner" type="file" class="button">
   <?php
   if($res[catBanner]!=""){
	   $file1=SITE_DIR_PATH."/uploaded-files/category/banners/".$res[catBanner];
	   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=category/banners/<?=$res[catBanner];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID?>&id=remove_image&field=catBanner">Remove</a></strong>
		   <?php
	   }
   }
   ?>
      </div>
  </td>
 </tr>
 <tr>
  <td class="brdrbtm" width="150"><b>Category Icon</b></td>
  <td class="brdrbtm" >
         <div class="form-group">
   <input name="icon" type="file" class="button">
   <?php
   if($res[catIcon]!=""){
	   $file2=SITE_DIR_PATH."/uploaded-files/category/icons/".$res[catIcon];
	   if(file_exists($file2)){
		   $dimens=GetImageSize($file2);
		   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=category/icons/<?=$res[catIcon];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID?>&id=remove_image&field=catIcon">Remove</a></strong>
		   <?php
	   }
   }
   ?>
      </div>
  </td>
 </tr>

 <tr>
  <td class="brdrbtm" width="150"><b>FAQ Icon</b></td>
  <td class="brdrbtm" >
         <div class="form-group">
   <input name="faq_icon" type="file" class="button">
   <?php
   if($res[faq_icon]!=""){
	   $file2=SITE_DIR_PATH."/uploaded-files/category/icons/".$res[faq_icon];
	   if(file_exists($file2)){
		   $dimens=GetImageSize($file2);
		   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=category/icons/<?=$res[faq_icon];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>&nbsp; <strong><a href="<?= SITE_ADMIN_URL ?>/addeditcategory.php?catID=<?= $catID?>&id=remove_image&field=faq_icon">Remove</a></strong>
		   <?php
	   }
   }
   ?>
      </div>
  </td>
 </tr>
 
<!--
  <tr>
  <td class="brdrbtm"><b> Footer Content</b></td>
  <td class="brdrbtm"><textarea name="fcontent" id="fcontent" rows="10" cols="70" class="txtfieldadmin" ><?=$footerContent?></textarea></td>
 </tr>
<script type="text/javascript">
	var oFCKeditor=new FCKeditor('fcontent');
	oFCKeditor.BasePath="fckeditor/";
	oFCKeditor.Width="600px";
	oFCKeditor.Height="400px";
	oFCKeditor.ReplaceTextarea();
	</script>
-->
<!--
 <tr>
  <td class="brdrbtm" width="150"><b>Catalog Image</b></td>
  <td class="brdrbtm" >
   <input name="catalog" type="file" class="button">
   <?php
   if($res[catalogImage]!=""){
	   $file1=SITE_DIR_PATH."/uploaded_files/catalog_images/".$res[catalogImage];
	   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.php?img=catalog_images/<?=$res[catalogImage];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong></a>
		   <?php
	   }
   }
   ?>
   <br/><strong>Image Size: Please upload square image for better view.</strong>
  </td>
 </tr>
-->
<!--
 <tr>
  <td class="brdrbtm" width="125"><b>Catalog Link</b>*</td>
  <td class="brdrbtm">
   <input name="catlink" class="txtfieldadmin" size="70" value="<?=$catalogLink?>" id="NOBLANK~Please enter category name~DM~">
  </td>
 </tr>
-->
 


</table>
    
    <table width="96%" align="right" cellpadding="0" cellspacing="0" border="0" style="margin-top:15px">
 <tbody><tr>  <td class="brdrbtm" >
   <?php
   if($res[catID]=="") {
	   ?>
	   <input type="submit" name="add_record" class="button" value="Add <?=$record_type;?>"><input type="hidden" name="action" value="add_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" name="update_record" class="button" value="Update <?=$record_type;?>"><input type="hidden" name="action" value="update_record">
	   <?php	   
   }
   ?>
   &nbsp;&nbsp;<input type="button" name="cancel" class="button" value="Cancel" onclick="javascript: window.location.href='<?=SITE_ADMIN_URL;?>/<?=$redirect_script_name;?>?parentID=<?=$parentID;?>&catID=<?=$catID;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>'">   
  </td></tr>
</tbody></table>
 

</form>

<?php
admin_footer();
exit;
?>