Преглед изворни кода

Up to added functions.get_maintainer()

Foppe Hemminga пре 5 година
родитељ
комит
c681649bc3
4 измењених фајлова са 199 додато и 7 уклоњено
  1. 59 0
      functions.php
  2. 53 6
      index.php
  3. 84 0
      snippets.php
  4. 3 1
      sqlite.php

+ 59 - 0
functions.php

@@ -0,0 +1,59 @@
+<?php
+
+function add_maintainer( $api ) {
+	/**
+	 * @param string $api Provided Torn API string
+	 * @return array XID and name of maintainer
+	 */
+	$maintainer = array(0, '?');
+	$url = "https://api.torn.com/user/?key=";
+	$url .= $api;
+
+	$response = json_decode( file_get_contents( $url ) );
+	if ( property_exists( $response, "error" ) ) {
+		print "ERROR " . $response->error->code . ": " . $response->error->error . "\n";
+		$maintainer[0] = $response->error->code;
+		$maintainer[1] = $response->error->error;
+	} else {
+		$pdo = null;
+		try {
+			$pdo = new PDO("sqlite:" . './slork.sqlite');
+		} catch (PDOException $e) {
+			// handle the exception here
+		}
+		$query = "INSERT OR REPLACE INTO maintainer (maintainer, api) VALUES (?, ?);";
+		$stmt = $pdo->prepare( $query );
+		$stmt->execute( [$response->player_id, $api] );
+		$query = "INSERT OR REPLACE INTO player (maintainer, player, property, value) VALUES (?, ?, ?, ?);";
+		$stmt = $pdo->prepare( $query );
+		$stmt->execute( [$response->player_id, $response->player_id,
+						'name', $response->name] );
+		// $pdo->commit();
+		$maintainer[0] = $response->player_id;
+		$maintainer[1] = $response->name;
+	}
+	return $maintainer;
+}
+
+
+function get_maintainer( $id ) {
+	/**
+	 * @param string $id Maintainer's XID
+	 * @return array XID and name of maintainer
+	 */
+	$pdo = null;
+	try {
+		$pdo = new PDO("sqlite:" . './slork.sqlite');
+	} catch (PDOException $e) {
+		// handle the exception here
+	}
+	$query = "SELECT value FROM player WHERE maintainer = ? AND player = ? AND property = 'name';";
+	$stmt = $pdo->prepare( $query );
+	$stmt->execute( [$id, $id] );
+	$maintainer = [0, '?'];
+	$row = $stmt->fetchAll();
+	$maintainer[0] = $id;
+	$maintainer[1] = $row['value'];
+
+	return $maintainer;
+}

+ 53 - 6
index.php

@@ -6,19 +6,50 @@ try {
 } catch (PDOException $e) {
 	// handle the exception here
 }
+
+require "functions.php";
+
 /**
  * BUSINESS LOGIC
  *
+ * $screen = 0; // start
  * 1) First we validate or create a maintainer.
+ * Adding a maintainer is a detour but results
+ * in the same situation as selecting one.
+ * 1b) Cannot validate maintainer
+ * $screen = 1;
+ * 1a) Can validate maintainer
+ * $screen = 2; $_REQUEST['m']; // maintainer exists
  * 2) The maintainer will be presented a page with
  *    players. He can edit or remove a player.
+ * $screen = 3; $_REQUEST['e']; // edit player
+ * $screen = 4; $_REQUEST['x']; // remove player
  * 3) The edit page allows the maintainer to add
  *    or remove features from the player.
  */
-if ( isset( $_REQUEST['m'] ) ) {
-    // There is a maintainer
-    echo "Empty statement";
+// global
+$screen = 0;
+$maintainer = array(0, '?');
+if ( isset( $_REQUEST['i'] ) && $_REQUEST['i'] != '' ) {
+	// Try adding new maintainer ...
+    $maintainer = add_maintainer( $_REQUEST['i'] );
+    // ... then return to start screen
+	$screen = 0;
+}
+if ( isset( $_REQUEST['m'] ) && $_REQUEST['m'] != '' ) {
+	// There is a maintainer
+    $maintainer = get_maintainer( $_REQUEST['m'] );
+	$screen = 2;
+	if (isset($_REQUEST['e'])) {
+		// Request for edit screen
+		$screen = 3;
+	}
+	if (isset($_REQUEST['x'])) {
+		// Request to remove player
+		$screen = 4;
+	}
 }
+assert( '$screen >= 0', "By now \$screen should be set to a value >= 0." );
 ?>
 
 <!doctype html>
@@ -40,10 +71,26 @@ if ( isset( $_REQUEST['m'] ) ) {
 			  One of three columns
 		  </div>
 		  <div class="col-md">
-			  One of three columns
-		  </div>
+              <?php
+			  require "snippets.php";
+              if ( $maintainer[0] > 0 && $maintainer[0] < 100 ) {
+                  // Error in creating maintainer
+                  print( "<pre>" );
+                  print( "ERROR $maintainer[0]: $maintainer[1]" );
+                  print( "</pre>" );
+                  // Try again
+                  $screen = 0;
+              }
+              // Choose or add a maintainer; default screen
+              print( $snippets[$screen] );
+              ?>
+          </div>
 		  <div class="col-md">
-			  One of three columns
+              <pre>
+                  <?php
+                  print_r( $_REQUEST );
+                  ?>
+              </pre>
 		  </div>
 	  </div>
   </div>

+ 84 - 0
snippets.php

@@ -0,0 +1,84 @@
+<?php
+
+// This file contains HTML snippets for the several possible forms
+
+$pdo = null;
+try {
+	$pdo = new PDO("sqlite:" . './slork.sqlite');
+} catch (PDOException $e) {
+	// handle the exception here
+}
+// Select all maintainers
+$query = <<<QUERY
+	SELECT maintainer.maintainer, player.value 
+	FROM maintainer 
+	JOIN player ON maintainer.maintainer = player.player
+	WHERE player.property = 'name';
+QUERY;
+$all_maintainers = [];
+$rows = $pdo->query( $query );
+foreach ( $rows as $row ) {
+	$all_maintainers[$row['maintainer']] = $row['value'];
+}
+$snippets = [];
+$snippets[0] = <<<HTML
+	<form>
+			<!-- This form adds a new maintainer -->
+		<div class="form-group">
+    		<label for="newAPIKey">New maintainer</label>
+    		<input type="password" class="form-control" name="i" id="newAPIKey" aria-describedby="newAPIKey" placeholder="Enter API key">
+    		<small id="newAPIKey" class="form-text text-muted">Assuming you are new here, add your Torn API key</small>
+  		</div>
+  		<h4>OR</h4>
+  		<!-- This part selects an existing maintainer -->
+  		<div class="form-group">
+			<label for="existingMaintainer">Existing maintainer</label>
+    		<select class="form-control" name="m" id="existingMaintainer">
+				<option></option>
+HTML;
+
+foreach ( $all_maintainers as $maintainer_key => $maintainer_value ) {
+	$snippets[0] .= "\t\t\t\t<option value=\"$maintainer_key\">$maintainer_value [$maintainer_key]</option>";
+}
+
+$snippets[0] .= <<<HTML
+			</select>
+  		</div>
+		<button type="submit" class="btn btn-primary">Submit</button>
+	</form>
+HTML;
+
+$query = <<<SQL
+	SELECT player, value FROM player 
+	WHERE maintainer = ?
+	AND property = 'name' 
+	--AND player != ?
+SQL;
+$stmt = $pdo->prepare( $query );
+$stmt->execute( [$maintainer[0]] );
+$rows = $stmt->fetchAll();
+print( "<pre>" );
+print_r( $rows );
+print( "</pre>" );
+$players_id = array();
+foreach ( $rows as $row ) {
+	$players_id[] = $row['value'] . " [" . $row['player'] . "]";
+}
+
+$snippets[2] = <<<HTML
+	<table class="table">
+		<thead>
+			<tr>
+				<th>Player</th><th>Edit</th><th>Remove</th>
+			</tr>
+		</thead>
+		<tbody>
+HTML;
+		foreach ( $players_id as $player ) {
+			$snippets[2] .= "<tr><td>$player</td><td><button class=\"btn btn-primary\">Edit</button></td><td><button class=\"btn btn-danger\">Remove</button></td>";
+		}
+
+$snippets[2] .= <<<HTML
+		</tbody>
+	</table>
+HTML;

+ 3 - 1
sqlite.php

@@ -14,7 +14,9 @@ if ( !$pdo ) {
 $pdo->exec("DROP TABLE IF EXISTS maintainer;");
 $query = <<<QUERY
 CREATE TABLE IF NOT EXISTS maintainer (
-		id INTEGER PRIMARY KEY, maintainer INTEGER, apt TEXT);
+		id INTEGER PRIMARY KEY, maintainer INTEGER, api TEXT, 
+		properties TEXT DEFAULT '["name"]',
+		UNIQUE( maintainer ));
 QUERY;
 $stmt = $pdo->exec( $query );
 //$stmt->execute();