36473-vm/index.php
Flatlogic Bot 0c66a3d3f1 1
2025-11-29 16:02:59 +00:00

128 lines
4.3 KiB
PHP

<?php
declare(strict_types=1);
// --- INITIALIZATION ---
session_start();
ini_set('display_errors', '1');
error_reporting(E_ALL);
date_default_timezone_set('UTC');
require_once 'db/config.php';
// --- DATABASE SETUP ---
try {
$pdo = db();
$pdo->exec("CREATE TABLE IF NOT EXISTS growth_entries (
id INT PRIMARY KEY AUTO_INCREMENT,
entry_date DATE NOT NULL,
mood VARCHAR(255) NOT NULL,
notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
} catch (PDOException $e) {
die("Database setup failed: " . $e->getMessage());
}
// --- FORM HANDLING ---
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$mood = $_POST['mood'] ?? '';
$notes = $_POST['notes'] ?? '';
$entry_date = date('Y-m-d');
if (!empty($mood)) {
try {
$stmt = $pdo->prepare("INSERT INTO growth_entries (entry_date, mood, notes) VALUES (?, ?, ?)");
$stmt->execute([$entry_date, $mood, $notes]);
header('Location: index.php?saved=1');
exit;
} catch (PDOException $e) {
// In a real app, log this error instead of dying
die("Failed to save entry: " . $e->getMessage());
}
}
}
// --- DATA FETCHING ---
$entries = [];
try {
$stmt = $pdo->query("SELECT * FROM growth_entries ORDER BY created_at DESC");
$entries = $stmt->fetchAll();
} catch (PDOException $e) {
// In a real app, you might want to display a friendly error to the user
error_log("Failed to fetch entries: " . $e->getMessage());
}
// --- METADATA ---
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Track your daily growth and progress.';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Daily Growth Tracker</title>
<!-- Meta description -->
<meta name="description" content="<?= htmlspecialchars($projectDescription) ?>" />
<!-- Open Graph & Twitter meta tags -->
<meta property="og:title" content="Daily Growth Tracker" />
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<?php if ($projectImageUrl): ?>
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<?php endif; ?>
<link rel="stylesheet" href="assets/css/style.css?v=<?= time() ?>">
</head>
<body>
<div class="container">
<header class="header">
<h1>Daily Growth Tracker</h1>
<p style="color: var(--subtle-text-color);">Your personal space to reflect and grow, one day at a time.</p>
</header>
<main>
<section class="entry-form">
<h2>How was your day?</h2>
<form action="index.php" method="POST">
<div class="mood-selector">
<?php
$moods = ['😞', '😐', '😊', '😄', '🚀'];
foreach ($moods as $m): ?>
<input type="radio" name="mood" value="<?= $m ?>" id="mood-<?= $m ?>" required>
<label for="mood-<?= $m ?>"><?= $m ?></label>
<?php endforeach; ?>
</div>
<textarea name="notes" placeholder="Add a few notes about your day..."></textarea>
<button type="submit" class="btn-submit">Save Growth</button>
</form>
</section>
<section class="entries-list">
<h2>Past Entries</h2>
<?php if (empty($entries)): ?>
<p style="text-align: center; color: var(--subtle-text-color);">No entries yet. Add one above to get started!</p>
<?php else: ?>
<?php foreach ($entries as $entry): ?>
<div class="entry-card">
<div class="mood"><?= htmlspecialchars($entry['mood']) ?></div>
<div class="content">
<div class="date"><?= date('l, F j, Y', strtotime($entry['entry_date'])) ?></div>
<p class="notes"><?= htmlspecialchars($entry['notes']) ?></p>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</section>
</main>
</div>
<div id="toast-container"></div>
<script src="assets/js/main.js?v=<?= time() ?>"></script>
</body>
</html>