📁 File Manager
Editing:
/home/wvl7mof7402e/public_html/chem/admin/forms.php
<?php /** * Pure Chemical Admin * File: admin/forms.php * * Existing table: * forms * * No new table required. */ if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } require_once __DIR__ . '/includes/db.php'; if (file_exists(__DIR__ . '/includes/auth.php')) { require_once __DIR__ . '/includes/auth.php'; } if (file_exists(__DIR__ . '/includes/functions.php')) { require_once __DIR__ . '/includes/functions.php'; } if (file_exists(__DIR__ . '/includes/csrf.php')) { require_once __DIR__ . '/includes/csrf.php'; } if (function_exists('requireAdmin')) { requireAdmin(); } /* ========================================================= HELPERS ========================================================= */ if (!function_exists('e')) { function e($value) { return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8'); } } function formsCsrfToken() { if (function_exists('csrf_token')) { return csrf_token(); } if (empty($_SESSION['forms_csrf_token'])) { $_SESSION['forms_csrf_token'] = bin2hex(random_bytes(32)); } return $_SESSION['forms_csrf_token']; } function formsVerifyCsrf($token) { if (function_exists('csrf_verify')) { return csrf_verify($token); } return isset($_SESSION['forms_csrf_token']) && hash_equals($_SESSION['forms_csrf_token'], (string) $token); } function formsSlugify($text) { $text = trim((string) $text); if ($text === '') { return ''; } if (function_exists('mb_strtolower')) { $text = mb_strtolower($text, 'UTF-8'); } else { $text = strtolower($text); } $text = preg_replace('/[^\pL\pN]+/u', '-', $text); $text = trim($text, '-'); return $text; } function formsTableExists(PDO $pdo, $table) { try { $stmt = $pdo->prepare(" SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ? "); $stmt->execute([$table]); return (bool) $stmt->fetchColumn(); } catch (Throwable $e) { return false; } } function formsGetFieldsCount(PDO $pdo, $formId) { if (!formsTableExists($pdo, 'form_fields')) { return 0; } try { $stmt = $pdo->prepare(" SELECT COUNT(*) FROM form_fields WHERE form_id = ? "); $stmt->execute([$formId]); return (int) $stmt->fetchColumn(); } catch (Throwable $e) { return 0; } } function formsFlash($type, $message) { $_SESSION['forms_flash'] = [ 'type' => $type, 'message' => $message ]; } function formsGetFlash() { if (empty($_SESSION['forms_flash'])) { return null; } $flash = $_SESSION['forms_flash']; unset($_SESSION['forms_flash']); return $flash; } /* ========================================================= ADMIN INFORMATION ========================================================= */ $adminName = $_SESSION['ADMIN_NAME'] ?? $_SESSION['admin_name'] ?? $_SESSION['name'] ?? 'Administrator'; $adminEmail = $_SESSION['ADMIN_EMAIL'] ?? $_SESSION['admin_email'] ?? ''; $csrfToken = formsCsrfToken(); /* ========================================================= ACTION HANDLER ========================================================= */ $action = $_POST['action'] ?? ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $postedToken = $_POST['csrf_token'] ?? ''; if (!formsVerifyCsrf($postedToken)) { formsFlash('error', 'Security verification failed. Please try again.'); header('Location: forms.php'); exit; } /* ===================================================== SAVE FORM ===================================================== */ if ($action === 'save') { $id = (int) ($_POST['id'] ?? 0); $name = trim($_POST['name'] ?? ''); $slug = trim($_POST['slug'] ?? ''); $successMessage = trim($_POST['success_message'] ?? ''); $emailNotification = trim($_POST['email_notification'] ?? ''); $status = isset($_POST['status']) ? 1 : 0; if ($name === '') { formsFlash('error', 'Form name is required.'); header('Location: forms.php' . ($id > 0 ? '?edit=' . $id : '')); exit; } if ($slug === '') { $slug = formsSlugify($name); } else { $slug = formsSlugify($slug); } if ($slug === '') { formsFlash('error', 'Please provide a valid form slug.'); header('Location: forms.php' . ($id > 0 ? '?edit=' . $id : '')); exit; } /* --------------------------------------------- Email validation --------------------------------------------- */ if ($emailNotification !== '') { $emails = preg_split( '/[,;\s]+/', $emailNotification, -1, PREG_SPLIT_NO_EMPTY ); foreach ($emails as $email) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { formsFlash( 'error', 'Invalid notification email: ' . $email ); header( 'Location: forms.php' . ($id > 0 ? '?edit=' . $id : '') ); exit; } } } /* --------------------------------------------- Slug uniqueness --------------------------------------------- */ try { if ($id > 0) { $stmt = $pdo->prepare(" SELECT id FROM forms WHERE slug = ? AND id != ? LIMIT 1 "); $stmt->execute([ $slug, $id ]); } else { $stmt = $pdo->prepare(" SELECT id FROM forms WHERE slug = ? LIMIT 1 "); $stmt->execute([ $slug ]); } if ($stmt->fetch()) { formsFlash( 'error', 'This slug is already used by another form.' ); header( 'Location: forms.php' . ($id > 0 ? '?edit=' . $id : '') ); exit; } /* ----------------------------------------- UPDATE ----------------------------------------- */ if ($id > 0) { $stmt = $pdo->prepare(" UPDATE forms SET name = ?, slug = ?, success_message = ?, email_notification = ?, status = ? WHERE id = ? "); $stmt->execute([ $name, $slug, $successMessage !== '' ? $successMessage : null, $emailNotification !== '' ? $emailNotification : null, $status, $id ]); formsFlash( 'success', 'Form updated successfully.' ); } else { /* ----------------------------------------- INSERT ----------------------------------------- */ $stmt = $pdo->prepare(" INSERT INTO forms ( name, slug, success_message, email_notification, status ) VALUES ( ?, ?, ?, ?, ? ) "); $stmt->execute([ $name, $slug, $successMessage !== '' ? $successMessage : null, $emailNotification !== '' ? $emailNotification : null, $status ]); formsFlash( 'success', 'New form created successfully.' ); } } catch (Throwable $e) { error_log( 'Forms Save Error: ' . $e->getMessage() ); formsFlash( 'error', 'Unable to save form. Please try again.' ); } header('Location: forms.php'); exit; } /* ===================================================== DELETE FORM ===================================================== */ if ($action === 'delete') { $id = (int) ($_POST['id'] ?? 0); if ($id <= 0) { formsFlash( 'error', 'Invalid form selected.' ); header('Location: forms.php'); exit; } try { /* --------------------------------------------- Check form exists --------------------------------------------- */ $stmt = $pdo->prepare(" SELECT id, name FROM forms WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); $form = $stmt->fetch(); if (!$form) { formsFlash( 'error', 'Form not found.' ); header('Location: forms.php'); exit; } /* --------------------------------------------- Check form fields --------------------------------------------- */ $fieldCount = formsGetFieldsCount($pdo, $id); if ($fieldCount > 0) { formsFlash( 'error', 'This form has ' . $fieldCount . ' field(s). Please remove its fields first from form field management.' ); header('Location: forms.php'); exit; } /* --------------------------------------------- Delete form --------------------------------------------- */ $stmt = $pdo->prepare(" DELETE FROM forms WHERE id = ? "); $stmt->execute([$id]); formsFlash( 'success', 'Form "' . $form['name'] . '" deleted successfully.' ); } catch (Throwable $e) { error_log( 'Forms Delete Error: ' . $e->getMessage() ); formsFlash( 'error', 'Unable to delete this form. It may be connected with other records.' ); } header('Location: forms.php'); exit; } /* ===================================================== TOGGLE STATUS ===================================================== */ if ($action === 'toggle_status') { $id = (int) ($_POST['id'] ?? 0); if ($id <= 0) { formsFlash( 'error', 'Invalid form selected.' ); header('Location: forms.php'); exit; } try { $stmt = $pdo->prepare(" UPDATE forms SET status = IF(status = 1, 0, 1) WHERE id = ? "); $stmt->execute([$id]); formsFlash( 'success', 'Form status updated.' ); } catch (Throwable $e) { error_log( 'Forms Status Error: ' . $e->getMessage() ); formsFlash( 'error', 'Unable to update form status.' ); } header('Location: forms.php'); exit; } } /* ========================================================= FLASH ========================================================= */ $flash = formsGetFlash(); /* ========================================================= FILTERS ========================================================= */ $search = trim($_GET['search'] ?? ''); $statusFilter = $_GET['status'] ?? 'all'; $page = max( 1, (int) ($_GET['page'] ?? 1) ); $perPage = 12; $offset = ($page - 1) * $perPage; /* ========================================================= EDIT FORM ========================================================= */ $editId = (int) ($_GET['edit'] ?? 0); $editForm = null; if ($editId > 0) { try { $stmt = $pdo->prepare(" SELECT id, name, slug, success_message, email_notification, status, created_at, updated_at FROM forms WHERE id = ? LIMIT 1 "); $stmt->execute([$editId]); $editForm = $stmt->fetch(); } catch (Throwable $e) { $editForm = null; } } /* ========================================================= STATS ========================================================= */ $stats = [ 'total' => 0, 'active' => 0, 'inactive' => 0, 'fields' => 0 ]; try { $stmt = $pdo->query(" SELECT COUNT(*) AS total, SUM(status = 1) AS active, SUM(status = 0) AS inactive FROM forms "); $row = $stmt->fetch(); if ($row) { $stats['total'] = (int) ($row['total'] ?? 0); $stats['active'] = (int) ($row['active'] ?? 0); $stats['inactive'] = (int) ($row['inactive'] ?? 0); } if (formsTableExists($pdo, 'form_fields')) { $stmt = $pdo->query(" SELECT COUNT(*) FROM form_fields "); $stats['fields'] = (int) $stmt->fetchColumn(); } } catch (Throwable $e) { // Keep default stats. } /* ========================================================= TOTAL RECORDS ========================================================= */ $where = []; $params = []; if ($search !== '') { $where[] = " ( f.name LIKE ? OR f.slug LIKE ? OR f.success_message LIKE ? OR f.email_notification LIKE ? ) "; $searchLike = '%' . $search . '%'; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; } if ($statusFilter === 'active') { $where[] = "f.status = 1"; } elseif ($statusFilter === 'inactive') { $where[] = "f.status = 0"; } $whereSql = ''; if (!empty($where)) { $whereSql = 'WHERE ' . implode(' AND ', $where); } /* ========================================================= COUNT ========================================================= */ $totalRows = 0; try { $stmt = $pdo->prepare(" SELECT COUNT(*) FROM forms f {$whereSql} "); $stmt->execute($params); $totalRows = (int) $stmt->fetchColumn(); } catch (Throwable $e) { $totalRows = 0; } $totalPages = max( 1, (int) ceil($totalRows / $perPage) ); if ($page > $totalPages) { $page = $totalPages; $offset = ($page - 1) * $perPage; } /* ========================================================= FETCH FORMS ========================================================= */ $forms = []; try { $stmt = $pdo->prepare(" SELECT f.id, f.name, f.slug, f.success_message, f.email_notification, f.status, f.created_at, f.updated_at, ( SELECT COUNT(*) FROM form_fields ff WHERE ff.form_id = f.id ) AS field_count FROM forms f {$whereSql} ORDER BY f.id DESC LIMIT {$perPage} OFFSET {$offset} "); $stmt->execute($params); $forms = $stmt->fetchAll(); } catch (Throwable $e) { error_log( 'Forms Fetch Error: ' . $e->getMessage() ); $forms = []; } /* ========================================================= PAGINATION URL ========================================================= */ function formsPageUrl($page) { $query = $_GET; $query['page'] = $page; return 'forms.php?' . http_build_query($query); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <title>Forms | Pure Chemical Admin</title> <meta name="description" content="Manage website forms" > <link rel="preconnect" href="https://fonts.googleapis.com" > <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin > <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet" > <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" > <style> :root { --primary: #176b52; --primary-dark: #0e4e3c; --primary-soft: #eaf7f1; --dark: #10231d; --muted: #71807a; --border: #e5ebe8; --bg: #f5f8f7; --white: #ffffff; --success: #14804a; --success-bg: #eaf8f0; --danger: #c53b3b; --danger-bg: #fff0f0; --warning: #b97800; --warning-bg: #fff8e8; --shadow: 0 15px 45px rgba(16, 35, 29, .07); --radius: 18px; } * { box-sizing: border-box; } html { scroll-behavior: smooth; } body { margin: 0; font-family: "Inter", sans-serif; background: var(--bg); color: var(--dark); } a { color: inherit; text-decoration: none; } button, input, textarea, select { font: inherit; } /* ================================================= MAIN AREA ================================================= */ .main { margin-left: 280px; min-height: 100vh; } /* ================================================= TOPBAR ================================================= */ .topbar { height: 78px; background: rgba(255, 255, 255, .94); backdrop-filter: blur(18px); border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; padding: 0 32px; position: sticky; top: 0; z-index: 50; } .topbar-left { display: flex; align-items: center; gap: 15px; } .page-icon { width: 45px; height: 45px; border-radius: 13px; display: flex; align-items: center; justify-content: center; background: var(--primary-soft); color: var(--primary); font-size: 19px; } .page-title h1 { margin: 0; font-size: 19px; font-weight: 800; letter-spacing: -.3px; } .page-title p { margin: 4px 0 0; color: var(--muted); font-size: 12px; } .admin-profile { display: flex; align-items: center; gap: 11px; } .admin-avatar { width: 42px; height: 42px; border-radius: 50%; background: linear-gradient( 135deg, var(--primary), #29966f ); color: white; display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 800; } .admin-info strong { display: block; font-size: 13px; } .admin-info span { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; } /* ================================================= CONTENT ================================================= */ .content { padding: 30px 32px 50px; } /* ================================================= FLASH ================================================= */ .alert { padding: 14px 17px; border-radius: 14px; margin-bottom: 22px; display: flex; align-items: center; gap: 11px; font-size: 13px; font-weight: 600; } .alert.success { background: var(--success-bg); color: var(--success); border: 1px solid #ccebd9; } .alert.error { background: var(--danger-bg); color: var(--danger); border: 1px solid #f1cccc; } /* ================================================= HERO ================================================= */ .hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 25px; margin-bottom: 25px; } .hero h2 { margin: 0; font-size: 27px; font-weight: 800; letter-spacing: -.8px; } .hero p { margin: 8px 0 0; color: var(--muted); font-size: 13px; } .btn { border: 0; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 9px; min-height: 43px; padding: 0 17px; border-radius: 12px; font-size: 13px; font-weight: 700; transition: .2s ease; } .btn-primary { background: var(--primary); color: white; box-shadow: 0 8px 20px rgba(23, 107, 82, .2); } .btn-primary:hover { background: var(--primary-dark); transform: translateY(-1px); } .btn-light { background: white; color: var(--dark); border: 1px solid var(--border); } .btn-light:hover { border-color: #c9d5d0; } /* ================================================= STAT CARDS ================================================= */ .stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 25px; } .stat-card { background: white; border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); position: relative; overflow: hidden; } .stat-card::after { content: ""; position: absolute; width: 75px; height: 75px; right: -30px; bottom: -30px; border-radius: 50%; background: var(--primary-soft); } .stat-top { display: flex; align-items: center; justify-content: space-between; } .stat-icon { width: 43px; height: 43px; display: flex; align-items: center; justify-content: center; border-radius: 12px; background: var(--primary-soft); color: var(--primary); } .stat-number { margin-top: 15px; font-size: 27px; line-height: 1; font-weight: 800; } .stat-label { margin-top: 7px; color: var(--muted); font-size: 12px; font-weight: 600; } /* ================================================= TOOLBAR ================================================= */ .panel { background: white; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); } .toolbar { padding: 18px; display: flex; align-items: center; justify-content: space-between; gap: 14px; border-bottom: 1px solid var(--border); } .search-form { display: flex; align-items: center; gap: 9px; flex: 1; } .search-box { position: relative; flex: 1; max-width: 460px; } .search-box i { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); color: #91a099; font-size: 13px; } .input { width: 100%; height: 43px; padding: 0 13px; border: 1px solid var(--border); border-radius: 11px; outline: none; background: #fbfcfc; color: var(--dark); font-size: 13px; transition: .2s ease; } .search-box .input { padding-left: 39px; } .input:focus { border-color: #91cbb7; box-shadow: 0 0 0 4px rgba(23, 107, 82, .08); background: white; } select.input { cursor: pointer; } /* ================================================= TABLE ================================================= */ .table-wrap { overflow-x: auto; } table { width: 100%; border-collapse: collapse; min-width: 950px; } th { padding: 14px 18px; text-align: left; color: #7a8983; font-size: 10px; text-transform: uppercase; letter-spacing: .8px; font-weight: 800; background: #fbfcfc; border-bottom: 1px solid var(--border); } td { padding: 17px 18px; border-bottom: 1px solid #edf1ef; vertical-align: middle; font-size: 13px; } tbody tr { transition: .18s ease; } tbody tr:hover { background: #fbfdfc; } tbody tr:last-child td { border-bottom: 0; } /* ================================================= FORM INFO ================================================= */ .form-name { display: flex; align-items: center; gap: 12px; } .form-icon { width: 40px; height: 40px; flex: 0 0 40px; display: flex; align-items: center; justify-content: center; background: var(--primary-soft); color: var(--primary); border-radius: 11px; } .form-name strong { display: block; font-size: 13px; } .form-name span { display: block; margin-top: 4px; color: var(--muted); font-size: 11px; } .slug { display: inline-block; padding: 6px 9px; border-radius: 8px; background: #f3f6f5; color: #61716b; font-size: 11px; font-family: monospace; } /* ================================================= BADGES ================================================= */ .badge { display: inline-flex; align-items: center; gap: 6px; padding: 6px 9px; border-radius: 100px; font-size: 10px; font-weight: 800; } .badge::before { content: ""; width: 5px; height: 5px; border-radius: 50%; } .badge.active { background: var(--success-bg); color: var(--success); } .badge.active::before { background: var(--success); } .badge.inactive { background: #f1f3f3; color: #7d8884; } .badge.inactive::before { background: #89938f; } /* ================================================= FIELD COUNT ================================================= */ .field-count { display: inline-flex; align-items: center; gap: 6px; color: var(--primary); font-weight: 700; font-size: 12px; } /* ================================================= ACTIONS ================================================= */ .actions { display: flex; align-items: center; gap: 7px; } .icon-btn { width: 35px; height: 35px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--border); background: white; color: #65736e; cursor: pointer; transition: .2s ease; } .icon-btn:hover { border-color: #bdd0c8; color: var(--primary); background: var(--primary-soft); } .icon-btn.delete:hover { color: var(--danger); background: var(--danger-bg); border-color: #f0cccc; } .icon-btn.status:hover { color: var(--warning); background: var(--warning-bg); } /* ================================================= EMPTY ================================================= */ .empty { padding: 70px 20px; text-align: center; } .empty-icon { width: 65px; height: 65px; margin: 0 auto 17px; display: flex; align-items: center; justify-content: center; border-radius: 18px; background: var(--primary-soft); color: var(--primary); font-size: 24px; } .empty h3 { margin: 0; font-size: 16px; } .empty p { margin: 7px 0 0; color: var(--muted); font-size: 12px; } /* ================================================= PAGINATION ================================================= */ .pagination { padding: 17px 18px; border-top: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 15px; } .pagination-info { color: var(--muted); font-size: 12px; } .pagination-links { display: flex; align-items: center; gap: 5px; } .page-link { min-width: 34px; height: 34px; padding: 0 9px; display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 9px; background: white; color: #63716c; font-size: 11px; font-weight: 700; } .page-link:hover { border-color: #b8cec5; color: var(--primary); } .page-link.active { background: var(--primary); color: white; border-color: var(--primary); } /* ================================================= MODAL ================================================= */ .modal { position: fixed; inset: 0; background: rgba(10, 27, 21, .58); backdrop-filter: blur(6px); display: none; align-items: center; justify-content: center; padding: 20px; z-index: 100; } .modal.show { display: flex; } .modal-card { width: min(680px, 100%); max-height: calc(100vh - 40px); overflow-y: auto; background: white; border-radius: 22px; box-shadow: 0 30px 90px rgba(0, 0, 0, .22); animation: modalIn .2s ease; } @keyframes modalIn { from { opacity: 0; transform: translateY(15px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } } .modal-header { padding: 21px 23px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); } .modal-title { display: flex; align-items: center; gap: 12px; } .modal-title-icon { width: 42px; height: 42px; border-radius: 12px; background: var(--primary-soft); color: var(--primary); display: flex; align-items: center; justify-content: center; } .modal-title h3 { margin: 0; font-size: 17px; } .modal-title p { margin: 4px 0 0; color: var(--muted); font-size: 11px; } .modal-close { width: 35px; height: 35px; border: 0; border-radius: 10px; background: #f3f5f4; color: #66736e; cursor: pointer; } .modal-body { padding: 23px; } .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 17px; } .field { margin-bottom: 17px; } .field.full { grid-column: 1 / -1; } .field label { display: block; margin-bottom: 7px; font-size: 11px; font-weight: 800; color: #44534d; } .required { color: var(--danger); } textarea.input { min-height: 100px; padding: 12px 13px; resize: vertical; } .help { margin-top: 6px; color: #89958f; font-size: 10px; line-height: 1.5; } .toggle-row { min-height: 43px; display: flex; align-items: center; justify-content: space-between; padding: 0 13px; border: 1px solid var(--border); border-radius: 11px; background: #fbfcfc; } .toggle-text strong { display: block; font-size: 12px; } .toggle-text span { display: block; margin-top: 2px; color: var(--muted); font-size: 9px; } .switch { position: relative; width: 42px; height: 23px; } .switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; inset: 0; cursor: pointer; background: #ccd5d1; border-radius: 50px; transition: .2s; } .slider::before { content: ""; position: absolute; width: 17px; height: 17px; left: 3px; top: 3px; border-radius: 50%; background: white; transition: .2s; box-shadow: 0 2px 5px rgba(0,0,0,.12); } .switch input:checked + .slider { background: var(--primary); } .switch input:checked + .slider::before { transform: translateX(19px); } .modal-footer { padding: 17px 23px; border-top: 1px solid var(--border); display: flex; align-items: center; justify-content: flex-end; gap: 9px; } /* ================================================= DELETE MODAL ================================================= */ .delete-card { width: min(440px, 100%); background: white; border-radius: 22px; padding: 28px; text-align: center; } .delete-icon { width: 58px; height: 58px; margin: 0 auto 15px; border-radius: 17px; display: flex; align-items: center; justify-content: center; background: var(--danger-bg); color: var(--danger); font-size: 21px; } .delete-card h3 { margin: 0; font-size: 18px; } .delete-card p { margin: 9px 0 0; color: var(--muted); font-size: 12px; line-height: 1.6; } .delete-actions { display: flex; justify-content: center; gap: 9px; margin-top: 22px; } .btn-danger { background: var(--danger); color: white; } .btn-danger:hover { background: #aa2e2e; } /* ================================================= RESPONSIVE ================================================= */ @media (max-width: 1200px) { .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } } @media (max-width: 1050px) { .main { margin-left: 0; } .topbar { padding: 0 20px; } .content { padding: 25px 20px 40px; } } @media (max-width: 720px) { .topbar { height: 70px; } .page-title p { display: none; } .admin-info { display: none; } .content { padding: 20px 14px 35px; } .hero { align-items: flex-start; flex-direction: column; } .hero h2 { font-size: 23px; } .stats { grid-template-columns: 1fr 1fr; gap: 10px; } .stat-card { padding: 15px; } .stat-number { font-size: 22px; } .toolbar { flex-direction: column; align-items: stretch; } .search-form { width: 100%; flex-direction: column; align-items: stretch; } .search-box { max-width: none; } .form-grid { grid-template-columns: 1fr; } .field.full { grid-column: auto; } .pagination { flex-direction: column; align-items: stretch; } .pagination-links { justify-content: center; } } @media (max-width: 450px) { .stats { grid-template-columns: 1fr; } .topbar-left { gap: 9px; } .page-icon { width: 40px; height: 40px; } .page-title h1 { font-size: 16px; } .modal { padding: 10px; } .modal-body { padding: 18px; } .modal-header, .modal-footer { padding-left: 18px; padding-right: 18px; } } </style> </head> <body> <?php /* |-------------------------------------------------------------------------- | Existing Sidebar |-------------------------------------------------------------------------- | IMPORTANT: | sidebar.php is intentionally included separately. */ require_once __DIR__ . '/sidebar.php'; ?> <div class="main"> <!-- ===================================================== TOPBAR ====================================================== --> <header class="topbar"> <div class="topbar-left"> <div class="page-icon"> <i class="fa-solid fa-file-lines"></i> </div> <div class="page-title"> <h1>Forms</h1> <p> Manage website forms and submissions structure </p> </div> </div> <div class="admin-profile"> <div class="admin-avatar"> <?php echo e( strtoupper( substr( trim($adminName), 0, 1 ) ) ); ?> </div> <div class="admin-info"> <strong> <?= e($adminName) ?> </strong> <span> <?= e($adminEmail ?: 'Administrator') ?> </span> </div> </div> </header> <!-- ===================================================== CONTENT ====================================================== --> <main class="content"> <?php if ($flash): ?> <div class="alert <?= e($flash['type']) ?>"> <?php if ($flash['type'] === 'success'): ?> <i class="fa-solid fa-circle-check"></i> <?php else: ?> <i class="fa-solid fa-circle-exclamation"></i> <?php endif; ?> <span> <?= e($flash['message']) ?> </span> </div> <?php endif; ?> <!-- ================================================= HERO ================================================== --> <section class="hero"> <div> <h2>Website Forms</h2> <p> Create and manage enquiry, contact and other website forms. </p> </div> <button type="button" class="btn btn-primary" onclick="openFormModal()" > <i class="fa-solid fa-plus"></i> Create Form </button> </section> <!-- ================================================= STATS ================================================== --> <section class="stats"> <div class="stat-card"> <div class="stat-top"> <div class="stat-icon"> <i class="fa-solid fa-file-lines"></i> </div> </div> <div class="stat-number"> <?= number_format($stats['total']) ?> </div> <div class="stat-label"> Total Forms </div> </div> <div class="stat-card"> <div class="stat-top"> <div class="stat-icon"> <i class="fa-solid fa-circle-check"></i> </div> </div> <div class="stat-number"> <?= number_format($stats['active']) ?> </div> <div class="stat-label"> Active Forms </div> </div> <div class="stat-card"> <div class="stat-top"> <div class="stat-icon"> <i class="fa-solid fa-circle-pause"></i> </div> </div> <div class="stat-number"> <?= number_format($stats['inactive']) ?> </div> <div class="stat-label"> Inactive Forms </div> </div> <div class="stat-card"> <div class="stat-top"> <div class="stat-icon"> <i class="fa-solid fa-list-check"></i> </div> </div> <div class="stat-number"> <?= number_format($stats['fields']) ?> </div> <div class="stat-label"> Total Form Fields </div> </div> </section> <!-- ================================================= MAIN PANEL ================================================== --> <section class="panel"> <!-- TOOLBAR --> <div class="toolbar"> <form method="get" action="forms.php" class="search-form" > <div class="search-box"> <i class="fa-solid fa-magnifying-glass"></i> <input type="text" name="search" class="input" placeholder="Search form name, slug or email..." value="<?= e($search) ?>" > </div> <select name="status" class="input" style="max-width:160px;" onchange="this.form.submit()" > <option value="all" <?= $statusFilter === 'all' ? 'selected' : '' ?> > All Status </option> <option value="active" <?= $statusFilter === 'active' ? 'selected' : '' ?> > Active </option> <option value="inactive" <?= $statusFilter === 'inactive' ? 'selected' : '' ?> > Inactive </option> </select> <button type="submit" class="btn btn-primary" > <i class="fa-solid fa-filter"></i> Filter </button> <?php if ($search !== '' || $statusFilter !== 'all'): ?> <a href="forms.php" class="btn btn-light" > Reset </a> <?php endif; ?> </form> </div> <!-- TABLE --> <?php if (!empty($forms)): ?> <div class="table-wrap"> <table> <thead> <tr> <th> Form </th> <th> Slug </th> <th> Fields </th> <th> Notification </th> <th> Status </th> <th> Created </th> <th> Actions </th> </tr> </thead> <tbody> <?php foreach ($forms as $form): ?> <tr> <!-- FORM --> <td> <div class="form-name"> <div class="form-icon"> <i class="fa-regular fa-file-lines"></i> </div> <div> <strong> <?= e($form['name']) ?> </strong> <span> ID #<?= (int) $form['id'] ?> </span> </div> </div> </td> <!-- SLUG --> <td> <span class="slug"> /<?= e($form['slug']) ?> </span> </td> <!-- FIELDS --> <td> <span class="field-count"> <i class="fa-solid fa-list-check"></i> <?= number_format( (int) ($form['field_count'] ?? 0) ) ?> </span> </td> <!-- EMAIL --> <td> <?php if (!empty($form['email_notification'])): ?> <span title="<?= e($form['email_notification']) ?>" style=" display:inline-flex; align-items:center; gap:6px; max-width:190px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; color:#53625c; font-size:11px; " > <i class="fa-regular fa-envelope" style="color:var(--primary);" ></i> <?= e($form['email_notification']) ?> </span> <?php else: ?> <span style=" color:#a0aaa6; font-size:11px; " > Not configured </span> <?php endif; ?> </td> <!-- STATUS --> <td> <?php if ((int) $form['status'] === 1): ?> <span class="badge active"> Active </span> <?php else: ?> <span class="badge inactive"> Inactive </span> <?php endif; ?> </td> <!-- CREATED --> <td> <span style=" color:#6e7b76; font-size:11px; white-space:nowrap; " > <?php echo !empty($form['created_at']) ? e( date( 'd M Y', strtotime($form['created_at']) ) ) : '—'; ?> </span> </td> <!-- ACTIONS --> <td> <div class="actions"> <!-- EDIT --> <a href="forms.php?edit=<?= (int) $form['id'] ?>" class="icon-btn" title="Edit Form" > <i class="fa-solid fa-pen"></i> </a> <!-- STATUS --> <form method="post" style="display:inline;" onsubmit="return confirmStatus();" > <input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>" > <input type="hidden" name="action" value="toggle_status" > <input type="hidden" name="id" value="<?= (int) $form['id'] ?>" > <button type="submit" class="icon-btn status" title="Toggle Status" > <?php if ((int) $form['status'] === 1): ?> <i class="fa-solid fa-toggle-on"></i> <?php else: ?> <i class="fa-solid fa-toggle-off"></i> <?php endif; ?> </button> </form> <!-- DELETE --> <button type="button" class="icon-btn delete" title="Delete Form" onclick="openDeleteModal( <?= (int) $form['id'] ?>, <?= htmlspecialchars( json_encode( $form['name'], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ), ENT_QUOTES, 'UTF-8' ) ?> )" > <i class="fa-regular fa-trash-can"></i> </button> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <!-- PAGINATION --> <?php if ($totalPages > 1): ?> <div class="pagination"> <div class="pagination-info"> Showing <strong> <?= $totalRows > 0 ? $offset + 1 : 0 ?> </strong> - <strong> <?= min( $offset + $perPage, $totalRows ) ?> </strong> of <strong> <?= number_format($totalRows) ?> </strong> forms </div> <div class="pagination-links"> <?php if ($page > 1): ?> <a href="<?= e(formsPageUrl($page - 1)) ?>" class="page-link" title="Previous" > <i class="fa-solid fa-chevron-left"></i> </a> <?php endif; ?> <?php $startPage = max( 1, $page - 2 ); $endPage = min( $totalPages, $page + 2 ); for ( $p = $startPage; $p <= $endPage; $p++ ): ?> <a href="<?= e(formsPageUrl($p)) ?>" class="page-link <?= $p === $page ? 'active' : '' ?>" > <?= $p ?> </a> <?php endfor; ?> <?php if ($page < $totalPages): ?> <a href="<?= e(formsPageUrl($page + 1)) ?>" class="page-link" title="Next" > <i class="fa-solid fa-chevron-right"></i> </a> <?php endif; ?> </div> </div> <?php endif; ?> <?php else: ?> <div class="empty"> <div class="empty-icon"> <i class="fa-regular fa-file-lines"></i> </div> <?php if ($search !== '' || $statusFilter !== 'all'): ?> <h3> No forms found </h3> <p> Try changing your search or status filter. </p> <br> <a href="forms.php" class="btn btn-light" > <i class="fa-solid fa-rotate-left"></i> Clear Filters </a> <?php else: ?> <h3> No forms created yet </h3> <p> Create your first website form to get started. </p> <br> <button type="button" class="btn btn-primary" onclick="openFormModal()" > <i class="fa-solid fa-plus"></i> Create First Form </button> <?php endif; ?> </div> <?php endif; ?> </section> </main> </div> <!-- ========================================================= CREATE / EDIT MODAL ========================================================= --> <div class="modal <?= $editForm ? 'show' : '' ?>" id="formModal" onclick="closeOnBackdrop(event)" > <div class="modal-card"> <div class="modal-header"> <div class="modal-title"> <div class="modal-title-icon"> <i class="fa-solid fa-file-pen"></i> </div> <div> <h3 id="modalTitle"> <?= $editForm ? 'Edit Form' : 'Create New Form' ?> </h3> <p> Configure your website form details </p> </div> </div> <button type="button" class="modal-close" onclick="closeFormModal()" > <i class="fa-solid fa-xmark"></i> </button> </div> <form method="post" action="forms.php" id="formEditor" > <input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>" > <input type="hidden" name="action" value="save" > <input type="hidden" name="id" id="formId" value="<?= $editForm ? (int) $editForm['id'] : 0 ?>" > <div class="modal-body"> <div class="form-grid"> <!-- NAME --> <div class="field full"> <label for="name"> Form Name <span class="required">*</span> </label> <input type="text" name="name" id="name" class="input" placeholder="e.g. Contact Enquiry" maxlength="255" required value="<?= e($editForm['name'] ?? '') ?>" > <div class="help"> Internal/display name used to identify this form. </div> </div> <!-- SLUG --> <div class="field"> <label for="slug"> Form Slug <span class="required">*</span> </label> <input type="text" name="slug" id="slug" class="input" placeholder="contact-enquiry" maxlength="255" required value="<?= e($editForm['slug'] ?? '') ?>" > <div class="help"> Used for identifying the form programmatically. </div> </div> <!-- STATUS --> <div class="field"> <label> Form Status </label> <div class="toggle-row"> <div class="toggle-text"> <strong> Enable Form </strong> <span> Allow this form to be used </span> </div> <label class="switch"> <input type="checkbox" name="status" value="1" <?= ( !$editForm || (int) $editForm['status'] === 1 ) ? 'checked' : '' ?> > <span class="slider"></span> </label> </div> </div> <!-- SUCCESS MESSAGE --> <div class="field full"> <label for="success_message"> Success Message </label> <textarea name="success_message" id="success_message" class="input" placeholder="Thank you! Your enquiry has been submitted successfully." ><?= e($editForm['success_message'] ?? '') ?></textarea> <div class="help"> Message shown after successful form submission. </div> </div> <!-- EMAIL NOTIFICATION --> <div class="field full"> <label for="email_notification"> Notification Email </label> <input type="text" name="email_notification" id="email_notification" class="input" placeholder="info@example.com" maxlength="500" value="<?= e($editForm['email_notification'] ?? '') ?>" > <div class="help"> You can add multiple emails separated by comma, semicolon or space. </div> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-light" onclick="closeFormModal()" > Cancel </button> <button type="submit" class="btn btn-primary" > <i class="fa-solid fa-check"></i> <?= $editForm ? 'Update Form' : 'Create Form' ?> </button> </div> </form> </div> </div> <!-- ========================================================= DELETE MODAL ========================================================= --> <div class="modal" id="deleteModal" onclick="closeDeleteOnBackdrop(event)" > <div class="delete-card"> <div class="delete-icon"> <i class="fa-regular fa-trash-can"></i> </div> <h3> Delete Form? </h3> <p> Are you sure you want to delete <strong id="deleteFormName"></strong>? This action cannot be undone. </p> <form method="post" action="forms.php" id="deleteForm" > <input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>" > <input type="hidden" name="action" value="delete" > <input type="hidden" name="id" id="deleteFormId" value="" > <div class="delete-actions"> <button type="button" class="btn btn-light" onclick="closeDeleteModal()" > Cancel </button> <button type="submit" class="btn btn-danger" > <i class="fa-regular fa-trash-can"></i> Delete Form </button> </div> </form> </div> </div> <script> /* ===================================================== FORM MODAL ====================================================== */ function openFormModal() { const modal = document.getElementById('formModal'); if (!modal) { return; } modal.classList.add('show'); document.body.style.overflow = 'hidden'; setTimeout(function () { const nameInput = document.getElementById('name'); if (nameInput) { nameInput.focus(); } }, 100); } function closeFormModal() { const modal = document.getElementById('formModal'); if (!modal) { return; } modal.classList.remove('show'); document.body.style.overflow = ''; /* * If this was an edit URL, remove query * without refreshing the page. */ if ( new URLSearchParams( window.location.search ).has('edit') ) { const cleanUrl = window.location.pathname; window.history.replaceState( {}, document.title, cleanUrl ); /* * Reset editor to create mode. */ const form = document.getElementById('formEditor'); if (form) { form.reset(); document.getElementById('formId').value = '0'; document.getElementById('modalTitle').textContent = 'Create New Form'; } } } function closeOnBackdrop(event) { if ( event.target === document.getElementById('formModal') ) { closeFormModal(); } } /* ===================================================== AUTO SLUG ====================================================== */ const nameInput = document.getElementById('name'); const slugInput = document.getElementById('slug'); let slugManuallyChanged = false; if (slugInput) { slugInput.addEventListener( 'input', function () { slugManuallyChanged = true; this.value = this.value .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); } ); } if (nameInput) { nameInput.addEventListener( 'input', function () { if (slugManuallyChanged) { return; } const slug = this.value .toLowerCase() .trim() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); if (slugInput) { slugInput.value = slug; } } ); } /* ===================================================== DELETE MODAL ====================================================== */ function openDeleteModal(id, name) { const modal = document.getElementById('deleteModal'); const idInput = document.getElementById('deleteFormId'); const nameElement = document.getElementById('deleteFormName'); if (!modal || !idInput || !nameElement) { return; } idInput.value = id; nameElement.textContent = name; modal.classList.add('show'); document.body.style.overflow = 'hidden'; } function closeDeleteModal() { const modal = document.getElementById('deleteModal'); if (!modal) { return; } modal.classList.remove('show'); document.body.style.overflow = ''; } function closeDeleteOnBackdrop(event) { if ( event.target === document.getElementById('deleteModal') ) { closeDeleteModal(); } } /* ===================================================== STATUS CONFIRM ====================================================== */ function confirmStatus() { return confirm( 'Are you sure you want to change this form status?' ); } /* ===================================================== ESC KEY ====================================================== */ document.addEventListener( 'keydown', function (event) { if (event.key !== 'Escape') { return; } closeFormModal(); closeDeleteModal(); } ); /* ===================================================== BODY SCROLL FIX ====================================================== */ window.addEventListener( 'pageshow', function () { const formModal = document.getElementById('formModal'); const deleteModal = document.getElementById('deleteModal'); if ( (!formModal || !formModal.classList.contains('show')) && (!deleteModal || !deleteModal.classList.contains('show')) ) { document.body.style.overflow = ''; } } ); </script> </body> </html>
Save
Cancel