-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
473 lines (443 loc) · 16.6 KB
/
index.php
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
// Connect to MariaDB
$host = 'localhost';
$user = 'test';
$pass = 'test';
$db = 'task_board';
$conn = new mysqli($host, $user, $pass, $db);
// Check for connection errors
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Check if the table exists, and if not, create it
$table_check = $conn->query("SHOW TABLES LIKE 'tasks'");
if ($table_check->num_rows == 0) {
$conn->query("CREATE TABLE tasks (
id INT AUTO_INCREMENT PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
images TEXT,
completed BOOLEAN DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
}
// Handle form submission to add a task
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add_task'])) {
$title = $_POST['title'];
$description = $_POST['description'];
// Handle image uploads
$imageNames = [];
if (!empty($_FILES['images']['name'][0])) {
foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name) {
$file_name = $_FILES['images']['name'][$key];
$target_file = "uploads/" . basename($file_name);
if (move_uploaded_file($tmp_name, $target_file)) {
$imageNames[] = $file_name;
}
}
}
$images = implode(',', $imageNames);
// Save task to the database
$stmt = $conn->prepare("INSERT INTO tasks (title, description, images) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $title, $description, $images);
$stmt->execute();
// Redirect to the same page to prevent form resubmission
header("Location: index.php");
exit;
}
// Fetch all tasks from the database
$tasks = $conn->query("SELECT * FROM tasks");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Taskboard</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #1c1c1c;
color: #fff;
margin: 0;
padding: 0;
}
.task-board {
display: flex;
flex-direction: column;
align-items: center;
margin: 50px auto;
max-width: 1200px;
}
.task-input {
width: 100%;
background-color: #2d2d2d;
padding: 20px;
border-radius: 10px;
margin-bottom: 30px;
display: flex;
flex-direction: column;
}
.task-input input, .task-input textarea {
margin-bottom: 10px;
padding: 10px;
border: 1px solid #444;
background-color: #333;
color: #fff;
border-radius: 5px;
}
.task-input button {
padding: 10px;
background-color: #00c1ff;
border: none;
color: white;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
.task-input button:hover {
background-color: #0098d4;
}
.task-list {
width: 100%;
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
.task-box {
background-color: #333;
padding: 20px;
border-radius: 10px;
width: 300px;
position: relative;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
}
.task-box h3 {
margin-top: 0;
}
.image-preview img {
width: 100%;
height: auto;
border-radius: 5px;
margin-top: 10px;
cursor: pointer;
}
.task-controls {
margin-top: 20px;
display: flex;
justify-content: space-between;
}
.task-controls button {
background-color: #444;
border: none;
color: white;
padding: 8px 12px;
cursor: pointer;
border-radius: 5px;
font-size: 14px;
transition: background-color 0.3s;
}
.task-controls button.complete-task {
background-color: #00ff85;
}
.task-controls button.complete-task:hover {
background-color: #00cc66;
}
.task-controls button.delete-task {
background-color: #ff5757;
}
.task-controls button.delete-task:hover {
background-color: #cc4d4d;
}
#edit-form {
display: none;
background-color: #2d2d2d;
padding: 20px;
border-radius: 10px;
position: fixed;
top: 20%;
left: 50%;
transform: translate(-50%, -20%);
z-index: 1000;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
max-width: 80%;
max-height: 80%;
overflow-y: auto;
}
#edit-form input, #edit-form textarea {
margin-bottom: 10px;
width: 100%;
padding: 10px;
border: 1px solid #444;
background-color: #333;
color: #fff;
border-radius: 5px;
}
#image-previews img {
width: 100px; /* Thumbnail size */
margin-right: 10px;
cursor: pointer;
}
#image-previews .image-container {
position: relative;
display: inline-block;
margin-right: 40px; /* Increase right margin */
margin-bottom: 10px;
}
#image-previews .delete-image {
position: absolute;
top: -10px;
right: -20px;
background-color: rgba(255, 0, 0, 0.7);
color: white;
border: none;
border-radius: 50%;
width: 20px;
height: 20px;
font-size: 12px;
cursor: pointer;
}
#success-message {
display: none;
background-color: #00ff85;
color: black;
padding: 10px;
border-radius: 5px;
position: fixed;
top: 20%;
left: 50%;
transform: translate(-50%, -20%);
z-index: 1000;
}
.task-box.completed {
background-color: #2a3a2a;
}
.task-box .complete-status {
position: absolute;
top: 10px;
right: 10px;
font-size: 24px;
color: #00ff85;
}
.title {
font-size: 3rem;
text-align: center;
margin-bottom: 20px;
animation: fadeIn 2s ease-in-out;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.image-popup {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
z-index: 1001;
text-align: center;
padding: 20px;
}
.image-popup img {
max-width: 80%;
max-height: 80%;
border-radius: 5px;
}
.image-popup .close-button {
position: absolute;
top: 10px;
right: 60px;
background-color: rgba(255, 0, 0, 0.7);
color: white;
border: none;
border-radius: 50%;
width: 30px;
height: 30px;
font-size: 18px;
cursor: pointer;
}
@media (max-width: 600px) {
.title {
font-size: 2rem;
}
.task-box {
width: 100%;
}
}
</style>
</head>
<body>
<div class="task-board">
<h1 class="title">Taskboard</h1>
<!-- Add Task Form -->
<form action="index.php" method="post" enctype="multipart/form-data">
<div class="task-input">
<input type="text" name="title" placeholder="Titel" required>
<textarea name="description" placeholder="Description (10 linien maximal)" rows="10"></textarea>
<input type="file" name="images[]" multiple>
<button type="submit" name="add_task">Add Task</button>
</div>
</form>
<!-- Task List -->
<div class="task-list">
<?php while ($task = $tasks->fetch_assoc()) : ?>
<div class="task-box <?php echo $task['completed'] ? 'completed' : ''; ?>">
<h3><?php echo htmlspecialchars($task['title']); ?></h3>
<p><?php echo nl2br(htmlspecialchars($task['description'])); ?></p>
<?php if ($task['completed']) : ?>
<span class="complete-status">✓</span>
<?php endif; ?>
<!-- Image preview -->
<?php if (!empty($task['images'])) : ?>
<div class="image-preview">
<?php $image_files = explode(',', $task['images']);
foreach ($image_files as $image) {
echo "<img src='uploads/$image' alt='$image' onclick='openImagePreview(\"uploads/$image\")' />";
} ?>
</div>
<?php endif; ?>
<!-- Task Controls -->
<div class="task-controls">
<button class="complete-task" data-id="<?php echo $task['id']; ?>">
<?php echo $task['completed'] ? 'Unmark' : 'Mark Complete'; ?>
</button>
<button class="edit-task" data-id="<?php echo $task['id']; ?>" onclick="openEditForm(<?php echo $task['id']; ?>)">Edit</button>
<button class="delete-task" data-id="<?php echo $task['id']; ?>">Delete</button>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<!-- Edit Form -->
<div id="edit-form">
<h2>Edit Task</h2>
<form id="edit-task-form" enctype="multipart/form-data">
<input type="hidden" name="edit_task_id" id="edit-task-id">
<input type="text" name="edit_title" id="edit-title" placeholder="Task Title" required>
<textarea name="edit_description" id="edit-description" placeholder="Task Description (10 lines max)" rows="10"></textarea>
<input type="file" name="edit_images[]" multiple>
<button type="submit">Save Changes</button>
</form>
<button onclick="closeEditForm()">Cancel</button>
<div id="image-previews"></div>
</div>
<!-- Success Message -->
<div id="success-message">Task successfully updated!</div>
<!-- Image Popup -->
<div class="image-popup" id="image-popup">
<button class="close-button" onclick="closeImagePopup()">×</button>
<img src="" alt="Image Preview" id="popup-image">
</div>
<!-- JavaScript embedded in the HTML -->
<script>
function openImagePreview(src) {
const popup = document.getElementById('image-popup');
const popupImage = document.getElementById('popup-image');
popupImage.src = src;
popup.style.display = 'block';
}
function closeImagePopup() {
const popup = document.getElementById('image-popup');
popup.style.display = 'none';
}
function closeEditForm() {
document.querySelector('#edit-form').style.display = 'none';
}
document.querySelectorAll('.complete-task').forEach(button => {
button.addEventListener('click', (e) => {
const taskId = e.target.getAttribute('data-id');
fetch(`task_actions.php?action=complete&id=${taskId}`)
.then(response => response.text())
.then(data => {
const taskBox = e.target.closest('.task-box');
taskBox.classList.toggle('completed');
const completeStatus = taskBox.querySelector('.complete-status');
if (completeStatus) {
completeStatus.remove();
} else {
taskBox.insertAdjacentHTML('afterbegin', '<span class="complete-status">✓</span>');
}
e.target.textContent = e.target.textContent === 'Mark Complete' ? 'Unmark' : 'Mark Complete';
// Reload the page after a short delay to show the UI update first
setTimeout(() => {
location.reload();
}, 500); // 500 milliseconds delay
})
.catch(error => console.error('Error:', error));
});
});
document.querySelectorAll('.delete-task').forEach(button => {
button.addEventListener('click', (e) => {
const taskId = e.target.getAttribute('data-id');
if (confirm("Are you sure you want to delete this task?")) {
fetch(`task_actions.php?action=delete&id=${taskId}`)
.then(response => response.text())
.then(data => {
alert(data);
location.reload(); // Reload the page to remove the deleted task
});
}
});
});
function openEditForm(taskId) {
fetch(`task_actions.php?action=get_task&id=${taskId}`)
.then(response => response.json())
.then(task => {
document.querySelector('#edit-title').value = task.title;
document.querySelector('#edit-description').value = task.description;
document.querySelector('#edit-task-id').value = taskId;
document.querySelector('#edit-form').style.display = 'block';
// Clear and set image previews
const previewContainer = document.querySelector('#image-previews');
previewContainer.innerHTML = ''; // Clear existing previews
const images = task.images.split(',').filter(img => img.trim() !== '');
images.forEach(image => {
const imageContainer = document.createElement('div');
imageContainer.className = 'image-container';
const img = document.createElement('img');
img.src = `uploads/${image}`;
img.style.width = '100px'; // Thumbnail size
img.addEventListener('click', () => openImagePreview(`uploads/${image}`));
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'x';
deleteBtn.className = 'delete-image';
deleteBtn.setAttribute('data-image', image);
deleteBtn.addEventListener('click', () => {
imageContainer.remove();
});
imageContainer.appendChild(img);
imageContainer.appendChild(deleteBtn);
previewContainer.appendChild(imageContainer);
});
});
}
document.querySelector('#edit-task-form').addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(e.target);
// Collect images to delete
const imagesToDelete = [];
document.querySelectorAll('#image-previews .delete-image').forEach(button => {
imagesToDelete.push(button.getAttribute('data-image'));
});
formData.append('images_to_delete', JSON.stringify(imagesToDelete));
fetch('task_actions.php', { method: 'POST', body: formData })
.then(response => response.text())
.then(data => {
document.querySelector('#success-message').style.display = 'block';
setTimeout(() => {
document.querySelector('#success-message').style.display = 'none';
closeEditForm();
location.reload();
}, 2000); // Show message for 2 seconds
})
.catch(error => console.error('Error:', error));
});
</script>
</body>
</html>