📁 File Manager
Editing:
/home/wvl7mof7402e/www/chem/admin/section_item_media.php
<?php /** * PURE CHEMICAL ADMIN * Section Item Media Management * * Database table: * page_section_item_media * * Existing related tables: * page_section_items * page_sections * pages * media */ 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 --------------------------------------------------------- */ function sim_e($value): string { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } function sim_post_string(string $key, string $default = ''): string { return isset($_POST[$key]) ? trim((string)$_POST[$key]) : $default; } function sim_post_int(string $key, int $default = 0): int { return isset($_POST[$key]) ? (int)$_POST[$key] : $default; } function sim_get_string(string $key, string $default = ''): string { return isset($_GET[$key]) ? trim((string)$_GET[$key]) : $default; } function sim_get_int(string $key, int $default = 0): int { return isset($_GET[$key]) ? (int)$_GET[$key] : $default; } function sim_csrf_field(): string { if (function_exists('csrf_field')) { return csrf_field(); } if (function_exists('csrf_token')) { $token = csrf_token(); return '<input type="hidden" name="csrf_token" value="' . sim_e($token) . '">'; } if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } return '<input type="hidden" name="csrf_token" value="' . sim_e($_SESSION['csrf_token']) . '">'; } function sim_verify_csrf(): bool { if (function_exists('csrf_verify')) { return (bool) csrf_verify(); } $token = $_POST['csrf_token'] ?? ''; if ( empty($_SESSION['csrf_token']) || empty($token) ) { return false; } return hash_equals( (string)$_SESSION['csrf_token'], (string)$token ); } function sim_redirect(array $params = []): never { $url = 'section_item_media.php'; if (!empty($params)) { $url .= '?' . http_build_query($params); } header('Location: ' . $url); exit; } function sim_flash(string $type, string $message): never { $_SESSION['sim_flash'] = [ 'type' => $type, 'message' => $message ]; sim_redirect(); } function sim_get_flash(): ?array { if (empty($_SESSION['sim_flash'])) { return null; } $flash = $_SESSION['sim_flash']; unset($_SESSION['sim_flash']); return $flash; } /* --------------------------------------------------------- Messages --------------------------------------------------------- */ $flash = sim_get_flash(); $errorMessage = ''; /* --------------------------------------------------------- Allowed media roles --------------------------------------------------------- */ $mediaRoles = [ 'image' => 'Image', 'video' => 'Video', 'poster' => 'Poster', 'background' => 'Background', 'pdf' => 'PDF' ]; /* --------------------------------------------------------- POST ACTIONS --------------------------------------------------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!sim_verify_csrf()) { $errorMessage = 'Security verification failed. Please refresh the page and try again.'; } else { $action = sim_post_string('action'); /* --------------------------------------------- SAVE / UPDATE --------------------------------------------- */ if ($action === 'save') { $id = sim_post_int('id'); $itemId = sim_post_int('item_id'); $mediaId = sim_post_int('media_id'); $mediaRole = sim_post_string('media_role', 'image'); $externalUrl = sim_post_string('external_url'); $title = sim_post_string('title'); $caption = sim_post_string('caption'); $altText = sim_post_string('alt_text'); $sortOrder = sim_post_int('sort_order', 0); $status = sim_post_int('status', 1); /* Validation */ if ($itemId <= 0) { $errorMessage = 'Please select a section item.'; } elseif ($mediaId <= 0) { $errorMessage = 'Please select a media file.'; } elseif (!array_key_exists($mediaRole, $mediaRoles)) { $errorMessage = 'Invalid media role selected.'; } elseif ($sortOrder < 0) { $errorMessage = 'Sort order cannot be negative.'; } elseif (!in_array($status, [0, 1], true)) { $errorMessage = 'Invalid status selected.'; } /* Verify section item */ if ($errorMessage === '') { $stmt = $pdo->prepare(" SELECT psi.id, psi.section_id, psi.item_type, psi.title AS item_title, psi.subtitle, 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 INNER JOIN page_sections ps ON ps.id = psi.section_id LEFT JOIN pages p ON p.id = ps.page_id WHERE psi.id = ? LIMIT 1 "); $stmt->execute([$itemId]); $itemExists = $stmt->fetch(); if (!$itemExists) { $errorMessage = 'Selected section item does not exist.'; } } /* Verify media */ if ($errorMessage === '') { $stmt = $pdo->prepare(" SELECT id, file_name, file_path, mime_type, title, alt_text FROM media WHERE id = ? LIMIT 1 "); $stmt->execute([$mediaId]); $mediaExists = $stmt->fetch(); if (!$mediaExists) { $errorMessage = 'Selected media does not exist.'; } } /* Check duplicate */ if ($errorMessage === '') { if ($id > 0) { $stmt = $pdo->prepare(" SELECT id FROM page_section_item_media WHERE item_id = ? AND media_id = ? AND id <> ? LIMIT 1 "); $stmt->execute([ $itemId, $mediaId, $id ]); } else { $stmt = $pdo->prepare(" SELECT id FROM page_section_item_media WHERE item_id = ? AND media_id = ? LIMIT 1 "); $stmt->execute([ $itemId, $mediaId ]); } if ($stmt->fetch()) { $errorMessage = 'This media is already assigned to the selected section item.'; } } /* Save */ if ($errorMessage === '') { if ($id > 0) { $stmt = $pdo->prepare(" UPDATE page_section_item_media SET item_id = ?, media_id = ?, media_role = ?, external_url = ?, title = ?, caption = ?, alt_text = ?, sort_order = ?, status = ? WHERE id = ? LIMIT 1 "); $stmt->execute([ $itemId, $mediaId, $mediaRole, $externalUrl !== '' ? $externalUrl : null, $title !== '' ? $title : null, $caption !== '' ? $caption : null, $altText !== '' ? $altText : null, $sortOrder, $status, $id ]); sim_flash( 'success', 'Section item media updated successfully.' ); } else { $stmt = $pdo->prepare(" INSERT INTO page_section_item_media ( item_id, media_id, media_role, external_url, title, caption, alt_text, sort_order, status ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? ) "); $stmt->execute([ $itemId, $mediaId, $mediaRole, $externalUrl !== '' ? $externalUrl : null, $title !== '' ? $title : null, $caption !== '' ? $caption : null, $altText !== '' ? $altText : null, $sortOrder, $status ]); sim_flash( 'success', 'Section item media added successfully.' ); } } } /* --------------------------------------------- DELETE --------------------------------------------- */ elseif ($action === 'delete') { $id = sim_post_int('id'); if ($id <= 0) { $errorMessage = 'Invalid media record selected.'; } else { $stmt = $pdo->prepare(" DELETE FROM page_section_item_media WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); sim_flash( 'success', 'Section item media deleted successfully.' ); } } /* --------------------------------------------- TOGGLE STATUS --------------------------------------------- */ elseif ($action === 'toggle_status') { $id = sim_post_int('id'); if ($id <= 0) { $errorMessage = 'Invalid media record selected.'; } else { $stmt = $pdo->prepare(" UPDATE page_section_item_media SET status = CASE WHEN status = 1 THEN 0 ELSE 1 END WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); sim_flash( 'success', 'Status updated successfully.' ); } } } } /* --------------------------------------------------------- Filters --------------------------------------------------------- */ $search = sim_get_string('search'); $filterItemId = sim_get_int('item_id'); $filterRole = sim_get_string('media_role'); $filterStatus = sim_get_string('status'); /* --------------------------------------------------------- Load section items --------------------------------------------------------- */ $items = []; try { $stmt = $pdo->query(" SELECT psi.id, psi.section_id, psi.item_type, psi.title AS item_title, psi.subtitle, psi.label, psi.value, psi.sort_order AS item_sort_order, psi.status AS item_status, 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 INNER JOIN page_sections ps ON ps.id = psi.section_id LEFT JOIN pages p ON p.id = ps.page_id ORDER BY p.title ASC, ps.sort_order ASC, psi.sort_order ASC, psi.id DESC "); $items = $stmt->fetchAll(); } catch (Throwable $e) { $items = []; } /* --------------------------------------------------------- Load media --------------------------------------------------------- */ $mediaLibrary = []; try { $stmt = $pdo->query(" SELECT id, file_name, file_path, mime_type, title, alt_text FROM media ORDER BY id DESC "); $mediaLibrary = $stmt->fetchAll(); } catch (Throwable $e) { $mediaLibrary = []; } /* --------------------------------------------------------- Edit record --------------------------------------------------------- */ $editId = sim_get_int('edit'); $editRecord = null; if ($editId > 0) { $stmt = $pdo->prepare(" SELECT id, item_id, media_id, media_role, external_url, title, caption, alt_text, sort_order, status, created_at FROM page_section_item_media WHERE id = ? LIMIT 1 "); $stmt->execute([$editId]); $editRecord = $stmt->fetch(); if (!$editRecord) { $editId = 0; } } /* --------------------------------------------------------- Statistics --------------------------------------------------------- */ $stats = [ 'total' => 0, 'active' => 0, 'inactive' => 0, 'items' => count($items) ]; try { $stmt = $pdo->query(" SELECT COUNT(*) AS total, COALESCE(SUM(status = 1), 0) AS active, COALESCE(SUM(status = 0), 0) AS inactive FROM page_section_item_media "); $row = $stmt->fetch(); if ($row) { $stats['total'] = (int)$row['total']; $stats['active'] = (int)$row['active']; $stats['inactive'] = (int)$row['inactive']; } } catch (Throwable $e) { // Safe fallback } /* --------------------------------------------------------- Listing --------------------------------------------------------- */ $listSql = " SELECT psim.id, psim.item_id, psim.media_id, psim.media_role, psim.external_url, psim.title, psim.caption, psim.alt_text, psim.sort_order, psim.status, psim.created_at, psi.item_type, psi.title AS item_title, psi.subtitle AS item_subtitle, ps.title AS section_title, ps.section_type, p.title AS page_title, p.slug AS page_slug, m.file_name, m.file_path, m.mime_type, m.title AS media_title, m.alt_text AS media_alt_text FROM page_section_item_media psim INNER JOIN page_section_items psi ON psi.id = psim.item_id INNER JOIN page_sections ps ON ps.id = psi.section_id LEFT JOIN pages p ON p.id = ps.page_id LEFT JOIN media m ON m.id = psim.media_id WHERE 1 = 1 "; $listParams = []; /* Search */ if ($search !== '') { $listSql .= " AND ( psim.title LIKE ? OR psim.caption LIKE ? OR psim.alt_text LIKE ? OR psi.title LIKE ? OR psi.subtitle LIKE ? OR ps.title LIKE ? OR p.title LIKE ? OR p.slug LIKE ? OR m.file_name LIKE ? OR m.title LIKE ? ) "; $searchLike = '%' . $search . '%'; for ($i = 0; $i < 10; $i++) { $listParams[] = $searchLike; } } /* Item filter */ if ($filterItemId > 0) { $listSql .= " AND psim.item_id = ? "; $listParams[] = $filterItemId; } /* Role filter */ if ( $filterRole !== '' && array_key_exists($filterRole, $mediaRoles) ) { $listSql .= " AND psim.media_role = ? "; $listParams[] = $filterRole; } /* Status filter */ if ($filterStatus !== '' && in_array($filterStatus, ['0', '1'], true)) { $listSql .= " AND psim.status = ? "; $listParams[] = (int)$filterStatus; } $listSql .= " ORDER BY p.title ASC, ps.sort_order ASC, psi.sort_order ASC, psim.sort_order ASC, psim.id DESC "; $listStmt = $pdo->prepare($listSql); $listStmt->execute($listParams); $records = $listStmt->fetchAll(); /* --------------------------------------------------------- Current form values --------------------------------------------------------- */ $form = [ 'id' => '', 'item_id' => '', 'media_id' => '', 'media_role' => 'image', 'external_url' => '', 'title' => '', 'caption' => '', 'alt_text' => '', 'sort_order' => 0, 'status' => 1 ]; if ($editRecord) { $form = [ 'id' => $editRecord['id'], 'item_id' => $editRecord['item_id'], 'media_id' => $editRecord['media_id'], 'media_role' => $editRecord['media_role'], 'external_url' => $editRecord['external_url'], 'title' => $editRecord['title'], 'caption' => $editRecord['caption'], 'alt_text' => $editRecord['alt_text'], 'sort_order' => $editRecord['sort_order'], 'status' => $editRecord['status'] ]; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <title> Section Item Media | 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 { --sim-primary: #2563eb; --sim-primary-dark: #1d4ed8; --sim-bg: #f5f7fb; --sim-card: #ffffff; --sim-text: #172033; --sim-muted: #718096; --sim-border: #e6eaf0; --sim-success: #16a34a; --sim-danger: #dc2626; --sim-warning: #d97706; --sim-shadow: 0 12px 40px rgba(15, 23, 42, .07); --sim-radius: 18px; } * { box-sizing: border-box; } html { scroll-behavior: smooth; } body { margin: 0; background: var(--sim-bg); color: var(--sim-text); font-family: "Inter", sans-serif; font-size: 14px; } button, input, select, textarea { font-family: inherit; } a { color: inherit; text-decoration: none; } .sim-main { margin-left: 285px; min-height: 100vh; } .sim-topbar { position: sticky; top: 0; z-index: 100; min-height: 76px; padding: 0 28px; background: rgba(255, 255, 255, .92); backdrop-filter: blur(18px); border-bottom: 1px solid var(--sim-border); display: flex; align-items: center; justify-content: space-between; gap: 20px; } .sim-top-left { display: flex; align-items: center; gap: 15px; min-width: 0; } .sim-menu-btn { width: 42px; height: 42px; border: 1px solid var(--sim-border); background: #fff; border-radius: 12px; display: none; align-items: center; justify-content: center; cursor: pointer; color: var(--sim-text); } .sim-heading h1 { margin: 0; font-size: 21px; font-weight: 800; letter-spacing: -.4px; } .sim-heading p { margin: 4px 0 0; color: var(--sim-muted); font-size: 12px; } .sim-top-actions { display: flex; align-items: center; gap: 10px; } .sim-top-badge { display: inline-flex; align-items: center; gap: 7px; padding: 9px 13px; border-radius: 999px; background: #eff6ff; color: #1d4ed8; font-size: 12px; font-weight: 700; } .sim-content { padding: 28px; } .sim-alert { margin-bottom: 20px; padding: 14px 16px; border-radius: 14px; display: flex; align-items: flex-start; gap: 11px; font-weight: 600; } .sim-alert.success { background: #ecfdf3; color: #15803d; border: 1px solid #bbf7d0; } .sim-alert.error { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; } .sim-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 17px; margin-bottom: 24px; } .sim-stat { background: var(--sim-card); border: 1px solid var(--sim-border); border-radius: var(--sim-radius); padding: 19px; box-shadow: var(--sim-shadow); display: flex; align-items: center; justify-content: space-between; gap: 15px; } .sim-stat-info span { display: block; color: var(--sim-muted); font-size: 12px; font-weight: 600; margin-bottom: 7px; } .sim-stat-info strong { display: block; font-size: 25px; font-weight: 800; letter-spacing: -.7px; } .sim-stat-icon { width: 46px; height: 46px; border-radius: 14px; background: #eff6ff; color: var(--sim-primary); display: flex; align-items: center; justify-content: center; font-size: 18px; } .sim-layout { display: grid; grid-template-columns: minmax(0, 1fr) 390px; gap: 22px; align-items: start; } .sim-card { background: var(--sim-card); border: 1px solid var(--sim-border); border-radius: var(--sim-radius); box-shadow: var(--sim-shadow); } .sim-card-header { padding: 19px 20px; border-bottom: 1px solid var(--sim-border); display: flex; align-items: center; justify-content: space-between; gap: 15px; } .sim-card-title { min-width: 0; } .sim-card-title h2 { margin: 0; font-size: 16px; font-weight: 800; } .sim-card-title p { margin: 4px 0 0; color: var(--sim-muted); font-size: 12px; } .sim-card-body { padding: 20px; } .sim-filters { padding: 16px; background: #f8fafc; border-bottom: 1px solid var(--sim-border); } .sim-filter-grid { display: grid; grid-template-columns: minmax(180px, 1.8fr) minmax(150px, 1fr) minmax(130px, .8fr) minmax(120px, .7fr) auto; gap: 10px; align-items: end; } .sim-field { margin-bottom: 15px; } .sim-field:last-child { margin-bottom: 0; } .sim-field label { display: block; margin-bottom: 7px; color: #344054; font-size: 12px; font-weight: 700; } .sim-input, .sim-select, .sim-textarea { width: 100%; border: 1px solid #dfe4ec; background: #fff; border-radius: 11px; padding: 11px 12px; outline: none; color: var(--sim-text); transition: .2s ease; } .sim-input:focus, .sim-select:focus, .sim-textarea:focus { border-color: #93c5fd; box-shadow: 0 0 0 4px rgba(37, 99, 235, .08); } .sim-textarea { min-height: 95px; resize: vertical; } .sim-btn { border: 0; border-radius: 11px; min-height: 42px; padding: 0 15px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-size: 13px; font-weight: 700; cursor: pointer; transition: .2s ease; white-space: nowrap; } .sim-btn:hover { transform: translateY(-1px); } .sim-btn-primary { color: #fff; background: var(--sim-primary); box-shadow: 0 7px 18px rgba(37, 99, 235, .18); } .sim-btn-primary:hover { background: var(--sim-primary-dark); } .sim-btn-light { background: #fff; color: #344054; border: 1px solid var(--sim-border); } .sim-btn-danger { background: #fef2f2; color: var(--sim-danger); border: 1px solid #fee2e2; } .sim-btn-success { background: #ecfdf3; color: var(--sim-success); border: 1px solid #bbf7d0; } .sim-filter-actions { display: flex; gap: 8px; } .sim-table-wrap { width: 100%; overflow-x: auto; } .sim-table { width: 100%; border-collapse: collapse; min-width: 900px; } .sim-table th { background: #fbfcfe; color: #667085; font-size: 11px; text-transform: uppercase; letter-spacing: .4px; font-weight: 800; text-align: left; padding: 13px 15px; border-bottom: 1px solid var(--sim-border); white-space: nowrap; } .sim-table td { padding: 14px 15px; border-bottom: 1px solid #edf0f5; vertical-align: middle; } .sim-table tbody tr:hover { background: #fbfdff; } .sim-record-title { max-width: 240px; font-weight: 750; color: #1d2939; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .sim-subtext { color: var(--sim-muted); font-size: 11px; margin-top: 4px; } .sim-item-box { max-width: 260px; } .sim-media-box { display: flex; align-items: center; gap: 11px; min-width: 230px; } .sim-media-preview { width: 48px; height: 48px; flex: 0 0 48px; border-radius: 11px; border: 1px solid var(--sim-border); background: #f8fafc; overflow: hidden; display: flex; align-items: center; justify-content: center; color: #64748b; } .sim-media-preview img { width: 100%; height: 100%; object-fit: cover; } .sim-media-name { min-width: 0; } .sim-media-name strong { display: block; max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; } .sim-role { display: inline-flex; align-items: center; gap: 5px; padding: 6px 9px; border-radius: 999px; background: #f1f5f9; color: #475569; font-size: 11px; font-weight: 800; text-transform: uppercase; } .sim-status { display: inline-flex; align-items: center; gap: 6px; padding: 6px 9px; border-radius: 999px; font-size: 11px; font-weight: 800; } .sim-status.active { background: #ecfdf3; color: #15803d; } .sim-status.inactive { background: #fef2f2; color: #b91c1c; } .sim-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; } .sim-actions { display: flex; align-items: center; gap: 6px; } .sim-icon-btn { width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--sim-border); background: #fff; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: #475467; transition: .2s ease; } .sim-icon-btn:hover { background: #f8fafc; transform: translateY(-1px); } .sim-icon-btn.edit:hover { color: var(--sim-primary); border-color: #bfdbfe; background: #eff6ff; } .sim-icon-btn.delete:hover { color: var(--sim-danger); border-color: #fecaca; background: #fef2f2; } .sim-icon-btn.status:hover { color: var(--sim-success); border-color: #bbf7d0; background: #ecfdf3; } .sim-form-card { position: sticky; top: 96px; } .sim-form-title { display: flex; align-items: center; gap: 10px; } .sim-form-icon { width: 38px; height: 38px; border-radius: 11px; background: #eff6ff; color: var(--sim-primary); display: flex; align-items: center; justify-content: center; } .sim-form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; } .sim-full { grid-column: 1 / -1; } .sim-form-footer { margin-top: 5px; padding-top: 17px; border-top: 1px solid var(--sim-border); display: flex; gap: 9px; } .sim-form-footer .sim-btn { flex: 1; } .sim-help { margin-top: 6px; color: #98a2b3; font-size: 11px; line-height: 1.5; } .sim-preview-card { margin-top: 16px; border: 1px dashed #cbd5e1; border-radius: 13px; padding: 11px; background: #f8fafc; display: none; } .sim-preview-card.show { display: block; } .sim-preview-card img { width: 100%; max-height: 150px; object-fit: contain; border-radius: 10px; background: #fff; } .sim-empty { text-align: center; padding: 55px 20px; color: var(--sim-muted); } .sim-empty-icon { width: 58px; height: 58px; border-radius: 18px; background: #f1f5f9; color: #94a3b8; margin: 0 auto 14px; display: flex; align-items: center; justify-content: center; font-size: 22px; } .sim-empty strong { display: block; color: #344054; font-size: 14px; margin-bottom: 5px; } .sim-empty span { font-size: 12px; } @media (max-width: 1250px) { .sim-layout { grid-template-columns: 1fr; } .sim-form-card { position: static; } .sim-filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .sim-filter-actions { grid-column: 1 / -1; } } @media (max-width: 991px) { .sim-main { margin-left: 0; } .sim-menu-btn { display: inline-flex; } .sim-content { padding: 20px; } .sim-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } } @media (max-width: 650px) { .sim-topbar { padding: 0 15px; } .sim-heading h1 { font-size: 17px; } .sim-heading p { display: none; } .sim-top-badge { display: none; } .sim-content { padding: 15px; } .sim-stats { grid-template-columns: 1fr 1fr; gap: 10px; } .sim-stat { padding: 14px; } .sim-stat-info strong { font-size: 21px; } .sim-filter-grid { grid-template-columns: 1fr; } .sim-filter-actions { grid-column: auto; } .sim-form-grid { grid-template-columns: 1fr; } .sim-full { grid-column: auto; } } </style> </head> <body> <?php require_once __DIR__ . '/sidebar.php'; ?> <div class="sim-main"> <!-- TOPBAR --> <header class="sim-topbar"> <div class="sim-top-left"> <button type="button" class="sim-menu-btn" id="simMenuBtn" aria-label="Open menu" > <i class="fa-solid fa-bars"></i> </button> <div class="sim-heading"> <h1> Section Item Media </h1> <p> Manage media attached to individual section items </p> </div> </div> <div class="sim-top-actions"> <div class="sim-top-badge"> <i class="fa-solid fa-photo-film"></i> Media Management </div> </div> </header> <!-- CONTENT --> <main class="sim-content"> <?php if ($flash): ?> <div class="sim-alert <?= $flash['type'] === 'success' ? 'success' : 'error' ?>"> <i class="fa-solid <?= $flash['type'] === 'success' ? 'fa-circle-check' : 'fa-circle-exclamation' ?>"></i> <span> <?= sim_e($flash['message']) ?> </span> </div> <?php endif; ?> <?php if ($errorMessage !== ''): ?> <div class="sim-alert error"> <i class="fa-solid fa-circle-exclamation"></i> <span> <?= sim_e($errorMessage) ?> </span> </div> <?php endif; ?> <!-- STATS --> <section class="sim-stats"> <div class="sim-stat"> <div class="sim-stat-info"> <span> Total Media Links </span> <strong> <?= number_format($stats['total']) ?> </strong> </div> <div class="sim-stat-icon"> <i class="fa-solid fa-photo-film"></i> </div> </div> <div class="sim-stat"> <div class="sim-stat-info"> <span> Active </span> <strong> <?= number_format($stats['active']) ?> </strong> </div> <div class="sim-stat-icon"> <i class="fa-solid fa-circle-check"></i> </div> </div> <div class="sim-stat"> <div class="sim-stat-info"> <span> Inactive </span> <strong> <?= number_format($stats['inactive']) ?> </strong> </div> <div class="sim-stat-icon"> <i class="fa-solid fa-circle-pause"></i> </div> </div> <div class="sim-stat"> <div class="sim-stat-info"> <span> Section Items </span> <strong> <?= number_format($stats['items']) ?> </strong> </div> <div class="sim-stat-icon"> <i class="fa-solid fa-layer-group"></i> </div> </div> </section> <!-- MAIN LAYOUT --> <div class="sim-layout"> <!-- LIST --> <section class="sim-card"> <div class="sim-card-header"> <div class="sim-card-title"> <h2> Media Assignments </h2> <p> <?= number_format(count($records)) ?> record<?= count($records) === 1 ? '' : 's' ?> found </p> </div> </div> <!-- FILTERS --> <form method="get" class="sim-filters" > <div class="sim-filter-grid"> <div> <label class="sim-field"> <span>Search</span> <input type="search" name="search" class="sim-input" value="<?= sim_e($search) ?>" placeholder="Search item, page, media..." > </label> </div> <div> <label class="sim-field"> <span>Section Item</span> <select name="item_id" class="sim-select" > <option value=""> All Section Items </option> <?php foreach ($items as $item): ?> <option value="<?= (int)$item['id'] ?>" <?= $filterItemId === (int)$item['id'] ? 'selected' : '' ?> > <?= sim_e( ($item['page_title'] ?: 'Page') . ' → ' . ($item['section_title'] ?: 'Section') . ' → ' . ($item['item_title'] ?: ('Item #' . $item['id'])) ) ?> </option> <?php endforeach; ?> </select> </label> </div> <div> <label class="sim-field"> <span>Role</span> <select name="media_role" class="sim-select" > <option value=""> All Roles </option> <?php foreach ($mediaRoles as $roleKey => $roleLabel): ?> <option value="<?= sim_e($roleKey) ?>" <?= $filterRole === $roleKey ? 'selected' : '' ?> > <?= sim_e($roleLabel) ?> </option> <?php endforeach; ?> </select> </label> </div> <div> <label class="sim-field"> <span>Status</span> <select name="status" class="sim-select" > <option value=""> All Status </option> <option value="1" <?= $filterStatus === '1' ? 'selected' : '' ?> > Active </option> <option value="0" <?= $filterStatus === '0' ? 'selected' : '' ?> > Inactive </option> </select> </label> </div> <div class="sim-filter-actions"> <button type="submit" class="sim-btn sim-btn-primary" > <i class="fa-solid fa-filter"></i> Filter </button> <a href="section_item_media.php" class="sim-btn sim-btn-light" > <i class="fa-solid fa-rotate-left"></i> Reset </a> </div> </div> </form> <!-- TABLE --> <div class="sim-table-wrap"> <?php if (!$records): ?> <div class="sim-empty"> <div class="sim-empty-icon"> <i class="fa-solid fa-photo-film"></i> </div> <strong> No section item media found </strong> <span> Add a media assignment using the form. </span> </div> <?php else: ?> <table class="sim-table"> <thead> <tr> <th> Item </th> <th> Media </th> <th> Role </th> <th> Order </th> <th> Status </th> <th> Actions </th> </tr> </thead> <tbody> <?php foreach ($records as $record): ?> <?php $previewUrl = ''; if (!empty($record['file_path'])) { $previewUrl = $record['file_path']; } $isImage = false; if (!empty($record['mime_type'])) { $isImage = str_starts_with( strtolower((string)$record['mime_type']), 'image/' ); } if (!$isImage && $previewUrl !== '') { $extension = strtolower( pathinfo( parse_url( $previewUrl, PHP_URL_PATH ) ?: '', PATHINFO_EXTENSION ) ); $isImage = in_array( $extension, [ 'jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg' ], true ); } ?> <tr> <!-- ITEM --> <td> <div class="sim-item-box"> <div class="sim-record-title"> <?= sim_e( $record['item_title'] ?: ('Item #' . $record['item_id']) ) ?> </div> <div class="sim-subtext"> <?= sim_e( $record['page_title'] ?: 'Page' ) ?> <span> → </span> <?= sim_e( $record['section_title'] ?: 'Section' ) ?> </div> <?php if (!empty($record['item_type'])): ?> <div class="sim-subtext"> <i class="fa-solid fa-tag"></i> <?= sim_e( $record['item_type'] ) ?> </div> <?php endif; ?> </div> </td> <!-- MEDIA --> <td> <div class="sim-media-box"> <div class="sim-media-preview"> <?php if ($isImage && $previewUrl !== ''): ?> <img src="<?= sim_e($previewUrl) ?>" alt="<?= sim_e( $record['alt_text'] ?: $record['media_title'] ?: $record['file_name'] ) ?>" loading="lazy" > <?php else: ?> <?php $mediaIcon = 'fa-file'; $mime = strtolower( (string)$record['mime_type'] ); if (str_contains($mime, 'video')) { $mediaIcon = 'fa-file-video'; } elseif (str_contains($mime, 'pdf')) { $mediaIcon = 'fa-file-pdf'; } elseif (str_contains($mime, 'audio')) { $mediaIcon = 'fa-file-audio'; } ?> <i class="fa-solid <?= $mediaIcon ?>"></i> <?php endif; ?> </div> <div class="sim-media-name"> <strong> <?= sim_e( $record['title'] ?: $record['media_title'] ?: $record['file_name'] ?: ('Media #' . $record['media_id']) ) ?> </strong> <div class="sim-subtext"> Media #<?= (int)$record['media_id'] ?> </div> <?php if (!empty($record['external_url'])): ?> <div class="sim-subtext"> <i class="fa-solid fa-link"></i> External URL </div> <?php endif; ?> </div> </div> </td> <!-- ROLE --> <td> <span class="sim-role"> <i class="fa-solid fa-layer-group"></i> <?= sim_e( $mediaRoles[$record['media_role']] ?? $record['media_role'] ) ?> </span> </td> <!-- ORDER --> <td> <strong> <?= (int)$record['sort_order'] ?> </strong> </td> <!-- STATUS --> <td> <?php if ((int)$record['status'] === 1): ?> <span class="sim-status active"> <span class="sim-dot"></span> Active </span> <?php else: ?> <span class="sim-status inactive"> <span class="sim-dot"></span> Inactive </span> <?php endif; ?> </td> <!-- ACTIONS --> <td> <div class="sim-actions"> <a href="section_item_media.php?edit=<?= (int)$record['id'] ?>" class="sim-icon-btn edit" title="Edit" > <i class="fa-solid fa-pen"></i> </a> <form method="post" style="display:inline" onsubmit="return confirm('Change status for this media assignment?');" > <?= sim_csrf_field() ?> <input type="hidden" name="action" value="toggle_status" > <input type="hidden" name="id" value="<?= (int)$record['id'] ?>" > <button type="submit" class="sim-icon-btn status" title="Toggle Status" > <i class="fa-solid fa-power-off"></i> </button> </form> <form method="post" style="display:inline" onsubmit="return confirm('Delete this media assignment permanently?');" > <?= sim_csrf_field() ?> <input type="hidden" name="action" value="delete" > <input type="hidden" name="id" value="<?= (int)$record['id'] ?>" > <button type="submit" class="sim-icon-btn delete" title="Delete" > <i class="fa-solid fa-trash"></i> </button> </form> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> <?php endif; ?> </div> </section> <!-- ADD / EDIT --> <aside class="sim-card sim-form-card"> <div class="sim-card-header"> <div class="sim-form-title"> <div class="sim-form-icon"> <i class="fa-solid <?= $editRecord ? 'fa-pen' : 'fa-plus' ?>"></i> </div> <div class="sim-card-title"> <h2> <?= $editRecord ? 'Edit Media' : 'Add Media' ?> </h2> <p> <?= $editRecord ? 'Update section item media' : 'Attach media to a section item' ?> </p> </div> </div> </div> <div class="sim-card-body"> <form method="post" autocomplete="off" > <?= sim_csrf_field() ?> <input type="hidden" name="action" value="save" > <input type="hidden" name="id" value="<?= sim_e($form['id']) ?>" > <div class="sim-form-grid"> <!-- SECTION ITEM --> <div class="sim-field sim-full"> <label for="item_id"> Section Item <span style="color:#dc2626">*</span> </label> <select id="item_id" name="item_id" class="sim-select" required > <option value=""> Select section item </option> <?php foreach ($items as $item): ?> <option value="<?= (int)$item['id'] ?>" <?= (int)$form['item_id'] === (int)$item['id'] ? 'selected' : '' ?> > <?= sim_e( ($item['page_title'] ?: 'Page') . ' → ' . ($item['section_title'] ?: 'Section') . ' → ' . ($item['item_title'] ?: ('Item #' . $item['id'])) ) ?> <?php if (!empty($item['item_type'])): ?> — <?= sim_e($item['item_type']) ?> <?php endif; ?> </option> <?php endforeach; ?> </select> <?php if (!$items): ?> <div class="sim-help"> No section items are available. Create a section item first. </div> <?php endif; ?> </div> <!-- MEDIA --> <div class="sim-field sim-full"> <label for="media_id"> Media <span style="color:#dc2626">*</span> </label> <select id="media_id" name="media_id" class="sim-select" required > <option value=""> Select media </option> <?php foreach ($mediaLibrary as $media): ?> <option value="<?= (int)$media['id'] ?>" data-url="<?= sim_e( $media['file_path'] ) ?>" data-mime="<?= sim_e( $media['mime_type'] ) ?>" <?= (int)$form['media_id'] === (int)$media['id'] ? 'selected' : '' ?> > #<?= (int)$media['id'] ?> — <?= sim_e( $media['title'] ?: $media['file_name'] ?: 'Untitled media' ) ?> <?php if (!empty($media['mime_type'])): ?> [<?= sim_e($media['mime_type']) ?>] <?php endif; ?> </option> <?php endforeach; ?> </select> <?php if (!$mediaLibrary): ?> <div class="sim-help"> No media is available. Upload media first from Media Library. </div> <?php endif; ?> <div class="sim-preview-card" id="simMediaPreview" > <img src="" alt="Media preview" id="simPreviewImage" > </div> </div> <!-- ROLE --> <div class="sim-field"> <label for="media_role"> Media Role </label> <select id="media_role" name="media_role" class="sim-select" > <?php foreach ($mediaRoles as $roleKey => $roleLabel): ?> <option value="<?= sim_e($roleKey) ?>" <?= $form['media_role'] === $roleKey ? 'selected' : '' ?> > <?= sim_e($roleLabel) ?> </option> <?php endforeach; ?> </select> </div> <!-- SORT ORDER --> <div class="sim-field"> <label for="sort_order"> Sort Order </label> <input type="number" min="0" step="1" id="sort_order" name="sort_order" class="sim-input" value="<?= sim_e($form['sort_order']) ?>" > </div> <!-- TITLE --> <div class="sim-field sim-full"> <label for="title"> Title </label> <input type="text" id="title" name="title" class="sim-input" maxlength="255" value="<?= sim_e($form['title']) ?>" placeholder="Optional media title" > </div> <!-- ALT --> <div class="sim-field sim-full"> <label for="alt_text"> Alt Text </label> <input type="text" id="alt_text" name="alt_text" class="sim-input" maxlength="255" value="<?= sim_e($form['alt_text']) ?>" placeholder="Accessibility / SEO alt text" > </div> <!-- EXTERNAL URL --> <div class="sim-field sim-full"> <label for="external_url"> External URL </label> <input type="url" id="external_url" name="external_url" class="sim-input" maxlength="1000" value="<?= sim_e($form['external_url']) ?>" placeholder="https://example.com/media..." > <div class="sim-help"> Optional. Use this when the media has an external source URL. </div> </div> <!-- CAPTION --> <div class="sim-field sim-full"> <label for="caption"> Caption </label> <textarea id="caption" name="caption" class="sim-textarea" placeholder="Optional media caption..." ><?= sim_e($form['caption']) ?></textarea> </div> <!-- STATUS --> <div class="sim-field sim-full"> <label for="status"> Status </label> <select id="status" name="status" class="sim-select" > <option value="1" <?= (int)$form['status'] === 1 ? 'selected' : '' ?> > Active </option> <option value="0" <?= (int)$form['status'] === 0 ? 'selected' : '' ?> > Inactive </option> </select> </div> </div> <!-- FOOTER --> <div class="sim-form-footer"> <?php if ($editRecord): ?> <a href="section_item_media.php" class="sim-btn sim-btn-light" > <i class="fa-solid fa-xmark"></i> Cancel </a> <?php endif; ?> <button type="submit" class="sim-btn sim-btn-primary" > <i class="fa-solid <?= $editRecord ? 'fa-floppy-disk' : 'fa-plus' ?>"></i> <?= $editRecord ? 'Update Media' : 'Add Media' ?> </button> </div> </form> </div> </aside> </div> </main> </div> <script> (function () { const menuBtn = document.getElementById('simMenuBtn'); if (menuBtn) { menuBtn.addEventListener('click', function () { const sidebar = document.getElementById('pcSidebar'); if (sidebar) { sidebar.classList.toggle('is-open'); } document.body.classList.toggle('pc-sidebar-open'); }); } /* * Media preview */ const mediaSelect = document.getElementById('media_id'); const previewBox = document.getElementById('simMediaPreview'); const previewImage = document.getElementById('simPreviewImage'); function updateMediaPreview() { if (!mediaSelect || !previewBox || !previewImage) { return; } const option = mediaSelect.options[ mediaSelect.selectedIndex ]; if (!option) { previewBox.classList.remove('show'); previewImage.removeAttribute('src'); return; } const url = option.getAttribute('data-url') || ''; const mime = (option.getAttribute('data-mime') || '').toLowerCase(); const isImage = mime.indexOf('image/') === 0 || /\.(jpg|jpeg|png|gif|webp|avif|svg)(\?.*)?$/i.test(url); if (url && isImage) { previewImage.src = url; previewBox.classList.add('show'); } else { previewBox.classList.remove('show'); previewImage.removeAttribute('src'); } } if (mediaSelect) { mediaSelect.addEventListener( 'change', updateMediaPreview ); updateMediaPreview(); } })(); </script> </body> </html>
Save
Cancel