📁 File Manager
Editing:
/home/wvl7mof7402e/public_html/chem/admin/page_media.php
<?php /* |-------------------------------------------------------------------------- | PURE CHEMICAL ADMIN - PAGE MEDIA |-------------------------------------------------------------------------- | File: admin/page_media.php | Table: page_media | No new table / no dummy data |-------------------------------------------------------------------------- */ 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 (function_exists('requireAdmin')) { requireAdmin(); } /* |-------------------------------------------------------------------------- | Helpers |-------------------------------------------------------------------------- */ function pm_e($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } function pm_redirect($url = 'page_media.php') { header('Location: ' . $url); exit; } function pm_flash($type, $message) { $_SESSION['pm_flash'] = [ 'type' => $type, 'message' => $message ]; } function pm_get_flash() { $flash = $_SESSION['pm_flash'] ?? null; unset($_SESSION['pm_flash']); return $flash; } /* |-------------------------------------------------------------------------- | CSRF |-------------------------------------------------------------------------- */ if (empty($_SESSION['pm_csrf'])) { $_SESSION['pm_csrf'] = bin2hex(random_bytes(32)); } $csrfToken = $_SESSION['pm_csrf']; function pm_verify_csrf() { $token = $_POST['csrf_token'] ?? ''; if ( empty($_SESSION['pm_csrf']) || empty($token) || !hash_equals($_SESSION['pm_csrf'], $token) ) { pm_flash('error', 'Security verification failed. Please try again.'); pm_redirect(); } } /* |-------------------------------------------------------------------------- | Detect useful media columns dynamically |-------------------------------------------------------------------------- | This keeps the page compatible with the existing media table while | allowing the UI to show whatever normal filename/path/title columns exist. |-------------------------------------------------------------------------- */ $mediaColumns = []; try { $mediaColumns = $pdo ->query("SHOW COLUMNS FROM media") ->fetchAll(PDO::FETCH_COLUMN); } catch (Throwable $e) { $mediaColumns = []; } function pm_first_column(array $columns, array $candidates) { foreach ($candidates as $candidate) { if (in_array($candidate, $columns, true)) { return $candidate; } } return null; } $mediaNameColumn = pm_first_column($mediaColumns, [ 'title', 'name', 'filename', 'file_name', 'original_name', 'original_filename' ]); $mediaPathColumn = pm_first_column($mediaColumns, [ 'file_path', 'path', 'url', 'file_url', 'media_path', 'src' ]); $mediaAltColumn = pm_first_column($mediaColumns, [ 'alt_text', 'alt', 'description' ]); /* |-------------------------------------------------------------------------- | Load pages |-------------------------------------------------------------------------- */ $pages = []; try { $pages = $pdo->query(" SELECT id, title, slug FROM pages ORDER BY title ASC, id ASC ")->fetchAll(); } catch (Throwable $e) { $pages = []; } /* |-------------------------------------------------------------------------- | Load media |-------------------------------------------------------------------------- */ $media = []; try { $media = $pdo->query(" SELECT * FROM media ORDER BY id DESC ")->fetchAll(); } catch (Throwable $e) { $media = []; } /* |-------------------------------------------------------------------------- | POST ACTIONS |-------------------------------------------------------------------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { pm_verify_csrf(); $action = $_POST['action'] ?? ''; /* |-------------------------------------------------------------------------- | SAVE |-------------------------------------------------------------------------- */ if ($action === 'save') { $id = (int)($_POST['id'] ?? 0); $pageId = (int)($_POST['page_id'] ?? 0); $mediaId = (int)($_POST['media_id'] ?? 0); $mediaRole = trim($_POST['media_role'] ?? 'gallery'); $title = trim($_POST['title'] ?? ''); $caption = trim($_POST['caption'] ?? ''); $altText = trim($_POST['alt_text'] ?? ''); $sortOrder = (int)($_POST['sort_order'] ?? 0); $status = isset($_POST['status']) ? (int)$_POST['status'] : 1; if ($pageId <= 0) { pm_flash('error', 'Please select a page.'); pm_redirect(); } if ($mediaId <= 0) { pm_flash('error', 'Please select media.'); pm_redirect(); } if ($mediaRole === '') { $mediaRole = 'gallery'; } if (mb_strlen($mediaRole) > 100) { $mediaRole = mb_substr($mediaRole, 0, 100); } $status = $status === 1 ? 1 : 0; try { /* |-------------------------------------------------------------------------- | Check referenced page |-------------------------------------------------------------------------- */ $stmt = $pdo->prepare(" SELECT id FROM pages WHERE id = ? LIMIT 1 "); $stmt->execute([$pageId]); if (!$stmt->fetchColumn()) { pm_flash('error', 'Selected page does not exist.'); pm_redirect(); } /* |-------------------------------------------------------------------------- | Check referenced media |-------------------------------------------------------------------------- */ $stmt = $pdo->prepare(" SELECT id FROM media WHERE id = ? LIMIT 1 "); $stmt->execute([$mediaId]); if (!$stmt->fetchColumn()) { pm_flash('error', 'Selected media does not exist.'); pm_redirect(); } /* |-------------------------------------------------------------------------- | Duplicate relationship check |-------------------------------------------------------------------------- */ if ($id > 0) { $stmt = $pdo->prepare(" SELECT id FROM page_media WHERE page_id = ? AND media_id = ? AND media_role = ? AND id <> ? LIMIT 1 "); $stmt->execute([ $pageId, $mediaId, $mediaRole, $id ]); } else { $stmt = $pdo->prepare(" SELECT id FROM page_media WHERE page_id = ? AND media_id = ? AND media_role = ? LIMIT 1 "); $stmt->execute([ $pageId, $mediaId, $mediaRole ]); } if ($stmt->fetchColumn()) { pm_flash( 'error', 'This media is already assigned to this page with the same media role.' ); pm_redirect(); } /* |-------------------------------------------------------------------------- | Update |-------------------------------------------------------------------------- */ if ($id > 0) { $stmt = $pdo->prepare(" UPDATE page_media SET page_id = ?, media_id = ?, media_role = ?, title = ?, caption = ?, alt_text = ?, sort_order = ?, status = ? WHERE id = ? LIMIT 1 "); $stmt->execute([ $pageId, $mediaId, $mediaRole, $title !== '' ? $title : null, $caption !== '' ? $caption : null, $altText !== '' ? $altText : null, $sortOrder, $status, $id ]); pm_flash('success', 'Page media updated successfully.'); } else { /* |-------------------------------------------------------------------------- | Insert |-------------------------------------------------------------------------- */ $stmt = $pdo->prepare(" INSERT INTO page_media ( page_id, media_id, media_role, title, caption, alt_text, sort_order, status ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([ $pageId, $mediaId, $mediaRole, $title !== '' ? $title : null, $caption !== '' ? $caption : null, $altText !== '' ? $altText : null, $sortOrder, $status ]); pm_flash('success', 'Page media added successfully.'); } } catch (Throwable $e) { error_log('Page Media Save Error: ' . $e->getMessage()); pm_flash( 'error', 'Unable to save page media. Please check the selected page and media.' ); } pm_redirect(); } /* |-------------------------------------------------------------------------- | DELETE |-------------------------------------------------------------------------- */ if ($action === 'delete') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { pm_flash('error', 'Invalid page media ID.'); pm_redirect(); } try { $stmt = $pdo->prepare(" DELETE FROM page_media WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); if ($stmt->rowCount() > 0) { pm_flash('success', 'Page media deleted successfully.'); } else { pm_flash('error', 'Page media record was not found.'); } } catch (Throwable $e) { error_log('Page Media Delete Error: ' . $e->getMessage()); pm_flash( 'error', 'Unable to delete this page media record.' ); } pm_redirect(); } /* |-------------------------------------------------------------------------- | TOGGLE STATUS |-------------------------------------------------------------------------- */ if ($action === 'toggle_status') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { pm_flash('error', 'Invalid page media ID.'); pm_redirect(); } try { $stmt = $pdo->prepare(" UPDATE page_media SET status = IF(status = 1, 0, 1) WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); pm_flash('success', 'Page media status updated.'); } catch (Throwable $e) { error_log('Page Media Status Error: ' . $e->getMessage()); pm_flash( 'error', 'Unable to update page media status.' ); } pm_redirect(); } } /* |-------------------------------------------------------------------------- | EDIT RECORD |-------------------------------------------------------------------------- */ $editId = (int)($_GET['edit'] ?? 0); $editRecord = null; if ($editId > 0) { try { $stmt = $pdo->prepare(" SELECT * FROM page_media WHERE id = ? LIMIT 1 "); $stmt->execute([$editId]); $editRecord = $stmt->fetch(); } catch (Throwable $e) { $editRecord = null; } } /* |-------------------------------------------------------------------------- | FILTERS |-------------------------------------------------------------------------- */ $search = trim($_GET['search'] ?? ''); $filterPage = (int)($_GET['page_id'] ?? 0); $filterRole = trim($_GET['media_role'] ?? ''); $filterStatus = $_GET['status'] ?? ''; $allowedRoles = [ 'gallery', 'banner', 'hero', 'thumbnail', 'featured', 'logo', 'background', 'document', 'attachment' ]; /* |-------------------------------------------------------------------------- | Pagination |-------------------------------------------------------------------------- */ $perPageOptions = [10, 20, 50, 100]; $perPage = (int)($_GET['per_page'] ?? 20); if (!in_array($perPage, $perPageOptions, true)) { $perPage = 20; } $currentPage = max(1, (int)($_GET['p'] ?? 1)); $where = []; $params = []; /* |-------------------------------------------------------------------------- | Search |-------------------------------------------------------------------------- */ if ($search !== '') { $where[] = "( pm.title LIKE ? OR pm.caption LIKE ? OR pm.alt_text LIKE ? OR pm.media_role LIKE ? OR p.title LIKE ? OR p.slug LIKE ? )"; $searchLike = '%' . $search . '%'; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; $params[] = $searchLike; } if ($filterPage > 0) { $where[] = "pm.page_id = ?"; $params[] = $filterPage; } if ($filterRole !== '') { $where[] = "pm.media_role = ?"; $params[] = $filterRole; } if ($filterStatus !== '' && in_array($filterStatus, ['0', '1'], true)) { $where[] = "pm.status = ?"; $params[] = (int)$filterStatus; } $whereSql = ''; if (!empty($where)) { $whereSql = 'WHERE ' . implode(' AND ', $where); } /* |-------------------------------------------------------------------------- | Count |-------------------------------------------------------------------------- */ $totalRows = 0; try { $countSql = " SELECT COUNT(*) FROM page_media pm INNER JOIN pages p ON p.id = pm.page_id {$whereSql} "; $stmt = $pdo->prepare($countSql); $stmt->execute($params); $totalRows = (int)$stmt->fetchColumn(); } catch (Throwable $e) { $totalRows = 0; } $totalPages = max(1, (int)ceil($totalRows / $perPage)); if ($currentPage > $totalPages) { $currentPage = $totalPages; } $offset = ($currentPage - 1) * $perPage; /* |-------------------------------------------------------------------------- | Listing |-------------------------------------------------------------------------- */ $records = []; try { $sql = " SELECT pm.*, p.title AS page_title, p.slug AS page_slug FROM page_media pm INNER JOIN pages p ON p.id = pm.page_id {$whereSql} ORDER BY pm.sort_order ASC, pm.id DESC LIMIT {$perPage} OFFSET {$offset} "; $stmt = $pdo->prepare($sql); $stmt->execute($params); $records = $stmt->fetchAll(); } catch (Throwable $e) { $records = []; error_log('Page Media Listing Error: ' . $e->getMessage()); } /* |-------------------------------------------------------------------------- | Stats |-------------------------------------------------------------------------- */ $stats = [ 'total' => 0, 'active' => 0, 'inactive' => 0, 'pages' => count($pages) ]; try { $stats['total'] = (int)$pdo ->query("SELECT COUNT(*) FROM page_media") ->fetchColumn(); $stats['active'] = (int)$pdo ->query("SELECT COUNT(*) FROM page_media WHERE status = 1") ->fetchColumn(); $stats['inactive'] = (int)$pdo ->query("SELECT COUNT(*) FROM page_media WHERE status = 0") ->fetchColumn(); } catch (Throwable $e) { // Keep defaults. } $flash = pm_get_flash(); /* |-------------------------------------------------------------------------- | Media display helpers |-------------------------------------------------------------------------- */ function pm_media_label($row, $nameColumn) { if ($nameColumn && isset($row[$nameColumn]) && $row[$nameColumn] !== '') { return $row[$nameColumn]; } if (isset($row['id'])) { return 'Media #' . $row['id']; } return 'Media'; } function pm_media_path($row, $pathColumn) { if ($pathColumn && isset($row[$pathColumn])) { return trim((string)$row[$pathColumn]); } return ''; } function pm_is_image_path($path) { if ($path === '') { return false; } $clean = parse_url($path, PHP_URL_PATH); if (!$clean) { $clean = $path; } $extension = strtolower(pathinfo($clean, PATHINFO_EXTENSION)); return in_array( $extension, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif', 'svg'], true ); } function pm_media_url($path) { if ($path === '') { return ''; } if ( preg_match( '#^(https?:)?//#i', $path ) ) { return $path; } if (strpos($path, '/') === 0) { return $path; } return '../' . ltrim($path, './'); } function pm_page_url_params(array $extra = []) { $base = $_GET; foreach ($extra as $key => $value) { if ($value === null || $value === '') { unset($base[$key]); } else { $base[$key] = $value; } } return http_build_query($base); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <title>Page Media | Pure Chemical Admin</title> <meta name="description" content="Manage page media for Pure Chemical website" > <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 { --pm-primary: #0f766e; --pm-primary-dark: #115e59; --pm-primary-soft: #ecfdf5; --pm-bg: #f6f8fb; --pm-card: #ffffff; --pm-text: #172033; --pm-muted: #6b7280; --pm-border: #e7ebf0; --pm-success: #15803d; --pm-danger: #dc2626; --pm-warning: #d97706; --pm-radius: 18px; --pm-shadow: 0 12px 35px rgba(15, 23, 42, 0.07); --pm-sidebar-width: 285px; } * { box-sizing: border-box; } html { scroll-behavior: smooth; } body { margin: 0; background: var(--pm-bg); color: var(--pm-text); font-family: "Inter", sans-serif; font-size: 14px; } button, input, select, textarea { font-family: inherit; } a { color: inherit; text-decoration: none; } /* |-------------------------------------------------------------------------- | Main Layout |-------------------------------------------------------------------------- */ .pm-page { margin-left: var(--pm-sidebar-width); min-height: 100vh; } .pm-topbar { position: sticky; top: 0; z-index: 900; height: 76px; display: flex; align-items: center; justify-content: space-between; padding: 0 30px; background: rgba(255, 255, 255, 0.94); backdrop-filter: blur(16px); border-bottom: 1px solid var(--pm-border); } .pm-topbar-left { display: flex; align-items: center; gap: 15px; } .pm-page-icon { width: 44px; height: 44px; border-radius: 13px; display: flex; align-items: center; justify-content: center; color: var(--pm-primary); background: var(--pm-primary-soft); font-size: 18px; } .pm-heading h1 { margin: 0; font-size: 19px; line-height: 1.2; font-weight: 800; letter-spacing: -0.02em; } .pm-heading span { display: block; margin-top: 4px; color: var(--pm-muted); font-size: 12px; } .pm-topbar-right { display: flex; align-items: center; gap: 10px; } .pm-live-badge { display: inline-flex; align-items: center; gap: 7px; padding: 8px 12px; border-radius: 999px; background: #f0fdf4; color: #15803d; font-size: 12px; font-weight: 700; } .pm-live-dot { width: 7px; height: 7px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 4px rgba(34, 197, 94, .12); } /* |-------------------------------------------------------------------------- | Content |-------------------------------------------------------------------------- */ .pm-content { padding: 30px; max-width: 1700px; margin: 0 auto; } /* |-------------------------------------------------------------------------- | Stats |-------------------------------------------------------------------------- */ .pm-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; margin-bottom: 22px; } .pm-stat { position: relative; overflow: hidden; background: var(--pm-card); border: 1px solid var(--pm-border); border-radius: var(--pm-radius); padding: 21px; box-shadow: var(--pm-shadow); } .pm-stat::after { content: ""; position: absolute; right: -35px; bottom: -45px; width: 110px; height: 110px; border-radius: 50%; background: var(--pm-primary-soft); } .pm-stat-top { display: flex; align-items: center; justify-content: space-between; } .pm-stat-icon { width: 42px; height: 42px; display: flex; align-items: center; justify-content: center; border-radius: 13px; background: var(--pm-primary-soft); color: var(--pm-primary); font-size: 17px; } .pm-stat-value { margin-top: 15px; font-size: 28px; font-weight: 800; letter-spacing: -0.04em; } .pm-stat-label { margin-top: 4px; color: var(--pm-muted); font-size: 12px; font-weight: 600; } /* |-------------------------------------------------------------------------- | Alerts |-------------------------------------------------------------------------- */ .pm-alert { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; padding: 14px 17px; border-radius: 14px; font-size: 13px; font-weight: 600; } .pm-alert.success { background: #ecfdf3; color: #166534; border: 1px solid #bbf7d0; } .pm-alert.error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; } .pm-alert button { margin-left: auto; border: 0; background: transparent; color: inherit; cursor: pointer; font-size: 16px; } /* |-------------------------------------------------------------------------- | Card |-------------------------------------------------------------------------- */ .pm-card { background: var(--pm-card); border: 1px solid var(--pm-border); border-radius: var(--pm-radius); box-shadow: var(--pm-shadow); overflow: hidden; margin-bottom: 22px; } .pm-card-header { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 22px 24px; border-bottom: 1px solid var(--pm-border); } .pm-card-title { display: flex; align-items: center; gap: 11px; } .pm-card-title-icon { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border-radius: 11px; background: #f1f5f9; color: var(--pm-primary); } .pm-card-title h2 { margin: 0; font-size: 15px; font-weight: 800; } .pm-card-title p { margin: 3px 0 0; color: var(--pm-muted); font-size: 11px; } /* |-------------------------------------------------------------------------- | Form |-------------------------------------------------------------------------- */ .pm-form { padding: 24px; } .pm-form-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 18px; } .pm-field { grid-column: span 4; } .pm-field.span-6 { grid-column: span 6; } .pm-field.span-8 { grid-column: span 8; } .pm-field.span-12 { grid-column: span 12; } .pm-field label { display: block; margin-bottom: 7px; color: #374151; font-size: 12px; font-weight: 700; } .pm-required { color: var(--pm-danger); } .pm-input, .pm-select, .pm-textarea { width: 100%; border: 1px solid #dce2e9; border-radius: 11px; background: #fff; color: var(--pm-text); outline: none; transition: border-color .2s ease, box-shadow .2s ease; } .pm-input, .pm-select { height: 45px; padding: 0 13px; } .pm-textarea { min-height: 95px; padding: 12px 13px; resize: vertical; } .pm-input:focus, .pm-select:focus, .pm-textarea:focus { border-color: var(--pm-primary); box-shadow: 0 0 0 4px rgba(15, 118, 110, .09); } .pm-help { margin-top: 6px; color: #8a94a6; font-size: 11px; } .pm-form-actions { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-top: 20px; padding-top: 20px; border-top: 1px solid var(--pm-border); } /* |-------------------------------------------------------------------------- | Buttons |-------------------------------------------------------------------------- */ .pm-btn { min-height: 42px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 0 15px; border-radius: 10px; border: 1px solid transparent; cursor: pointer; font-size: 12px; font-weight: 700; transition: transform .15s ease, box-shadow .15s ease, background .15s ease; } .pm-btn:hover { transform: translateY(-1px); } .pm-btn-primary { background: var(--pm-primary); color: #fff; box-shadow: 0 7px 18px rgba(15, 118, 110, .18); } .pm-btn-primary:hover { background: var(--pm-primary-dark); } .pm-btn-light { background: #fff; color: #374151; border-color: var(--pm-border); } .pm-btn-danger { background: #fff1f2; color: #be123c; border-color: #ffe4e6; } .pm-btn-warning { background: #fffbeb; color: #b45309; border-color: #fef3c7; } .pm-btn-sm { min-height: 34px; padding: 0 10px; font-size: 11px; } /* |-------------------------------------------------------------------------- | Search / Filters |-------------------------------------------------------------------------- */ .pm-filter { padding: 18px 24px; background: #fbfcfe; border-bottom: 1px solid var(--pm-border); } .pm-filter-grid { display: grid; grid-template-columns: minmax(220px, 2fr) minmax(170px, 1fr) minmax(140px, 1fr) minmax(130px, 1fr) auto; gap: 10px; } .pm-search { position: relative; } .pm-search i { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: #9ca3af; pointer-events: none; } .pm-search .pm-input { padding-left: 38px; } /* |-------------------------------------------------------------------------- | Table |-------------------------------------------------------------------------- */ .pm-table-wrap { overflow-x: auto; } .pm-table { width: 100%; border-collapse: collapse; min-width: 1100px; } .pm-table th { padding: 13px 17px; background: #fafbfc; color: #6b7280; border-bottom: 1px solid var(--pm-border); text-align: left; white-space: nowrap; font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; } .pm-table td { padding: 15px 17px; border-bottom: 1px solid #edf0f4; vertical-align: middle; color: #374151; font-size: 12px; } .pm-table tbody tr { transition: background .15s ease; } .pm-table tbody tr:hover { background: #fbfefd; } .pm-id { color: #9ca3af; font-weight: 700; } .pm-page-cell { display: flex; align-items: center; gap: 10px; } .pm-page-avatar { width: 38px; height: 38px; flex: 0 0 38px; display: flex; align-items: center; justify-content: center; border-radius: 10px; background: var(--pm-primary-soft); color: var(--pm-primary); font-size: 14px; } .pm-page-name { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #1f2937; font-weight: 700; } .pm-slug { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 3px; color: #9ca3af; font-size: 10px; } .pm-media-cell { display: flex; align-items: center; gap: 10px; max-width: 240px; } .pm-media-thumb { width: 44px; height: 44px; flex: 0 0 44px; display: flex; align-items: center; justify-content: center; overflow: hidden; border-radius: 10px; background: #f1f5f9; color: #64748b; border: 1px solid #e5e7eb; } .pm-media-thumb img { width: 100%; height: 100%; object-fit: cover; } .pm-media-name { max-width: 175px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; } .pm-role { display: inline-flex; padding: 6px 9px; border-radius: 7px; background: #f1f5f9; color: #475569; font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .03em; } .pm-status { display: inline-flex; align-items: center; gap: 6px; padding: 6px 9px; border-radius: 999px; font-size: 10px; font-weight: 800; } .pm-status.active { background: #ecfdf5; color: #15803d; } .pm-status.inactive { background: #fef2f2; color: #b91c1c; } .pm-status-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; } .pm-actions { display: flex; align-items: center; gap: 6px; } .pm-icon-btn { width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; border-radius: 9px; border: 1px solid var(--pm-border); background: #fff; color: #64748b; cursor: pointer; transition: .15s ease; } .pm-icon-btn:hover { border-color: #cbd5e1; color: var(--pm-primary); background: #f8fafc; } .pm-icon-btn.danger:hover { color: var(--pm-danger); background: #fff1f2; border-color: #fecdd3; } /* |-------------------------------------------------------------------------- | Empty State |-------------------------------------------------------------------------- */ .pm-empty { padding: 65px 20px; text-align: center; } .pm-empty-icon { width: 60px; height: 60px; display: flex; align-items: center; justify-content: center; margin: 0 auto 15px; border-radius: 18px; background: #f1f5f9; color: #94a3b8; font-size: 23px; } .pm-empty h3 { margin: 0; font-size: 15px; } .pm-empty p { margin: 7px 0 0; color: var(--pm-muted); font-size: 12px; } /* |-------------------------------------------------------------------------- | Pagination |-------------------------------------------------------------------------- */ .pm-pagination { display: flex; align-items: center; justify-content: space-between; gap: 15px; padding: 17px 24px; border-top: 1px solid var(--pm-border); } .pm-pagination-info { color: var(--pm-muted); font-size: 11px; font-weight: 600; } .pm-pagination-links { display: flex; align-items: center; gap: 5px; } .pm-page-link { min-width: 34px; height: 34px; padding: 0 8px; display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--pm-border); border-radius: 8px; background: #fff; color: #64748b; font-size: 11px; font-weight: 700; } .pm-page-link:hover, .pm-page-link.active { color: #fff; background: var(--pm-primary); border-color: var(--pm-primary); } .pm-page-link.disabled { opacity: .45; pointer-events: none; } /* |-------------------------------------------------------------------------- | Modal |-------------------------------------------------------------------------- */ .pm-modal { position: fixed; inset: 0; z-index: 2000; display: none; align-items: center; justify-content: center; padding: 20px; background: rgba(15, 23, 42, .58); backdrop-filter: blur(5px); } .pm-modal.show { display: flex; } .pm-modal-box { width: min(620px, 100%); max-height: 90vh; overflow-y: auto; border-radius: 20px; background: #fff; box-shadow: 0 30px 80px rgba(15, 23, 42, .22); } .pm-modal-header { display: flex; align-items: center; justify-content: space-between; padding: 19px 21px; border-bottom: 1px solid var(--pm-border); } .pm-modal-header h3 { margin: 0; font-size: 15px; } .pm-modal-close { width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; border: 0; border-radius: 9px; background: #f1f5f9; cursor: pointer; color: #64748b; } .pm-modal-body { padding: 21px; } /* |-------------------------------------------------------------------------- | Responsive |-------------------------------------------------------------------------- */ @media (max-width: 1200px) { .pm-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } .pm-filter-grid { grid-template-columns: 1fr 1fr; } } @media (max-width: 991px) { .pm-page { margin-left: 0; } .pm-topbar { height: 68px; padding: 0 18px; } .pm-content { padding: 20px 16px; } .pm-live-badge { display: none; } } @media (max-width: 700px) { .pm-stats { grid-template-columns: 1fr; } .pm-filter-grid { grid-template-columns: 1fr; } .pm-field, .pm-field.span-6, .pm-field.span-8, .pm-field.span-12 { grid-column: span 12; } .pm-card-header { align-items: flex-start; flex-direction: column; } .pm-form-actions { flex-direction: column-reverse; align-items: stretch; } .pm-form-actions .pm-btn { width: 100%; } .pm-pagination { flex-direction: column; align-items: stretch; } .pm-pagination-links { justify-content: center; } .pm-heading span { display: none; } } </style> </head> <body> <?php /* |-------------------------------------------------------------------------- | Existing Admin Sidebar |-------------------------------------------------------------------------- */ require_once __DIR__ . '/sidebar.php'; ?> <div class="pm-page"> <!-- ============================================================== TOPBAR ============================================================== --> <header class="pm-topbar"> <div class="pm-topbar-left"> <div class="pm-page-icon"> <i class="fa-solid fa-images"></i> </div> <div class="pm-heading"> <h1>Page Media</h1> <span> Manage media attached to website pages </span> </div> </div> <div class="pm-topbar-right"> <div class="pm-live-badge"> <span class="pm-live-dot"></span> Admin Panel </div> </div> </header> <!-- ============================================================== MAIN CONTENT ============================================================== --> <main class="pm-content"> <!-- ========================================================== FLASH MESSAGE =========================================================== --> <?php if ($flash): ?> <div class="pm-alert <?= pm_e($flash['type']) ?>"> <i class="fa-solid <?= $flash['type'] === 'success' ? 'fa-circle-check' : 'fa-circle-exclamation' ?>"> </i> <span> <?= pm_e($flash['message']) ?> </span> <button type="button" onclick="this.parentElement.remove()" aria-label="Close" > <i class="fa-solid fa-xmark"></i> </button> </div> <?php endif; ?> <!-- ========================================================== STATS =========================================================== --> <section class="pm-stats"> <div class="pm-stat"> <div class="pm-stat-top"> <div class="pm-stat-icon"> <i class="fa-solid fa-photo-film"></i> </div> </div> <div class="pm-stat-value"> <?= number_format($stats['total']) ?> </div> <div class="pm-stat-label"> Total Page Media </div> </div> <div class="pm-stat"> <div class="pm-stat-top"> <div class="pm-stat-icon"> <i class="fa-solid fa-circle-check"></i> </div> </div> <div class="pm-stat-value"> <?= number_format($stats['active']) ?> </div> <div class="pm-stat-label"> Active Media </div> </div> <div class="pm-stat"> <div class="pm-stat-top"> <div class="pm-stat-icon"> <i class="fa-solid fa-circle-pause"></i> </div> </div> <div class="pm-stat-value"> <?= number_format($stats['inactive']) ?> </div> <div class="pm-stat-label"> Inactive Media </div> </div> <div class="pm-stat"> <div class="pm-stat-top"> <div class="pm-stat-icon"> <i class="fa-solid fa-file-lines"></i> </div> </div> <div class="pm-stat-value"> <?= number_format($stats['pages']) ?> </div> <div class="pm-stat-label"> Available Pages </div> </div> </section> <!-- ========================================================== ADD / EDIT FORM =========================================================== --> <section class="pm-card"> <div class="pm-card-header"> <div class="pm-card-title"> <div class="pm-card-title-icon"> <i class="fa-solid <?= $editRecord ? 'fa-pen-to-square' : 'fa-plus' ?>"> </i> </div> <div> <h2> <?= $editRecord ? 'Edit Page Media' : 'Add Page Media' ?> </h2> <p> Assign an existing media item to an existing page </p> </div> </div> <?php if ($editRecord): ?> <a href="page_media.php" class="pm-btn pm-btn-light" > <i class="fa-solid fa-xmark"></i> Cancel Edit </a> <?php endif; ?> </div> <form method="post" action="page_media.php" class="pm-form" > <input type="hidden" name="csrf_token" value="<?= pm_e($csrfToken) ?>" > <input type="hidden" name="action" value="save" > <input type="hidden" name="id" value="<?= $editRecord ? (int)$editRecord['id'] : 0 ?>" > <div class="pm-form-grid"> <!-- PAGE --> <div class="pm-field span-6"> <label> Page <span class="pm-required">*</span> </label> <select name="page_id" class="pm-select" required > <option value=""> Select Page </option> <?php foreach ($pages as $page): ?> <option value="<?= (int)$page['id'] ?>" <?= ( $editRecord && (int)$editRecord['page_id'] === (int)$page['id'] ) ? 'selected' : '' ?> > <?= pm_e($page['title']) ?> <?php if (!empty($page['slug'])): ?> — <?= pm_e($page['slug']) ?> <?php endif; ?> </option> <?php endforeach; ?> </select> <div class="pm-help"> Select the page where this media will be attached. </div> </div> <!-- MEDIA --> <div class="pm-field span-6"> <label> Media <span class="pm-required">*</span> </label> <select name="media_id" class="pm-select" required > <option value=""> Select Media </option> <?php foreach ($media as $mediaRow): ?> <?php $mediaLabel = pm_media_label( $mediaRow, $mediaNameColumn ); ?> <option value="<?= (int)$mediaRow['id'] ?>" <?= ( $editRecord && (int)$editRecord['media_id'] === (int)$mediaRow['id'] ) ? 'selected' : '' ?> > #<?= (int)$mediaRow['id'] ?> — <?= pm_e($mediaLabel) ?> </option> <?php endforeach; ?> </select> <div class="pm-help"> Select an existing record from the media library. </div> </div> <!-- ROLE --> <div class="pm-field span-4"> <label> Media Role <span class="pm-required">*</span> </label> <input type="text" name="media_role" class="pm-input" maxlength="100" value="<?= pm_e( $editRecord['media_role'] ?? 'gallery' ) ?>" placeholder="gallery" required > <div class="pm-help"> Example: gallery, hero, banner, thumbnail. </div> </div> <!-- TITLE --> <div class="pm-field span-8"> <label> Title </label> <input type="text" name="title" class="pm-input" maxlength="255" value="<?= pm_e( $editRecord['title'] ?? '' ) ?>" placeholder="Media title" > </div> <!-- ALT --> <div class="pm-field span-6"> <label> Alt Text </label> <input type="text" name="alt_text" class="pm-input" maxlength="255" value="<?= pm_e( $editRecord['alt_text'] ?? '' ) ?>" placeholder="Describe the image for accessibility" > </div> <!-- SORT --> <div class="pm-field span-3"> <label> Sort Order </label> <input type="number" name="sort_order" class="pm-input" value="<?= $editRecord ? (int)$editRecord['sort_order'] : 0 ?>" min="0" > </div> <!-- STATUS --> <div class="pm-field span-3"> <label> Status </label> <select name="status" class="pm-select" > <option value="1" <?= ( !$editRecord || (int)$editRecord['status'] === 1 ) ? 'selected' : '' ?> > Active </option> <option value="0" <?= ( $editRecord && (int)$editRecord['status'] === 0 ) ? 'selected' : '' ?> > Inactive </option> </select> </div> <!-- CAPTION --> <div class="pm-field span-12"> <label> Caption </label> <textarea name="caption" class="pm-textarea" placeholder="Optional caption for this page media..." ><?= pm_e( $editRecord['caption'] ?? '' ) ?></textarea> </div> </div> <div class="pm-form-actions"> <?php if ($editRecord): ?> <a href="page_media.php" class="pm-btn pm-btn-light" > <i class="fa-solid fa-rotate-left"></i> Reset </a> <?php endif; ?> <button type="submit" class="pm-btn pm-btn-primary" > <i class="fa-solid <?= $editRecord ? 'fa-floppy-disk' : 'fa-plus' ?>"> </i> <?= $editRecord ? 'Update Page Media' : 'Add Page Media' ?> </button> </div> </form> </section> <!-- ========================================================== LISTING =========================================================== --> <section class="pm-card"> <div class="pm-card-header"> <div class="pm-card-title"> <div class="pm-card-title-icon"> <i class="fa-solid fa-list"></i> </div> <div> <h2> Page Media Records </h2> <p> <?= number_format($totalRows) ?> matching record<?= $totalRows == 1 ? '' : 's' ?> </p> </div> </div> </div> <!-- FILTERS --> <form method="get" action="page_media.php" class="pm-filter" > <div class="pm-filter-grid"> <div class="pm-search"> <i class="fa-solid fa-magnifying-glass"></i> <input type="text" name="search" class="pm-input" value="<?= pm_e($search) ?>" placeholder="Search page, title, caption, role..." > </div> <select name="page_id" class="pm-select" > <option value=""> All Pages </option> <?php foreach ($pages as $page): ?> <option value="<?= (int)$page['id'] ?>" <?= $filterPage === (int)$page['id'] ? 'selected' : '' ?> > <?= pm_e($page['title']) ?> </option> <?php endforeach; ?> </select> <select name="media_role" class="pm-select" > <option value=""> All Roles </option> <?php foreach ($allowedRoles as $role): ?> <option value="<?= pm_e($role) ?>" <?= $filterRole === $role ? 'selected' : '' ?> > <?= ucfirst(pm_e($role)) ?> </option> <?php endforeach; ?> </select> <select name="status" class="pm-select" > <option value=""> All Status </option> <option value="1" <?= $filterStatus === '1' ? 'selected' : '' ?> > Active </option> <option value="0" <?= $filterStatus === '0' ? 'selected' : '' ?> > Inactive </option> </select> <div style="display:flex;gap:7px;"> <button type="submit" class="pm-btn pm-btn-primary" > <i class="fa-solid fa-filter"></i> Filter </button> <a href="page_media.php" class="pm-btn pm-btn-light" title="Reset filters" > <i class="fa-solid fa-rotate-left"></i> </a> </div> </div> </form> <!-- TABLE --> <?php if (!empty($records)): ?> <div class="pm-table-wrap"> <table class="pm-table"> <thead> <tr> <th> ID </th> <th> Page </th> <th> Media </th> <th> Role </th> <th> Title / Alt </th> <th> Sort </th> <th> Status </th> <th> Actions </th> </tr> </thead> <tbody> <?php foreach ($records as $record): ?> <?php /* |-------------------------------------------------------------------------- | Find media information for current row |-------------------------------------------------------------------------- */ $mediaRow = null; foreach ($media as $m) { if ( (int)$m['id'] === (int)$record['media_id'] ) { $mediaRow = $m; break; } } $mediaLabel = $mediaRow ? pm_media_label( $mediaRow, $mediaNameColumn ) : 'Media #' . (int)$record['media_id']; $mediaPath = $mediaRow ? pm_media_path( $mediaRow, $mediaPathColumn ) : ''; $mediaUrl = pm_media_url($mediaPath); $isImage = pm_is_image_path($mediaPath); ?> <tr> <!-- ID --> <td> <span class="pm-id"> #<?= (int)$record['id'] ?> </span> </td> <!-- PAGE --> <td> <div class="pm-page-cell"> <div class="pm-page-avatar"> <i class="fa-solid fa-file-lines"></i> </div> <div> <div class="pm-page-name"> <?= pm_e( $record['page_title'] ?? 'Untitled Page' ) ?> </div> <?php if (!empty($record['page_slug'])): ?> <div class="pm-slug"> /<?= pm_e( ltrim( $record['page_slug'], '/' ) ) ?> </div> <?php endif; ?> </div> </div> </td> <!-- MEDIA --> <td> <div class="pm-media-cell"> <div class="pm-media-thumb"> <?php if ( $isImage && $mediaUrl !== '' ): ?> <img src="<?= pm_e($mediaUrl) ?>" alt="<?= pm_e( $record['alt_text'] ?? $mediaLabel ) ?>" loading="lazy" onerror="this.style.display='none';this.nextElementSibling.style.display='flex';" > <i class="fa-solid fa-image" style="display:none;" ></i> <?php else: ?> <i class="fa-solid fa-photo-film"></i> <?php endif; ?> </div> <div> <div class="pm-media-name"> <?= pm_e($mediaLabel) ?> </div> <div class="pm-slug"> Media ID #<?= (int)$record['media_id'] ?> </div> </div> </div> </td> <!-- ROLE --> <td> <span class="pm-role"> <?= pm_e( $record['media_role'] ) ?> </span> </td> <!-- TITLE --> <td> <div style="font-weight:700;"> <?= pm_e( $record['title'] ?: '—' ) ?> </div> <?php if (!empty($record['alt_text'])): ?> <div class="pm-slug"> Alt: <?= pm_e( $record['alt_text'] ) ?> </div> <?php endif; ?> </td> <!-- SORT --> <td> <strong> <?= (int)$record['sort_order'] ?> </strong> </td> <!-- STATUS --> <td> <span class="pm-status <?= (int)$record['status'] === 1 ? 'active' : 'inactive' ?>" > <span class="pm-status-dot"></span> <?= (int)$record['status'] === 1 ? 'Active' : 'Inactive' ?> </span> </td> <!-- ACTIONS --> <td> <div class="pm-actions"> <!-- EDIT --> <a href="page_media.php?edit=<?= (int)$record['id'] ?>" class="pm-icon-btn" title="Edit" > <i class="fa-solid fa-pen"></i> </a> <!-- STATUS --> <form method="post" style="display:inline;" > <input type="hidden" name="csrf_token" value="<?= pm_e( $csrfToken ) ?>" > <input type="hidden" name="action" value="toggle_status" > <input type="hidden" name="id" value="<?= (int)$record['id'] ?>" > <button type="submit" class="pm-icon-btn" title="<?= (int)$record['status'] === 1 ? 'Deactivate' : 'Activate' ?>" > <i class="fa-solid <?= (int)$record['status'] === 1 ? 'fa-eye-slash' : 'fa-eye' ?>"> </i> </button> </form> <!-- DELETE --> <form method="post" style="display:inline;" onsubmit="return confirmDelete();" > <input type="hidden" name="csrf_token" value="<?= pm_e( $csrfToken ) ?>" > <input type="hidden" name="action" value="delete" > <input type="hidden" name="id" value="<?= (int)$record['id'] ?>" > <button type="submit" class="pm-icon-btn danger" title="Delete" > <i class="fa-solid fa-trash"></i> </button> </form> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <!-- ================================================== PAGINATION =================================================== --> <div class="pm-pagination"> <div class="pm-pagination-info"> Showing <strong> <?= $totalRows > 0 ? number_format($offset + 1) : 0 ?> </strong> to <strong> <?= min( $offset + $perPage, $totalRows ) ?> </strong> of <strong> <?= number_format($totalRows) ?> </strong> records </div> <div class="pm-pagination-links"> <!-- PREVIOUS --> <?php $prevQuery = pm_page_url_params([ 'p' => max(1, $currentPage - 1) ]); ?> <a href="?<?= pm_e($prevQuery) ?>" class="pm-page-link <?= $currentPage <= 1 ? 'disabled' : '' ?>" > <i class="fa-solid fa-chevron-left"></i> </a> <?php $startPage = max( 1, $currentPage - 2 ); $endPage = min( $totalPages, $currentPage + 2 ); for ( $pageNumber = $startPage; $pageNumber <= $endPage; $pageNumber++ ): $pageQuery = pm_page_url_params([ 'p' => $pageNumber ]); ?> <a href="?<?= pm_e($pageQuery) ?>" class="pm-page-link <?= $pageNumber === $currentPage ? 'active' : '' ?>" > <?= $pageNumber ?> </a> <?php endfor; ?> <!-- NEXT --> <?php $nextQuery = pm_page_url_params([ 'p' => min( $totalPages, $currentPage + 1 ) ]); ?> <a href="?<?= pm_e($nextQuery) ?>" class="pm-page-link <?= $currentPage >= $totalPages ? 'disabled' : '' ?>" > <i class="fa-solid fa-chevron-right"></i> </a> </div> </div> <?php else: ?> <!-- EMPTY --> <div class="pm-empty"> <div class="pm-empty-icon"> <i class="fa-solid fa-photo-film"></i> </div> <h3> No page media found </h3> <p> Add your first page media record using the form above. </p> </div> <?php endif; ?> </section> </main> </div> <!-- ============================================================== JAVASCRIPT ================================================================ --> <script> function confirmDelete() { return confirm( "Are you sure you want to delete this page media record?\n\n" + "This will remove only the page_media relationship. " + "The original media file will not be deleted." ); } /* |-------------------------------------------------------------------------- | Auto hide flash |-------------------------------------------------------------------------- */ setTimeout(function () { const alert = document.querySelector('.pm-alert'); if (alert) { alert.style.transition = 'opacity .3s ease, transform .3s ease'; alert.style.opacity = '0'; alert.style.transform = 'translateY(-5px)'; setTimeout(function () { if (alert.parentNode) { alert.remove(); } }, 320); } }, 4500); /* |-------------------------------------------------------------------------- | Confirm status forms |-------------------------------------------------------------------------- */ document.querySelectorAll( 'form input[name="action"][value="toggle_status"]' ).forEach(function () { const form = this.closest('form'); if (!form) { return; } form.addEventListener('submit', function (event) { const button = form.querySelector('button'); if (!button) { return; } const icon = button.querySelector('i'); if ( icon && icon.classList.contains('fa-eye-slash') ) { if ( !confirm( 'Are you sure you want to deactivate this page media?' ) ) { event.preventDefault(); } } }); }); </script> </body> </html>
Save
Cancel