-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.php
More file actions
230 lines (205 loc) · 10 KB
/
task.php
File metadata and controls
230 lines (205 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
session_start();
require 'assets/include/db.php';
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$user_id = $_SESSION['user_id'];
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['task'])) {
$task = $_POST['task'];
$category = $_POST['category'];
$start_date = $_POST['start_date'];
$finish_date = $_POST['finish_date'];
$stmt = $conn->prepare("INSERT INTO tasks (user_id, task, category, start_date, finish_date) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("issss", $user_id, $task, $category, $start_date, $finish_date);
if ($stmt->execute()) {
header("Location: task.php");
exit();
} else {
echo "Failed to add task: " . $stmt->error;
}
$stmt->close();
} elseif (isset($_GET['action']) && isset($_GET['id'])) {
$task_id = $_GET['id'];
if ($_GET['action'] == 'edit') {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$task = $_POST['task'];
$category = $_POST['category'];
$start_date = $_POST['start_date'];
$finish_date = $_POST['finish_date'];
$stmt = $conn->prepare("UPDATE tasks SET task = ?, category = ?, start_date = ?, finish_date = ? WHERE id = ? AND user_id = ?");
$stmt->bind_param("ssssii", $task, $category, $start_date, $finish_date, $task_id, $user_id);
if ($stmt->execute()) {
header("Location: task.php");
exit();
} else {
echo "Failed to edit task: " . $stmt->error;
}
$stmt->close();
} else {
// Fetch existing task details for the form
$stmt = $conn->prepare("SELECT task, category, start_date, finish_date FROM tasks WHERE id = ? AND user_id = ?");
$stmt->bind_param("ii", $task_id, $user_id);
$stmt->execute();
$result = $stmt->get_result();
$existingTask = $result->fetch_assoc();
$stmt->close();
}
}
if ($_GET['action'] == 'done') {
$stmt = $conn->prepare("UPDATE tasks SET status = 'done' WHERE id = ? AND user_id = ?");
$stmt->bind_param("ii", $task_id, $user_id);
if ($stmt->execute()) {
header("Location: task.php");
exit();
} else {
echo "Failed to mark task as done: " . $stmt->error;
}
$stmt->close();
}
if ($_GET['action'] == 'delete') {
$stmt = $conn->prepare("DELETE FROM tasks WHERE id = ? AND user_id = ?");
$stmt->bind_param("ii", $task_id, $user_id);
if ($stmt->execute()) {
header("Location: task.php");
exit();
} else {
echo "Failed to delete task: " . $stmt->error;
}
$stmt->close();
}
}
$stmt = $conn->prepare("SELECT id, task, category, start_date, finish_date, status FROM tasks WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$tasks = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trackit</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;700&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.10.3/font/bootstrap-icons.min.css"
rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="stylesheet" href="assets/css/style.css">
<link rel="stylesheet" href="assets/css/style.css">
</head>
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand" href="index.php">TRACKIT</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
My Account
</a>
<ul class="dropdown-menu dropdown-menu-end bg-black text-white" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="profile.php">Profile</a></li>
<li><a class="dropdown-item" href="settings.php">Settings</a></li>
<li>
<hr class="dropdown-divider">
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link btn btn-primary text-white ms-2" href="logout.php">Logout</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<body class="bg-black">
<div class="container my-5 px-5">
<h2 class="text-white">Your Trackit</h2>
<form method="POST" action="task.php" class="mb-4">
<div class="row g-3">
<div class="col-12 col-md-3">
<input type="text" name="task" class="form-control" placeholder="Add a new task" required>
</div>
<div class="col-12 col-md-2">
<select name="category" class="form-select" required>
<option value="Work">Work</option>
<option value="Personal">Personal</option>
<option value="Shopping">Shopping</option>
</select>
</div>
<div class="col-12 col-md-3">
<input type="datetime-local" name="start_date" class="form-control" required>
</div>
<div class="col-12 col-md-3">
<input type="datetime-local" name="finish_date" class="form-control" required>
</div>
<div class="col-12 col-md-1">
<button type="submit" class="btn btn-primary w-100">Add</button>
</div>
</div>
</form>
<?php if (isset($existingTask)): ?>
<h3>Edit Task</h3>
<form method="POST" action="task.php?action=edit&id=<?php echo $task_id; ?>" class="mb-4">
<div class="input-group mb-3">
<input type="text" name="task" class="form-control"
value="<?php echo htmlspecialchars($existingTask['task']); ?>" required>
<select name="category" class="form-select" required>
<option value="Work" <?php echo $existingTask['category'] == 'Work' ? 'selected' : ''; ?>>Work
</option>
<option value="Personal" <?php echo $existingTask['category'] == 'Personal' ? 'selected' : ''; ?>>
Personal</option>
<option value="Shopping" <?php echo $existingTask['category'] == 'Shopping' ? 'selected' : ''; ?>>
Shopping</option>
</select>
<input type="datetime-local" name="start_date" class="form-control"
value="<?php echo htmlspecialchars($existingTask['start_date']); ?>" required>
<input type="datetime-local" name="finish_date" class="form-control"
value="<?php echo htmlspecialchars($existingTask['finish_date']); ?>" required>
<button type="submit" class="btn btn-warning">Update Task</button>
</div>
</form>
<?php endif; ?>
<h3 class="text-white">Task List</h3>
<ul class="list-group shadow-lg">
<?php foreach ($tasks as $task): ?>
<li class="list-group-item d-flex flex-column flex-md-row justify-content-between align-items-start align-items-md-center">
<div class="mb-2 mb-md-0">
<strong><?php echo htmlspecialchars($task['task']); ?></strong> -
<?php echo htmlspecialchars($task['category']); ?>
<br>
<small>Start: <?php echo htmlspecialchars($task['start_date']); ?></small> |
<small>Finish: <?php echo htmlspecialchars($task['finish_date']); ?></small>
<br>
<small>Status: <?php echo htmlspecialchars($task['status']); ?></small>
</div>
<div>
<a href="task.php?action=edit&id=<?php echo urlencode($task['id']); ?>" class="btn btn-primary btn-sm">Edit</a>
<?php if ($task['status'] === 'done'): ?>
<span class="btn btn-success btn-sm disabled">Done</span>
<?php else: ?>
<a href="task.php?action=done&id=<?php echo urlencode($task['id']); ?>" class="btn btn-success btn-sm">Mark Done</a>
<?php endif; ?>
<a href="task.php?action=delete&id=<?php echo urlencode($task['id']); ?>" class="btn btn-danger btn-sm"
onclick="return confirm('Are you sure you want to delete this task?')">Delete</a>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>