 <?php 
 
	include_once("./ARC2/arc/ARC2.php");
	include('geolocation.class.php');
	$googleMapKey = "ABQIAAAA43NnoyWgXT_X5ElzZjWA8RT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTVMBv8J1Lcc-8GuZ2rSwv4M7zdVw";
	
	error_reporting(0); // disable error messages
	// see http://arc.semsol.org/docs for document of ARC2
			
	echo "ARC2 Demo... ";

	$config = array(
	  /* db */
	  'db_name' => 'arc2',
	  'db_user' => 'arc2',
	  'db_pwd' => 'arc2',
	  /* store */
	  'store_name' => 'arc_tests',
	  /* stop after 100 errors */
	  'max_errors' => 100,
	);
	$store = ARC2::getStore($config);
	if ($store)
	{
		echo "Connected to ARC2 database<br/>\n";
	}
	if (!$store->isSetUp()) {	  
	  $store->setUp();
	}
	
	echo "<table>";
	$q = '
		PREFIX foaf: <http://xmlns.com/foaf/0.1/> .
		SELECT DISTINCT ?name ?person ?workplace WHERE {
		  <http://people.csail.mit.edu/lkagal/foaf#me> foaf:knows ?person .
		  ?person foaf:workplaceHomepage ?workplace .
		  OPTIONAL {?person foaf:name ?name }
		}';
	if ($rows = $store->query($q, 'rows')) {
		foreach ($rows as $row) {
			// from workplace homepage to map
			$image = '';
			$url = parse_url($row['workplace']);
			$host = $url['host'];
			$geolocation = new geolocation(true);
			$geolocation->setDomain($host);
			$locations = $geolocation->getGeoLocation();			
			if (is_array($locations)) {
				$location = $locations[0];
				$image = makeMap($location['Latitude'], $location['Longitude'], $googleMapKey);				
			}
			
			echo '<tr><td><b>' . $row['name'] . '</b><br/>' . $row['person'] . '<br/>' . 
				$row['workplace'] .	'</td><td>' . $image .'</td></tr>';
		}
	}			
	echo "</table>";
	
	function makeMap ($lat, $long, $key)
	{
		$image = "<img src=\"http://maps.google.com/maps/api/staticmap?" .
				        "center=" . $lat. ','. $long .
						"&zoom=4" .
						"&markers=" . $lat. ',' . $long .
						"&size=200x200".
						"&sensor=true".								
						"&key=". $key . "\">";
		return $image;				
	}
 ?> 
 
