216 lines
9.6 KiB
PHP
216 lines
9.6 KiB
PHP
<?php
|
|
// index.php
|
|
|
|
// --- CONFIG AND DB SETUP ---
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$message = null;
|
|
$message_type = 'success';
|
|
|
|
try {
|
|
$pdo = db();
|
|
// Idempotent table creation
|
|
$pdo->exec("
|
|
CREATE TABLE IF NOT EXISTS tasks (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
title VARCHAR(255) NOT NULL,
|
|
description TEXT,
|
|
status ENUM('pending', 'completed') DEFAULT 'pending' NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
");
|
|
|
|
// --- HANDLE POST REQUESTS ---
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (session_status() == PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
if ($action === 'add_task') {
|
|
$title = trim($_POST['title'] ?? '');
|
|
$description = trim($_POST['description'] ?? '');
|
|
|
|
if (!empty($title)) {
|
|
$stmt = $pdo->prepare("INSERT INTO tasks (title, description) VALUES (?, ?)");
|
|
$stmt->execute([$title, $description]);
|
|
$_SESSION['message'] = 'Task added successfully!';
|
|
$_SESSION['message_type'] = 'success';
|
|
} else {
|
|
$_SESSION['message'] = 'Task title cannot be empty.';
|
|
$_SESSION['message_type'] = 'danger';
|
|
}
|
|
} elseif ($action === 'update_status') {
|
|
$task_id = filter_var($_POST['task_id'] ?? 0, FILTER_VALIDATE_INT);
|
|
if ($task_id) {
|
|
$stmt = $pdo->prepare("UPDATE tasks SET status = 'completed' WHERE id = ?");
|
|
$stmt->execute([$task_id]);
|
|
$_SESSION['message'] = 'Task marked as completed!';
|
|
$_SESSION['message_type'] = 'success';
|
|
}
|
|
} elseif ($action === 'delete_task') {
|
|
$task_id = filter_var($_POST['task_id'] ?? 0, FILTER_VALIDATE_INT);
|
|
if ($task_id) {
|
|
$stmt = $pdo->prepare("DELETE FROM tasks WHERE id = ?");
|
|
$stmt->execute([$task_id]);
|
|
$_SESSION['message'] = 'Task deleted successfully!';
|
|
$_SESSION['message_type'] = 'success';
|
|
}
|
|
}
|
|
|
|
// Redirect to self to prevent form resubmission
|
|
header("Location: " . $_SERVER['PHP_SELF']);
|
|
exit;
|
|
}
|
|
|
|
// Check for session message
|
|
if (session_status() == PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
if (isset($_SESSION['message'])) {
|
|
$message = $_SESSION['message'];
|
|
$message_type = $_SESSION['message_type'];
|
|
unset($_SESSION['message']);
|
|
unset($_SESSION['message_type']);
|
|
}
|
|
|
|
|
|
// --- FETCH TASKS FOR DISPLAY ---
|
|
$stmt = $pdo->query("SELECT * FROM tasks ORDER BY status ASC, created_at DESC");
|
|
$tasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
|
$error_message = "Database error: " . $e->getMessage();
|
|
$tasks = [];
|
|
$message = $error_message;
|
|
$message_type = 'danger';
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<!-- SEO and Meta Tags -->
|
|
<title>MyTaskManager</title>
|
|
<meta name="description" content="A simple and beautiful task manager built with Flatlogic.">
|
|
<meta name="keywords" content="task manager, to-do list, productivity, project management, simple tasks, online to-do, Flatlogic Generator, php task manager">
|
|
<meta property="og:title" content="MyTaskManager">
|
|
<meta property="og:description" content="A simple and beautiful task manager built with Flatlogic.">
|
|
<meta property="og:image" content="<?php echo htmlspecialchars($_SERVER['PROJECT_IMAGE_URL'] ?? ''); ?>">
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
<meta name="twitter:image" content="<?php echo htmlspecialchars($_SERVER['PROJECT_IMAGE_URL'] ?? ''); ?>">
|
|
|
|
<!-- Stylesheets -->
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<!-- Navbar -->
|
|
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="#">
|
|
<i class="bi bi-check2-square text-primary"></i>
|
|
MyTaskManager
|
|
</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<!-- Main Content -->
|
|
<main class="container my-5">
|
|
|
|
<!-- Add Task Form -->
|
|
<section id="add-task" class="mb-5">
|
|
<div class="card task-card">
|
|
<div class="card-body p-4">
|
|
<h2 class="card-title h4 mb-3">Add a New Task</h2>
|
|
<form action="index.php" method="POST">
|
|
<input type="hidden" name="action" value="add_task">
|
|
<div class="mb-3">
|
|
<label for="title" class="form-label">Task Title</label>
|
|
<input type="text" class="form-control" id="title" name="title" placeholder="e.g., Finish project report" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Description (Optional)</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3" placeholder="Add more details about the task..."></textarea>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100 py-2">
|
|
<i class="bi bi-plus-lg"></i> Add Task
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- Task List -->
|
|
<section id="task-list">
|
|
<h2 class="h4 mb-4">Your Tasks</h2>
|
|
<?php if (empty($tasks)): ?>
|
|
<div class="text-center text-muted p-5 bg-light rounded-3">
|
|
<i class="bi bi-clipboard-check" style="font-size: 3rem;"></i>
|
|
<p class="mt-3 mb-0">You have no tasks yet. Add one above to get started!</p>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="list-group">
|
|
<?php foreach ($tasks as $task): ?>
|
|
<div class="list-group-item list-group-item-action task-card <?php echo $task['status'] === 'completed' ? 'completed' : ''; ?>">
|
|
<div class="d-flex w-100 justify-content-between">
|
|
<div>
|
|
<h5 class="mb-1"><?php echo htmlspecialchars($task['title']); ?></h5>
|
|
<p class="mb-1 small text-muted"><?php echo htmlspecialchars($task['description']); ?></p>
|
|
</div>
|
|
<div class="d-flex gap-2 align-items-center">
|
|
<?php if ($task['status'] === 'pending'): ?>
|
|
<form action="index.php" method="POST" class="d-inline">
|
|
<input type="hidden" name="action" value="update_status">
|
|
<input type="hidden" name="task_id" value="<?php echo $task['id']; ?>">
|
|
<button type="submit" class="btn btn-sm btn-success" title="Mark as completed">
|
|
<i class="bi bi-check-lg"></i>
|
|
</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
<form action="index.php" method="POST" class="d-inline delete-task-form">
|
|
<input type="hidden" name="action" value="delete_task">
|
|
<input type="hidden" name="task_id" value="<?php echo $task['id']; ?>">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger" title="Delete task">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</section>
|
|
|
|
</main>
|
|
|
|
<!-- Toast Notification -->
|
|
<?php if ($message): ?>
|
|
<div class="toast-container position-fixed bottom-0 end-0 p-3">
|
|
<div class="toast align-items-center text-white bg-<?php echo $message_type; ?> border-0" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="d-flex">
|
|
<div class="toast-body">
|
|
<?php echo htmlspecialchars($message); ?>
|
|
</div>
|
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Footer -->
|
|
<footer class="text-center text-muted py-4 mt-5 border-top">
|
|
<p class="mb-0">© <?php echo date('Y'); ?> MyTaskManager. Built with <a href="https://flatlogic.com" target="_blank" class="text-decoration-none">Flatlogic</a>.</p>
|
|
</footer>
|
|
|
|
<!-- Scripts -->
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|