๐ File Manager
Editing:
/home/wvl7mof7402e/public_html/chem/admin/section_items.php
<?php /** * PURE CHEMICAL ADMIN * section_items.php * * Manages: page_section_items * Existing table only โ NO new table is created. */ 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('sectionItemE')) { function sectionItemE($value): string { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } } if (!function_exists('sectionItemCsrfField')) { function sectionItemCsrfField(): string { if (function_exists('csrf_field')) { return (string) csrf_field(); } if (empty($_SESSION['section_items_csrf'])) { $_SESSION['section_items_csrf'] = bin2hex(random_bytes(32)); } return '<input type="hidden" name="csrf_token" value="' . sectionItemE($_SESSION['section_items_csrf']) . '">'; } } if (!function_exists('sectionItemVerifyCsrf')) { function sectionItemVerifyCsrf(): bool { if (function_exists('csrf_verify')) { return (bool) csrf_verify(); } $token = $_POST['csrf_token'] ?? ''; return !empty($_SESSION['section_items_csrf']) && is_string($token) && hash_equals($_SESSION['section_items_csrf'], $token); } } function sectionItemPostString(string $key, ?string $default = null): ?string { if (!isset($_POST[$key])) { return $default; } return trim((string)$_POST[$key]); } function sectionItemPostInt(string $key, int $default = 0): int { return isset($_POST[$key]) && is_numeric($_POST[$key]) ? (int)$_POST[$key] : $default; } /* --------------------------------------------------------- Flash --------------------------------------------------------- */ $flashSuccess = $_SESSION['section_items_success'] ?? ''; $flashError = $_SESSION['section_items_error'] ?? ''; unset( $_SESSION['section_items_success'], $_SESSION['section_items_error'] ); /* --------------------------------------------------------- Handle POST --------------------------------------------------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!sectionItemVerifyCsrf()) { $_SESSION['section_items_error'] = 'Security verification failed. Please try again.'; header('Location: section_items.php'); exit; } $action = $_POST['action'] ?? ''; /* ----------------------------------------------------- SAVE ----------------------------------------------------- */ if ($action === 'save') { $id = sectionItemPostInt('id', 0); $sectionId = sectionItemPostInt('section_id', 0); $itemType = sectionItemPostString('item_type', 'item'); $title = sectionItemPostString('title'); $subtitle = sectionItemPostString('subtitle'); $content = sectionItemPostString('content'); $label = sectionItemPostString('label'); $value = sectionItemPostString('value'); $icon = sectionItemPostString('icon'); $errors = []; $imageIdRaw = sectionItemPostString('image_id'); $imageId = ($imageIdRaw !== null && $imageIdRaw !== '' && ctype_digit($imageIdRaw)) ? (int)$imageIdRaw : null; /* Optional direct image upload; register it in the existing media table. */ $upload = $_FILES['item_image'] ?? null; if (is_array($upload) && (int)($upload['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_NO_FILE) { try { if ((int)($upload['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) { throw new RuntimeException('Image upload failed. Please choose the image again.'); } if (empty($upload['tmp_name']) || !is_uploaded_file($upload['tmp_name'])) { throw new RuntimeException('Invalid image upload.'); } if ((int)($upload['size'] ?? 0) > 8 * 1024 * 1024) { throw new RuntimeException('Image size must be 8 MB or less.'); } $finfo = new finfo(FILEINFO_MIME_TYPE); $mime = $finfo->file($upload['tmp_name']); $allowed = ['image/jpeg'=>'jpg', 'image/png'=>'png', 'image/gif'=>'gif', 'image/webp'=>'webp']; if (!isset($allowed[$mime])) { throw new RuntimeException('Only JPG, PNG, GIF and WEBP images are allowed.'); } $dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'media'; if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) { throw new RuntimeException('Could not create uploads/media directory.'); } $ext = $allowed[$mime]; $fileName = 'section_item_' . date('Ymd_His') . '_' . bin2hex(random_bytes(5)) . '.' . $ext; $absolute = $dir . DIRECTORY_SEPARATOR . $fileName; if (!move_uploaded_file($upload['tmp_name'], $absolute)) { throw new RuntimeException('Could not save uploaded image. Check uploads folder permissions.'); } $relative = 'uploads/media/' . $fileName; $original = mb_substr(basename((string)($upload['name'] ?? $fileName)), 0, 255, 'UTF-8'); $dims = @getimagesize($absolute); $mediaStmt = $pdo->prepare("INSERT INTO media (original_name, file_name, file_path, mime_type, file_type, extension, file_size, width, height, uploaded_by, status) VALUES (?, ?, ?, ?, 'image', ?, ?, ?, ?, NULL, 1)"); $mediaStmt->execute([$original, $fileName, $relative, $mime, $ext, (int)filesize($absolute), is_array($dims) ? (int)$dims[0] : null, is_array($dims) ? (int)$dims[1] : null]); $imageId = (int)$pdo->lastInsertId(); } catch (Throwable $uploadError) { $errors[] = $uploadError->getMessage(); } } $linkText = sectionItemPostString('link_text'); $linkUrl = sectionItemPostString('link_url'); $linkTarget = sectionItemPostString('link_target', '_self'); $referenceType = sectionItemPostString('reference_type'); $referenceIdRaw = sectionItemPostString('reference_id'); $referenceId = ($referenceIdRaw !== null && $referenceIdRaw !== '' && ctype_digit($referenceIdRaw)) ? (int)$referenceIdRaw : null; $sortOrder = sectionItemPostInt('sort_order', 0); $status = isset($_POST['status']) ? 1 : 0; if ($sectionId <= 0) { $errors[] = 'Please select a section.'; } if ($itemType === '') { $errors[] = 'Item type is required.'; } if (mb_strlen($itemType) > 100) { $errors[] = 'Item type cannot exceed 100 characters.'; } if ($linkTarget === '') { $linkTarget = '_self'; } $allowedTargets = ['_self', '_blank', '_parent', '_top']; if (!in_array($linkTarget, $allowedTargets, true)) { $linkTarget = '_self'; } if ($sortOrder < 0) { $sortOrder = 0; } /* Verify section exists */ if (!$errors) { $sectionCheck = $pdo->prepare( "SELECT id FROM page_sections WHERE id = ? LIMIT 1" ); $sectionCheck->execute([$sectionId]); if (!$sectionCheck->fetchColumn()) { $errors[] = 'Selected section does not exist.'; } } if (!$errors && $imageId !== null) { try { $mediaCheck = $pdo->prepare("SELECT id FROM media WHERE id = ? LIMIT 1"); $mediaCheck->execute([$imageId]); if (!$mediaCheck->fetchColumn()) { $errors[] = 'Selected image was not found in the media library. Choose a valid image or leave it empty.'; } } catch (Throwable $mediaError) { $errors[] = 'Unable to verify the selected media. Check the media table structure.'; error_log('section_items.php media validation error: ' . $mediaError->getMessage()); } } if (!$errors) { try { if ($id > 0) { $check = $pdo->prepare( "SELECT id FROM page_section_items WHERE id = ? LIMIT 1" ); $check->execute([$id]); if (!$check->fetchColumn()) { throw new RuntimeException('Section item not found.'); } $stmt = $pdo->prepare( "UPDATE page_section_items SET section_id = ?, item_type = ?, title = ?, subtitle = ?, content = ?, label = ?, value = ?, icon = ?, image_id = ?, link_text = ?, link_url = ?, link_target = ?, reference_type = ?, reference_id = ?, sort_order = ?, status = ? WHERE id = ?" ); $stmt->execute([ $sectionId, $itemType, $title !== '' ? $title : null, $subtitle !== '' ? $subtitle : null, $content !== '' ? $content : null, $label !== '' ? $label : null, $value !== '' ? $value : null, $icon !== '' ? $icon : null, $imageId, $linkText !== '' ? $linkText : null, $linkUrl !== '' ? $linkUrl : null, $linkTarget, $referenceType !== '' ? $referenceType : null, $referenceId, $sortOrder, $status, $id ]); $_SESSION['section_items_success'] = 'Section item updated successfully.'; } else { $stmt = $pdo->prepare( "INSERT INTO page_section_items ( section_id, item_type, title, subtitle, content, label, value, icon, image_id, link_text, link_url, link_target, reference_type, reference_id, sort_order, status ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" ); $stmt->execute([ $sectionId, $itemType, $title !== '' ? $title : null, $subtitle !== '' ? $subtitle : null, $content !== '' ? $content : null, $label !== '' ? $label : null, $value !== '' ? $value : null, $icon !== '' ? $icon : null, $imageId, $linkText !== '' ? $linkText : null, $linkUrl !== '' ? $linkUrl : null, $linkTarget, $referenceType !== '' ? $referenceType : null, $referenceId, $sortOrder, $status ]); $_SESSION['section_items_success'] = 'Section item created successfully.'; } } catch (Throwable $e) { error_log( 'section_items.php save error: ' . $e->getMessage() ); $_SESSION['section_items_error'] = 'Unable to save section item. Check that the selected section and media exist, then review the PHP error log for the exact database error.'; } } else { $_SESSION['section_items_error'] = implode(' ', $errors); } header('Location: section_items.php'); exit; } /* ----------------------------------------------------- DELETE ----------------------------------------------------- */ if ($action === 'delete') { $id = sectionItemPostInt('id', 0); if ($id <= 0) { $_SESSION['section_items_error'] = 'Invalid section item ID.'; header('Location: section_items.php'); exit; } try { $stmt = $pdo->prepare( "DELETE FROM page_section_items WHERE id = ?" ); $stmt->execute([$id]); $_SESSION['section_items_success'] = 'Section item deleted successfully.'; } catch (Throwable $e) { error_log( 'section_items.php delete error: ' . $e->getMessage() ); $_SESSION['section_items_error'] = 'Unable to delete section item.'; } header('Location: section_items.php'); exit; } } /* --------------------------------------------------------- Filters --------------------------------------------------------- */ $search = trim((string)($_GET['search'] ?? '')); $filterSection = isset($_GET['section_id']) ? (int)$_GET['section_id'] : 0; $filterType = trim((string)($_GET['item_type'] ?? '')); $filterStatus = isset($_GET['status']) ? (string)$_GET['status'] : ''; $editId = isset($_GET['edit']) ? (int)$_GET['edit'] : 0; /* --------------------------------------------------------- Sections --------------------------------------------------------- */ $sections = []; try { $sectionStmt = $pdo->query( "SELECT ps.id, ps.title, ps.section_type, ps.page_id, p.title AS page_title, p.slug AS page_slug FROM page_sections ps LEFT JOIN pages p ON p.id = ps.page_id ORDER BY p.title ASC, ps.sort_order ASC, ps.id DESC" ); $sections = $sectionStmt->fetchAll(); } catch (Throwable $e) { error_log( 'section_items.php sections error: ' . $e->getMessage() ); $sections = []; } /* --------------------------------------------------------- Existing item types --------------------------------------------------------- */ $itemTypes = []; try { $typeStmt = $pdo->query( "SELECT DISTINCT item_type FROM page_section_items WHERE item_type IS NOT NULL AND item_type <> '' ORDER BY item_type ASC" ); $itemTypes = $typeStmt->fetchAll(PDO::FETCH_COLUMN); } catch (Throwable $e) { $itemTypes = []; } /* --------------------------------------------------------- Edit record --------------------------------------------------------- */ $editItem = null; if ($editId > 0) { try { $stmt = $pdo->prepare( "SELECT * FROM page_section_items WHERE id = ? LIMIT 1" ); $stmt->execute([$editId]); $editItem = $stmt->fetch() ?: null; } catch (Throwable $e) { error_log( 'section_items.php edit error: ' . $e->getMessage() ); } } /* --------------------------------------------------------- Listing --------------------------------------------------------- */ $where = []; $params = []; if ($search !== '') { $where[] = "( psi.title LIKE ? OR psi.subtitle LIKE ? OR psi.item_type LIKE ? OR psi.label LIKE ? OR psi.value LIKE ? OR psi.content LIKE ? )"; $searchLike = '%' . $search . '%'; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; } if ($filterSection > 0) { $where[] = "psi.section_id = ?"; $params[] = $filterSection; } if ($filterType !== '') { $where[] = "psi.item_type = ?"; $params[] = $filterType; } if ($filterStatus === '1' || $filterStatus === '0') { $where[] = "psi.status = ?"; $params[] = (int)$filterStatus; } $whereSql = $where ? 'WHERE ' . implode(' AND ', $where) : ''; $items = []; try { $sql = " SELECT psi.*, ps.title AS section_title, ps.section_type, ps.page_id, p.title AS page_title, p.slug AS page_slug FROM page_section_items psi LEFT JOIN page_sections ps ON ps.id = psi.section_id LEFT JOIN pages p ON p.id = ps.page_id {$whereSql} ORDER BY p.title ASC, ps.sort_order ASC, psi.sort_order ASC, psi.id DESC LIMIT 500 "; $stmt = $pdo->prepare($sql); $stmt->execute($params); $items = $stmt->fetchAll(); } catch (Throwable $e) { error_log( 'section_items.php listing error: ' . $e->getMessage() ); $items = []; if ($flashError === '') { $flashError = 'Unable to load section items.'; } } /* --------------------------------------------------------- Statistics --------------------------------------------------------- */ $totalItems = 0; $activeItems = 0; $inactiveItems = 0; try { $statsStmt = $pdo->query( "SELECT COUNT(*) AS total, SUM(status = 1) AS active, SUM(status = 0) AS inactive FROM page_section_items" ); $stats = $statsStmt->fetch() ?: []; $totalItems = (int)($stats['total'] ?? 0); $activeItems = (int)($stats['active'] ?? 0); $inactiveItems = (int)($stats['inactive'] ?? 0); } catch (Throwable $e) { // Keep safe zero values. } $sectionCount = count($sections); /* --------------------------------------------------------- Media library choices --------------------------------------------------------- */ $mediaItems = []; try { $mediaStmt = $pdo->query("SELECT id, original_name, file_name, file_type FROM media WHERE status = 1 ORDER BY id DESC LIMIT 500"); $mediaItems = $mediaStmt->fetchAll(PDO::FETCH_ASSOC); } catch (Throwable $e) { error_log('section_items.php media list error: ' . $e->getMessage()); } /* --------------------------------------------------------- Form defaults --------------------------------------------------------- */ $form = [ 'id' => '', 'section_id' => '', 'item_type' => 'item', 'title' => '', 'subtitle' => '', 'content' => '', 'label' => '', 'value' => '', 'icon' => '', 'image_id' => '', 'link_text' => '', 'link_url' => '', 'link_target' => '_self', 'reference_type' => '', 'reference_id' => '', 'sort_order' => 0, 'status' => 1 ]; if ($editItem) { foreach ($form as $key => $default) { if (array_key_exists($key, $editItem)) { $form[$key] = $editItem[$key]; } } } $isEditing = !empty($form['id']); /* --------------------------------------------------------- Sidebar --------------------------------------------------------- */ require_once __DIR__ . '/sidebar.php'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <title>Section Items | Pure Chemical Admin</title> <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: #0f766e; --primary-dark: #115e59; --primary-soft: #ecfdf5; --text: #172033; --muted: #64748b; --border: #e5e7eb; --surface: #ffffff; --background: #f6f8fb; --success: #059669; --danger: #dc2626; --warning: #d97706; --radius: 18px; --shadow: 0 12px 40px rgba(15, 23, 42, .07); --sidebar-width: 285px; } * { box-sizing: border-box; } html { scroll-behavior: smooth; } body { margin: 0; background: var(--background); color: var(--text); font-family: Inter, sans-serif; font-size: 14px; } button, input, textarea, select { font: inherit; } a { color: inherit; text-decoration: none; } .pc-main { margin-left: var(--sidebar-width); min-height: 100vh; } .pc-topbar { position: sticky; top: 0; z-index: 80; min-height: 76px; padding: 0 28px; background: rgba(255, 255, 255, .92); backdrop-filter: blur(18px); border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 20px; } .topbar-left { display: flex; align-items: center; gap: 15px; min-width: 0; } .mobile-menu-btn { display: none; width: 42px; height: 42px; border: 1px solid var(--border); background: #fff; border-radius: 12px; cursor: pointer; color: var(--text); } .page-heading { min-width: 0; } .page-heading h1 { margin: 0; font-size: 20px; font-weight: 800; letter-spacing: -.4px; } .page-heading p { margin: 4px 0 0; color: var(--muted); font-size: 12px; } .topbar-right { display: flex; align-items: center; gap: 10px; } .topbar-badge { display: inline-flex; align-items: center; gap: 7px; padding: 9px 13px; border-radius: 999px; background: var(--primary-soft); color: var(--primary-dark); font-size: 12px; font-weight: 700; } .page-container { max-width: 1600px; margin: 0 auto; padding: 28px; } /* ---------------------------------------------- Flash ---------------------------------------------- */ .alert { display: flex; align-items: flex-start; gap: 12px; padding: 15px 17px; margin-bottom: 22px; border-radius: 14px; border: 1px solid; background: #fff; } .alert i { margin-top: 2px; } .alert-success { color: #047857; background: #ecfdf5; border-color: #a7f3d0; } .alert-danger { color: #b91c1c; background: #fef2f2; border-color: #fecaca; } /* ---------------------------------------------- Stats ---------------------------------------------- */ .stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 22px; } .stat-card { position: relative; overflow: hidden; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); } .stat-card::after { content: ""; position: absolute; right: -30px; bottom: -40px; width: 110px; height: 110px; border-radius: 50%; background: var(--primary-soft); } .stat-icon { width: 42px; height: 42px; display: flex; align-items: center; justify-content: center; border-radius: 12px; background: var(--primary-soft); color: var(--primary); margin-bottom: 15px; position: relative; z-index: 2; } .stat-value { font-size: 25px; line-height: 1; font-weight: 800; position: relative; z-index: 2; } .stat-label { color: var(--muted); font-size: 12px; margin-top: 7px; position: relative; z-index: 2; } /* ---------------------------------------------- Layout ---------------------------------------------- */ .content-grid { display: grid; grid-template-columns: minmax(0, 1fr) 440px; gap: 22px; align-items: start; } .card { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; } .card-header { padding: 20px 22px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; gap: 15px; } .card-title { display: flex; align-items: center; gap: 11px; } .card-title-icon { width: 38px; height: 38px; display: flex; align-items: center; justify-content: center; border-radius: 11px; background: var(--primary-soft); color: var(--primary); } .card-title h2 { margin: 0; font-size: 15px; font-weight: 800; } .card-title p { margin: 3px 0 0; color: var(--muted); font-size: 11px; } .card-body { padding: 22px; } /* ---------------------------------------------- Filters ---------------------------------------------- */ .filters { padding: 18px 22px; border-bottom: 1px solid var(--border); background: #fafbfc; } .filter-grid { display: grid; grid-template-columns: 1.6fr 1fr 1fr 120px auto; gap: 10px; } .field { margin-bottom: 15px; } .field:last-child { margin-bottom: 0; } .field label { display: block; margin-bottom: 7px; color: #334155; font-size: 12px; font-weight: 700; } .input, .select, .textarea { width: 100%; border: 1px solid #dbe1e8; border-radius: 11px; background: #fff; color: var(--text); outline: none; transition: border-color .18s ease, box-shadow .18s ease; } .input, .select { height: 43px; padding: 0 13px; } .textarea { min-height: 110px; resize: vertical; padding: 12px 13px; line-height: 1.6; } .textarea.large { min-height: 175px; } .input:focus, .select:focus, .textarea:focus { border-color: rgba(15, 118, 110, .55); box-shadow: 0 0 0 4px rgba(15, 118, 110, .08); } /* ---------------------------------------------- Buttons ---------------------------------------------- */ .btn { min-height: 42px; padding: 0 15px; border: 1px solid transparent; border-radius: 11px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-size: 12px; font-weight: 700; cursor: pointer; transition: .18s ease; } .btn:hover { transform: translateY(-1px); } .btn-primary { color: #fff; background: var(--primary); box-shadow: 0 7px 18px rgba(15, 118, 110, .18); } .btn-primary:hover { background: var(--primary-dark); } .btn-light { color: #334155; background: #fff; border-color: var(--border); } .btn-danger { color: #b91c1c; background: #fff; border-color: #fecaca; } .btn-danger:hover { background: #fef2f2; } .btn-small { min-height: 34px; padding: 0 10px; border-radius: 9px; font-size: 11px; } /* ---------------------------------------------- Table ---------------------------------------------- */ .table-wrap { overflow-x: auto; } table { width: 100%; border-collapse: collapse; min-width: 950px; } th { padding: 12px 16px; text-align: left; background: #f8fafc; border-bottom: 1px solid var(--border); color: #64748b; font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .55px; } td { padding: 15px 16px; border-bottom: 1px solid #eef1f4; vertical-align: middle; font-size: 12px; } tbody tr { transition: background .15s ease; } tbody tr:hover { background: #fbfefd; } .item-main { display: flex; align-items: flex-start; gap: 11px; } .item-icon { flex: 0 0 auto; width: 37px; height: 37px; display: flex; align-items: center; justify-content: center; border-radius: 10px; background: var(--primary-soft); color: var(--primary); } .item-title { font-weight: 750; color: #172033; } .item-subtitle { margin-top: 4px; color: #64748b; font-size: 11px; max-width: 290px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .section-name { font-weight: 700; } .page-name { color: var(--muted); font-size: 10px; margin-top: 4px; } .type-badge { display: inline-flex; align-items: center; padding: 5px 8px; border-radius: 7px; background: #f1f5f9; color: #475569; font-size: 10px; font-weight: 750; } .status { display: inline-flex; align-items: center; gap: 6px; padding: 6px 9px; border-radius: 999px; font-size: 10px; font-weight: 750; } .status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; } .status-active { color: #047857; background: #ecfdf5; } .status-active::before { background: #10b981; } .status-inactive { color: #b45309; background: #fffbeb; } .status-inactive::before { background: #f59e0b; } .action-group { display: flex; align-items: center; justify-content: flex-end; gap: 6px; } .icon-btn { width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; border-radius: 9px; background: #fff; border: 1px solid var(--border); cursor: pointer; color: #64748b; transition: .18s ease; } .icon-btn:hover { color: var(--primary); border-color: rgba(15, 118, 110, .25); background: var(--primary-soft); } .icon-btn.delete:hover { color: var(--danger); border-color: #fecaca; background: #fef2f2; } /* ---------------------------------------------- Form ---------------------------------------------- */ .form-card { position: sticky; top: 98px; } .form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; } .full { grid-column: 1 / -1; } .field-help { color: #94a3b8; font-size: 10px; margin-top: 5px; line-height: 1.45; } .toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 13px 14px; border: 1px solid var(--border); border-radius: 11px; background: #fafbfc; } .toggle-info strong { display: block; font-size: 12px; } .toggle-info span { display: block; margin-top: 3px; color: var(--muted); font-size: 10px; } .switch { position: relative; width: 42px; height: 23px; } .switch input { opacity: 0; width: 0; height: 0; } .slider { position: absolute; inset: 0; cursor: pointer; background: #cbd5e1; border-radius: 999px; transition: .2s; } .slider::before { content: ""; position: absolute; width: 17px; height: 17px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 4px rgba(0,0,0,.2); } .switch input:checked + .slider { background: var(--primary); } .switch input:checked + .slider::before { transform: translateX(19px); } .form-actions { display: flex; align-items: center; justify-content: flex-end; gap: 9px; margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--border); } .edit-banner { margin: -6px 0 20px; display: flex; align-items: center; gap: 9px; padding: 11px 13px; color: #92400e; background: #fffbeb; border: 1px solid #fde68a; border-radius: 10px; font-size: 11px; font-weight: 650; } .empty { padding: 55px 25px; text-align: center; color: var(--muted); } .empty-icon { width: 54px; height: 54px; margin: 0 auto 14px; display: flex; align-items: center; justify-content: center; border-radius: 16px; background: #f1f5f9; color: #94a3b8; font-size: 21px; } .empty h3 { margin: 0 0 6px; color: #334155; font-size: 14px; } .empty p { margin: 0; font-size: 11px; } .sort-number { display: inline-flex; min-width: 28px; height: 28px; align-items: center; justify-content: center; border-radius: 8px; background: #f8fafc; border: 1px solid var(--border); font-size: 11px; font-weight: 750; color: #64748b; } .url-text { max-width: 220px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: #64748b; font-size: 10px; } /* ---------------------------------------------- Responsive ---------------------------------------------- */ @media (max-width: 1250px) { .content-grid { grid-template-columns: 1fr; } .form-card { position: static; } .filter-grid { grid-template-columns: 1fr 1fr; } .filter-actions { grid-column: 1 / -1; } } @media (max-width: 991px) { .pc-main { margin-left: 0; } .mobile-menu-btn { display: inline-flex; align-items: center; justify-content: center; } .page-container { padding: 20px; } .stats-grid { grid-template-columns: repeat(2, 1fr); } } @media (max-width: 650px) { .pc-topbar { padding: 0 15px; } .topbar-badge { display: none; } .page-container { padding: 15px; } .stats-grid { grid-template-columns: 1fr 1fr; gap: 10px; } .stat-card { padding: 15px; } .stat-value { font-size: 21px; } .filter-grid, .form-grid { grid-template-columns: 1fr; } .full, .filter-actions { grid-column: auto; } .card-header, .card-body, .filters { padding: 16px; } } </style> </head> <body> <div class="pc-main"> <!-- TOPBAR --> <header class="pc-topbar"> <div class="topbar-left"> <button type="button" class="mobile-menu-btn" id="mobileMenuBtn" aria-label="Open sidebar" > <i class="fa-solid fa-bars"></i> </button> <div class="page-heading"> <h1>Section Items</h1> <p> Manage dynamic items inside your page sections </p> </div> </div> <div class="topbar-right"> <div class="topbar-badge"> <i class="fa-solid fa-layer-group"></i> Page Builder </div> </div> </header> <main class="page-container"> <!-- FLASH --> <?php if ($flashSuccess): ?> <div class="alert alert-success"> <i class="fa-solid fa-circle-check"></i> <div> <?= sectionItemE($flashSuccess) ?> </div> </div> <?php endif; ?> <?php if ($flashError): ?> <div class="alert alert-danger"> <i class="fa-solid fa-circle-exclamation"></i> <div> <?= sectionItemE($flashError) ?> </div> </div> <?php endif; ?> <!-- STATS --> <section class="stats-grid"> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-layer-group"></i> </div> <div class="stat-value"> <?= number_format($totalItems) ?> </div> <div class="stat-label"> Total Section Items </div> </div> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-circle-check"></i> </div> <div class="stat-value"> <?= number_format($activeItems) ?> </div> <div class="stat-label"> Active Items </div> </div> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-circle-pause"></i> </div> <div class="stat-value"> <?= number_format($inactiveItems) ?> </div> <div class="stat-label"> Inactive Items </div> </div> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-table-columns"></i> </div> <div class="stat-value"> <?= number_format($sectionCount) ?> </div> <div class="stat-label"> Available Sections </div> </div> </section> <!-- MAIN --> <section class="content-grid"> <!-- LIST --> <div class="card"> <div class="card-header"> <div class="card-title"> <div class="card-title-icon"> <i class="fa-solid fa-list-check"></i> </div> <div> <h2>All Section Items</h2> <p> Manage content blocks and section elements </p> </div> </div> </div> <!-- FILTERS --> <form method="get" class="filters" > <div class="filter-grid"> <div> <input type="search" name="search" class="input" placeholder="Search title, content, label..." value="<?= sectionItemE($search) ?>" > </div> <div> <select name="section_id" class="select" > <option value=""> All Sections </option> <?php foreach ($sections as $section): ?> <option value="<?= (int)$section['id'] ?>" <?= $filterSection === (int)$section['id'] ? 'selected' : '' ?> > <?= sectionItemE( ($section['page_title'] ? $section['page_title'] . ' โ ' : '') . $section['title'] ) ?> </option> <?php endforeach; ?> </select> </div> <div> <select name="item_type" class="select" > <option value=""> All Types </option> <?php foreach ($itemTypes as $type): ?> <option value="<?= sectionItemE($type) ?>" <?= $filterType === $type ? 'selected' : '' ?> > <?= sectionItemE($type) ?> </option> <?php endforeach; ?> </select> </div> <div> <select name="status" class="select" > <option value=""> Status </option> <option value="1" <?= $filterStatus === '1' ? 'selected' : '' ?> > Active </option> <option value="0" <?= $filterStatus === '0' ? 'selected' : '' ?> > Inactive </option> </select> </div> <div class="filter-actions"> <button type="submit" class="btn btn-primary" > <i class="fa-solid fa-magnifying-glass"></i> Filter </button> </div> </div> </form> <!-- TABLE --> <div class="table-wrap"> <?php if (!$items): ?> <div class="empty"> <div class="empty-icon"> <i class="fa-regular fa-folder-open"></i> </div> <h3> No section items found </h3> <p> Create your first section item from the form. </p> </div> <?php else: ?> <table> <thead> <tr> <th>Item</th> <th>Section</th> <th>Type</th> <th>Link</th> <th>Order</th> <th>Status</th> <th style="text-align:right;"> Actions </th> </tr> </thead> <tbody> <?php foreach ($items as $item): ?> <tr> <td> <div class="item-main"> <div class="item-icon"> <?php $itemIcon = trim((string)($item['icon'] ?? '')); if ($itemIcon !== '') { echo '<i class="' . sectionItemE($itemIcon) . '"></i>'; } else { echo '<i class="fa-solid fa-cube"></i>'; } ?> </div> <div> <div class="item-title"> <?= sectionItemE( $item['title'] ?: 'Untitled item' ) ?> </div> <?php if (!empty($item['subtitle'])): ?> <div class="item-subtitle"> <?= sectionItemE($item['subtitle']) ?> </div> <?php elseif (!empty($item['label'])): ?> <div class="item-subtitle"> <?= sectionItemE($item['label']) ?> </div> <?php endif; ?> </div> </div> </td> <td> <div class="section-name"> <?= sectionItemE( $item['section_title'] ?: 'Section #' . $item['section_id'] ) ?> </div> <?php if (!empty($item['page_title'])): ?> <div class="page-name"> <i class="fa-regular fa-file-lines"></i> <?= sectionItemE($item['page_title']) ?> </div> <?php endif; ?> </td> <td> <span class="type-badge"> <?= sectionItemE( $item['item_type'] ?: 'item' ) ?> </span> </td> <td> <?php if (!empty($item['link_url'])): ?> <div class="url-text"> <?= sectionItemE($item['link_url']) ?> </div> <?php else: ?> <span style="color:#94a3b8;"> โ </span> <?php endif; ?> </td> <td> <span class="sort-number"> <?= (int)$item['sort_order'] ?> </span> </td> <td> <?php if ((int)$item['status'] === 1): ?> <span class="status status-active"> Active </span> <?php else: ?> <span class="status status-inactive"> Inactive </span> <?php endif; ?> </td> <td> <div class="action-group"> <a href="?edit=<?= (int)$item['id'] ?>" class="icon-btn" title="Edit" > <i class="fa-solid fa-pen"></i> </a> <form method="post" style="display:inline;" onsubmit="return confirm('Delete this section item? This action cannot be undone.');" > <?= sectionItemCsrfField() ?> <input type="hidden" name="action" value="delete" > <input type="hidden" name="id" value="<?= (int)$item['id'] ?>" > <button type="submit" class="icon-btn delete" title="Delete" > <i class="fa-solid fa-trash"></i> </button> </form> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?> </div> </div> <!-- FORM --> <div class="card form-card"> <div class="card-header"> <div class="card-title"> <div class="card-title-icon"> <i class="fa-solid <?= $isEditing ? 'fa-pen' : 'fa-plus' ?>"></i> </div> <div> <h2> <?= $isEditing ? 'Edit Section Item' : 'Add Section Item' ?> </h2> <p> <?= $isEditing ? 'Update existing section item' : 'Create a new dynamic item' ?> </p> </div> </div> </div> <div class="card-body"> <?php if ($isEditing): ?> <div class="edit-banner"> <i class="fa-solid fa-pen-to-square"></i> Editing item #<?= (int)$form['id'] ?> </div> <?php endif; ?> <form method="post" enctype="multipart/form-data" autocomplete="off" > <?= sectionItemCsrfField() ?> <input type="hidden" name="action" value="save" > <?php if ($isEditing): ?> <input type="hidden" name="id" value="<?= (int)$form['id'] ?>" > <?php endif; ?> <div class="form-grid"> <!-- SECTION --> <div class="field full"> <label> Section <span style="color:#dc2626;">*</span> </label> <select name="section_id" class="select" required > <option value=""> Select section </option> <?php foreach ($sections as $section): ?> <option value="<?= (int)$section['id'] ?>" <?= (int)$form['section_id'] === (int)$section['id'] ? 'selected' : '' ?> > <?= sectionItemE( ($section['page_title'] ? $section['page_title'] . ' โ ' : '') . ($section['title'] ?: 'Section #' . $section['id']) . ($section['section_type'] ? ' [' . $section['section_type'] . ']' : '') ) ?> </option> <?php endforeach; ?> </select> <div class="field-help"> Select the existing page section where this item belongs. </div> </div> <!-- ITEM TYPE --> <div class="field"> <label> Item Type <span style="color:#dc2626;">*</span> </label> <input type="text" name="item_type" class="input" maxlength="100" required list="sectionItemTypes" placeholder="item, feature, card..." value="<?= sectionItemE($form['item_type']) ?>" > <datalist id="sectionItemTypes"> <?php foreach ($itemTypes as $type): ?> <option value="<?= sectionItemE($type) ?>"></option> <?php endforeach; ?> </datalist> </div> <!-- SORT --> <div class="field"> <label> Sort Order </label> <input type="number" name="sort_order" class="input" min="0" value="<?= (int)$form['sort_order'] ?>" > </div> <!-- TITLE --> <div class="field full"> <label> Title </label> <input type="text" name="title" class="input" maxlength="500" placeholder="Enter item title" value="<?= sectionItemE($form['title']) ?>" > </div> <!-- SUBTITLE --> <div class="field full"> <label> Subtitle </label> <input type="text" name="subtitle" class="input" maxlength="500" placeholder="Optional subtitle" value="<?= sectionItemE($form['subtitle']) ?>" > </div> <!-- CONTENT --> <div class="field full"> <label> Content </label> <textarea name="content" class="textarea large" placeholder="Enter item content..." ><?= sectionItemE($form['content']) ?></textarea> </div> <!-- LABEL --> <div class="field"> <label> Label </label> <input type="text" name="label" class="input" maxlength="255" placeholder="Optional label" value="<?= sectionItemE($form['label']) ?>" > </div> <!-- VALUE --> <div class="field"> <label> Value </label> <input type="text" name="value" class="input" maxlength="255" placeholder="Optional value" value="<?= sectionItemE($form['value']) ?>" > </div> <!-- ICON --> <div class="field"> <label> Icon Class </label> <input type="text" name="icon" class="input" maxlength="255" placeholder="fa-solid fa-star" value="<?= sectionItemE($form['icon']) ?>" > <div class="field-help"> Example: fa-solid fa-star </div> </div> <!-- IMAGE ID --> <div class="field"> <label> Image / Media ID </label> <select name="image_id" class="select"> <option value="">No image</option> <?php foreach ($mediaItems as $media): ?> <?php $mediaLabel = (string)($media['original_name'] ?: $media['file_name']); ?> <option value="<?= (int)$media['id'] ?>" <?= (string)$form['image_id'] === (string)$media['id'] ? 'selected' : '' ?>> <?= sectionItemE($mediaLabel) ?> (#<?= (int)$media['id'] ?> ยท <?= sectionItemE($media['file_type']) ?>) </option> <?php endforeach; ?> </select> <div class="field-help">Choose an existing image from the media library, or upload a new one below.</div> <label style="margin-top:12px;display:block;">Upload New Image</label> <input type="file" name="item_image" class="input" accept="image/jpeg,image/png,image/gif,image/webp"> <div class="field-help">JPG, PNG, GIF or WEBP ยท Maximum 8 MB. Uploaded images are registered in the media table.</div> </div> <!-- LINK TEXT --> <div class="field"> <label> Link Text </label> <input type="text" name="link_text" class="input" maxlength="255" placeholder="Read More" value="<?= sectionItemE($form['link_text']) ?>" > </div> <!-- LINK TARGET --> <div class="field"> <label> Link Target </label> <select name="link_target" class="select" > <option value="_self" <?= $form['link_target'] === '_self' ? 'selected' : '' ?> > Same Window (_self) </option> <option value="_blank" <?= $form['link_target'] === '_blank' ? 'selected' : '' ?> > New Window (_blank) </option> <option value="_parent" <?= $form['link_target'] === '_parent' ? 'selected' : '' ?> > Parent (_parent) </option> <option value="_top" <?= $form['link_target'] === '_top' ? 'selected' : '' ?> > Top (_top) </option> </select> </div> <!-- LINK URL --> <div class="field full"> <label> Link URL </label> <input type="text" name="link_url" class="input" maxlength="1000" placeholder="/contact-us or https://example.com" value="<?= sectionItemE($form['link_url']) ?>" > </div> <!-- REFERENCE TYPE --> <div class="field"> <label> Reference Type </label> <input type="text" name="reference_type" class="input" maxlength="100" placeholder="product, service, page..." value="<?= sectionItemE($form['reference_type']) ?>" > </div> <!-- REFERENCE ID --> <div class="field"> <label> Reference ID </label> <input type="number" name="reference_id" class="input" min="1" placeholder="Optional record ID" value="<?= sectionItemE($form['reference_id']) ?>" > </div> <!-- STATUS --> <div class="field full"> <div class="toggle-row"> <div class="toggle-info"> <strong> Publish Item </strong> <span> Active items can be displayed on the frontend. </span> </div> <label class="switch"> <input type="checkbox" name="status" value="1" <?= (int)$form['status'] === 1 ? 'checked' : '' ?> > <span class="slider"></span> </label> </div> </div> </div> <!-- ACTIONS --> <div class="form-actions"> <?php if ($isEditing): ?> <a href="section_items.php" class="btn btn-light" > <i class="fa-solid fa-xmark"></i> Cancel </a> <?php else: ?> <button type="reset" class="btn btn-light" > <i class="fa-solid fa-rotate-left"></i> Reset </button> <?php endif; ?> <button type="submit" class="btn btn-primary" > <i class="fa-solid <?= $isEditing ? 'fa-floppy-disk' : 'fa-plus' ?>"></i> <?= $isEditing ? 'Update Item' : 'Create Item' ?> </button> </div> </form> </div> </div> </section> </main> </div> <script> (function () { const mobileMenuBtn = document.getElementById('mobileMenuBtn'); const sidebar = document.getElementById('pcSidebar'); if (mobileMenuBtn && sidebar) { mobileMenuBtn.addEventListener('click', function () { sidebar.classList.toggle('pc-sidebar-open'); document.body.classList.toggle( 'pc-sidebar-mobile-open' ); }); } /* Close sidebar when clicking outside on mobile */ document.addEventListener('click', function (event) { if ( window.innerWidth <= 991 && sidebar && sidebar.classList.contains('pc-sidebar-open') && !sidebar.contains(event.target) && !mobileMenuBtn.contains(event.target) ) { sidebar.classList.remove('pc-sidebar-open'); document.body.classList.remove( 'pc-sidebar-mobile-open' ); } }); })(); </script> </body> </html>
Save
Cancel