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

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=100:$pagesize;
if($parentID=="") {
	$parentID=0;
}

$heading="Manage Categories";
$record_type="Category";
$script_name="manage_categories.php";
$table_name="category_tbl";
#################### Add Record ##################
if($_REQUEST['action']=="add_record" && $catName!="") {
	$count_category=mysqli_query($dbconn,"select * from $table_name ");
	
		$sql=mysqli_query($dbconn,"select * from $table_name where catName='$catName' and parentID='$parentID'");
		$num=mysqli_num_rows($sql);
		if($num>0) {
		$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' already exist  </div>';
        
		}
		else{
			if($_FILES['image']['nam']!="") {
				$file_extension=strtolower(getExtension($_FILES['image']['name']));
				if($_FILES['image']['name']!="") {
					$img_name=$catName.date("s");
					$img=upload_file($img_name,"image","uploaded-files/category/images");
					
					$width="50";
					$height="50";
					$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['banner']['name']!="") {
				$file_extension=strtolower(getExtension($_FILES['banner']['name']));
				if($_FILES['banner']['name']!="") {
					$banner_name=$catName."-banner".date("s");
					$banner=upload_file($banner_name,"banner","uploaded-files/category/banners");
				}
			}
			$maxRes=db_fetch_array(db_query("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', catDesc='$catDesc', catImage='$img', catOrder='$catOrder', catBanner='$banner'");
			$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
            
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
			exit;
		}
	

	
}

####################################################

if($action=="Update") {
	while (list($key,$val)=each($_POST[])) {
		if (substr($key,0,6)=="order_") {
			$key1 = substr($key,6,strlen($key)-6);
			$qry1=db_query("update $table_name set catOrder='$val' where catID='$key1'");
		}
	}
	$_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($id=="delete_record") {		
		$categoryRes=getResult($table_name," where catID='$catID'");
		$filename=$categoryRes['catImage'];
		$bannername=$categoryRes['catBanner'];
		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);
			}
			$file_banner=SITE_DIR_PATH."/uploaded-files/category/banners/".$filename;
			if(file_exists($file_thumb)) {
				unlink($file_thumb);
			}
		}			
		db_query("delete from $table_name where catID='$catID'");
		$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been deleted successfully  </div>';	
        
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;	 
}



if($id=="deactivate_record") {
	updateTable($table_name," status='N' where catID='$catID' and status='Y' ");
	$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been deactivated successfully  </div>';
    
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}

if($id=="activate_record") {
	updateTable($table_name," status='Y' where catID='$catID' and status='N' ");
	$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been activated successfully  </div>';
    
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}

if($id=="set_top_business_category") {
	updateTable($table_name," topBusinessCategory='Y', status='Y' where catID='$recordID'");
	$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been set as top business category successfully  </div>';
    
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}
if($id=="change_status") {
	mysqli_query($dbconn,"UPDATE $table_name SET status='$status' 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(isset($home_cat_set)){
	foreach($cat_arr as $id){mysqli_query($dbconn,"UPDATE $table_name SET home_category = '1' where catID='$id'");}
	$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been set as Home Category  </div>';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
		exit;
}
if(isset($home_cat_unset)){
	foreach($cat_arr as $id){
		mysqli_query($dbconn,"UPDATE $table_name SET home_category = '0' where catID='$id'");
	}
	$_SESSION['session_message']='<div class="btn btn-warning">' . $record_type . ' has been unset as Home Category  </div>';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
		exit;
}

if($id=="unset_top_business_category") {
	
	$homeCnt=getCount($table_name," where topBusinessCategory='Y' and parentID='0'");
	// if ($homeCnt < 7) {
	// 	$_SESSION['session_message']=$record_type.' is required hence can not be unset as top business category.';
	// 	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	// 	exit;
	// }
	
	updateTable($table_name," topBusinessCategory='N' where catID='$recordID'");
	$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been unset as top business category successfully.</div>';
    
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;	
}

########################################################
admin_header("Manage $record_type");	
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pagesize_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">Select</option>
	     <?php
	     foreach($_GLOBALS['pgsize_arr'] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>
	    <input type="hidden" name="catID" value="<?=$catID;?>">
	    <input type="hidden" name="parentID" value="<?=$parentID;?>">
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
<?php
##################Product Search Option###################
?>
<form name="srch" method="post" action="" style="display:inline" enctype="multipart/form-data">
<table width="100%"  border="0" cellpadding="0" cellspacing="2">
 <tr>
  <td align="right"><b>Search Category </b> [by name etc.] :-
   <input type="text" name="keyword" value="" size="40" class="search-input-xs">
   <input type="submit" class="button" value="GO">   
  </td>
 </tr>   
</table>
</form>

<?php
$cond="";
if (strlen($keyword)) {
	?>
	<br/>
	<div style="width:99%;">
	 <div style="float:left;"><strong>Search For :</strong> <?=$keyword;?></div>
	 <div style="float:right;"><a href="javascript:history.go(-1);"><strong>Go Back</strong></a></div>
	</div> 
	<?php
	$cond=" and (catName like '%$keyword%' or catDesc like '%$keyword%')";
}
$qry1= mysqli_query($dbconn,"select * from $table_name where parentID='$parentID' $cond ");
$reccnt = mysqli_num_rows($qry1);
$qry= mysqli_query($dbconn,"select * from $table_name where parentID='$parentID' $cond order by catID asc, catName asc limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>
<table width="100%"  border="0" cellpadding="0" cellspacing="2">
 <tr>
  <td>
   <?php
   if($parentID!="" && $parentID!=0) {
	   page_nav($parentID);
   }
   ?>
  </td>
  <td align="right">
   <input type="button" onclick="window.location.href='<?=SITE_ADMIN_URL;?>/addeditcategory.php?parentID=<?=$parentID;?>'" value="Add New <?=$record_type;?>" class="button add-category-btn">
  </td>
 </tr>
</table><br />

<form name="formn" method="post" action="" enctype="multipart/form-data">
    <div class="table-responsive">
<table width="100%"  border="0" cellpadding="2" cellspacing="2" class="lightBorder table table-bordered table-hover">
 <?php if($reccnt>0) { ?>
 <tr bgcolor="#EAEAEA">
  <td></td>
  <td width="5%" height="20" align="left"><strong style="
    color: #3331ca;">S.No.</strong></td>
  <td width="40%" align="left"><strong style="
    color: #3331ca;">Category Name</strong></td>
  <td width="15%" align="center"><strong style="
    color: #3331ca;">Category Image</strong></td>
  <td width="20%" align="center"><strong style="
    color: #3331ca;">Top Business Category</strong></td>
  <td width="5%" align="center"><strong style="
    color: #3331ca;">Order</strong></td>
  <td width="15%" align="left"><strong style="
    color: #3331ca;">Options</strong></td>
 </tr>
 
 <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) {
	 $catID = $arr['catID'];
	 $sub_cat_query = mysqli_query($dbconn,"SELECT * from $table_name where parentID = '$catID'");
	 $faq_query = mysqli_query($dbconn,"SELECT * from faq_tbl where category_id = '$catID'");
	 if($sl%2!=0) {
		 $color="#f9f9f9";
	 }
	 else{
		 $color="#f0f0f0";
	 }
	 
	 $catgType1=$arr['catgType'];
	 
	  if($catgType1=='0') {
		 $catgType2="Products";
	 }
	 	  if($catgType1=='1') {
		 $catgType2="Services";
	 }
	 
	 	 	  if($catgType1=='2') {
		 $catgType2="Courses";
	 }
?>
	 
	 <tr  bgcolor="<?=$color;?>">
         <td><label class="container1"><input type="checkbox" name="cat_arr[]" value="<?= $arr['catID'] ?>" />  <span class="checkmark"></span></label></td>
	  <td align="left"><?=$sl;?>.</td>
	  <td align="left" class="<?=$class?>">
	   <b><?=$arr['catName'];?></b>   &nbsp;&nbsp;[<?= $catgType2;?>]
	   <?php
	   if($arr['status']=="N") {
		   echo "&nbsp;<span class='icon'>Inactive</span>";
	   }
	   echo ($arr['home_category'] == '1') ? 'Set As Home Category' : "";?>

	   	<br/><a href="<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?parentID=<?=$arr['catID'];?>">Sub Categories[<?= mysqli_num_rows($sub_cat_query) ?>]</a>&nbsp; 
	   	<a href="<?=SITE_ADMIN_URL;?>/manage_faq.php?category_id=<?=$arr['catID'];?>">FAQ[<?= mysqli_num_rows($faq_query) ?>] </a>
   		<a href="<?=SITE_ADMIN_URL;?>/manage_location_content.php?category_id=<?=$arr['catID'];?>">Sub Domain Content </a>
	 

	  </td>
	  <td class="<?=$class?>" align="center">
	   <?php
	   if($arr['catImage']!="") {
		   $file1=SITE_DIR_PATH."/uploaded-files/category/images/".$arr['catImage'];

			   $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/<?=$arr['catImage'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>View Image</strong>
			   <?php
	
	   }
	   ?>	   
	  </td>
	  <td align="center" valign='top'>
	   <?php
	   if ($arr['topBusinessCategory']=="N") {
		   ?>
		   <strong>No</strong>&nbsp;&nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=set_top_business_category&start=<?=$start;?>&pagesize=<?=$pagesize;?>&recordID=<?=$arr['catID'];?>"><strong>Set</strong></a>
		   <?php
	   }
	   else {
		   ?>
		   <strong>Yes</strong>&nbsp;&nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=unset_top_business_category&start=<?=$start;?>&pagesize=<?=$pagesize;?>&recordID=<?=$arr['catID'];?>"><strong>Unset</strong></a>
		   <?php
	   }
	   ?>
	  </td>
	  <td class="<?=$class?>" align="center"><input name="order_<?=$arr['catID'];?>" class="txtfieldadmin" type="text" size="2" value="<?=$arr['catOrder'];?>"></td>
	  <td class="<?=$class?>" align="left">
          <a href="<?=SITE_ADMIN_URL?>/addeditcategory.php?start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><i class="fa fa-edit"></i></a>
	   <?php if($arr['status']=='Y' && $parentID!=0) { ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate_record&start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/active.gif" alt="Deactivate This <?=$record_type;?>" title="Deactivate This <?=$record_type;?>" border="0"></a>
	   <?php }
	   if($arr['status']=='N' && $parentID!=0) { ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate_record&start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><img src="<?=SITE_ADMIN_URL?>/images/deactive.gif" alt="Activate This <?=$record_type;?>" title="Activate This <?=$record_type;?>" border="0"></a>
	   <?php } ?>
	   
	   <?php /*if(!anyreference($arr[catID]) && $parentID!=0) { */ ?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=delete_record&start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>" onClick="return confirm('Are you sure to delete this <?=$record_type;?>...');"><i class="fa fa-trash"></i></a>
	   <?php /*} */?>
	   &nbsp;<a href="<?=SITE_ADMIN_URL?>/manage_meta_tags_category.php?start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>"><i class="fa fa-briefcase"></i></a>
	   <?php if($arr['status'] == 'Y'){?>
	    	&nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=change_status&status=N&start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>">Inactive</a>
		<?php }else{ ?>
			&nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=change_status&status=Y&start=<?=$start;?>&catID=<?=$arr['catID'];?>&parentID=<?=$parentID;?>&pagesize=<?=$pagesize;?>">Active</a>
		<?php }?>
	  </td>
	 </tr>
	 <?php
	 $sl++;
 }
 ?>

 <?php } else{ ?>
 <tr><td><b>No Record Found........</b></td></tr>
 <?php } ?>
</table>
    </div>
    <table width="96%" align="center" cellpadding="0" cellspacing="0" border="0">
 <tr class="btn-content"><td align="right"></td> <td class="'.$class.'" align="right">
   <input type="submit" name="action" value="Update" class="button">
   <input type="submit" name="home_cat_set" value="Set as Home Category" class="button">
   <input type="submit" name="home_cat_unset" value="Unset as Home Category" class="button">
<!--     <input type="button" onclick="window.location.href='<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?id=add_new#bot'" value="Add New <?=$record_type;?>" class="button">-->
  </td></tr>
</table>
</form><br/>



<?php
if ($id == "add_new" || $id == "edit_record") {
	?>
	<a name="bot"></a>
	<br/><br/> 
	<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<?php echo $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">
	   <input name="catName" class="txtfieldadmin" size="70" value="<?=$catName?>" id="NOBLANK~Please enter category name~DM~">
	  </td>
	 </tr>
	 <!--
	 <tr>
	  <td class="brdrbtm"><b>Category Description</b></td>
	  <td class="brdrbtm"><textarea name="catDesc" rows="10" cols="70" class="txtfieldadmin" ><?=$catDesc?></textarea></td>
	 </tr>
	 !-->
	 <tr>
	  <td class="brdrbtm" width="150"><b>Category Image</b></td>
	  <td class="brdrbtm" >
	   <input name="image" type="file" class="button">
	   <?php
	   if($res['catImage']!="") {
		   $file1=SITE_DIR_PATH."/uploaded_files/category_images/".$res['catImage'];
		 
			   $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=category_images/<?=$res['catImage'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>&nbsp;View Image</strong>
			   <?php
		   
	   }
	   ?>
	   <br/><strong>Image Size: Please upload square image for better view.</strong>
	  </td>
	 </tr>

	 <tr>
	  <td></td>
	  <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
	   }
	   ?>
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	   &nbsp;&nbsp;<input type="button" name="cancel" class="button" value="Cancel" onclick="javascript: window.location.href='<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?parentID=<?=$parentID;?>&catID=<?=$catID;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>'">
	  </td>
	 </tr>
	</table>
	</form>
	<?php
}

admin_footer();
exit;
?>