35632-vm/index.php
2025-11-20 02:21:39 +00:00

442 lines
22 KiB
PHP

<?php
require_once 'auth.php';
// Check if user is logged in
if (!is_logged_in()) {
header('Location: login.php');
exit;
}
require_once __DIR__ . '/db/config.php';
// Handle delete task
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_task']) && hasPermission('manage_tasks')) {
$task_id = $_POST['delete_task_id'] ?? null;
if (!empty($task_id)) {
try {
$pdo = db();
$stmt = $pdo->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 '';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- SEO & Meta Tags -->
<title>FinMox Flow</title>
<meta name="description" content="FinMox Flow - a multi-tenant SaaS platform for HR and Operations teams. Built with Flatlogic Generator.">
<meta name="keywords" content="finmox, hr, operations, saas, candidate tracking, onboarding, automations, ai copilot, flatlogic">
<!-- Social Media Meta Tags -->
<meta property="og:title" content="FinMox Flow">
<meta property="og:description" content="A multi-tenant SaaS platform for HR and Operations teams.">
<meta property="og:image" content="<?php echo htmlspecialchars($_SERVER['PROJECT_IMAGE_URL'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="<?php echo htmlspecialchars($_SERVER['PROJECT_IMAGE_URL'] ?? '', ENT_QUOTES, 'UTF-8'); ?>">
<!-- Stylesheets -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<header class="header d-flex justify-content-between align-items-center">
<div class="logo">FinMox<span class="dot">.</span></div>
<nav class="d-flex align-items-center">
<a href="index.php" class="btn btn-outline-primary me-2">Home</a>
<a href="chat.php" class="btn btn-outline-primary me-2">Chat</a>
<a href="dashboard.php" class="btn btn-outline-primary me-2">Dashboard</a>
<a href="workflows.php" class="btn btn-outline-primary me-3">Workflows</a>
<div class="dropdown">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" id="userDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<?php echo htmlspecialchars($_SESSION['username']); ?>
</button>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
<?php if (hasPermission('manage_roles')): ?>
<li><a class="dropdown-item" href="roles.php">Manage Roles</a></li>
<li><hr class="dropdown-divider"></li>
<?php endif; ?>
<li><a class="dropdown-item" href="logout.php">Logout</a></li>
</ul>
</div>
</nav>
</header>
<main class="main-content">
<div class="container-fluid">
<?php if (hasPermission('view_candidates')): ?>
<div class="page-header">
<h1 class="page-title">Candidates</h1>
<?php if (hasPermission('manage_candidates')): ?>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addCandidateModal">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-lg me-1" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 2a.5.5 0 0 1 .5.5v5h5a.5.5 0 0 1 0 1h-5v5a.5.5 0 0 1-1 0v-5h-5a.5.5 0 0 1 0-1h5v-5A.5.5 0 0 1 8 2Z"/>
</svg>
Add Candidate
</button>
<?php endif; ?>
</div>
<div class="card">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table mb-0">
<thead>
<tr>
<th class="ps-4">Name</th>
<th>Phone</th>
<th>Status</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($candidates as $candidate): ?>
<tr>
<td class="ps-4">
<div class="d-flex align-items-center">
<div>
<div class="candidate-name"><?php echo htmlspecialchars($candidate['name']); ?></div>
<div class="candidate-email"><?php echo htmlspecialchars($candidate['email']); ?></div>
</div>
</div>
</td>
<td><?php echo htmlspecialchars($candidate['phone'] ?? 'N/A'); ?></td>
<td>
<span class="status-badge <?php echo getStatusClass($candidate['status']); ?>">
<?php echo htmlspecialchars($candidate['status']); ?>
</span>
</td>
<td class="text-end pe-4">
<a href="edit_candidate.php?id=<?php echo $candidate['id']; ?>" class="btn btn-sm btn-outline-primary">Edit</a>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="display: inline;">
<input type="hidden" name="delete_candidate_id" value="<?php echo $candidate['id']; ?>">
<button type="submit" name="delete_candidate" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this candidate?');">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<?php if (hasPermission('view_tasks')): ?>
<div class="page-header mt-5">
<h1 class="page-title">Tasks</h1>
<?php if (hasPermission('manage_tasks')): ?>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addTaskModal">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-lg me-1" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 2a.5.5 0 0 1 .5.5v5h5a.5.5 0 0 1 0 1h-5v5a.5.5 0 0 1-1 0v-5h-5a.5.5 0 0 1 0-1h5v-5A.5.5 0 0 1 8 2Z"/>
</svg>
Add Task
</button>
<?php endif; ?>
</div>
<div class="card">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table mb-0">
<thead>
<tr>
<th class="ps-4">Task Name</th>
<th>Assigned To</th>
<th>Due Date</th>
<th>Status</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($tasks as $task): ?>
<tr>
<td class="ps-4"><?php echo htmlspecialchars($task['task_name']); ?></td>
<td><?php echo htmlspecialchars($task['candidate_name'] ?? 'N/A'); ?></td>
<td><?php echo htmlspecialchars($task['due_date'] ?? 'N/A'); ?></td>
<td>
<span class="status-badge <?php echo getStatusClass($task['status']); ?>">
<?php echo htmlspecialchars($task['status']); ?>
</span>
</td>
<td class="text-end pe-4">
<a href="edit_task.php?id=<?php echo $task['id']; ?>" class="btn btn-sm btn-outline-primary">Edit</a>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="display: inline;">
<input type="hidden" name="delete_task_id" value="<?php echo $task['id']; ?>">
<button type="submit" name="delete_task" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this task?');">Delete</button>
</form>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" style="display: inline;">
<input type="hidden" name="task_id" value="<?php echo $task['id']; ?>">
<button type="submit" name="complete_task" class="btn btn-sm btn-success">Complete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
</div>
</main>
<!-- Add Candidate Modal -->
<div class="modal fade" id="addCandidateModal" tabindex="-1" aria-labelledby="addCandidateModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addCandidateModalLabel">Add New Candidate</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="add_candidate" value="1">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="text" class="form-control" id="phone" name="phone">
</div>
<div class="mb-3">
<label for="status" class="form-label">Status</label>
<select class="form-select" id="status" name="status">
<option value="Applied" selected>Applied</option>
<option value="Interviewing">Interviewing</option>
<option value="Offered">Offered</option>
<option value="Hired">Hired</option>
<option value="Rejected">Rejected</option>
</select>
</div>
<div class="mb-3">
<label for="notes" class="form-label">Notes</label>
<textarea class="form-control" id="notes" name="notes" rows="3"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Candidate</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Add Task Modal -->
<div class="modal fade" id="addTaskModal" tabindex="-1" aria-labelledby="addTaskModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addTaskModalLabel">Add New Task</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="add_task" value="1">
<div class="mb-3">
<label for="task_name" class="form-label">Task Name</label>
<input type="text" class="form-control" id="task_name" name="task_name" required>
</div>
<div class="mb-3">
<label for="candidate_id" class="form-label">Assign to Candidate</label>
<select class="form-select" id="candidate_id" name="candidate_id" required>
<option value="" disabled selected>Select a candidate</option>
<?php foreach ($candidates as $candidate): ?>
<option value="<?php echo $candidate['id']; ?>"><?php echo htmlspecialchars($candidate['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label for="due_date" class="form-label">Due Date</label>
<input type="date" class="form-control" id="due_date" name="due_date">
</div>
<div class="mb-3">
<label for="task_status" class="form-label">Status</label>
<select class="form-select" id="task_status" name="status">
<option value="To Do" selected>To Do</option>
<option value="In Progress">In Progress</option>
<option value="Done">Done</option>
</select>
</div>
<div class="mb-3">
<label for="task_description" class="form-label">Description</label>
<textarea class="form-control" id="task_description" name="description" rows="3"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Task</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
<!-- AI Chat Interface -->
<div id="chat-container" class="chat-container">
<div id="chat-header" class="chat-header">
<span>AI Assistant</span>
<button id="close-chat" class="btn-close btn-sm" aria-label="Close"></button>
</div>
<div id="chat-body" class="chat-body">
<!-- Chat messages will be appended here -->
</div>
<div id="chat-input-container" class="chat-input-container">
<input type="text" id="chat-input" class="form-control" placeholder="Ask a question...">
<button id="send-chat" class="btn btn-primary">Send</button>
</div>
</div>
<button id="chat-toggle" class="chat-toggle-button">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-chat-dots" viewBox="0 0 16 16">
<path d="M5 8a1 1 0 1 1-2 0 1 1 0 0 1 2 0zm4 0a1 1 0 1 1-2 0 1 1 0 0 1 2 0zm3 1a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/>
<path d="M2.165 15.803l-1.57-1.57a1 1 0 0 1 0-1.414L2.165 10.5l1.57 1.57-1.57 1.57zm1.57-1.57l-1.57-1.57a1 1 0 0 1 0-1.414l1.57-1.57 1.57 1.57-1.57 1.57zM12.235 2.165l1.57 1.57a1 1 0 0 1 0 1.414l-1.57 1.57-1.57-1.57 1.57-1.57z"/>
<path d="M15.657 2.343a1 1 0 0 1 0 1.414l-1.57 1.57-1.57-1.57 1.57-1.57a1 1 0 0 1 1.414 0z"/>
<path d="M.5 1a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v7a1 1 0 0 1-1 1H2.707l-2.147 2.146a.5.5 0 0 0 .708.708L3.293 11H15a2 2 0 0 0 2-2V1a2 2 0 0 0-2-2H1.5A1.5 1.5 0 0 0 0 1.5v12.793a.5.5 0 0 0 .854.353L.5 1z"/>
</svg>
</button>
</body>
</html>