<?php


/* Check for the required libraries 
 * g2/curl are used for printing. If you're not using printing
 * you can avoid using these modules.
 *
 * mapscript and dbase are used for most operations using them
 * cannot be avoided
 */

if(!extension_loaded('gd')) {
	dl('php_gd2.'.PHP_SHLIB_SUFFIX);
}

if(!extension_loaded('curl')) {
	dl('php_curl.'.PHP_SHLIB_SUFFIX);
}

if(!extension_loaded('MapScript')) {
	dl('php_mapscript.'.PHP_SHLIB_SUFFIX);
}

if(!extension_loaded('dbase')) {
	dl('php_dbase.'.PHP_SHLIB_SUFFIX);
}

/*
 * These are a set of functions used by the GeoMOOSE PHP Scripts
 */



/*
 * appError()
 * Show an error indicated by $msg in HTML to the user
 */
function appError($msg) {
	print "<html>";
	print "<head><title>GeoMOOSE Service Error</title></head>";
	print "<body>";
	print "<h3>Service Message</h3>";
	print "<b style='color:red'>".$msg."</b>";
	print "</body>";
	print "</html>";
	exit(0);
}

/*
 * writeShapeFile()
 * This is a very terse way to create a shapefile.
 * $shapeFileName - Filename for the shape file to write
 * $shapeFileType - Mapscript Shapefile Constant indicating whether the shape
 *	file is to be point, line, polygon, etc.
 * $shapes - Array containing the shapes to be written to the shape file.
 * $class - Identifier to associate with the shape.
 */

function writeShapeFile($shapeFileName, $shapeFileType, $shapes, $class) {
	$fields = array(
		array("class", "C", 32)
	);

	$shapefile = ms_newShapefileObj($shapeFileName, $shapeFileType);
	if(!isset($shapefile)) { appError("Cannot Open Shapefile! ".$shapeFileName.".shp\n"); }

	$dbaseFilename = $shapeFileName.'.dbf';
	if(!dbase_create($dbaseFilename, $fields)) {
		appError('Could not create dbase file: '.$shapeFileName);
	}

	$dbase = dbase_open($dbaseFilename, 2);
	foreach ($shapes as $shape) {
		dbase_add_record($dbase, array($class));
		$shapefile->addShape($shape);
	}
	dbase_close($dbase);

	$shapefile->free();
}

/*
 * getShapesFromLayer()
 * Searches a layer in a map with a specific shape, returns an array of found shapes
 * $map - Map to search
 * $layer - Name of the layer to search or "*" to search all layers
 * $searchShape - Shape to be used in the search for other shapes.
 */
function getShapesFromLayer($map, $layerName, $searchShape) {
	$shapes = array();	# New array to hold results
	$map->getLayerByName($layerName)->set('status',MS_ON);

	$map->queryByShape($searchShape);		# Query the map by the search shape
	for($l = 0; $l < $map->{numlayers}; $l++) {	# Cycle through the layers
		$layer = $map->getLayer($l);		# Get they layer object from the map.
		if($layer->{name} == $layerName or $layerName == '*') {	# Check to make sure it's our layer of interest.
			$layer->open();			# Open the layer for operation.
			if($layer->getNumResults() > 0) {  # Check to make sure we have SOME results
				for($result = 0; $result < $layer->getNumResults(); $result++) {
					# Get the member of the result set
					$queryResult = $layer->getResult($result);
					# Get the feature from that result.
					$queryFeature = $layer->getShape($queryResult->{tileindex}, $queryResult->{shapeindex}); 
					array_push($shapes, $queryFeature);
				}
			}
			$layer->close(); # Close the layer
		}
	}
	return $shapes;

}

/*
 * getLayerColumnHash()
 * Returns a hash/dictionary/associative-array of the layer's columns.
 * If a layer contained the columns 'Name','Rank','Serial', then
 * this function would return an array with items 'Name' = 0, 'Rank' = 1, 'Serial' = 2
 */
function getLayerColumnHash($layer) {
	$arr = array();
	$items = $layer->getItems();
	for($i = 0; $i < count($items); $i++) {
		$arr[$items[$i]] = $i;
	}
	return $arr;
}

function getPath($file) {
	global $PARENT_DIRECTORY;
	if(substr($file,0,1) == '/' || ereg("[A-Za-z]\:", $file)) {
		return $file;
	}
	return $PARENT_DIRECTORY.$file;
}
