<?php

//-------------------------------------------------------------------------------
// FileHandler.php
//
// Handle input- and outputfiles by calling appropriate helper classes
//
// written by Bart Meganck, Royal Museum for Central Africa
// bart.meganck@africamuseum.be
//
//
// This program is free & open source, under the GNU General Public License, v.2
// See : http://www.gnu.org/copyleft/gpl.html for more details.
//
//-------------------------------------------------------------------------------


// ******************************* helper classes ****************************

// class for handling database storage
require ("Database.php");

// class for handling GML files
require ("FileHandler_modules/GML_Module.php");

// class for handling KML files
require ("FileHandler_modules/KML_Module.php");

// class for handling GPX files
require ("FileHandler_modules/GPX_Module.php");

// class for handling CSV files
require ("FileHandler_modules/CSV_Module.php");

// class for handling ITML/ItinML files
require ("FileHandler_modules/ITML_Module.php");




class FileHandler {
	
	private $output_base_path = '/home/bmeganck/public_html/itin/php_coding/final/downloads/';
	//private $output_base_path = '/tmp/itin/';
	private $output_file_path = 'test_itintool';
	private $output_file;
	private $file_type;

	
	// data points as parsed from input file
	private $input_array = array (

		 "id_array" 				=> array(),
		 "unique_id_array"		=> array(),
		 "name_array" 				=> array(),
		 "latitude_array" 		=> array(),
		 "longitude_array" 		=> array(),
		 "coord_accuracy_array" => array(),
		 "begin_date_array" 		=> array(),
		 "end_date_array" 		=> array()
 
		 );
		 
	// data structure	 
	$structure_array = array ( "startnode" => "",
										"endnode" => "",
										"node_count" => 0,
										"distance" => 0,
										"route_array" => array()
							 );	
		 	 

	// extensions and their file type description
	//  all keys are kept in lowercase
	private $filetype_array = array(
		
		"gml"		=> "Geographical Markup Language", 
		"xml"		=> "Extensible Markup Language",
		"kml"		=> "Google Earth Keyhole Markup Language",
		"csv"		=> "comma separated value (csv)",
		"shp"		=> "ESRI shapefile",
		"itml"	=> "Itinerary XML format",
		"gpx"		=> "GPS exchange format"
		
		);

		
	private $supported_filetypes = array("gml","xml","kml","csv","gpx","itml");
	private $unsupported_filetypes = array("shp"); 
	
		
	public function readPointsFromFile($input_file_path) {
	
		
	
		// TODO : run some checks on the file 
				 
		$this->file_type = $this->determineFiletype($input_file_path);
		
		if ( strpos ($this->file_type,"ERROR") <> 0 ) {
		
			// return error message, do not continue file processing
				
		} else {
		
			// the filetype is determined, we proceed..
			
			// depending on filetype, call module for parsing input file
			if (in_array($this->file_type,$this->supported_filetypes)) {

				switch ($this->file_type) {
	
				case ("gml"):
				$my_GML_Module = new GML_Module();
				$this->input_array=$my_GML_Module->readPointsFromGMLFile($input_file_path);
				break;
		
				case ("gpx"):
				$my_GPX_Module = new GPX_Module();
				$this->input_array=$my_GPX_Module->readPointsfromGPXFile($input_file_path);
				break;
		
				case ("kml"):
				$my_KML_Module = new KML_Module();
				$this->input_array=$my_KML_Module->readPointsFromKMLFile($input_file_path);
				break;
	
				case ("csv"):
				$my_CSV_Module = new CSV_Module();
				$this->input_array=$my_CSV_Module->readPointsFromCSVFile($input_file_path);
				break;
		
				case ("itml"):
				$my_ITML_Module = new ITML_Module();
				$this->input_array=$my_ITML_Module->readPointsFromITMLFile($input_file_path);
				break;
		
				default : 
				// TODO : provide an error warning
				echo ("default");
				
				} // switch
			
			} elseif (in_array($input_filetype_from_extension,$unsupported_filetypes)) {

				// TODO : return an error, no echo
				echo ('filetype not supported');

			} else {

				// TODO : return an error, no echo
				echo ('filetype unknown');

			}
		
		
		} // if-else ERROR in filetype
		
		return ($this->input_array);
					
	} // function readPointsFromFile
	
	
	
	public function writePointsToFile(&$points_array,$structure_array,$output_type,$colour="none",$output_file_name="test_itintool") {

		// Note that the (big) data_points_array structure is passed by reference, not by value,
		//   for performance and memory reasons
	
		// output type is broader than just filetype : for example,
		// kmlpath and kmlwpt are two .kml filetype outputs
		// "dbase" is not a file, but a database layer (possibly connecting to a WMS)
	
		switch ($output_type) {
	
			case ("gml"):
			// write point data to output file in GML 
			break;
		
			case ("kmlwpt"):
			// write point data to output file in KML, as waypoints
			$my_KML_Module = new KML_Module();
			$this->output_file_path = $this->output_base_path . $output_file_name.'_wpts.kml';
			$this->return_string = $my_KML_Module->writePointsToKMLFile_asWaypoints($points_array,$structure_array,$this->output_file_path,$colour);
			break;
			
			case ("kmlpath"):
			// write point data to output file in KML, as a path
			$my_KML_Module = new KML_Module();
			$this->output_file_path = $this->output_base_path . $output_file_name.'_path.kml';
			$this->return_string = $my_KML_Module->writePointsToKMLFile_asPath($points_array,$structure_array,$this->output_file_path,$colour);
			break;

			case ("csv"):
			// write point data to output file in CSV 
			$my_CSV_Module = new CSV_module();
			$this->output_file_path = $this->output_base_path . $output_file_name.'.csv';
			$this->return_string = $my_CSV_Module->writePointsToCSVFile($points_array,$structure_array,$this->output_file_path);
			break;

			case ("itml"):
			// write point data to an ITML output file
			$my_ITML_Module = new ITML_Module();
			$this->output_file_path = $this->output_base_path . $output_file_name.'.itml';
			$this->return_string = $my_ITML_Module->writePointsToITMLFile($points_array,$structure_array,$this->output_file_path);
			break;
			
			case ("dbase"):
			// store point data in a database layer
			$my_Database = new Database();
			$this->output_file_path = "database";
			$my_Database->clearDatabaseGeoreferenceTable();  // TESTING ONLY
			//var_dump($points_array);
			$this->return_string = $my_Database->writePointsToDatabase($points_array);
			break;
		
			default : // TODO make a nice default		

		} // switch
	
		//fwrite ($this->outputfile,$this->return_string);
		return($this->output_file_path);
	
	} // function writePointsToFile



	private function determineFiletype($file_path) {
	


		if (strrpos($file_path, ".") > 0 ) { 
		
			$this->file_type = strtolower(substr($file_path, strrpos($file_path, ".")+1) );

		} else { 
		
			$this->file_type = "ERROR : Your file doesn't have a filename extension"; 
		
		}
		
		return $this->file_type;
		
	} // function determineFiletype	
		
} // class FileHandler

?>