<?php
   include_once("../include/main.inc.php");
   check_session('MyCpanel','index.php');
   ################ Paging Starts From Here ##################
   @extract($_REQUEST);
   $start = (intval($start)<0 or $start=="")?0:$start;
   $pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;
   
   $heading="Manage Subdomain Content";
   $record_type="Content";
   $script_name="manage_location_content.php";
   $table_name="location_content_tbl";
   ####################################################
   
   if($id=="add_record") {
       $location_name = array();
   	foreach($location_id as $key => $location){
           $location_sql = mysqli_query($dbconn,"SELECT * FROM $table_name where location_id='$location' AND category_id='$category_id'");
           if(mysqli_num_rows($location_sql)==0){
               $sql=db_query("insert into $table_name set category_id='$category_id', location_id='0', meta_keyword='$meta_keyword', meta_description='$meta_description', meta_title='$meta_title', description='$description', short_description='$short_description',keyword1='$keyword1',keyword2='$keyword2',keyword3='$keyword3'");
           } else{ 
              $check=true;
               $location_name[] = get_location_name($location); 
           }
   		
   	}
       if($check==true){
           $location_string = implode(',', $location_name);
            $_SESSION['session_message']=$record_type.' for '.$location_string.' is Already Submitted.';
   		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
   		exit;
       }
   		$_SESSION['session_message']='New '.$record_type.' has been added successfully';
   		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
   		exit;
   	}
   if($id=="update_record") {

   		updateTable($table_name,"location_id='0', category_id='$category_id', meta_keyword='$meta_keyword', meta_description='$meta_description', meta_title='$meta_title', description='$description', short_description='$short_description', keyword1='$keyword1',keyword2='$keyword2', keyword3='$keyword3' where id='$recordID'");
//$query="UPDATE $table_name SET `location_id`='$location', `category_id`='$category_id', `meta_keyword`='$meta_keyword' ,`meta_description`='$meta_description' ,`meta_title`='$meta_title',`description`='$description',`short_description`='$short_description' ,keyword1='$keyword1',keyword2='$keyword2, keyword3='$keyword3' where id='$recordID'";
//            mysqli_query($dbconn,$query);
   	
   		$_SESSION['session_message']=$record_type.' has been updated successfully';
   		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
   		exit;
   	}
   if($id=="delete_record") {
   	db_query("delete from $table_name where id='$recordID'");
   	$_SESSION['session_message']=$record_type.' has been deleted successfully';
   	header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
   	exit;
   }
   
   admin_header("");
   ?>
<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="pgimg_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>
                  </td>
               </tr>
            </table>
         </form>
      </td>
   </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
<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 Location </b> [by name etc.] :-
            <input type="text" name="keyword" value="" size="40">
            <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
   $qry = mysqli_query($dbconn,"SELECT * FROM $table_name as content LEFT JOIN location_tbl ON location_tbl.locationID = content.location_id WHERE location_tbl.locationName LIKE '%$keyword%'");
   }else{
   $qry= mysqli_query($dbconn,"select * from $table_name WHERE category_id='$category_id' $cond order by id desc limit $start, $pagesize"); 
   }
   
   $qry1= mysqli_query($dbconn,"select * from $table_name");
   $reccnt = mysqli_num_rows($qry1);
//    echo pageNavigation($reccnt,$pagesize,$start);
   print_message();
   ?>
<br/>
<table width="99%" border="0" cellpadding="2" cellspacing="2" class="table table-striped">
   <form action="" method="POST">
      <?php
         if($reccnt>0) {
          ?>
       <thead>
      <tr height="25" bgcolor="#EAEAEA">
         <td width="5%" align="center"><strong>S.No.</strong></td>
         <td width="20%" align="left"><strong>Category</strong></td>
         <td width="20%" align="left"><strong>Location</strong></td>
         <td width="10%" align="left"><strong>Options</strong></td>
      </tr>
    </thead>
      <?php
         $sl=$start+1;
         while($arr=mysqli_fetch_array($qry)) {
         
          if($sl%2!=0) {
         	 $color="#f9f9f9";
          }
          else{
         	 $color="#f0f0f0";
          }
          $location_name = ($arr['location_id'] == 0)?'All':get_location_name($arr['location_id']);
               $category_name = ($arr['category_id'] == 0)?'Home':get_category_name($arr['category_id']);
         
          ?>
       <tbody>
      <tr bgcolor="<?=$color;?>">
         <td align="center"><?=$sl;?></td>
         <td align="left"><?= $category_name?></td>
         <td align="left"><?=$location_name?></td>
         <td align="center">
            &nbsp;<a href="<?=$script_name;?>?category_id=<?= $category_id ?>&id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['id'];?>#bot"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>&nbsp;
            <a href="<?=$script_name;?>?category_id=<?= $category_id?>&id=delete_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['id'];?>" onClick="return confirm('Are you sure to delete this record...');">
                <img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" border="0"></a>
         </td>
      </tr>
      <?php
         $sl++;
         }?>

      <?php }
         else{
          ?>
      <tr><td><b>No Record Found........</b></td></tr>
      <?php
         }
         ?>
   </form>
</table>
<br />
<form name="bannerFrm" method=post action="" enctype="multipart/form-data" onSubmit="return validate('bannerFrm') && chk_valid_date();">
<?php
   if($_GET['recordID']=="" || $id!="edit_record") {
    ?>
<div class="text-center"><h3 >Add New <?=$record_type;?></h3></div>
<?php
   }
   else {
    ?>
    <div align="left" height="20"><h3>Edit <?=$record_type;?></h3></div>
    <div align="right" height="20"><a  class="btn btn-primary text-light" href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a></div>
<br/>
<?php
$gid = $_GET['recordID'];
   }
   $res=getResult($table_name," where id='$gid'");
   $location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID ='0'");
   $category_name = ($category_id == 0)?'Home':get_category_name($category_id);
   ?>
<div class="form-group">
    <label>Category<span>*</span></label>
    <input type="text" name="category_name"  value="<?= $category_name ?>" readonly class="form-control"/>
</div> 
<div class="form-group">
    <label>Location<span>*</span></label>
   <select name="location_id[]" multiple class="form-control">
       <option value='0'>Select Location</option>
       <?php while($location = mysqli_fetch_array($location_sql)){
   if($location['locationID'] == $res['location_id']){$sel = 'selected';}else{$sel = '';}
   $parentID = $location['locationID'];
   $sub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID='$parentID'");
   ?>
<option style="font-weight:bold;" value="<?= $location['locationID']?>" <?= $sel?>> <?= $location['locationName']?></option>
<?php 
   if(mysqli_num_rows($sub_location_sql)>0){ 
   	while($sub_location = mysqli_fetch_array($sub_location_sql)){
          if($sub_location['locationID'] == $res['location_id']){$sel = 'selected';}else{$sel = '';}
        $subparentID = $sub_location['locationID'];
        $sub_sub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID='$subparentID'");
      ?>
<option <?=$sel?> value="<?= $sub_location['locationID']?>"> --<?= $sub_location['locationName']?></option>
       <?php 
        while($cityloc = mysqli_fetch_assoc($sub_sub_location_sql))
        {if($cityloc['locationID'] == $res['location_id']){$sel = 'selected';}else{$sel = '';}
       ?>
    <option <?= $sel ?> value="<?=$cityloc['locationID']?>"> --<?=$cityloc['locationName']?></option>
<?php } }
   } }?>
    </select>
</div>
    <div class="form-group">
    <label>Keyword 1 <span>*</span></label>
    <input type="text" name="keyword1"  value="<?=$res['keyword1'];?>"  class="form-control" placeholder="Keyword 1"/>
</div> 
      <div class="form-group">
    <label>Keyword 2 <span>*</span></label>
    <input type="text" name="keyword2"  value="<?=$res['keyword2'];?>"  class="form-control" placeholder="Keyword 2"/>
</div> 
    <div class="form-group">
    <label>Keyword 3 <span>*</span></label>
    <input type="text" name="keyword3"  value="<?=$res['keyword3'];?>"  class="form-control" placeholder="Keyword 3"/>
</div>  
<div class="form-group">
    <label>Meta Title <span>*</span></label>
    <input type="text" name="meta_title"  value="<?=$res['meta_title'];?>"  class="form-control"/>
</div> 
<div class="form-group">
    <label>Meta Keywords<span>*</span></label>
    <input type="text" name="meta_keyword"  value="<?= $res['meta_keyword'] ?>"  class="form-control"/>
</div> 
<div class="form-group">
    <label>Meta Description<span>*</span></label>
   <textarea rows="5" cols="60" name="meta_description" class="form-control"><?= $res['meta_description'] ?></textarea>
</div> 
 <div class="form-group">
    <label>Short Description<span>*</span></label>
   <textarea rows="5" cols="60" name="short_description" id="short_description" class="form-control"><?=$res['short_description'] ?></textarea>
</div>    
<div class="form-group">
    <label>Description<span>*</span></label>
   <textarea rows="5" cols="60" id="description" name="description" class="form-control"><?=$res['description'] ?></textarea>
</div>   
 <div class="form-group">
    <?php
   if($recordID !="") {
    ?>
<input type="submit" name="submit" value="Update <?=$record_type;?>" class="button btn btn-success">
<input type="hidden" name="recordID" value="<?=$res['id'];?>">
<input type="hidden" name="id" value="update_record">
<?php	   
   }
   else{
    ?>
<input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button btn btn-success">
<input type="hidden" name="id" value="add_record">
<?php
   }
   ?>
<input type="hidden" name="start" value="<?=$start;?>">
<input type="hidden" name="pagesize" value="<?=$pagesize;?>">
</div>      
    
</form>
<script src="jquery.min.js"></script>
<script type="text/javascript">
   $("#quickCity").change(function(){	
   
           var cityid  =  $('#quickCity').val();
   
          $.ajax({   
   
               url: "ajax99.php", //The url where the server req would we made.
   
               async: false,
   
               type: "POST", //The type which you want to use: GET/POST
   
               data: "cityid="+cityid, //The variables which are going.
   
               dataType: "html", //Return data type (what we expect).
   
               //This is the function which will be called if ajax call is successful.
   
               success: function(data) {
   
                   //data is the html of the page where the request is made.
   
                   $('#locatity').html(data);
   
               }
   
           })
   
   });
   
</script>
<iframe width=174 height=189 name="gToday:normal:agenda.js" id="gToday:normal:agenda.js" src="<?=SITE_URL_PATH;?>/js/calendar/themes/Normal/ipopeng.htm" scrolling="no" frameborder="0" style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;">
</iframe>
<?php
   admin_footer();
   exit;
   ?>