<?php

include_once("../include/main.inc.php");

check_session('MyCpanel','index.php');

@extract($_REQUEST);



if (empty($catID)) {

	$heading="Add New Location";

}

else {

	$heading="Edit Location";

}

$res=getResult("admin_details"," where user_type='admin'");



$record_type="Location";

$script_name="addeditlocation.php";

$redirect_script_name="manage_locations.php";

$table_name="location_tbl";

#################### Add Record ##################



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

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



	$numCnt=getCount($table_name," where locationName='$catName' and parentID='$parentID' and locationID!='$catID' and locationType='$catgType' ");

	$count_cat = mysqli_query($GLOBALS['dbcon'],"SELECT * FROM $table_name");

	if(mysqli_num_rows($count_cat) >= $res['location_count']){

		$_SESSION[session_message]=$record_type." count exceeds the allowed limit[$res[location_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."&locationID=".$catID."&pagesize=".$pagesize);

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

				exit;

			}

			if($_FILES[image][name]!=""){

				$img_name=date("d_m_Y_H_i_s");

				$img=upload_file($img_name,"image","uploaded_files/location_images");	

				

				$width="100";

				$height="100";

				$source_folder=SITE_DIR_PATH."/uploaded_files/location_images/";

				$destination_folder=SITE_DIR_PATH."/uploaded_files/location_images/thumbs/";

				saveResizedImage($img, $width, $height, $source_folder, $destination_folder);			

			}

		}

		

		if($_FILES[catIcon][name]!=""){

			$file_extension=strtolower(getExtension($_FILES[catIcon][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."&locationID=".$catID."&pagesize=".$pagesize);

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

				exit;

			}

			

		}

		

		$maxRes=db_fetch_array(db_query("select max(locationOrder) from $table_name where parentID='$parentID'"));

		$catOrder=$maxRes[0]+1;

		$sql=db_query("insert into $table_name set parentID='$parentID', locationName='$catName', locationType='$catgType', locationtDesc='$catDesc', locationDesc1='$catDesc1', locationImage='$img', locationOrder='$catOrder'");

		$_SESSION[session_message]=$record_type.' has been added successfully';

		//header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);

		$redirect_url = SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$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 locationName='$catName' and parentID='$parentID' and locationID!='$catID' and locationType='$catgType'");

	if($numCnt>0) {

		$_SESSION[session_message]=$record_type.' already exist';

		//header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);

		$redirect_url = SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&locationID=".$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."&locationID=".$catID."&pagesize=".$pagesize);

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

		exit;

		}

		if($_FILES[image][name]!=""){

			$categoryRes=getResult($table_name," where locationID='$catID'","locationImage");

			$filename=$categoryRes['catImage'];

			if($filename!="") {

				$file=SITE_DIR_PATH."/uploaded_files/location_images/".$filename;

				if(file_exists($file)) {

					unlink($file);

				}

				

				$file_thumb=SITE_DIR_PATH."/uploaded_files/location_images/thumbs/".$filename;

				if(file_exists($file_thumb)) {

					unlink($file_thumb);

				}

			}

			

			$img_name=date("d_m_Y_H_i_s");

			$img=upload_file($img_name,"image","uploaded_files/location_images");

			updateTable($table_name," locationImage='$img' where locationID='$catID'");

			

			$width="100";

			$height="100";

			$source_folder=SITE_DIR_PATH."/uploaded_files/location_images/";

			$destination_folder=SITE_DIR_PATH."/uploaded_files/location_images/thumbs/";

			saveResizedImage($img, $width, $height, $source_folder, $destination_folder);

		}

	}

		

	updateTable($table_name," locationName='$catName', locationType='$catgType', locationtDesc='$catDesc', locationDesc1='$catDesc1' where locationID='$catID'");

    $_SESSION[session_message]=$record_type.' has been updated successfully';

	//header("Location: ".SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$catID."&pagesize=".$pagesize);

	$redirect_url = SITE_ADMIN_URL."/".$redirect_script_name."?start=".$start."&parentID=".$parentID."&locationID=".$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 locationID='$catID'");



	$catName=$res[locationName];

	$catDesc=$res[locationtDesc];

	$catDesc1=$res[locationDesc1];

	$catgType=$res[locationType];



echo $catgType;



?>

<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;?>&locationID=<?=$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>Location 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>Description</b></td>

  <td class="brdrbtm"><textarea name="catDesc" id="catDesc" rows="10" cols="70" class="txtfieldadmin" ><?=$catDesc?></textarea></td>

  <script type="text/javascript">

	var oFCKeditor=new FCKeditor('catDesc');

	oFCKeditor.BasePath="fckeditor/";

	oFCKeditor.Width="600px";

	oFCKeditor.Height="400px";

	oFCKeditor.ReplaceTextarea();

	</script>

 </tr>

  <!--<tr>

  <td class="brdrbtm" width="125"><b>Category Type</b>*</td>

  <td class="brdrbtm">

     <select name="catgType" class="p5px ml10" id="catgType">

	   <option value="1" <?if($catgType=="1"){ echo "selected";}?>>Services</option>   	    

        <option value="0" <?if($catgType=="0"){ echo "selected";}?>>Products</option> 

		<option value="2" <?if($catgType=="2"){ echo "selected";}?>>Cources</option>

		     </select>

  </td>

 </tr>



  <tr>

  <td class="brdrbtm"><b> Companies Category Description</b></td>

  <td class="brdrbtm"><textarea name="catDesc1" id="catDesc1" rows="10" cols="70" class="txtfieldadmin" ><?=$catDesc1?></textarea></td>

 </tr>-->

<tr>

<td class="brdrbtm" width="150"><b>Location Image</b></td>

<td class="brdrbtm" >

<input name="image" type="file" class="button">

   <?php

   if($res[catImage]!=""){

	   $file1=SITE_DIR_PATH."/uploaded_files/location_images/".$res[locationImage];

	   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=location_images/<?=$res[locationImage];?>', '<?=$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="150"><b>Icon Image</b></td>

  <td class="brdrbtm" >

   <input name="catIcon" type="file" class="button">

   <?php

   if($res[catImage]!=""){

	   $file1=SITE_DIR_PATH."/uploaded_files/catIcon_images/".$res[catIcon];

	   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=catIcon_images/<?=$res[catIcon];?>', '<?=$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></td>

  <td class="brdrbtm">

   <?php

   if($res[locationID]=="") {

	   ?>

	   <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;?>&locationID=<?=$catID;?>&start=<?=$start;?>&pagesize=<?=$pagesize;?>'">   

  </td>

 </tr>

</table>

</form>



<?php

admin_footer();

exit;

?>