Commits

Anonymous committed 619e8a9

Better xslt, docs en coding

Comments (0)

Files changed (8)

 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/([a-z]{2}).(xml|json)$ matches.php?shortcode=$1&type=$2
 RewriteRule ^matches.(xml|json)$ matches.php?type=$1
 RewriteRule ^matches/([0-9]{2})$ matches.php?id=$1
 //ini_set("display_errors",false) ;
 
 $method = $_SERVER['REQUEST_METHOD'];
-$siteurl = "http://localhost/ek-api/";
+$siteurl = "http://".$_SERVER['HTTP_HOST']."/ek-api/";
 
 if ( isset($_GET['shortcode']) && !empty($_GET['shortcode']) ) {
 	$shortcode = $_GET['shortcode'];
 			$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 = $db->prepare("SELECT * FROM countries WHERE code = :code");
 			$stmt->bindParam(':code', $shortcode, PDO::PARAM_STR, 2);
 			
 			
 			if ( $type == 'json' ) {
 				// Add links
+				$array['links']['self'] = $siteurl."countries/".$array['code'].".".$type ;
 				$array['links']['group'] = $siteurl."countries/".$array['group'].".".$type ;
+				$array['links']['matches'] = $siteurl."matches/".$array['code'].".".$type ;
 				$array = array('countries' => $array);
 				echo json_encode($array);
 			}
-			else {			
+			else {	
+				//creating an xslt adding processing line
+				$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'country.xsl"');
+				//adding it to the xml
+				$doc->appendChild($xslt);
+			
 				$country = $doc->createElement( "country" );
 				$country->setAttribute('code', $array['code']);
 				$country->setAttribute('name', $array['name']);
 					$links = $doc->createElement( "links"  );
 					$country->appendChild( $links );
 					
+						$self = $doc->createElement( "self", $siteurl."countries/".$array['code'].".".$type  );
+						$links->appendChild( $self );		
 						$group = $doc->createElement( "group", $siteurl."countries/".$array['group'].".".$type  );
 						$links->appendChild( $group );
+						$matches = $doc->createElement( "matches", $siteurl."matches/".$array['code'].".".$type  );
+						$links->appendChild( $matches );
 				
 				echo $doc->saveXML();
 			}
 		}
-		else {
+		else {		
 		// Groep of alles
 			if ( $group ) {
 				$stmt = $db->prepare("SELECT * FROM countries WHERE `group` = :group");
 				foreach($array as $row) {
 					$row['links']['self'] = $siteurl."countries/".$row['code'].".".$type ;
 					$row['links']['group'] = $siteurl."countries/".$row['group'].".".$type ;
+					$row['links']['matches'] = $siteurl."matches/".$row['code'].".".$type ;
 					$new_array[] = $row;
 				}
 				$new_array = array('countries' => $new_array);
 				echo json_encode($new_array);
 			}
 			else {
+				//creating an xslt adding processing line
+				$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'countries.xsl"');
+				//adding it to the xml
+				$doc->appendChild($xslt);
 				$r = $doc->createElement( "countries" );
 				$doc->appendChild( $r );			
 			
 						$links->appendChild( $self );
 						$group = $doc->createElement( "group", $siteurl."countries/".$row['group'].".".$type  );
 						$links->appendChild( $group );
+						$matches = $doc->createElement( "matches", $siteurl."matches/".$row['code'].".".$type  );
+						$links->appendChild( $matches );						
 				}
 				echo $doc->saveXML();
 			}
 				echo json_encode($array);
 			}
 			else {
+				//creating an xslt adding processing line
+				$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'country.xsl"');
+				//adding it to the xml
+				$doc->appendChild($xslt);			
 				header( "content-type: text/xml; charset=utf-8" );
 				
 				$doc = new DOMDocument('1.0', 'utf-8');
 					$links = $doc->createElement( "links"  );
 					$country->appendChild( $links );
 					
+						$self = $doc->createElement( "self", $siteurl."countries/".$array['code'].".".$type  );
+						$links->appendChild( $self );		
 						$group = $doc->createElement( "group", $siteurl."countries/".$array['group'].".".$type  );
 						$links->appendChild( $group );
+						$matches = $doc->createElement( "matches", $siteurl."matches/".$array['code'].".".$type  );
+						$links->appendChild( $matches );							
 				
 				echo $doc->saveXML();
 			}	
 		$stmt->execute();
 		
 		if ( $stmt->rowCount() ) {
-			header('http/1.0 201 Created');
+			header('http/1.0 200 Created');
 			
 			$get = $db->prepare("SELECT * FROM countries WHERE code = :code");
 			$get->bindParam(':code', $code, PDO::PARAM_STR, 2);
 				$array['links']['group'] = $siteurl."countries/".$array['group'].".".$type ;
 				echo json_encode($array);
 			}
-			else {
+			else {	
 				header( "content-type: text/xml; charset=utf-8" );
 				
 				$doc = new DOMDocument('1.0', 'utf-8');
 				$doc->formatOutput = true;
-				
+				//creating an xslt adding processing line
+				$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'country.xsl"');
+				//adding it to the xml
+				$doc->appendChild($xslt);	
 				$country = $doc->createElement( "country" );
 				$country->setAttribute('code', $array['code']);
 				$country->setAttribute('name', $array['name']);
     <tr bgcolor="#9acd32">
       <th>Team</th>
       <th>Group</th>
+	  <th>Matches</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>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="links/self"/></xsl:attribute> 
+			<xsl:value-of select="@name"/>
+		</a>
+	  </td>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="links/group"/></xsl:attribute> 
+			<xsl:value-of select="@group"/>
+		</a>	  
+	  </td>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="links/matches"/></xsl:attribute> 
+			bekijk
+		</a>	  
+	  </td>	  
     </tr>
     </xsl:for-each>
   </table>
+<?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>
+	  <th>Matches</th>
+    </tr>
+    <tr>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="country/links/self"/></xsl:attribute> 
+			<xsl:value-of select="country/@name"/>
+		</a>
+	  </td>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="country/links/group"/></xsl:attribute> 
+			<xsl:value-of select="country/@group"/>
+		</a>	  
+	  </td>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="country/links/matches"/></xsl:attribute> 
+			bekijk
+		</a>	  
+	  </td>	  
+    </tr>
+  </table>
+  </body>
+  </html>
+</xsl:template>
+
+</xsl:stylesheet> 

documentation.html

 
     <div class="container">
 	<div class="span5">	
-		<h1><a name="countries"></a>Landen</h1>
+		<a name="countries"></a>
+		<h1>Landen</h1>
 		<h3>Alle landen die meespelen</h3>
 		<pre><span class="label label-info">GET</span> /countries.(xml|json)</pre>
 		<pre><span class="label">Voorbeeld</span> <a href="countries.xml">/countries.xml</a></pre>	
-		<pre><p><span class="label label-inverse">Headers</span> Succes: 201 Error: 500</p></pre>	
+
 		<h3>Alle landen van een groep</h3>
 		<p>Mogelijkheden: A, B, C en D</p>
 		<pre><span class="label label-info">GET</span> /countries/[A-D].(xml|json)</pre>
 		<pre><span class="label label-info">GET</span> /countries/([a-z]{2}).(xml|json)</pre>
 		<pre><span class="label">Voorbeeld</span> <a href="countries/nl.xml">/countries/nl.xml</a></pre>
 
+		<h3>Resultaat bewerken</h3>
+		<pre><span class="label label-success">PUT</span> /matches.xml</pre>
+		<p>Format die gestuurd moet worden.</p>
+		<pre><span class="label">Data</span> 
+&lt;country&gt;
+	&lt;code&gt;de&lt;/code&gt;
+	&lt;name&gt;Germany&lt;/name&gt;
+	&lt;group&gt;D&lt;/group&gt;
+&lt;/country&gt;</pre>			
+
 		<h3>Informatie van een land toevoegen</h3>
 		<pre><span class="label label-important">POST</span> /countries.(xml|json)</pre>
 		<pre><span class="label">Voorbeeld</span> /countries.xml</pre>
 		<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</pre>
-		
-	<dl class="dl-horizontal">	
-		<h1><a name="matches"></a>Wedstrijden</h1>
+
+		<a name="matches"></a>
+		<h1>Wedstrijden</h1>
 		
 		<h3>Informatie van een wedstrijd</h3>
 		<p>Aan de hand van de wedstrijdnummer de wedstrijd verwijderen.</p>
 		<pre><span class="label label-info">GET</span> /matches/([0-9]{2}).(xml|json)</pre>
 		<pre><span class="label">Voorbeeld</span> <a href="matches/12.xml">/matches/12.xml</a></pre>		
 
+		<h3>Alle wedstrijden van een land</h3>
+		<p>Alle wedstrijden laten zien die gespeeld zijn en gespeeld gaan worden, door een land.</p>
+		<pre><span class="label label-info">GET</span> /matches/([a-z]{2}).(xml|json)</pre>
+		<pre><span class="label">Voorbeeld</span> <a href="matches/nl.xml">/matches/nl.xml</a></pre>			
+		
 		<h3>Alle wedstrijden</h3>
 		<p>Alle wedstrijden laten zien die gespeeld zijn en gespeeld worden.</p>
 		<pre><span class="label label-info">GET</span> /matches(xml|json)</pre>
 )</pre>
 
 		<h3>Een wedstrijd verwijderen</h3>
-		<pre><span class="label label-warning">DELETE</span> /matches/([0-9]{2})/</pre>
+		<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>
+		<pre><p><span class="label label-success">Succes</span> 200</p></pre>
+		<pre><p><span class="label label-success">Succes & Added</span> 201</p></pre>	
+		<pre><p><span class="label label-important">Server fout</span> 500</p></pre>		
 	</div>
 		
     </div> <!-- /container -->
+<?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>
+    <tr>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="match/links/player_home"/></xsl:attribute> 
+			<xsl:value-of select="match/player_home"/>
+		</a>
+	  </td>
+      <td><xsl:value-of select="match/player_home/@goals"/></td>
+	  <td> - </td>
+      <td><xsl:value-of select="match/player_out/@goals"/></td>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="match/links/player_out"/></xsl:attribute> 
+			<xsl:value-of select="match/player_out"/>
+		</a>
+	  </td>
+    </tr>
+  </table>
+  </body>
+  </html>
+</xsl:template>
+
+</xsl:stylesheet> 
 //ini_set("display_errors",false) ;
 
 $method = $_SERVER['REQUEST_METHOD'];
-$siteurl = "http://localhost/ek-api/";
+$siteurl = "http://".$_SERVER['HTTP_HOST']."/ek-api/";
 
 if ( isset($_GET['shortcode']) && !empty($_GET['shortcode']) ) {
 	$shortcode = $_GET['shortcode'];
 			$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 = $db->prepare("SELECT m.*, 
 									phome.name AS player_home,
 									pout.name AS player_out,
 				echo json_encode($array);
 			}
 			else {
+				//creating an xslt adding processing line
+				$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'match.xsl"');
+				//adding it to the xml
+				$doc->appendChild($xslt);
+			
 				$match = $doc->createElement( "match" );
 				$doc->appendChild( $match );
 				$match->setAttribute('id', $array['id']);
 				echo json_encode($new_array);
 			}
 			else {
+				//creating an xslt adding processing line
+				$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'matches.xsl"');
+				//adding it to the xml
+				$doc->appendChild($xslt);			
 				$r = $doc->createElement( "matches" );
 				$doc->appendChild( $r );
 
 				echo $doc->saveXML();
 			}
 		}
-	
 	 break;
 
 	case 'PUT':		
 			}
 			else {
 				header( "content-type: text/xml; charset=utf-8" );
-				
+				//creating an xslt adding processing line
+				$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'match.xsl"');
+				//adding it to the xml
+				$doc->appendChild($xslt);
 				$doc = new DOMDocument('1.0', 'utf-8');
 				$doc->formatOutput = true;
 				
 				
 				echo $doc->saveXML();
 			}	
-			
 		}
 		else {
 			header('http/1.0 500 Internal Server Error');
 				else {
 					header( "content-type: text/xml; charset=utf-8" );
 					
+					//creating an xslt adding processing line
+					$xslt = $doc->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$siteurl.'match.xsl"');
+					//adding it to the xml
+					$doc->appendChild($xslt);
 					$doc = new DOMDocument('1.0', 'utf-8');
 					$doc->formatOutput = true;
 					
 	 
 	case 'OPTIONS':
 	default:
-		header('Allow: OPTIONS, HEAD, GET, PUT');
+		header('Allow: OPTIONS, HEAD, GET, PUT, POST');
 	 break;
 }
 ?>
     </tr>
     <xsl:for-each select="matches/match">
     <tr>
-      <td><xsl:value-of select="player_home"/></td>
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="links/player_home"/></xsl:attribute> 
+			<xsl:value-of select="player_home"/>
+		</a>
+	  </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>	  
+      <td>
+		<a><xsl:attribute name="href">
+			<xsl:value-of select="links/player_out"/></xsl:attribute> 
+			<xsl:value-of select="player_out"/>
+		</a>
+	  </td>
     </tr>
     </xsl:for-each>
   </table>