Files
Critters/index.php
T
pauljdandClaude Sonnet 5 aab614f811
pauljd/davidson-ci: Davidson CI / quality (push) Successful in 13s
pauljd/davidson-ci: Davidson CI / ai-review (push) Successful in 1m2s
Add auth, critter creation with AI image generation, and Tailwind styling
Adds login/logout/session handling, a create-critter form backed by
adjectives/nouns/verbs/locations lookup tables, Replicate-generated
critter images saved to disk, and a Tailwind-styled critter gallery
on the homepage.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-05 22:41:20 +01:00

127 lines
6.1 KiB
PHP

<?php
require __DIR__ . '/session.php';
require __DIR__ . '/db.php';
start_app_session();
$pdo = getDbConnection();
$tableExists = $pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'adjectives'")->fetch();
if (!$tableExists) {
$pdo->exec('CREATE TABLE adjectives (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value TEXT NOT NULL
)');
$stmt = $pdo->prepare('INSERT INTO adjectives (value) VALUES (:value)');
foreach (['black', 'blue', 'brown', 'crochet', 'cute', 'fluffy', 'fuzzy', 'glittery', 'glowing neon', 'gray', 'green', 'lime green', 'magenta', 'orange', 'pastel', 'pink', 'purple', 'rainbow', 'red', 'sleepy', 'soft', 'sparkly', 'stone', 'teal', 'white', 'wooden', 'yellow'] as $adjective) {
$stmt->execute(['value' => $adjective]);
}
}
$tableExists = $pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'nouns'")->fetch();
if (!$tableExists) {
$pdo->exec('CREATE TABLE nouns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value TEXT NOT NULL
)');
$stmt = $pdo->prepare('INSERT INTO nouns (value) VALUES (:value)');
foreach (['beaver', 'bird', 'bunny rabbit', 'cat', 'chipmunk', 'cow', 'elephant', 'fox', 'frog', 'hamster', 'horse', 'kangaroo', 'koala', 'lemur', 'lion', 'monkey', 'moose', 'mouse', 'panda', 'penguin', 'pig', 'pika', 'puppy', 'quokka', 'raccoon', 'rat', 'sea otter', 'sheep', 'skunk', 'sloth', 'squirrel', 'teddy bear', 'tiger', 'turtle', 'wombat'] as $noun) {
$stmt->execute(['value' => $noun]);
}
}
$tableExists = $pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'verbs'")->fetch();
if (!$tableExists) {
$pdo->exec('CREATE TABLE verbs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value TEXT NOT NULL
)');
$stmt = $pdo->prepare('INSERT INTO verbs (value) VALUES (:value)');
foreach (['runs', 'jumps', 'waves hello', 'snuggles their blanket', 'swims', 'takes a bubble bath', 'wears a chef hat and stirs a baking bowl', 'hula hoops', 'builds with blocks', 'flies a kite', 'skateboards', 'rides their scooter', 'plays chess', 'writes with a pen and pad', 'plays guitar', 'plays the drums', 'plays the piano', 'roller skates', 'plays the saxophone', 'plays basketball', 'plays baseball', 'plays hockey', 'plays soccer', 'reads a book', 'wears pajamas', 'sleeps in bed', 'sings into a microphone', 'jumps rope', 'plays with a yo-yo', 'waters a plant with a watering can', 'sits by a campfire', 'naps in a hammock', 'plays golf', 'plays tennis', 'bakes cookies', 'does yoga'] as $verb) {
$stmt->execute(['value' => $verb]);
}
}
$tableExists = $pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'locations'")->fetch();
if (!$tableExists) {
$pdo->exec('CREATE TABLE locations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value TEXT NOT NULL
)');
$stmt = $pdo->prepare('INSERT INTO locations (value) VALUES (:value)');
foreach (['at the beach', 'on a sunny island', 'in the backyard', 'inside a snow globe', 'inside a Christmas ornament', 'safely inside a hot air balloon up in the sky', 'on the moon', 'in front of a gnomes house', 'in a majestic waterfall', 'on a trampoline', 'inside an igloo', 'on a rainbow', 'underwater inside an aquarium', 'at the pumpkin patch', 'at the north pole', 'in a candy land', 'on a farm', 'in the frontyard', 'in a big grassy field', 'on top of a cloud', 'on a bridge', 'in a forest', 'at the top of a mountain', 'in a boat on a river', 'at the mall', 'on a baseball field', 'on a soccer field', 'on a basketball court', 'on a hockey rink', 'in a swimming pool', 'in a hot tub', 'in the kitchen'] as $location) {
$stmt->execute(['value' => $location]);
}
}
$dayOfWeek = date('l');
$isLoggedIn = isset($_SESSION['username']) && isset($_SESSION['user_id']);
$myCritters = [];
if ($isLoggedIn) {
$crittersTableExists = $pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'critters'")->fetch();
if ($crittersTableExists) {
$stmt = $pdo->prepare('
SELECT a.value AS adjective, n.value AS noun, v.value AS verb, l.value AS location, c.image AS image
FROM critters c
JOIN adjectives a ON a.id = c.adjective_id
JOIN nouns n ON n.id = c.noun_id
JOIN verbs v ON v.id = c.verb_id
JOIN locations l ON l.id = c.location_id
WHERE c.author_id = :author_id
ORDER BY c.id DESC
');
$stmt->execute(['author_id' => $_SESSION['user_id']]);
$myCritters = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome</title>
<link rel="stylesheet" href="/media/output.css">
</head>
<body>
<?php if ($isLoggedIn): ?>
<h1>Welcome <?php echo htmlspecialchars($_SESSION['username'], ENT_QUOTES, 'UTF-8'); ?></h1>
<?php else: ?>
<h1>Welcome guest</h1>
<?php endif; ?>
<p>Happy <?php echo $dayOfWeek; ?>! We're glad you're here.</p>
<?php if ($isLoggedIn): ?>
<p><a href="create-critter.php">Create New Critter</a></p>
<p><a href="logout.php">Log out</a></p>
<div class="max-w-4xl mx-auto px-4">
<h2 class="text-xl font-semibold mb-4">My Critters</h2>
<?php if (empty($myCritters)): ?>
<p>You haven't created any critters yet.</p>
<?php else: ?>
<div class="grid grid-cols-4 gap-4">
<?php foreach ($myCritters as $critter): ?>
<?php if (!empty($critter['image'])): ?>
<img
src="critter-images/<?php echo htmlspecialchars($critter['image'], ENT_QUOTES, 'UTF-8'); ?>"
alt="AI-generated image of your critter"
class="w-full aspect-square object-cover rounded-lg border border-gray-200 shadow-sm m-1"
>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php else: ?>
<p><a href="register.php">Sign Up</a></p>
<p><a href="login.php">Log In</a></p>
<?php endif; ?>
</body>
</html>