<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
 validate_admin();
 
if($_REQUEST['id']!=''){
$sqlcity=$obj->query("select * from tbl_city where status=1");
$cityarr=array();
while($rescity=$obj->fetchNextObject($sqlcity)){
array_push($cityarr,$rescity->city);
}
}
$cityarr=array_values(array_filter($cityarr));

	$maincategory=mysqli_real_escape_string($GLOBALS['conn'],$_POST['maincategory']);
	$parent_id=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['parent_id']);
	$h_onearr=$_REQUEST['h1'];
  	$meta_titlearr=$_REQUEST['metatitle'];
  	$meta_keyarr=$_REQUEST['metakeyword'];
  	$meta_descriptionarr=$_REQUEST['metadescription'];
	$content=mysqli_real_escape_string($_REQUEST['content']);
  
  if($_REQUEST['submitForm']=='yes'){
  	$bay_area_category= (isset($_POST['bay_area_category'])) ? mysqli_real_escape_string($GLOBALS['conn'],$_POST['bay_area_category']) : 0;
   	$nationwide_category= (isset($_POST['nationwide_category'])) ? mysqli_real_escape_string($GLOBALS['conn'],$_POST['nationwide_category']) : 0;
   	$description=mysqli_real_escape_string($GLOBALS['conn'],$_POST['description']);

  	//category update
  	$obj->query("update $tbl_maincategory set is_bay_area_category='$bay_area_category',is_nationwide_category='$nationwide_category',short_description='$description' where id=".$_REQUEST['id']);
  	//end
  
  for($x=0; $x<count($cityarr); $x++){ 
  $sql_meta=$obj->query("select id from tbl_meta where city='$cityarr[$x]' and catid='".$_REQUEST['id']."' and filename='subcategory.php' and storeid=0");

  if($obj->numRows($sql_meta)){
  $result_meta=$obj->fetchNextObject($sql_meta);
  
$obj->query("update tbl_meta set meta_title='$meta_titlearr[$x]',meta_key='$meta_keyarr[$x]',meta_description='$meta_descriptionarr[$x]',h1='$h_onearr[$x]',content='$content[$x]' where id='$result_meta->id'");
  }else{
  $pagetitle="main category page for ".$cityarr[$x];
  $obj->query("insert into tbl_meta set catid='".$_REQUEST['id']."',storeid=0,pagetitle='$pagetitle',filename='subcategory.php',city='$cityarr[$x]', meta_title='$meta_titlearr[$x]',meta_key='$meta_keyarr[$x]',meta_description='$meta_descriptionarr[$x]',h1='$h_onearr[$x]', content='$content[$x]', status=1, canonical='self'");
  }
	  
$_SESSION['sess_msg']='Category updated sucessfully';   
    }
     $section=getSection($parent_id);
    if($section==1)
         {
             $section='grocery';
             $parameter='grocery-category';
             clearlaravelcache($section,$parameter);
         }
   header("location:maincategory-list.php");
   exit();
  } 
  if($_REQUEST['id']!=''){

$sql=$obj->query("select * from $tbl_maincategory where id=".$_REQUEST['id']);

$result=$obj->fetchNextObject($sql);
}     
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo SITE_TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
function validate(obj)
{

if(obj.maincategory.value==''){
alert("Please enter maincategory");
obj.maincategory.focus();
return false;
}
}
</script>
<script type="text/javascript" src="../include/ckeditor/ckeditor.js"></script>

</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php") ?>
<tr>
	<td align="right" class="paddRtLt70" valign="top">
		<table width="99%" border="0" cellspacing="0" cellpadding="0">
			<tr>
			
				<td align="right" valign="top">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td align="left" valign="middle" class="headingbg bodr text14">
					<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Update Category 
					<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View Category"  class="button" onclick="location.href='maincategory-list.php'" /></span></td>
						</tr>
						
						<tr>
							<td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">
                            <form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)">
						<input type="hidden" name="submitForm" value="yes" />
						<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
							<table width="100%" cellpadding="0" cellspacing="0">
									<tr>
										<td align="center" colspan="2" class="paddRt14 paddBot11">
										<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']='';?></strong></font></td>
									</tr>
									<tr>
										<td width="18%" align="right" class="paddBot11 paddRt14">&nbsp;</td>
										<td width="82%" align="left" class="paddBot11"></td>
									</tr>
								<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Parent Category:</strong></td>
									  <td align="left" class="paddBot11"><select name="parent_id" style="width:240px;">
                                      <option value="">-Select Parent-</option>
                                      <?php 
									  //$catArr=$obj->query("select * from $tbl_maincategory where status=1 order by parent_id");
									  $catArr=$obj->query("select 	c1.id, CASE WHEN c1.parent_id = 0 OR c2.id IS NULL THEN c1.maincategory 
										  									ELSE CONCAT(c2.maincategory, ' >> ', c1.maincategory) END maincategory_merged
																from 	tbl_maincategory c1
																		LEFT JOIN tbl_maincategory c2 on c1.parent_id = c2.id and c2.status = 1
																where 	c1.status=1
																order by maincategory_merged;");

									   while($resultP=$obj->fetchNextObject($catArr)){?>
										<option value="<?php echo stripslashes($resultP->id); ?>" <?php if($resultP->id==$result->parent_id){?>selected<?php }?>>
										<?php 
											echo $resultP->maincategory_merged
											//echo getCategoryTree($resultP->id,$array=''); 
										?></option>   
										<?php } ?>                                      
                                      </select></td>
							  </tr>
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Category:</strong></td>
									  <td align="left" class="paddBot11"><input name="maincategory" type="text" id="maincategory" size="36" value="<?php echo stripslashes($result->maincategory);?>" /></td>
							  </tr>

							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Available for Bay Area:</strong></td>
									  <td align="left" class="paddBot11"><input name="bay_area_category" type="checkbox" value="1" <?php echo ((int) $result->is_bay_area_category == 1) ? 'checked' : ''; ?> /></td>
							  </tr>

							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Available for Nationwide:</strong></td>
									  <td align="left" class="paddBot11"><input name="nationwide_category" type="checkbox" value="1" <?php echo ((int) $result->is_nationwide_category == 1) ? 'checked' : ''; ?> /></td>
							  </tr>

							  <tr>
				                  <td align="right" class="paddBot11 paddRt14"><strong>Description:</strong></td>
				                  <td align="left" class="paddBot11">
				                     <textarea name="description"  class="ckeditor" id="description" rows="5" cols="40"><?php echo stripslashes($result->short_description); ?></textarea>
				                  </td>
				               </tr>
							  
							  <?php for($x=0; $x<count($cityarr); $x++){ 
							  $sql_meta=$obj->query("select h1 from tbl_meta where city='$cityarr[$x]' and catid='".$_REQUEST['id']."' and filename='subcategory.php' and storeid=0");
$result=$obj->fetchNextObject($sql_meta);?>
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong><?php echo ucwords($cityarr[$x]); ?> H1 Tag:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="h1[]" rows="3" cols="40"><?php echo stripslashes($result->h1); ?></textarea></td>
							  </tr>
							  <?php } ?>
								
									 <tr>
                                      <td align="right" class="paddBot11 paddRt14"><h2>Meta Tags</h2></td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							       </tr>
                             <?php for($x=0; $x<count($cityarr); $x++){ 
							 $sql_meta=$obj->query("select meta_title from tbl_meta where city='$cityarr[$x]' and catid='".$_REQUEST['id']."' and filename='subcategory.php' and storeid=0");
$result=$obj->fetchNextObject($sql_meta); ?>
							 <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong><?php echo ucwords($cityarr[$x]); ?> Meta Title:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="metatitle[]" rows="5" cols="40"><?php echo stripslashes($result->meta_title); ?></textarea></td>
							  </tr>
							  <?php } ?>
                              <?php for($x=0; $x<count($cityarr); $x++){ 
							  $sql_meta=$obj->query("select meta_key from tbl_meta where city='$cityarr[$x]' and catid='".$_REQUEST['id']."' and filename='subcategory.php' and storeid=0");
$result=$obj->fetchNextObject($sql_meta); ?>
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong><?php echo ucwords($cityarr[$x]);?> Meta Keywords:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="metakeyword[]" rows="5" cols="40"><?php echo stripslashes($result->meta_keywords); ?></textarea></td>
							  </tr>
							  <?php } ?>
                              <?php for($x=0; $x<count($cityarr); $x++){ 
							  $sql_meta=$obj->query("select meta_description from tbl_meta where city='$cityarr[$x]' and catid='".$_REQUEST['id']."' and filename='subcategory.php' and storeid=0");
$result=$obj->fetchNextObject($sql_meta); ?>
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong><?php echo ucwords($cityarr[$x]);?>  Description:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="metadescription[]" rows="5" cols="40"><?php echo stripslashes($result->meta_description); ?></textarea></td>
							  </tr> 
							  <?php } ?>
							  <?php for($x=0; $x<count($cityarr); $x++){ 
							  $sql_meta=$obj->query("select content from tbl_meta where city='$cityarr[$x]' and catid='".$_REQUEST['id']."' and filename='subcategory.php' and storeid=0");
$result=$obj->fetchNextObject($sql_meta); ?>
 <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong><?php echo ucwords($cityarr[$x]);?> Content:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="content[]" class="ckeditor" id="content" rows="10" cols="40"><?php echo stripslashes($result->content); ?></textarea></td>
							  </tr> 
							  <?php } ?>
									
									<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							  </tr>
									<tr>
										<td width="18%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
										<td width="82%" align="left" class="paddBot11">
											<input type="submit" name="submit" value="Submit"  class="submit" border="0"/> 	                  		 &nbsp;&nbsp;
											<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" /></td>
									</tr>
								</table></form>
							</td>
						</tr>
						
					</table>
				</td>
			</tr>
		</table>
	</td>
</tr>
<?php include('footer.php'); ?>
</table>
</body>
</html>


