Commits

stollie  committed 543fc22 Merge

Merge

  • Participants
  • Parent commits 5c7d83b, ba8bea5

Comments (0)

Files changed (6)

 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/([a-z]{2})$ matches.php?shortcode=$1
 
 RewriteRule ^matches/([0-9]{2}).(xml|json)$ matches.php?id=$1&type=$2
-RewriteRule ^matches.(xml|json)$ matches.php?type=$1
+RewriteRule ^matches.(xml|json)$ matches.php?type=$1
+RewriteRule ^matches/([0-9]{2})$ matches.php?id=$1

File countries.php

 			header( "content-type: text/xml; charset=utf-8" );
 			
 			$doc = new DOMDocument('1.0', 'utf-8');
+			$doc->preserveWhiteSpace = false;
 			$doc->formatOutput = true;
+			//creating an xslt adding processing line
+			$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="countries.xsl"');
+			//adding it to the xml
+			$doc->appendChild($xslt);
 		}
 
 		if  ( $shortcode ) {
 		$stmt->bindParam(':code', $shortcode , PDO::PARAM_STR, 2 );
 		$stmt->execute();
 		if ( $stmt->rowCount() ) {
-			header('http/1.0 201');
+			header('http/1.0 200');
 		}
 		else {
 			header('http/1.0 500 Internal Server Error');

File countries.xsl

+<?xml version="1.0" encoding="utf-8"?>
+<xsl:stylesheet version="1.0"
+xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:template match="/">
+  <html>
+  <body>
+  <h2>Teams</h2>
+  <table border="1">
+    <tr bgcolor="#9acd32">
+      <th>Team</th>
+      <th>Group</th>
+    </tr>
+    <xsl:for-each select="countries/country">
+		<xsl:sort select="@group"/>
+    <tr>
+      <td><xsl:value-of select="@name"/></td>
+      <td><xsl:value-of select="@group"/></td>
+    </tr>
+    </xsl:for-each>
+  </table>
+  </body>
+  </html>
+</xsl:template>
+
+</xsl:stylesheet> 

File documentation.html

 		<h3>Een land verwijderen</h3>
 		<p>Aan de hand van de landcode het land verwijderen.</p>
 		<pre><span class="label label-warning">DELETE</span> /countries/([a-z]{2}).(xml|json)</pre>
-		<pre><span class="label">Voorbeeld</span> /countries/nl.xml</pre>		
+		<pre><span class="label">Voorbeeld</span> /countries/nl</pre>
 		
 	<dl class="dl-horizontal">	
 		<h1><a name="matches"></a>Wedstrijden</h1>
     ['group'] =&gt; B
 )</pre>
 
+		<h3>Een wedstrijd verwijderen</h3>
+		<pre><span class="label label-warning">DELETE</span> /matches/([0-9]{2})/</pre>
+		<pre><span class="label">Voorbeeld</span> /matches/15</pre>
+
+
 	</div>
 	<div class="span5 offset1">	
 		<h3>Header errors</h3>
 		<p>500 bij server error</p>
 	</div>
-
+		
     </div> <!-- /container -->
 
     <!-- Le javascript
 			header( "content-type: text/xml; charset=utf-8" );
 			
 			$doc = new DOMDocument('1.0', 'utf-8');
+			$doc->preserveWhiteSpace = false;
 			$doc->formatOutput = true;
+			//creating an xslt adding processing line
+			$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="matches.xsl"');
+			//adding it to the xml
+			$doc->appendChild($xslt);
 		}
 		
 		if ( $id ) {
 		$stmt->bindParam(':id', $id , PDO::PARAM_STR, 2 );
 		$stmt->execute();
 		if ( $stmt->rowCount() ) {
-			header('http/1.0 201');
+			header('http/1.0 200');
 		}
 		else {
 			header('http/1.0 500 Internal Server Error');
 		}
 	 break;		 
-	 
+	
+	case 'POST':
+		$inputData = $_SERVER['HTTP_USER_AGENT'];
+		// schrijf bestand.
+		$handle = fopen('logs/log.txt', 'w');
+		fwrite($handle, $inputData);
+		fclose($handle);
+	
+		if ( !empty($_POST) ) {
+			$home = $_POST['home'];
+			$out = $_POST['out'];
+			$goals_home = $_POST['goals_home'];
+			$goals_out = $_POST['goals_out'];
+
+			$stmt = $db->prepare("INSERT INTO `matches`(`home`, `out`, `goals_home`, `goals_out`) VALUES (:home, :out, :goals_home, :goals_out )");
+			$stmt->bindParam(':home', $home , PDO::PARAM_STR, 2);
+			$stmt->bindParam(':out', $out , PDO::PARAM_STR, 2);
+			$stmt->bindParam(':goals_home', $goals_home , PDO::PARAM_INT, 2);
+			$stmt->bindParam(':goals_out', $goals_out , PDO::PARAM_INT, 2 );
+
+			if ( $stmt->execute() ) {
+				header('http/1.0 201');
+				$id = $stmt->lastInsertId();
+				$get = $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");
+				$get->bindParam(':id', $id, PDO::PARAM_INT, 2);
+				$get->execute();
+				
+				$array = $get->fetch(PDO::FETCH_ASSOC);
+			
+				if ( $type == 'json' ) {
+					header( "content-type: application/json; charset=utf-8" );
+					// 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 {
+					header( "content-type: text/xml; charset=utf-8" );
+					
+					$doc = new DOMDocument('1.0', 'utf-8');
+					$doc->formatOutput = true;
+					
+					$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 {
+				header('http/1.0 500 Internal Server Error');
+			}
+		}
+		else {
+			header('http/1.0 400 Bad Request');
+		}
+	 break;
+	
 	case 'HEAD':
 		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
 		header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
+<?xml version="1.0" encoding="utf-8"?>
+<xsl:stylesheet version="1.0"
+xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:template match="/">
+  <html>
+  <body>
+  <h2>Matches</h2>
+  <table border="1">
+    <tr bgcolor="#9acd32">
+      <th>Team</th>
+      <th>Goals</th>
+      <th> - </th>
+      <th>Goals</th>
+	  <th>Team</th>
+    </tr>
+    <xsl:for-each select="matches/match">
+    <tr>
+      <td><xsl:value-of select="player_home"/></td>
+      <td><xsl:value-of select="player_home/@goals"/></td>
+	  <td> - </td>
+      <td><xsl:value-of select="player_out/@goals"/></td>
+      <td><xsl:value-of select="player_out"/></td>	  
+    </tr>
+    </xsl:for-each>
+  </table>
+  </body>
+  </html>
+</xsl:template>
+
+</xsl:stylesheet>