38921-vm/edit_template.php
2026-03-01 22:53:30 +00:00

250 lines
13 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id']) || !isset($_SESSION['msp_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
$msp_id = $_SESSION['msp_id'];
$template_id = $_GET['id'] ?? null;
if (!$template_id) {
header('Location: templates.php');
exit;
}
// Security: Check if template belongs to the current MSP
$stmt = db()->prepare("SELECT * FROM onboarding_templates WHERE id = ? AND msp_id = ?");
$stmt->execute([$template_id, $msp_id]);
$template = $stmt->fetch();
if (!$template) {
header('Location: templates.php');
exit;
}
// Handle Form Submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'add_step') {
$title = $_POST['title'];
$description = $_POST['description'];
$stmt = db()->prepare("SELECT MAX(order_index) FROM onboarding_template_steps WHERE template_id = ?");
$stmt->execute([$template_id]);
$max_order = $stmt->fetchColumn() ?: 0;
$stmt = db()->prepare("INSERT INTO onboarding_template_steps (template_id, title, description, order_index) VALUES (?, ?, ?, ?)");
$stmt->execute([$template_id, $title, $description, $max_order + 1]);
header("Location: edit_template.php?id=$template_id");
exit;
}
if ($_POST['action'] === 'add_field') {
$step_id = $_POST['step_id'];
$type = $_POST['type'];
$label = $_POST['label'];
$content = $_POST['content'] ?? '';
$is_required = isset($_POST['is_required']) ? 1 : 0;
$stmt = db()->prepare("SELECT MAX(order_index) FROM onboarding_template_fields WHERE step_id = ?");
$stmt->execute([$step_id]);
$max_order = $stmt->fetchColumn() ?: 0;
$stmt = db()->prepare("INSERT INTO onboarding_template_fields (step_id, type, label, content, is_required, order_index) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$step_id, $type, $label, $content, $is_required, $max_order + 1]);
header("Location: edit_template.php?id=$template_id");
exit;
}
if ($_POST['action'] === 'delete_step') {
$step_id = $_POST['step_id'];
$stmt = db()->prepare("DELETE FROM onboarding_template_steps WHERE id = ? AND template_id = ?");
$stmt->execute([$step_id, $template_id]);
header("Location: edit_template.php?id=$template_id");
exit;
}
}
// Fetch all steps and their fields
$stmt = db()->prepare("SELECT * FROM onboarding_template_steps WHERE template_id = ? ORDER BY order_index ASC");
$stmt->execute([$template_id]);
$steps = $stmt->fetchAll();
foreach ($steps as &$step) {
$stmt = db()->prepare("SELECT * FROM onboarding_template_fields WHERE step_id = ? ORDER BY order_index ASC");
$stmt->execute([$step['id']]);
$step['fields'] = $stmt->fetchAll();
}
include 'header.php';
?>
<div class="container py-5">
<div class="mb-4">
<a href="templates.php" class="btn btn-outline-secondary btn-sm mb-3"><i class="bi bi-arrow-left"></i> Back to Templates</a>
<div class="d-flex justify-content-between align-items-end">
<div>
<h1 class="mb-1">Editing Template: <?php echo htmlspecialchars($template['name']); ?></h1>
<p class="text-muted"><?php echo htmlspecialchars($template['description']); ?></p>
</div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addStepModal">
<i class="bi bi-plus-circle"></i> Add Step/Page
</button>
</div>
</div>
<?php if (empty($steps)): ?>
<div class="alert alert-info py-5 text-center">
<h4 class="mb-3">This template has no steps yet.</h4>
<p class="text-muted">Start by adding a new page to your onboarding flow.</p>
<button type="button" class="btn btn-primary mt-3" data-bs-toggle="modal" data-bs-target="#addStepModal">
<i class="bi bi-plus-circle"></i> Add First Step
</button>
</div>
<?php endif; ?>
<div class="onboarding-flow mt-5">
<?php foreach ($steps as $index => $step): ?>
<div class="card shadow-sm border-0 mb-5">
<div class="card-header bg-white p-4 border-bottom-0 d-flex justify-content-between align-items-center">
<h3 class="fw-bold mb-0">Step <?php echo $index + 1; ?>: <?php echo htmlspecialchars($step['title']); ?></h3>
<div>
<button type="button" class="btn btn-outline-primary btn-sm me-2" data-bs-toggle="modal" data-bs-target="#addFieldModal<?php echo $step['id']; ?>">
<i class="bi bi-plus-lg"></i> Add Item
</button>
<form method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this entire step?');">
<input type="hidden" name="action" value="delete_step">
<input type="hidden" name="step_id" value="<?php echo $step['id']; ?>">
<button type="submit" class="btn btn-outline-danger btn-sm"><i class="bi bi-trash"></i></button>
</form>
</div>
</div>
<div class="card-body p-4 pt-0">
<p class="text-muted mb-4"><?php echo htmlspecialchars($step['description']); ?></p>
<div class="list-group list-group-flush border rounded">
<?php foreach ($step['fields'] as $field): ?>
<div class="list-group-item p-3">
<div class="d-flex justify-content-between align-items-center">
<div>
<span class="badge bg-light text-dark border me-2"><?php echo strtoupper(str_replace('_', ' ', $field['type'])); ?></span>
<span class="fw-bold"><?php echo htmlspecialchars($field['label']); ?></span>
<?php if ($field['is_required']): ?>
<span class="text-danger">*</span>
<?php endif; ?>
</div>
<div>
<button class="btn btn-link btn-sm p-0 text-muted me-2"><i class="bi bi-pencil"></i></button>
<button class="btn btn-link btn-sm p-0 text-danger"><i class="bi bi-x-lg"></i></button>
</div>
</div>
<?php if ($field['type'] === 'text'): ?>
<div class="mt-2 small text-muted border-start ps-3 py-2 italic"><?php echo nl2br(htmlspecialchars($field['content'])); ?></div>
<?php elseif ($field['type'] === 'download'): ?>
<div class="mt-2"><a href="<?php echo htmlspecialchars($field['content']); ?>" target="_blank" class="btn btn-light btn-sm"><i class="bi bi-download"></i> Download Resource</a></div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php if (empty($step['fields'])): ?>
<div class="list-group-item p-5 text-center text-muted italic">
No items added to this step yet.
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Add Field Modal for this step -->
<div class="modal fade" id="addFieldModal<?php echo $step['id']; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Add Item to Step: <?php echo htmlspecialchars($step['title']); ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="add_field">
<input type="hidden" name="step_id" value="<?php echo $step['id']; ?>">
<div class="mb-3">
<label class="form-label">Type</label>
<select name="type" class="form-select" required onchange="toggleFieldOptions(this, 'options_<?php echo $step['id']; ?>')">
<option value="text">Informational Text (HTML Allowed)</option>
<option value="form_input">Form Input (Single Line)</option>
<option value="form_textarea">Form Textarea (Multi-line)</option>
<option value="form_select">Dropdown Menu</option>
<option value="download">Download Link/File</option>
<option value="upload">Customer Upload Field</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Label/Title</label>
<input type="text" name="label" class="form-control" placeholder="e.g. Please enter your VAT number" required>
</div>
<div id="options_<?php echo $step['id']; ?>">
<div class="mb-3">
<label class="form-label">Content/Options</label>
<textarea name="content" class="form-control" rows="3" placeholder="Enter text content, download URL, or comma-separated options for dropdown."></textarea>
</div>
</div>
<div class="mb-3 form-check">
<input type="checkbox" name="is_required" class="form-check-input" id="req_<?php echo $step['id']; ?>">
<label class="form-check-label" for="req_<?php echo $step['id']; ?>">Is this field required?</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Add Item</button>
</div>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<!-- Add Step Modal -->
<div class="modal fade" id="addStepModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Add New Page/Step</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="add_step">
<div class="mb-3">
<label class="form-label">Step Title</label>
<input type="text" name="title" class="form-control" placeholder="e.g. Company Details" required>
</div>
<div class="mb-3">
<label class="form-label">Description/Instruction</label>
<textarea name="description" class="form-control" rows="2" placeholder="Tell the customer what this step is about."></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Add Step</button>
</div>
</form>
</div>
</div>
</div>
<script>
function toggleFieldOptions(select, optionsId) {
// We could hide/show specific inputs based on type here
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<?php include 'footer.php'; ?>