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

144 lines
6.2 KiB
PHP

<?php
require_once 'auth.php';
// Check if user is logged in
if (!is_logged_in()) {
header('Location: login.php');
exit;
}
if (!hasPermission('view_workflows')) {
header('Location: index.php');
exit;
}
require_once 'db/config.php';
$pdo = db();
// Handle form submission for new workflow
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_workflow']) && hasPermission('manage_workflows')) {
$name = $_POST['name'] ?? '';
$trigger = $_POST['trigger'] ?? '';
if (!empty($name) && !empty($trigger)) {
try {
$stmt = $pdo->prepare("INSERT INTO workflows (name, `trigger`) VALUES (?, ?)");
$stmt->execute([$name, $trigger]);
header("Location: " . $_SERVER['PHP_SELF']);
exit;
} catch (PDOException $e) {
error_log("Error adding workflow: " . $e->getMessage());
}
}
}
// Fetch workflows from the database
$stmt = $pdo->query("SELECT * FROM workflows ORDER BY created_at DESC");
$workflows = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Workflows</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</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="container-fluid">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2>Workflows</h2>
<?php if (hasPermission('manage_workflows')): ?>
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addWorkflowModal">Add Workflow</button>
<?php endif; ?>
</div>
<div class="card">
<div class="card-body">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Trigger</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($workflows as $workflow): ?>
<tr>
<td><?php echo htmlspecialchars($workflow['name']); ?></td>
<td><?php echo htmlspecialchars($workflow['trigger']); ?></td>
<td>
<?php if (hasPermission('manage_workflows')): ?>
<a href="workflow_actions.php?workflow_id=<?php echo $workflow['id']; ?>" class="btn btn-sm btn-outline-primary">Manage Actions</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</main>
<!-- Add Workflow Modal -->
<div class="modal fade" id="addWorkflowModal" tabindex="-1" aria-labelledby="addWorkflowModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addWorkflowModalLabel">Add New Workflow</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form method="POST">
<input type="hidden" name="add_workflow" 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="trigger" class="form-label">Trigger</label>
<select class="form-select" id="trigger" name="trigger" required>
<option value="" disabled selected>Select a trigger</option>
<option value="candidate_created">New Candidate is Created</option>
<option value="task_completed">Task is Completed</option>
</select>
</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 Workflow</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>