Commits

stollie committed 50a9264

Aardig wat veranderd.

Comments (0)

Files changed (3)

 RewriteEngine On
+#RewriteBase /
+
+RewriteRule ^countries/([a-z]{2}).(xml|json)$ countries.php?shortcode=$1&type=$2
+RewriteRule ^countries/([A-D]{1}).(xml|json)$ countries.php?group=$1&type=$2
+RewriteRule ^countries.(xml|json)$ countries.php?type=$1
 
 RewriteRule ^matches/([0-9]{2}).(xml|json)$ matches.php?id=$1&type=$2
-RewriteRule ^countries/([a-z]{3}).(xml|json)$ counties.php?shortcode=$1&type=$2
+RewriteRule ^matches.(xml|json)$ matches.php?type=$2
 	$shortcode = false;
 }
 
+if ( isset($_GET['group']) && !empty($_GET['group']) ) {
+	$group = $_GET['group'];
+}
+else {
+	$group = false;
+}
+
 if ( isset($_GET['type']) && !empty($_GET['type']) ) {
 	$type = $_GET['type'];
 }
 else {
-	$type = 'xml';
+	$type = 'json';
 }
 
+try {
+
 switch ( $method ) {
 	case 'GET':
-		header( "content-type: text/xml; charset=utf-8" );
-		
-		$doc = new DOMDocument('1.0', 'utf-8');
-		$doc->formatOutput = true;
-		$r = $doc->createElement( "countries" );
-		$doc->appendChild( $r );
-				
+		if ( $type == 'json' ) {
+			header( "content-type: application/json; charset=utf-8" );
+		}
+		else {
+			header( "content-type: text/xml; charset=utf-8" );
+			
+			$doc = new DOMDocument('1.0', 'utf-8');
+			$doc->formatOutput = true;
+		}
+
 		if  ( $shortcode ) {
 			$stmt = $db->prepare("SELECT * FROM countries WHERE code = :code");
-			$stmt->bindParam(':code', $shortcode, PDO::PARAM_STR, 3);
+			$stmt->bindParam(':code', $shortcode, PDO::PARAM_STR, 2);
 			
 			$stmt->execute();
 			
-			$obj = $stmt->fetch(PDO::FETCH_OBJ);
+			$array = $stmt->fetch(PDO::FETCH_ASSOC);
 			
-			$country = $doc->createElement( "country" );
-			$country->setAttribute('code', $obj->code);
-			$r->appendChild( $country );
-			
-				$name = $doc->createElement( "name", $obj->name  );
-				$country->appendChild( $name );	
-			
-				$group = $doc->createElement( "group", $obj->group  );
-				$country->appendChild( $group );				
+			if ( $type == 'json' ) {
+				// Add links
+				$array['links']['group'] = "http://localhost/ek-api/countries/".$array['group'].".".$type ;
+				echo json_encode($array);
+			}
+			else {
+				$country = $doc->createElement( "country" );
+				$country->setAttribute('code', $array['code']);
+				$country->setAttribute('name', $array['name']);
+				$country->setAttribute('group', $array['group']);
+				$doc->appendChild( $country );
+				
+					$links = $doc->createElement( "links"  );
+					$country->appendChild( $links );
+					
+						$group = $doc->createElement( "group", "http://localhost/ek-api/countries/".$array['group'].".".$type  );
+						$links->appendChild( $group );
+				
+				echo $doc->saveXML();
+			}
 		}
 		else {
+		// Groep of alles
+			if ( $group ) {
+				$stmt = $db->prepare("SELECT * FROM countries WHERE `group` = :group");
+				$stmt->bindParam(':group', $group);
 			
-			foreach($db->query('SELECT * FROM countries') as $row) {
+				$stmt->execute();
+			}
+			else {
+				$stmt = $db->query('SELECT * FROM countries');
+				
+			}
+		
+			$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
 			
-			$country = $doc->createElement( "country" );
-			$country->setAttribute('code', $row['code']);
-			$r->appendChild( $country );
+			if ( $type == 'json' ) {
+				$new_array = Array();
+				foreach($array as $row) {
+					$row['links']['self'] = "http://localhost/ek-api/countries/".$row['code'].".".$type ;
+					$row['links']['group'] = "http://localhost/ek-api/countries/".$row['group'].".".$type ;
+					$new_array[] = $row;
+				}
+
+				echo json_encode($new_array);
+			}
+			else {
+				$r = $doc->createElement( "countries" );
+				$doc->appendChild( $r );			
 			
-				$name = $doc->createElement( "name", $row['name']  );
-				$country->appendChild( $name );	
+				foreach($array as $row) {
+				
+				$country = $doc->createElement( "country" );
+				$country->setAttribute('code', $row['code']);
+				$country->setAttribute('name', $row['name']);
+				$country->setAttribute('group', $row['group']);
+				$r->appendChild( $country );
 			
-				$group = $doc->createElement( "group", $row['group']  );
-				$country->appendChild( $group );			
+					$links = $doc->createElement( "links"  );
+					$country->appendChild( $links );
+					
+						$self = $doc->createElement( "self", "http://localhost/ek-api/countries/".$row['code'].".".$type  );
+						$links->appendChild( $self );
+						$group = $doc->createElement( "group", "http://localhost/ek-api/countries/".$row['group'].".".$type  );
+						$links->appendChild( $group );
+				}
+				echo $doc->saveXML();
 			}
-			
 		}	
-		echo $doc->saveXML();
 	 break;
 	
 	case 'PUT':		
-	/*
+	
 		$inputSocket = fopen("php://input", "r");
 		$inputData = '';
 		
 		fclose($inputSocket);
 		
 		//$readData = simplexml_load_string($inputData);
-		if ( file_exists('reviews/'.$id.'.xml') ) {
-			$isNewFile = true;
-		} else {
-			$isNewFile = false;
+		
+		$readData = new SimpleXMLElement($inputData);
+		print_r((string)$readData->code);
+		
+		$code = (string)$readData->code;
+		$name = (string)$readData->name;
+		$group = (string)$readData->group;
+			$stmt = $db->prepare("INSERT INTO countries(code, name, group) VALUES (:code, :name, :group)");
+			$stmt->bindParam(':code', $code, PDO::PARAM_STR, 2);
+			$stmt->bindParam(':name', $name, PDO::PARAM_STR, 50);
+			$stmt->bindParam(':group', $group, PDO::PARAM_STR, 1);
+		    $count = $db->exec($stmt);
+
+		if ( $count ) {
+			header('http/1.0 201 Created');
+			
+			$sth = $db->prepare("SELECT * FROM countries WHERE code = :code");
+			$sth->bindParam(':code', $code, PDO::PARAM_STR, 2);
+			$sth->execute();
+			
+			$array = $sth->fetch(PDO::FETCH_ASSOC);
+		
+			if ( $type == 'json' ) {
+				header( "content-type: application/json; charset=utf-8" );
+				
+				$array['links']['group'] = "http://localhost/ek-api/countries/".$array['group'].".".$type ;
+				echo json_encode($array);
+			}
+			else {
+				header( "content-type: text/xml; charset=utf-8" );
+				
+				$doc = new DOMDocument('1.0', 'utf-8');
+				$doc->formatOutput = true;
+				
+				$country = $doc->createElement( "country" );
+				$country->setAttribute('code', $array['code']);
+				$country->setAttribute('name', $array['name']);
+				$country->setAttribute('group', $array['group']);
+				$doc->appendChild( $country );
+				
+					$links = $doc->createElement( "links"  );
+					$country->appendChild( $links );
+					
+						$group = $doc->createElement( "group", "http://localhost/ek-api/countries/".$array['group'].".".$type  );
+						$links->appendChild( $group );
+				
+				echo $doc->saveXML();
+			}	
+			
 		}
-		// schrijf bestand.
+		else {
+			header('http/1.0 500 Internal Server Error');
+		}
+
+		/* query uitvoeren
 		$handle = fopen('reviews/'.$id.'.xml', 'w');
 		if ( fwrite($handle, $inputData) ) {
 			if ($isNewFile) {
-				header('http/1.0 201 Created');
+				
 			} else {
 				header('http/1.0 204');
 			}
 		else {
 			header('http/1.0 500 Internal Server Error');
 		}
-		fclose($handle);	*/		
+		fclose($handle);	*/
 	 break;
 	 
 	case 'HEAD':
 		header('Allow: OPTIONS, HEAD, GET, PUT');
 	 break;
 }
+
+}
+catch(PDOException $e)
+{
+	header('http/1.0 500 Internal Server Error');
+    echo $e->getMessage();
+}
 ?>
 	$id = $_GET['id'];
 }
 else {
-	$id = 'undefined';
+	$id = false;
 }
 
 if ( isset($_GET['type']) && !empty($_GET['type']) ) {
 }
 
 switch ( $method ) {
+	// Match data opvragen
 	case 'GET':
+		if ( $type == 'json' ) {
+			header( "content-type: application/json; charset=utf-8" );
+		}
+		else {
+			header( "content-type: text/xml; charset=utf-8" );
+			
+			$doc = new DOMDocument('1.0', 'utf-8');
+			$doc->formatOutput = true;
+		}
+		
+		if ( $id ) {
 
-		switch( $type ) {
-			case 'xml':
-				header( "content-type: text/xml; charset=utf-8" );
-				echo '<match><id>3154</id></match>';
-			 break;
+			$stmt = $db->prepare("SELECT m.*, 
+									phome.name AS player_home,
+									pout.name AS player_out,
+									phome.group AS group_home,
+									pout.group AS group_out
+								FROM matches AS m 
+								LEFT JOIN countries AS phome
+									ON m.home = phome.code
+								LEFT JOIN countries AS pout
+									ON m.out = pout.code
+								WHERE m.id = :id ");
+			$stmt->bindParam(':id', $id);
+			
+			// wedstrijd 25 is niet meer in een groep
+			//if ( $id > 24 )
+			
+			$stmt->execute();
+			
+			$array = $stmt->fetch(PDO::FETCH_ASSOC);
+			
+			if ( $type == 'json' ) {
+				// Add links
+				$array['links']['player_home'] = "http://localhost/ek-api/countries/".$array['home'].".".$type ;
+				$array['links']['group_home'] = "http://localhost/ek-api/countries/".$array['group_home'].".".$type ;
+				$array['links']['player_out'] = "http://localhost/ek-api/countries/".$array['out'].".".$type ;
+				$array['links']['group_out'] = "http://localhost/ek-api/countries/".$array['group_out'].".".$type ;
+				echo json_encode($array);
+			}
+			else {
+				$match = $doc->createElement( "match" );
+				$doc->appendChild( $match );
+				$match->setAttribute('id', $array['id']);
+					$home = $doc->createElement( "player_home",  $array['player_home'] );
+					$home->setAttribute('goals', $array['goals_home']);	
+					$match->appendChild( $home );
+		
+					$out = $doc->createElement( "player_out",  $array['player_out'] );
+					$out->setAttribute('goals', $array['goals_out']);	
+					$match->appendChild( $out );
+		
+
+					$links = $doc->createElement( "links"  );
+					$match->appendChild( $links );
+					
+						$player_home = $doc->createElement( "player_home", "http://localhost/ek-api/countries/".$array['home'].".".$type  );
+						$links->appendChild( $player_home );
+						
+						$group_home = $doc->createElement( "group_home", "http://localhost/ek-api/countries/".$array['group_home'].".".$type  );
+						$links->appendChild( $group_home );
+						
+						$player_out = $doc->createElement( "player_out", "http://localhost/ek-api/countries/".$array['out'].".".$type  );
+						$links->appendChild( $player_out );
+						
+						$group_out = $doc->createElement( "group_out", "http://localhost/ek-api/countries/".$array['group_out'].".".$type  );
+						$links->appendChild( $group_out );
+				
+				echo $doc->saveXML();
+			}		
+		}
+		else {
+			$r = $doc->createElement( "matches" );
+			$doc->appendChild( $r );
+		
+			$stmt = $db->query("SELECT m.*, 
+									phome.name AS player_home,
+									pout.name AS player_out,
+									phome.group AS group_home,
+									pout.group AS group_out
+								FROM matches AS m 
+								LEFT JOIN countries AS phome
+									ON m.home = phome.code
+								LEFT JOIN countries AS pout
+									ON m.out = pout.code");
+			$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
+			
+			if ( $type == 'json' ) {
+			
+				$new_array = Array();
+				foreach($array as $row) {
+					$row['links']['player_home'] = "http://localhost/ek-api/countries/".$row['home'].".".$type ;
+					$row['links']['group_home'] = "http://localhost/ek-api/countries/".$row['group_home'].".".$type ;
+					$row['links']['player_out'] = "http://localhost/ek-api/countries/".$row['out'].".".$type ;
+					$row['links']['group_out'] = "http://localhost/ek-api/countries/".$row['group_out'].".".$type ;					
+					$new_array[] = $row;
+				}
+
+				echo json_encode($new_array);
+				//echo json_encode($array);
+			}
+			else {
+				//echo '<pre>';
+				//print_r($array);
+				//echo '</pre>';
+				
+				foreach($array as $row) {
+				
+				$match = $doc->createElement( "match" );
+				$match->setAttribute('id', $row['id']);
+				$r->appendChild( $match );
+				
+					$home = $doc->createElement( "player_home",  $row['player_home'] );
+					$home->setAttribute('goals', $row['goals_home']);	
+					$match->appendChild( $home );
+		
+					$out = $doc->createElement( "player_out",  $row['player_out'] );
+					$out->setAttribute('goals', $row['goals_out']);	
+					$match->appendChild( $out );
+				
+					$links = $doc->createElement( "links"  );
+					$match->appendChild( $links );
+					
+						$player_home = $doc->createElement( "player_home", "http://localhost/ek-api/countries/".$row['home'].".".$type  );
+						$links->appendChild( $player_home );
+						
+						$group_home = $doc->createElement( "group_home", "http://localhost/ek-api/countries/".$row['group_home'].".".$type  );
+						$links->appendChild( $group_home );
+						
+						$player_out = $doc->createElement( "player_out", "http://localhost/ek-api/countries/".$row['out'].".".$type  );
+						$links->appendChild( $player_out );
+						
+						$group_out = $doc->createElement( "group_out", "http://localhost/ek-api/countries/".$row['group_out'].".".$type  );
+						$links->appendChild( $group_out );
+
+				}
+				echo $doc->saveXML();
+			}
 		}
 	
 	 break;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.