From 2e1528a5b54728325bb0d0ddd48480de3640e56d Mon Sep 17 00:00:00 2001 From: Edward Wibisono Yulianto Date: Fri, 29 Aug 2025 03:53:24 -0400 Subject: [PATCH] Upload files to "/" --- index.php | 1177 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1177 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 0000000..2080da8 --- /dev/null +++ b/index.php @@ -0,0 +1,1177 @@ +conn = null; + try { + $this->conn = new PDO( + "pgsql:host=" . $this->host . ";port=" . $this->port . ";dbname=" . $this->db_name, + $this->username, + $this->password + ); + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } catch(PDOException $exception) { + echo "Connection error: " . $exception->getMessage(); + } + return $this->conn; + } +} + +// Initialize database and create tables if they don't exist +function initializeDatabase() { + $database = new Database(); + $conn = $database->getConnection(); + + try { + // Create groups table + $query = "CREATE TABLE IF NOT EXISTS groups ( + group_id SERIAL PRIMARY KEY, + group_name VARCHAR(255) NOT NULL UNIQUE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )"; + $conn->exec($query); + + // Create tasks table + $query = "CREATE TABLE IF NOT EXISTS tasks ( + task_id SERIAL PRIMARY KEY, + task_name VARCHAR(255) NOT NULL, + task_description TEXT, + group_id INTEGER REFERENCES groups(group_id) ON DELETE CASCADE, + is_done BOOLEAN DEFAULT FALSE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )"; + $conn->exec($query); + + // Insert default groups if none exist + $stmt = $conn->prepare("SELECT COUNT(*) FROM groups"); + $stmt->execute(); + $count = $stmt->fetchColumn(); + + if ($count == 0) { + $defaultGroups = ['Personal', 'Work', 'Urgent']; + foreach ($defaultGroups as $group) { + $stmt = $conn->prepare("INSERT INTO groups (group_name) VALUES (?)"); + $stmt->execute([$group]); + } + } + + } catch(PDOException $exception) { + echo "Error creating tables: " . $exception->getMessage(); + } +} + +// Handle AJAX requests and form submissions +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $database = new Database(); + $conn = $database->getConnection(); + + $action = $_POST['action'] ?? ''; + + try { + switch ($action) { + case 'add_group': + $groupName = trim($_POST['group_name']); + if (!empty($groupName)) { + $stmt = $conn->prepare("INSERT INTO groups (group_name) VALUES (?)"); + $stmt->execute([$groupName]); + echo json_encode(['success' => true, 'message' => 'Group added successfully']); + } else { + echo json_encode(['success' => false, 'message' => 'Group name cannot be empty']); + } + exit; + + case 'add_task': + $taskName = trim($_POST['task_name']); + $taskDesc = trim($_POST['task_description']); + $groupId = $_POST['group_id']; + + if (!empty($taskName) && !empty($groupId)) { + $stmt = $conn->prepare("INSERT INTO tasks (task_name, task_description, group_id) VALUES (?, ?, ?)"); + $stmt->execute([$taskName, $taskDesc, $groupId]); + echo json_encode(['success' => true, 'message' => 'Task added successfully']); + } else { + echo json_encode(['success' => false, 'message' => 'Task name and group are required']); + } + exit; + + case 'toggle_task': + $taskId = $_POST['task_id']; + $isDone = $_POST['is_done'] === 'true' ? 'TRUE' : 'FALSE'; + + $stmt = $conn->prepare("UPDATE tasks SET is_done = $isDone WHERE task_id = ?"); + $stmt->execute([$taskId]); + echo json_encode(['success' => true]); + exit; + + case 'delete_task': + $taskId = $_POST['task_id']; + $stmt = $conn->prepare("DELETE FROM tasks WHERE task_id = ?"); + $stmt->execute([$taskId]); + echo json_encode(['success' => true, 'message' => 'Task deleted successfully']); + exit; + + case 'delete_group': + $groupId = $_POST['group_id']; + $stmt = $conn->prepare("DELETE FROM groups WHERE group_id = ?"); + $stmt->execute([$groupId]); + echo json_encode(['success' => true, 'message' => 'Group and all associated tasks deleted successfully']); + exit; + + case 'update_task': + $taskId = $_POST['task_id']; + $taskName = trim($_POST['task_name']); + $taskDesc = trim($_POST['task_description']); + $groupId = $_POST['group_id']; + + if (!empty($taskName) && !empty($groupId)) { + $stmt = $conn->prepare("UPDATE tasks SET task_name = ?, task_description = ?, group_id = ? WHERE task_id = ?"); + $stmt->execute([$taskName, $taskDesc, $groupId, $taskId]); + echo json_encode(['success' => true, 'message' => 'Task updated successfully']); + } else { + echo json_encode(['success' => false, 'message' => 'Task name and group are required']); + } + exit; + } + } catch(PDOException $exception) { + echo json_encode(['success' => false, 'message' => 'Database error: ' . $exception->getMessage()]); + exit; + } +} + +// Initialize database +initializeDatabase(); + +// Fetch data for display +$database = new Database(); +$conn = $database->getConnection(); + +// Get all groups with their tasks +$query = "SELECT g.group_id, g.group_name, + t.task_id, t.task_name, t.task_description, t.is_done, t.created_at as task_created + FROM groups g + LEFT JOIN tasks t ON g.group_id = t.group_id + ORDER BY g.group_name, t.created_at DESC"; +$stmt = $conn->prepare($query); +$stmt->execute(); +$results = $stmt->fetchAll(PDO::FETCH_ASSOC); + +// Organize data by groups +$groupsData = []; +foreach ($results as $row) { + $groupId = $row['group_id']; + if (!isset($groupsData[$groupId])) { + $groupsData[$groupId] = [ + 'group_id' => $row['group_id'], + 'group_name' => $row['group_name'], + 'tasks' => [] + ]; + } + + if ($row['task_id']) { + $groupsData[$groupId]['tasks'][] = [ + 'task_id' => $row['task_id'], + 'task_name' => $row['task_name'], + 'task_description' => $row['task_description'], + 'is_done' => $row['is_done'], + 'task_created' => $row['task_created'] + ]; + } +} + +// Get all groups for the dropdown +$stmt = $conn->prepare("SELECT group_id, group_name FROM groups ORDER BY group_name"); +$stmt->execute(); +$allGroups = $stmt->fetchAll(PDO::FETCH_ASSOC); +?> + + + + + + + Advanced To-Do List Manager + + + +
+

🚀 Advanced To-Do List Manager

+ +
+ +
+

📁 Create New Group

+
+
+ + +
+ +
+
+ + +
+

✅ Create New Task

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
+ + +
+ +
+

No groups found

+

Start by creating your first group!

+
+ + +
+
+

+ +
+
+ +
No tasks in this group yet
+ + +
+ + onchange="toggleTask(, this.checked)"> +
+
+ +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + + +
+ + + + \ No newline at end of file