add admin page for npcs

This commit is contained in:
Sundog Jones 2024-05-16 12:07:00 -04:00
parent fcfbcdeb0d
commit 53da09bd98

View File

@ -0,0 +1,148 @@
<?php
$db_path = realpath(dirname(__FILE__) . '/../config/db.php');
require_once($db_path);
require_once(realpath(dirname(__FILE__) . '/../header.php'));
?>
<h1>Wander Admin</h1>
<h2>Non-Player Characters</h2>
<p><a href='./npcs.php?action=create'>Create New NPC</a></p>
<?php
if (!isset($_GET['action']) && !isset($_POST['action'])) {
?>
<table border=1>
<tr>
<td>ID</td>
<td>Name</td>
<td>Description</td>
<td>Active?</td>
<td>Unique?</td>
<td>Model Path</td>
<td>Image Path</td>
<td>Avatar Path</td>
<td>Minimum Group Size</td>
<td>Maximum Group Size</td>
</tr>
<?php
$results = $conn->query('SELECT * FROM NPCs');
while ($row = $results->fetchArray()) {
?>
<tr>
<td><a href='./npcs.php?action=edit&id=<?= $row['id'] ?>'><?= $row['id'] ?></a></td>
<td><?= $row['name'] ?></td>
<td><?= $row['description'] ?></td>
<td><?= $row['is_active'] ?></td>
<td><?= $row['is_unique'] ?></td>
<td><?= $row['model_path'] ?></td>
<td><?= $row['image_path'] ?></td>
<td><?= $row['avatar_path'] ?></td>
<td><?= $row['minimum_group'] ?></td>
<td><?= $row['maximum_group'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
} else {
switch ($_REQUEST['action']) {
case 'create':
// create NPC form
echo "<h3>Create a New NPC</h3>";
displayForm(false);
break;
case 'edit':
// edit POI form
echo "<h3>Edit NPC</h3>";
displayForm(true);
break;
case 'save':
if (isset($_POST['name']) && isset($_POST['description']) && isset($_POST['is_active'])) {
// do the insert or update
if (!isset($_POST['id'])) {
// insert
$stmt = $conn->prepare('INSERT INTO NPCs (name, description, is_active, is_unique, model_path, image_path, avatar_path, minimum_group, maximum_group) VALUES (:name, :description, :is_active, :is_unique, :model_path, :image_path, :avatar_path, :minimum_group, :maximum_group)');
} else {
// update
$stmt = $conn->prepare('UPDATE NPCs SET name=:name, description=:description, is_active=:is_active, is_unique=:is_unique, model_path=:model_path, image_path=:image_path, avatar_path=:avatar_path, minimum_group=:minimum_group, maximum_group=:maximum_group WHERE id = :id');
if ($stmt) $stmt->bindValue(':id', $_POST['id']);
}
if ($stmt) {
$stmt->bindValue(':name', $_POST['name'], SQLITE3_TEXT);
$stmt->bindValue(':description', $_POST['description'], SQLITE3_TEXT);
$stmt->bindValue(':is_active', $_POST['is_active'] === "on" ? 1 : 0, SQLITE3_INTEGER);
$stmt->bindValue(':is_unique', $_POST['is_unique'] === "on" ? 1 : 0, SQLITE3_INTEGER);
$stmt->bindValue(':model_path', $_POST['model_path'], SQLITE3_TEXT);
$stmt->bindValue(':image_path', $_POST['image_path'], SQLITE3_TEXT);
$stmt->bindValue(':avatar_path', $_POST['avatar_path'], SQLITE3_TEXT);
$stmt->bindValue(':minimum_group', is_numeric($_POST['minimum_group']) ? intval($_POST['minimum_group']) : 1, SQLITE3_INTEGER);
$stmt->bindValue(':maximum_group', is_numeric($_POST['maximum_group']) ? intval($_POST['maximum_group']) : 10, SQLITE3_INTEGER);
$result = $stmt->execute();
if ($result) {
if (isset($_POST['id'])) {
echo "<p>Record updated. <a href='npcs.php'>&lt; back to Non-Player Characters</a></p>\n";
} else {
echo "<p>Record created. <a href='npcs.php'>&lt; back to Non-Player Characters</a></p>\n";
}
} else {
echo "<p><strong>OOPS!</strong> There was a problem adding the new NPC!</p>\n";
}
} else {
echo "<p><strong>OOPS!</strong> There was a problem adding the new NPC!!</p>\n";
}
} else {
echo "<strong>Name and Description are required - please go back and try again</strong>\n";
}
break;
default:
echo "Unknown action: " . $_GET['action'];
}
}
?>
<p><a href="index.php">&lt; back to Admin</a></p>
<?php
require_once(realpath(dirname(__FILE__) . '/../footer.php'));
// page functions
function displayForm($editMode = false) {
global $conn;
?>
<form method='POST' action='npcs.php'>
<input type='hidden' name='action' value='save' />
<?php
if ($editMode) {
$lookupStmt = $conn->prepare('SELECT * FROM NPCs WHERE id = :id');
if ($lookupStmt) {
$lookupStmt->bindValue(':id', $_REQUEST['id']);
$lookupResult = $lookupStmt->execute();
if ($lookupResult) {
$npc = $lookupResult->fetchArray();
}
}
echo "<p>ID: " . $_REQUEST['id'] . "</p>\n";
echo "<input type='hidden' name='id' value='" . $_REQUEST['id'] . "' />\n";
}
?>
<p><label for='name'>Name: </label><input type='text' name='name' <?= $editMode ? "value='" . $npc['name'] . "'" : "" ?> /></p>
<p><label for='description'>Description: </label><input type='text' name='description' <?= $editMode ? "value='" . $npc['description'] . "'" : "" ?> /></p>
<p><label for='is_active'>Is Active: </label><input type='checkbox' name='is_active' <?= $editMode ? ($npc['is_active'] ? "checked" : "") : "checked" ?> /></p>
<p><label for='is_unique'>Is Unique: </label><input type='checkbox' name='is_unique' <?= $editMode ? ($npc['is_unique'] ? "checked" : "") : "" ?> /></p>
<p><label for='model_path'>Model Path: </label><input type='text' name='model_path' <?= $editMode ? "value='" . $npc['model_path'] . "'" : "" ?> /></p>
<p><label for='image_path'>Image Path: </label><input type='text' name='image_path' <?= $editMode ? "value='" . $npc['image_path'] . "'" : "" ?> /></p>
<p><label for='avatar_path'>Avatar Path: </label><input type='text' name='avatar_path' <?= $editMode ? "value='" . $npc['avatar_path'] . "'" : "" ?> /></p>
<p><label for='minimum_group'>Minimum NPC Group Size: </label><input type='text' name='minimum_group' <?= $editMode ? "value='" . $npc['minimum_group'] . "'" : "" ?> /></p>
<p><label for='maximum_group'>Maximum NPC Group Size: </label><input type='text' name='maximum_group' <?= $editMode ? "value='" . $npc['maximum_group'] . "'" : "" ?> /></p>
<p><input type='submit' value='Save NPC' /></p>
</form>
<?php
}
?>