<?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") {
    if(!empty($location_ids)){
     $location_name = array();
     $loc = implode(',',$location_ids);
     // print_r($loc);die;
         $location_sql = mysqli_query($dbconn,"SELECT * FROM $table_name where location_ids='$loc' AND category_id='$category_id'");
         if(mysqli_num_rows($location_sql)==0){
            $short_description = str_ireplace("'", "&apos;", $short_description);
            $meta_title = str_ireplace("'", "&apos;", $meta_title);
            $meta_keyword = str_ireplace("'", "&apos;", $meta_keyword);
            $meta_description = str_ireplace("'", "&apos;", $meta_description);
            $subheadingtwo = str_ireplace("'", "&apos;", $subheadingtwo);
             $sql=db_query("insert into $table_name set category_id='$category_id', location_id='0', location_ids='$loc', meta_keyword='$meta_keyword',subheadingtwo='$subheadingtwo', meta_description='$meta_description', meta_title='$meta_title', description='$description', description2='$description2',description3='$description3',short_description='$short_description'");
         } else{ 
             $check=true;
             $location_name[] = get_location_name($location); 
         }
     if($check==true){
             $_SESSION['session_message']=$record_type.' for these locations is Already Created.';
         header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
         exit;
     }
     }else{
         $_SESSION['session_message']='Please Select Location';
         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") {
 if(!empty($location_ids)){
     // print_r($locations);die;
         $loc = implode(',',$location_ids);
         $short_description = str_ireplace("'", "&apos;", $short_description);
         $meta_title = str_ireplace("'", "&apos;", $meta_title);
         $meta_keyword = str_ireplace("'", "&apos;", $meta_keyword);
         $meta_description = str_ireplace("'", "&apos;", $meta_description);
         $subheadingtwo = str_ireplace("'", "&apos;", $subheadingtwo);

         updateTable($table_name,"location_id='0',location_ids='$loc', category_id='$category_id', subheadingtwo='$subheadingtwo',  meta_keyword='$meta_keyword', meta_description='$meta_description', meta_title='$meta_title', description='$description', short_description='$short_description',description2='$description2',description3='$description3' where id='$recordID'");
 }else{
     $_SESSION['session_message']='Please Select Location';
     header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&id=edit_record&start=".$start."&pagesize=".$pagesize."&recordID=".$recordID);
     exit;
 }
 $_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("");
?>
<style>
 select[multiple],
 select[size] {
     height: 200px;
 }

</style>
<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 asc 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;
      $sno = 0;
      while($arr=mysqli_fetch_array($qry)) {
      $sno++;
       if($sl%2!=0) {
           $color="#f9f9f9";
       }
       else{
           $color="#f0f0f0";
       }
       $location_name = ($sno == 1)?'All': 'Selected Locations';
            $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.'?category_id='.$category_id;?>"><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 parentID ='0' order by locationName asc");
$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">
     <!-- <div class="col-sm-6"> -->
     <label>Location<span>*</span></label>
     <label><input type="checkbox" name="check_all" id="checkAll"> All locations</label>
     <!-- </div> -->
     <div class="col-sm-6" style="float:right;">
         <label><input type="checkbox" name="check_half" id="checkHalf"> 50% Locations</label>
     </div>
     <div class="market-locations" style="height:400px;overflow-y:scroll;">
     <?php while($location = mysqli_fetch_array($location_sql)){ 
         $selct_cat_query = "select * from $table_name WHERE id ='$gid'";
         $res_cat = $dbconn->query($selct_cat_query);
         $cat_loca = $res_cat->fetch_assoc();
         $loca_cat = explode(",", $cat_loca['location_ids']);
         // print_r($loca_cat);die; 
         $checked = '';
         
             if(in_array($location['locationID'], $loca_cat)){
                 $checked = 'checked';
             }else{
                 $checked = '';
             }
         
         ?>
     <div class="row"><div class="col-sm-12 location-cls checked-loca">
         <?php if($location['status'] == 'Y') { ?><input type="checkbox" name="location_ids[]" value="<?= $location['locationID']; ?>" <?= $checked; ?>> <strong><?= $location['locationName']; ?></strong>
             <?php } else { ?><label><?= $location['locationName']; ?></label>
            <?php } ?>
     </div>
     <?php $parentID = $location['locationID'];
             $sub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE  parentID='$parentID'  order by locationName asc"); 
             $total_sub_loca = mysqli_num_rows($sub_location_sql);
             
             if(mysqli_num_rows($sub_location_sql)>0){ 
                 while($sub_location = mysqli_fetch_array($sub_location_sql)){ 
                     $selct_cat_query = "select * from $table_name WHERE id='$gid'";
                     $res_cat = $dbconn->query($selct_cat_query);
                     $cat_loca = $res_cat->fetch_assoc();
                     $loca_cat = explode(",", $cat_loca['location_ids']);
                     $checked = '';
                     if(in_array($sub_location['locationID'], $loca_cat)){
                         $checked = 'checked';
                     }else{
                         $checked = '';
                     }
                     ?>
                 <div class="col-sm-12 location-cls checked-loca">
                 <input type="checkbox" name="location_ids[]" value="<?= $sub_location['locationID']; ?>" <?= $checked; ?>> <label><?= $sub_location['locationName']; ?></label>
                 </div>
                 <?php $subsub_location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE  parentID='".$sub_location['locationID']."'  order by locationName asc"); 
                        $total_subsub_loca = mysqli_num_rows($subsub_location_sql);
                        $half_subsub_loca = 0;
                        if($half_subsub_loca % 2 == 0){
                            $half_subsub_loca = $total_subsub_loca / 2;
                        }else{
                            $half_subsub_loca = ($total_subsub_loca + 1) / 2;
                        }
                        if(mysqli_num_rows($subsub_location_sql)>0){ 
                            $counting = 0;
                            while($subsub_location = mysqli_fetch_array($subsub_location_sql)){ 
                                $selct_cat_query = "select * from $table_name WHERE id='$gid'";
                                $res_cat = $dbconn->query($selct_cat_query);
                                $cat_loca = $res_cat->fetch_assoc();
                                $loca_cat = explode(",", $cat_loca['location_ids']);
                                $checked = '';
                                if(in_array($subsub_location['locationID'], $loca_cat)){
                                    $checked = 'checked';
                                }else{
                                    $checked = '';
                                }
                                $counting++;
                                ?>
                            <div class="col-sm-3 location-cls <?php if($counting <= $half_subsub_loca) { echo 'checked-loca'; } ?>">
                            <input type="checkbox" name="location_ids[]" value="<?= $subsub_location['locationID']; ?>" <?= $checked; ?>> <?= $subsub_location['locationName']; ?>
                            </div>
     <?php } } } } ?></div><?php }
     $location_sql = mysqli_query($dbconn,"SELECT * FROM location_tbl WHERE status='Y' and parentID ='0' order by locationName asc");
     ?>
     </div>
     </div>
   <div class="form-group">
     <label>Sub-Domain Heading H2 <span>*</span></label>
     <input type="text" name="subheadingtwo" value="<?=$res['subheadingtwo'];?>" class="form-control" />
 </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"  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">
     <label>Description 2<span>*</span></label>
     <textarea rows="5" cols="60" id="description2" name="description2" class="form-control"><?=$res['description2'] ?></textarea>
 </div>
 <div class="form-group">
     <label>Description 3<span>*</span></label>
     <textarea rows="5" cols="60" id="description3" name="description3" class="form-control"><?=$res['description3'] ?></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);

         }

     })

 });
 $(document).ready(function(){
     $('#checkAll').change(function() {   
         if (this.checked) {
             $('.location-cls > input:checkbox').attr('checked', 'checked');  
         }else{ 
             $('.location-cls > input:checkbox').attr('checked', '');  
         }
     });
     $('#checkHalf').change(function(){
         if(this.checked){
             $('.checked-loca > input:checkbox').attr('checked', 'checked'); 
         }else{
             $('.checked-loca > input:checkbox').attr('checked', '');
         }
     })
 })
</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;
?>
