<?php
//$file contains place of the KML file we will be getting our node information from
function get_node_array($file)
{
	//Check if the file exists, if it does we load it. If it doesn't we exit and return an error message
	if (file_exists($file)) 
	{
		$xml = simplexml_load_file($file);
	} 
	else 
	{
		exit('Failed to open example.xml.');
	}

	//Counter starts at 0. For every foreach done counter will go up by one. Thus resulting in total nodes added(counter starts at 0 so add 1 to the total )
	$counter = 0;

	//Now we go through the xml files, storing the data in an array called $markers. More data can be stored by adding more rows in the array 
	foreach($xml->Document->Folder->Placemark as $placemark)
	{
		/*
		 * Getting all data from the datatags in the KML file.
		 * First we declare variables we will use.
		 * Then we itirate trough all the data and fill the variables
		 */
		$location;
		$status;
		$interfaces;
		$masterIP;
		$nodeType;
		$type;
		$hostname;
		$hasBeenChecked;
		$checkExecutionTime;
		$currentState;
		$lastCheck;
		$problemHasBeenAcknowledged;
		
		foreach($placemark->ExtendedData->Data as $data)
		{
			foreach($data->attributes() as $nameAttribute => $name)
			{
				if($name == "location")
					$location = $data->value;
				elseif($name == "status")
					$status = $data->value;
				elseif($name == "interfaces")
					$interfaces = $data->value;
				elseif($name == "masterIP")
					$masterIP = $data->value;
				elseif($name == "nodeType")
					$nodeType = $data->value;
				elseif($name == "type")
					$type = $data->value;
				elseif($name == "hostname")
					$hostname = $data->value;
				elseif($name == "hasBeenChecked")
					$hasBeenChecked = $data->value;	
				elseif($name == "checkExecutionTime")
					$checkExecutionTime = $data->value;
				elseif($name == "currentState")
					$currentState = $data->value;
				elseif($name == "problemHasBeenAcknowledged")
					$problemHasBeenAcknowledged = $data->value;
				elseif($name == "location")
					$location = $data->value;
			}
		}
		
		/*
		 * We now fill up our array with Longitude and Latitude and Name by accessing $placemark
		 * Then we fill the rest of the array with the variables we declared and filled earlier
		 */
		
		$markers[] = array(	"latitude"=>$placemark->LookAt->latitude,
							"longitude"=>$placemark->LookAt->longitude,
							"name"=>$placemark->name,
							"location"=>$location,
							"status"=>$status);
		
	}
	return $markers;
}