Kaynağa Gözat

Added 'remove player'

Foppe Hemminga 5 yıl önce
ebeveyn
işleme
172671f11c
3 değiştirilmiş dosya ile 123 ekleme ve 86 silme
  1. 23 0
      functions.php
  2. 16 9
      index.php
  3. 84 77
      snippets.php

+ 23 - 0
functions.php

@@ -55,5 +55,28 @@ function get_maintainer( $id ) {
 	$maintainer[0] = $id;
 	$maintainer[1] = $row['value'];
 
+	return $maintainer;
+}
+
+function delete_player( $player ) {
+	$pdo = null;
+	try {
+		$pdo = new PDO("sqlite:" . './slork.sqlite');
+	} catch (PDOException $e) {
+		// handle the exception here
+	}
+	$maintainer = $_REQUEST['m'];
+
+	$query = "DELETE FROM player WHERE player = ? AND maintainer = ?;";
+	$stmt = $pdo->prepare( $query );
+	$stmt->execute( [$player, $maintainer] );
+	$maintainer = get_maintainer( $maintainer );
+	if ( $player == $maintainer ) {
+		// Poof
+		$query = "DELETE FROM maintainer WHERE maintainer = ?";
+		$stmt = $pdo->prepare( $query );
+		$stmt->execute( [$maintainer] );
+		$maintainer = [0, '?'];
+	}
 	return $maintainer;
 }

+ 16 - 9
index.php

@@ -15,15 +15,16 @@ require "functions.php";
  * $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
+ * in the start page with the new maintainer added
+ * to be selected.
+ * $_REQUEST['action'] == 'i';  // New maintainer
+ * $_REQUEST['action'] == 'm';  // Maintainer selected
+ * 1a) Cannot validate new maintainer
+ * $screen = 1; @TODO
  * 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
+ * $screen = 3; $_REQUEST['action'] == 'm';  // edit player
+ * $screen = 4; $_REQUEST['action'] == 'x';  // remove player
  * 3) The edit page allows the maintainer to add
  *    or remove features from the player.
  */
@@ -47,11 +48,17 @@ if ( $action == 'm' ) {
 }
 if ( $action == 'e' ) {
     // Request for edit screen
+	$maintainer = get_maintainer( $_REQUEST['maintainer'] );
     $screen = 3;
 }
 if ( $action == 'x' ) {
     // Request to remove player
-    $screen = 4;
+    $maintainer = delete_player( $_REQUEST['player'] );
+    if ( $maintainer[0] == 0 ) {
+        $screen = 0;
+    } else {
+		$screen = 2;
+	}
 }
 assert( '$screen >= 0', "By now \$screen should be set to a value >= 0." );
 ?>
@@ -66,7 +73,7 @@ assert( '$screen >= 0', "By now \$screen should be set to a value >= 0." );
     <!-- Bootstrap CSS -->
     <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
 
-    <title>Hello, world!</title>
+    <title>Slork</title>
   </head>
   <body>
   <div class="container">

+ 84 - 77
snippets.php

@@ -8,93 +8,100 @@ try {
 } 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="hidden" name="action" value="i">
-    		<input type="password" class="form-control" name="api" 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>
-  		<button type="submit" class="btn btn-primary">Submit</button>
-  	</form>
-  	<div><p>&nbsp;</p></div>
-  	<h4>OR</h4>
-  	<!-- This part selects an existing maintainer -->
-  	<form>
-  		<div class="form-group">
-			<label for="existingMaintainer">Existing maintainer</label>
-			<input type="hidden" name="action" value="m">
-    		<select class="form-control" name="maintainer" id="existingMaintainer">
-				<option></option><!-- Empty option -->
+
+if ( $screen == 0 ) {
+	// 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[0] = <<<HTML
+		<form>
+				<!-- This form adds a new maintainer -->
+			<div class="form-group">
+				<label for="newAPIKey">New maintainer</label>
+				<input type="hidden" name="action" value="i">
+				<input type="password" class="form-control" name="api" 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>
+			<button type="submit" class="btn btn-primary">Submit</button>
+		</form>
+		<div><p>&nbsp;</p></div>
+		<h4>OR</h4>
+		<!-- This part selects an existing maintainer -->
+		<form>
+			<div class="form-group">
+				<label for="existingMaintainer">Existing maintainer</label>
+				<input type="hidden" name="action" value="m">
+				<select class="form-control" name="maintainer" id="existingMaintainer">
+					<option></option><!-- Empty 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>\n";
-}
+	foreach ($all_maintainers as $maintainer_key => $maintainer_value) {
+		$snippets[0] .= "\t\t\t\t<option value=\"$maintainer_key\">$maintainer_value [$maintainer_key]</option>\n";
+	}
 
-$snippets[0] .= <<<HTML
-			</select>
-  		</div>
-		<button type="submit" class="btn btn-primary">Submit</button>
-	</form>
+	$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 != ?
+	$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[] = array($row['player'], $row['value'] . " [" . $row['player'] . "]");
+	$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[] = array($row['player'], $row['value'] . " [" . $row['player'] . "]");
+	}
 }
 
-$snippets[2] = <<<HTML
-	<form id="player">
-		<div class="form-group">
-			<table class="table">
-				<thead>
-					<tr>
-						<th>Player</th><th>Edit</th><th>Remove</th>
-					</tr>
-				</thead>
-				<tbody>
+if ( $screen == 2 ) {
+	// Let maintainer select (or delete) a player
+	$snippets[2] = <<<HTML
+		<form id="player">
+			<div class="form-group">
+				<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] .= "\n\t\t\t\t\t<tr>";
-			$snippets[2] .= "\n\t\t\t\t\t\t<td>$player[1]";
-			$snippets[2] .= "<input type=\"hidden\" name=\"player\" value=\"$player[0]\" form=\"player\"></td><td>";
-			$snippets[2] .= "<input type=\"hidden\" name=\"maintainer\" value=\"$maintainer[0]\" form=\"player\">";
-			$snippets[2] .= "<button class=\"btn btn-primary\" name=\"action\" value=\"e\" form=\"player\">Edit</button></td>";
-			$snippets[2] .= "<td><button class=\"btn btn-danger\" name=\"action\" value=\"x\" form=\"player\">Remove</button></td>\n\t\t\t\t\t</tr>";
-		}
+	foreach ($players_id as $player) {
+		$snippets[2] .= "\n\t\t\t\t\t<tr>";
+		$snippets[2] .= "\n\t\t\t\t\t\t<td>$player[1]";
+		$snippets[2] .= "<input type=\"hidden\" name=\"player\" value=\"$player[0]\" form=\"player\"></td><td>";
+		$snippets[2] .= "<input type=\"hidden\" name=\"maintainer\" value=\"$maintainer[0]\" form=\"player\">";
+		$snippets[2] .= "<button class=\"btn btn-primary\" name=\"action\" value=\"e\" form=\"player\">Edit</button></td>";
+		$snippets[2] .= "<td><button class=\"btn btn-danger\" name=\"action\" value=\"x\" form=\"player\">Remove</button></td>\n\t\t\t\t\t</tr>";
+	}
 
-$snippets[2] .= <<<HTML
-\n				</tbody>
-			</table>
-		</div>
-	</form>
+	$snippets[2] .= <<<HTML
+	\n				</tbody>
+				</table>
+			</div>
+		</form>
 HTML;
+}