prepare("DELETE FROM tasks WHERE id = ?"); $stmt->execute([$task_id]); header("Location: " . $_SERVER['PHP_SELF']); exit; } catch (PDOException $e) { error_log("Error deleting task: " . $e->getMessage()); } } } // Handle delete candidate if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_candidate']) && hasPermission('manage_candidates')) { $candidate_id = $_POST['delete_candidate_id'] ?? null; if (!empty($candidate_id)) { try { $pdo = db(); $stmt = $pdo->prepare("DELETE FROM candidates WHERE id = ?"); $stmt->execute([$candidate_id]); header("Location: " . $_SERVER['PHP_SELF']); exit; } catch (PDOException $e) { error_log("Error deleting candidate: " . $e->getMessage()); } } } // Handle form submission for new candidate if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_candidate']) && hasPermission('manage_candidates')) { $name = $_POST['name'] ?? ''; $email = $_POST['email'] ?? ''; $phone = $_POST['phone'] ?? ''; $status = $_POST['status'] ?? 'Applied'; $notes = $_POST['notes'] ?? ''; if (!empty($name) && !empty($email)) { try { $pdo = db(); $stmt = $pdo->prepare("INSERT INTO candidates (name, email, phone, status, notes) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$name, $email, $phone, $status, $notes]); require_once 'workflow_engine.php'; trigger_workflow('candidate_created', ['candidate.id' => $pdo->lastInsertId(), 'candidate.name' => $name, 'candidate.email' => $email]); // Redirect to avoid form resubmission header("Location: " . $_SERVER['PHP_SELF']); exit; } catch (PDOException $e) { // Handle error, e.g., show an error message error_log("Error adding candidate: " . $e->getMessage()); } } } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['complete_task']) && hasPermission('manage_tasks')) { $task_id = $_POST['task_id'] ?? null; if (!empty($task_id)) { try { $pdo = db(); $stmt = $pdo->prepare("UPDATE tasks SET status = 'Done' WHERE id = ?"); $stmt->execute([$task_id]); // Fetch task details to pass to the workflow $stmt = $pdo->prepare("SELECT * FROM tasks WHERE id = ?"); $stmt->execute([$task_id]); $task = $stmt->fetch(); require_once 'workflow_engine.php'; trigger_workflow('task_completed', ['task.id' => $task['id'], 'task.name' => $task['task_name'], 'task.status' => $task['status']]); header("Location: " . $_SERVER['PHP_SELF']); exit; } catch (PDOException $e) { error_log("Error completing task: " . $e->getMessage()); } } } // Handle form submission for new task if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_task']) && hasPermission('manage_tasks')) { $task_name = $_POST['task_name'] ?? ''; $candidate_id = $_POST['candidate_id'] ?? null; $due_date = $_POST['due_date'] ?? null; $status = $_POST['status'] ?? 'To Do'; $description = $_POST['description'] ?? ''; if (!empty($task_name) && !empty($candidate_id)) { try { $pdo = db(); $stmt = $pdo->prepare("INSERT INTO tasks (task_name, candidate_id, due_date, status, description) VALUES (?, ?, ?, ?, ?)"); $stmt->execute([$task_name, $candidate_id, $due_date, $status, $description]); header("Location: " . $_SERVER['PHP_SELF']); exit; } catch (PDOException $e) { error_log("Error adding task: " . $e->getMessage()); } } } // Fetch tasks from the database try { $pdo = db(); $stmt = $pdo->query("SELECT tasks.*, candidates.name as candidate_name FROM tasks JOIN candidates ON tasks.candidate_id = candidates.id ORDER BY created_at DESC"); $tasks = $stmt->fetchAll(); } catch (PDOException $e) { error_log("Error fetching tasks: " . $e->getMessage()); $tasks = []; // Ensure $tasks is an array } // Fetch candidates from the database try { $pdo = db(); $stmt = $pdo->query("SELECT * FROM candidates ORDER BY created_at DESC"); $candidates = $stmt->fetchAll(); } catch (PDOException $e) { // Handle error, e.g., show an error message error_log("Error fetching candidates: " . $e->getMessage()); $candidates = []; // Ensure $candidates is an array } function getStatusClass($status) { switch ($status) { case 'Applied': return 'status-new'; case 'Interviewing': return 'status-interview'; case 'Hired': return 'status-hired'; case 'Rejected': return 'status-rejected'; case 'Offered': return 'status-offered'; case 'To Do': return 'status-todo'; case 'In Progress': return 'status-in-progress'; case 'Done': return 'status-done'; default: return ''; } } ?>