 <?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... ";

	/* configuration */ 
	$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();
	}
	
	$config_dbpedia = array(
	  /* remote endpoint */
	  'remote_store_endpoint' => 'http://dbpedia.org/sparql'
	);
	
	$store_dbpedia = ARC2::getRemoteStore($config_dbpedia);
	
	if ($store_dbpedia)
	{
		echo "Connected to DBPedia database<br/>\n";
	}
	
	echo "<table>";
	$q = '
		PREFIX foaf: <http://xmlns.com/foaf/0.1/> .
		SELECT ?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'];
			
			$dbpediaQuery = createQuery("http://" . $host. "/");
			//echo $dbpediaQuery . '<br/>\n';

			$image = '';	
			if ($rows1 = $store_dbpedia->query($dbpediaQuery, 'rows')) {
				foreach ($rows1 as $row1) {
					if ( $row1['lat'] != '' & $row1['long'] != '' ) {
						$image = makeMap($row1['lat'], $row1['long'], $googleMapKey);		
						break;
					}
				}		 
			}			
			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;				
	}
	
	function createQuery($website){
    	return 'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
			PREFIX type: <http://dbpedia.org/class/yago/> 
			PREFIX prop: <http://dbpedia.org/property/> 
			PREFIX ont: <http://dbpedia.org/ontology/> 
			PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
			PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

			SELECT DISTINCT ?label ?lat ?long WHERE {
			 {
			  {?resource prop:website <'. $website .'>.} 
			  UNION 
			  {?resource foaf:homepage <'. $website . '>}
			 }
			{
			   {?resource ont:location ?location.
			   OPTIONAL {?location geo:lat ?lat.
								   ?location geo:long ?long.}
			   OPTIONAL {?location rdfs:label ?label.}
			   }
			   UNION 
			   {?subject prop:employer ?resource} 
			   UNION 
			   {?subject prop:institution ?resource} 
			   UNION 
			   {?subject prop:workInstitution ?resource} 
				UNION 
				{?subject prop:workInstitutions ?resource} 
				UNION 
				{?subject prop:workplaces ?resource} 
				UNION 
				{?subject ont:occupation ?resource} 
				OPTIONAL {?resource geo:lat ?lat.
								   ?resource geo:long ?long.}
				OPTIONAL {?resource rdfs:label ?label.}
			}}';                        
}
 ?> 
 
