<?php
/*
 * zoomto.php
 * A "zoom to item query" service for GeoMOOSE
 * (c) 2008 Dan "Ducky" Little for the GeoMOOSE Project
 */

# We really don't want PHP complaining about not-found things
error_reporting(E_ERROR | E_PARSE);

# Include GeoMOOSE PHP Utility Function Library
include('geomoose_php_util.php');
include('Configuration.php');

$mapfile = $_REQUEST['map'];
$layers = $_REQUEST['layer'];
$item = $_REQUEST['qitem'];
$string = $_REQUEST['qstring'];

$mode = $_REQUEST['mode'];

$buffer = $_REQUEST['buffer'];
$highlight = $_REQUEST['highlight'];

# If the buffer isn't set, set it to zero!
if(!(isset($buffer) and is_numeric($buffer) and $buffer > 0)) {
	$buffer = 0;
}

# Turn the highlight string into a boolean
$highlight = strtolower($highlight);
if($highlight == 'on' or $highlight == 'true') {
	$highlight = true;
} else {
	$highlight = false;
}

$map = ms_newMapObj($mapfile) or appError('Could not open "'.$mapfile.'!"');

$layers = explode(',', $layers);

$found = false;
$ext = 0;

$minx = 0;
$miny = 0;
$maxx = 0;
$maxy = 0;

$selectedIds = array();

$foundOnLayer = '';
for($l = 0; $l < $map->numlayers and !$found; $l++) {
	$lay = $map->getLayer($l);
	$inCGI = false;
	for($cgi = 0; $cgi < count($layers); $cgi++) {
		if($lay->name == $layers[$cgi]) {
			$inCGI = true;
		}
	}
	if($lay->status == MS_DEFAULT or $inCGI == true) {
		$queryTemplate = $lay->getMetaData("ITEMQUERY_TEMPLATE");
		$queryHeader = $lay->getMetaData("ITEMQUERY_HEADER");
		$queryFooter = $lay->getMetaData("ITEMQUERY_FOOTER");

		if($queryTemplate != "") {
			$lay->set('template', $queryTemplate);
		} else {
			$lay->set('template', 'dummy.html');
		}

		$lay->set('header', $queryHeader);
		$lay->set('footer', $queryFooter);

		$result = $lay->queryByAttributes($item, $string, MS_SINGLE);
		if($result == MS_SUCCESS) {
			$lay->open();
			$res = $lay->getResult(0);
			$sh = $lay->getFeature($res->shapeindex, $res->tileindex);
			$minx = $sh->bounds->minx - $buffer;
			$miny = $sh->bounds->miny - $buffer;
			$maxx = $sh->bounds->maxx + $buffer;
			$maxy = $sh->bounds->maxy + $buffer;

			array_push($selectedIds, $sh->values[$item]);

			$lay->close();
			$found = true;

			$foundOnLayer = $lay->name;
		}
	}
}

$template = $map->processQueryTemplate(array(), array());

$replaceArr = array();
$replaceArr['[MAPFILE]'] = $mapfile;
$replaceArr['[map]'] = $mapfile;
$replaceArr['[QSTRING]'] = $string;
$replaceArr['[QITEM]'] = $item;

if(!$found) {
	if(isset($ZOOMTO_EMPTY_PAGE)) {
		$contents = file_get_contents($ZOOMTO_EMPTY_PAGE);
		foreach($replaceArr as $key=>$value) {
			$contents = str_replace($key, $value, $contents);
		}
		print $contents;
	} else {
		$errorStr = sprintf("Cound not find results.<br/>Mapfile: %s<br/>Layers (Non-Default): %s<br/>Query Item: %s<br/>Query String: %s", $mapfile, $_REQUEST['layers'], $item, $string);
		appError($errorStr);
	}
} else {
	if($mode == 'bounds-only') {
		header("Content-type: text/plain");
		printf("%0.3f,%0.3f,%0.3f,%0.3f", $minx, $miny, $maxx, $maxy);
	} else if($mode == 'xml') {
		header("Content-type: text/xml");
		#print "<return>";
		print "<mapbook>";
		printf("<zoomto default='true'>%0.3f,%0.3f,%0.3f,%0.3f</zoomto>", $minx, $miny, $maxx, $maxy);
		printf("<map title='%s' default='true' invisible='false'>", 'Highlight');
		printf("<file>%s</file>", $mapfile);
		foreach($selectedIds as $id) {
			printf("<selected id='%s'/>", $id);
		}
		printf("<param name='qlayer' value='%s'/>", $foundOnLayer);
		printf("<param name='mode' value='itemquerymap'/>");
		printf("<param name='qitem' value='%s'/>", $item);
		printf("<param name='qstring' value='%s'/>", $string);
		printf("</map>");
		print "</mapbook>";
		#print "</return>";
	} else {
		header("Content-type: text/html");
		print '<html xmlns:moose="http://geomoose.org/">';
		print "<head>\n";
		print "<moose:mapbook>\n";
		printf("<moose:zoomto default='true'>%0.3f,%0.3f,%0.3f,%0.3f</moose:zoomto>", $minx, $miny, $maxx, $maxy);
		if($highlight) {
			printf("<moose:map title='%s' default='true' invisible='false'>", 'Highlight');
			printf("<moose:file>%s</moose:file>", $mapfile);
			foreach($selectedIds as $id) {
				printf("<moose:selected id='%s'/>", $id);
			}
			printf("<moose:param name='qlayer' value='%s'/>", $foundOnLayer);
			printf("<moose:param name='mode' value='itemquerymap'/>");
			printf("<moose:param name='qitem' value='%s'/>", $item);
			printf("<moose:param name='qstring' value='%s'/>", $string);
			printf("</moose:map>");
		}
		print "</moose:mapbook>\n";

		print "</head>\n";
		print "<body>";
		print "<![CDATA[";

		foreach($replaceArr as $key=>$value) {
			$template = str_replace($key, $value, $template);
		}
		print $template;

		print "]]>";
		print "</body>";
		print "</html>";
	}
}

?>
