<?
/*
#============================================================================
# Program: Topology-Loced.php
# Programmer: Remo Rickli
#
# DATE		COMMENT
# -----------------------------------------------------------
# 26/05/07	initial version.
# 13/07/07	new location scheme
# 31/08/07	implemented CSS scheme
# 20/09/07	implemented language support
*/

include_once ("inc/header.php");
include_once ('inc/libdev.php');

$_GET = sanitize($_GET);
$id   = isset($_GET['id']) ? $_GET['id'] : "";
$reg  = isset($_GET['reg']) ? $_GET['reg'] : "";
$cty  = isset($_GET['cty']) ? $_GET['cty'] : "";
$bld  = isset($_GET['bld']) ? $_GET['bld'] : "";
$x    = isset($_GET['x']) ? $_GET['x'] : 0;
$y    = isset($_GET['y']) ? $_GET['y'] : 0;
$com  = isset($_GET['com']) ? $_GET['com'] : "";
$loco = 0;
$mapbg= TopoMap();
$now  = date($datfmt);

$link	= @DbConnect($dbhost,$dbuser,$dbpass,$dbname);
if (isset($_GET['add']) and $reg){
	$query	= GenQuery('locations','i','','','',array('region','city','building','x','y','comment'),'',array($reg,$cty,$bld,$x,$y,$com) );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$addbtn $reg $cty $bld OK</h5>";}
}elseif (isset($_GET['up']) and $id){
	$query	= GenQuery('locations','u','id',$id,'',array('region','city','building','x','y','comment'),'',array($reg,$cty,$bld,$x,$y,$com) );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$updlbl $reg $cty $bld OK</h5>";}
}elseif(isset($_GET['del']) and $id){
	$query	= GenQuery('locations','d','','','',array('id'),array('='),array($id) );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$delbtn $reg $cty $bld OK</h5>";}
	$id = $reg = $cty = $bld = $x = $y = $com = "";
}

$query	= GenQuery('devices','s','location');
$res	= @DbQuery($query,$link);
if($res){
	while( ($d = @DbFetchRow($res)) ){
		$l = explode($locsep, $d[0]);
		$lopt[$l[0]][$l[1]][$l[2]]++;
	}
	@DbFreeResult($res);
}else{
	echo @DbError($link);
}

if($bld){
	$query	= GenQuery('locations','s','id,x,y,comment','','',array('region','city','building'),array('=','=','='),array($reg,$cty,$bld),array('AND','AND'));
	$mapbg = TopoMap($reg,$cty);
	$nam = $bld;
	$com = "Building, ".$lopt[$reg][$cty][$bld] ." devices ($now)";
}elseif($cty){
	$query	= GenQuery('locations','s','id,x,y,comment','','',array('region','city','building'),array('=','=','='),array($reg,$cty,''),array('AND','AND'));
	$mapbg = TopoMap($reg);
	$nam = $cty;
	$com = "City, ".count(array_keys($lopt[$reg][$cty]))." buildings ($now)";
}elseif($reg){
	$query	= GenQuery('locations','s','id,x,y,comment','','',array('region','city','building'),array('=','=','='),array($reg,'',''),array('AND','AND'));
	$nam = $reg;
	$com = "Region, ".count(array_keys($lopt[$reg]))." cities ($now)";
}else{
	$query = "";
}

if($query){
	$res	= @DbQuery($query,$link);
	$nloc	= @DbNumRows($res);
	if ($nloc == 1) {
		list($id,$x,$y,$com) = @DbFetchRow($res);
		$loco = 1;
	}
}

?>
<h1><?=$loclbl?> Editor</h1>
<form method="get" action="<?=$self?>.php" name="lof">
<table class="content" ><tr class="<?=$modgroup[$self]?>1">
<th width=80><a href="<?=$self?>.php"><img src="img/32/home.png"></a></th>
<th valign="top"><h3>Region</h3><select size=4 name="reg" onchange="document.lof.cty.selectedIndex = -1; document.lof.bld.selectedIndex = -1; document.lof.x.value = ''; document.lof.y.value = ''; this.form.submit();">
<?
ksort($lopt);
foreach(array_keys($lopt) as $r){
	echo "<option value=\"$r\"".(($reg == $r)?"selected":"").">$r\n";
}
?>
</select></th>
<th valign="top"><h3>City</h3><select size=4 name="cty" onchange="document.lof.bld.selectedIndex = -1; document.lof.x.value = ''; document.lof.y.value = ''; this.form.submit();">
<?
if($reg){
ksort($lopt[$reg]);
	foreach(array_keys($lopt[$reg]) as $c){
		echo "<option value=\"$c\"".(($cty == $c)?"selected":"").">$c\n";
	}
}
?>
</select></th>
<th valign="top"><h3>Building</h3><select size=4 name="bld" onchange="document.lof.x.value = ''; document.lof.y.value = ''; this.form.submit();">
<?
if($cty){
ksort($lopt[$reg][$cty]);
	foreach(array_keys($lopt[$reg][$cty]) as $b){
		echo "<option value=\"$b\"".(($bld == $b)?"selected":"").">$b\n";
	}
}
?>
</select></th>
<th width=400 valign="top">
<h3><?=$nam?> <?=$loclbl?></h3>
X <input type="text" name="x" size=6 value="<?=$x?>" >
Y <input type="text" name="y" size=6 value="<?=$y?>" >
<p>
<?=$cmtlbl?> <input type="text" name="com" size=30 value="<?=$com?>" onfocus=select();>
</th>
<th width=80 valign="top">
<h3>Action</h3>
<? 
if($loco){
?>
<input type="hidden" name="id" value="<?=$id?>">
<input type="submit" name="up" value="<?=$updlbl?>"><p>
<input type="submit" name="del" value="<?=$delbtn?>">
<?
}else{
?>
<input type="submit" name="add" value="<?=$addbtn?>"><p>
<?
}
$bgsize = getimagesize ("log/$mapbg");
?>

</th>
</tr></table></form><p>
<h2>Click on map for coordinates</h2>
<div align="center">
<div id="map" onclick="getcoord(event)" style="background-image:url('log/<?=$mapbg?>');width:<?=$bgsize[0]?>px;height:<?=$bgsize[1]?>px;border:1px solid black">
<img src="img/32/bcnl.png" id="loc" style="position:relative;visibility:hidden;z-index:2;"></div>
</div>
<script language="JavaScript">
<? if($x and $y){?>
document.getElementById("loc").style.left = "<?=($x-$bgsize[0]/2)?>px";
document.getElementById("loc").style.top = "<?=($y-15)?>px" ;
document.getElementById("loc").style.visibility = "visible" ;
<?}?>
function getcoord(event){
	mapx = event.offsetX?(event.offsetX):event.pageX-document.getElementById("map").offsetLeft;
	mapy = event.offsetY?(event.offsetY):event.pageY-document.getElementById("map").offsetTop;
	document.lof.x.value = mapx;
	document.lof.y.value = mapy;
	document.getElementById("loc").style.visibility = "visible" ;
	document.getElementById("loc").style.left = (mapx-<?=$bgsize[0]/2?>)+'px';
	document.getElementById("loc").style.top = (mapy-15)+'px';
}
</script>
<?
include_once ("inc/footer.php");
?>