-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_actions.php
94 lines (87 loc) · 3.21 KB
/
task_actions.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
<?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);
}
// Handle different actions
$action = $_GET['action'] ?? '';
$taskId = $_GET['id'] ?? '';
if ($action == 'complete') {
// Toggle the completed status of a task
$stmt = $conn->prepare("UPDATE tasks SET completed = NOT completed WHERE id = ?");
$stmt->bind_param("i", $taskId);
$stmt->execute();
echo "Task status updated.";
} elseif ($action == 'delete') {
// Delete a task and its associated images
$stmt = $conn->prepare("SELECT images FROM tasks WHERE id = ?");
$stmt->bind_param("i", $taskId);
$stmt->execute();
$result = $stmt->get_result();
$task = $result->fetch_assoc();
$images = explode(',', $task['images']);
foreach ($images as $image) {
if (file_exists("uploads/$image")) {
unlink("uploads/$image");
}
}
$stmt = $conn->prepare("DELETE FROM tasks WHERE id = ?");
$stmt->bind_param("i", $taskId);
$stmt->execute();
echo "Task deleted.";
} elseif ($action == 'get_task') {
// Fetch task details for editing
$stmt = $conn->prepare("SELECT * FROM tasks WHERE id = ?");
$stmt->bind_param("i", $taskId);
$stmt->execute();
$result = $stmt->get_result();
$task = $result->fetch_assoc();
echo json_encode($task);
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Handle form submission to update a task
$taskId = $_POST['edit_task_id'];
$title = $_POST['edit_title'];
$description = $_POST['edit_description'];
// Handle image uploads
$imageNames = [];
if (!empty($_FILES['edit_images']['name'][0])) {
foreach ($_FILES['edit_images']['tmp_name'] as $key => $tmp_name) {
$file_name = $_FILES['edit_images']['name'][$key];
$target_file = "uploads/" . basename($file_name);
if (move_uploaded_file($tmp_name, $target_file)) {
$imageNames[] = $file_name;
}
}
}
$images = implode(',', $imageNames);
// Handle image deletions
$imagesToDelete = json_decode($_POST['images_to_delete'], true);
if (!empty($imagesToDelete)) {
$stmt = $conn->prepare("SELECT images FROM tasks WHERE id = ?");
$stmt->bind_param("i", $taskId);
$stmt->execute();
$result = $stmt->get_result();
$task = $result->fetch_assoc();
$imagesArray = explode(',', $task['images']);
$imagesArray = array_filter($imagesArray, function($img) use ($imagesToDelete) {
return !in_array($img, $imagesToDelete);
});
$updatedImages = implode(',', $imagesArray);
$stmt = $conn->prepare("UPDATE tasks SET images = ? WHERE id = ?");
$stmt->bind_param("si", $updatedImages, $taskId);
$stmt->execute();
}
// Update task in the database
$stmt = $conn->prepare("UPDATE tasks SET title = ?, description = ?, images = CONCAT_WS(',', ?, images) WHERE id = ?");
$stmt->bind_param("sssi", $title, $description, $images, $taskId);
$stmt->execute();
echo "Task updated.";
}
$conn->close();
?>