📁 File Manager
Editing:
/home/wvl7mof7402e/www/chem/admin/media_folders.php
<?php /** * Pure Chemical Admin * File: admin/media_folders.php * * Existing table: * media_folders * * Columns: * id * parent_id * name * slug * sort_order * status * created_at * updated_at */ 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/csrf.php')) { require_once __DIR__ . '/includes/csrf.php'; } if (file_exists(__DIR__ . '/includes/functions.php')) { require_once __DIR__ . '/includes/functions.php'; } if (function_exists('requireAdmin')) { requireAdmin(); } /* ----------------------------------------------------------- Helpers ----------------------------------------------------------- */ if (!function_exists('e')) { function e($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } } if (!function_exists('csrf_field')) { function csrf_field() { if (function_exists('csrf_token')) { $token = csrf_token(); } elseif (function_exists('generateCsrfToken')) { $token = generateCsrfToken(); } else { if (empty($_SESSION['_csrf_token'])) { $_SESSION['_csrf_token'] = bin2hex(random_bytes(32)); } $token = $_SESSION['_csrf_token']; } return '<input type="hidden" name="csrf_token" value="' . e($token) . '">'; } } if (!function_exists('verify_local_csrf')) { function verify_local_csrf() { if (function_exists('csrf_verify')) { csrf_verify(); return; } if (function_exists('verifyCsrfToken')) { verifyCsrfToken(); return; } $sessionToken = $_SESSION['_csrf_token'] ?? ''; $postedToken = $_POST['csrf_token'] ?? ''; if (!$sessionToken || !$postedToken || !hash_equals($sessionToken, $postedToken)) { http_response_code(419); exit('Invalid CSRF token.'); } } } if (!function_exists('slugify_folder')) { function slugify_folder($text) { $text = trim((string)$text); if ($text === '') { return ''; } $text = function_exists('iconv') ? (iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $text) ?: $text) : $text; $text = strtolower($text); $text = preg_replace('/[^a-z0-9]+/', '-', $text); $text = trim($text, '-'); return $text ?: 'folder'; } } if (!function_exists('redirect_self')) { function redirect_self($params = []) { $url = basename($_SERVER['PHP_SELF']); if (!empty($params)) { $url .= '?' . http_build_query($params); } header('Location: ' . $url); exit; } } /* ----------------------------------------------------------- Flash ----------------------------------------------------------- */ $flashSuccess = $_SESSION['media_folder_success'] ?? ''; $flashError = $_SESSION['media_folder_error'] ?? ''; unset( $_SESSION['media_folder_success'], $_SESSION['media_folder_error'] ); /* ----------------------------------------------------------- Current edit folder ----------------------------------------------------------- */ $editId = isset($_GET['edit']) ? (int)$_GET['edit'] : 0; $editFolder = null; if ($editId > 0) { $stmt = $pdo->prepare(" SELECT id, parent_id, name, slug, sort_order, status, created_at, updated_at FROM media_folders WHERE id = ? LIMIT 1 "); $stmt->execute([$editId]); $editFolder = $stmt->fetch(); if (!$editFolder) { $editId = 0; } } /* ----------------------------------------------------------- POST ACTIONS ----------------------------------------------------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { verify_local_csrf(); $action = $_POST['action'] ?? ''; /* ------------------------------------------------------- SAVE FOLDER ------------------------------------------------------- */ if ($action === 'save') { $id = (int)($_POST['id'] ?? 0); $name = trim($_POST['name'] ?? ''); $slug = trim($_POST['slug'] ?? ''); $parentId = isset($_POST['parent_id']) && $_POST['parent_id'] !== '' ? (int)$_POST['parent_id'] : null; $sortOrder = (int)($_POST['sort_order'] ?? 0); $status = isset($_POST['status']) ? (int)$_POST['status'] : 1; if ($name === '') { $_SESSION['media_folder_error'] = 'Folder name is required.'; redirect_self($id > 0 ? ['edit' => $id] : []); } if ($slug === '') { $slug = slugify_folder($name); } else { $slug = slugify_folder($slug); } if ($slug === '') { $_SESSION['media_folder_error'] = 'Please provide a valid folder slug.'; redirect_self($id > 0 ? ['edit' => $id] : []); } if ($sortOrder < 0) { $sortOrder = 0; } $status = $status === 1 ? 1 : 0; /* --------------------------------------------------- Prevent folder becoming its own parent --------------------------------------------------- */ if ($parentId !== null && $id > 0 && $parentId === $id) { $_SESSION['media_folder_error'] = 'A folder cannot be its own parent.'; redirect_self(['edit' => $id]); } /* --------------------------------------------------- Check parent exists --------------------------------------------------- */ if ($parentId !== null) { $stmt = $pdo->prepare(" SELECT id FROM media_folders WHERE id = ? LIMIT 1 "); $stmt->execute([$parentId]); if (!$stmt->fetchColumn()) { $parentId = null; } } /* --------------------------------------------------- Prevent circular hierarchy --------------------------------------------------- */ if ($id > 0 && $parentId !== null) { $checkParent = $parentId; $circular = false; $visited = []; while ($checkParent !== null) { if (isset($visited[$checkParent])) { break; } $visited[$checkParent] = true; if ($checkParent === $id) { $circular = true; break; } $stmt = $pdo->prepare(" SELECT parent_id FROM media_folders WHERE id = ? LIMIT 1 "); $stmt->execute([$checkParent]); $nextParent = $stmt->fetchColumn(); if ($nextParent === false || $nextParent === null) { break; } $checkParent = (int)$nextParent; } if ($circular) { $_SESSION['media_folder_error'] = 'Invalid parent folder. Circular folder hierarchy is not allowed.'; redirect_self(['edit' => $id]); } } /* --------------------------------------------------- Unique slug check --------------------------------------------------- */ $slugCheckSql = " SELECT id FROM media_folders WHERE slug = ? "; $slugParams = [$slug]; if ($id > 0) { $slugCheckSql .= " AND id != ?"; $slugParams[] = $id; } $slugCheckSql .= " LIMIT 1"; $stmt = $pdo->prepare($slugCheckSql); $stmt->execute($slugParams); if ($stmt->fetchColumn()) { $baseSlug = $slug; $counter = 2; while (true) { $newSlug = $baseSlug . '-' . $counter; $checkSql = " SELECT id FROM media_folders WHERE slug = ? "; $checkParams = [$newSlug]; if ($id > 0) { $checkSql .= " AND id != ?"; $checkParams[] = $id; } $checkSql .= " LIMIT 1"; $checkStmt = $pdo->prepare($checkSql); $checkStmt->execute($checkParams); if (!$checkStmt->fetchColumn()) { $slug = $newSlug; break; } $counter++; if ($counter > 9999) { $_SESSION['media_folder_error'] = 'Unable to generate a unique slug.'; redirect_self($id > 0 ? ['edit' => $id] : []); } } } /* --------------------------------------------------- Insert --------------------------------------------------- */ if ($id <= 0) { $stmt = $pdo->prepare(" INSERT INTO media_folders ( parent_id, name, slug, sort_order, status ) VALUES ( :parent_id, :name, :slug, :sort_order, :status ) "); $stmt->execute([ ':parent_id' => $parentId, ':name' => $name, ':slug' => $slug, ':sort_order'=> $sortOrder, ':status' => $status ]); $_SESSION['media_folder_success'] = 'Media folder created successfully.'; redirect_self(); } /* --------------------------------------------------- Update --------------------------------------------------- */ $stmt = $pdo->prepare(" UPDATE media_folders SET parent_id = :parent_id, name = :name, slug = :slug, sort_order = :sort_order, status = :status WHERE id = :id LIMIT 1 "); $stmt->execute([ ':parent_id' => $parentId, ':name' => $name, ':slug' => $slug, ':sort_order'=> $sortOrder, ':status' => $status, ':id' => $id ]); $_SESSION['media_folder_success'] = 'Media folder updated successfully.'; redirect_self(); } /* ------------------------------------------------------- DELETE FOLDER ------------------------------------------------------- */ if ($action === 'delete') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { $_SESSION['media_folder_error'] = 'Invalid folder selected.'; redirect_self(); } /* Check children */ $stmt = $pdo->prepare(" SELECT COUNT(*) FROM media_folders WHERE parent_id = ? "); $stmt->execute([$id]); $childCount = (int)$stmt->fetchColumn(); if ($childCount > 0) { $_SESSION['media_folder_error'] = 'This folder contains sub-folders. Move or delete the sub-folders first.'; redirect_self(); } /* Delete */ $stmt = $pdo->prepare(" DELETE FROM media_folders WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); $_SESSION['media_folder_success'] = 'Media folder deleted successfully.'; redirect_self(); } /* ------------------------------------------------------- QUICK STATUS ------------------------------------------------------- */ if ($action === 'toggle_status') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { $_SESSION['media_folder_error'] = 'Invalid folder.'; redirect_self(); } $stmt = $pdo->prepare(" UPDATE media_folders SET status = CASE WHEN status = 1 THEN 0 ELSE 1 END WHERE id = ? LIMIT 1 "); $stmt->execute([$id]); $_SESSION['media_folder_success'] = 'Folder status updated successfully.'; redirect_self(); } } /* ----------------------------------------------------------- Filters ----------------------------------------------------------- */ $search = trim($_GET['q'] ?? ''); $statusFilter = $_GET['status'] ?? ''; $parentFilter = isset($_GET['parent']) && $_GET['parent'] !== '' ? (int)$_GET['parent'] : null; /* ----------------------------------------------------------- Parent folders for dropdown ----------------------------------------------------------- */ $parentStmt = $pdo->query(" SELECT id, parent_id, name, slug FROM media_folders ORDER BY sort_order ASC, name ASC, id ASC "); $allFolders = $parentStmt->fetchAll(); /* ----------------------------------------------------------- Build folder hierarchy ----------------------------------------------------------- */ $folderMap = []; foreach ($allFolders as $folder) { $folderMap[(int)$folder['id']] = $folder; } $childrenMap = []; foreach ($allFolders as $folder) { $pid = $folder['parent_id'] !== null ? (int)$folder['parent_id'] : 0; if (!isset($childrenMap[$pid])) { $childrenMap[$pid] = []; } $childrenMap[$pid][] = $folder; } function render_folder_options( $childrenMap, $parentId = 0, $level = 0, $excludeId = 0 ) { if (empty($childrenMap[$parentId])) { return; } foreach ($childrenMap[$parentId] as $folder) { $folderId = (int)$folder['id']; if ($folderId === $excludeId) { continue; } $prefix = str_repeat('— ', $level); echo '<option value="' . $folderId . '">' . e($prefix . $folder['name']) . '</option>'; render_folder_options( $childrenMap, $folderId, $level + 1, $excludeId ); } } /* ----------------------------------------------------------- Filtered folder listing ----------------------------------------------------------- */ $where = []; $params = []; if ($search !== '') { $where[] = "( mf.name LIKE :search OR mf.slug LIKE :search )"; $params[':search'] = '%' . $search . '%'; } if ($statusFilter !== '' && ($statusFilter === '0' || $statusFilter === '1')) { $where[] = "mf.status = :status"; $params[':status'] = (int)$statusFilter; } if ($parentFilter !== null) { if ($parentFilter === 0) { $where[] = "mf.parent_id IS NULL"; } else { $where[] = "mf.parent_id = :parent_id_filter"; $params[':parent_id_filter'] = $parentFilter; } } $sql = " SELECT mf.id, mf.parent_id, mf.name, mf.slug, mf.sort_order, mf.status, mf.created_at, mf.updated_at, parent.name AS parent_name, ( SELECT COUNT(*) FROM media_folders child WHERE child.parent_id = mf.id ) AS child_count FROM media_folders mf LEFT JOIN media_folders parent ON parent.id = mf.parent_id "; if ($where) { $sql .= " WHERE " . implode(' AND ', $where); } $sql .= " ORDER BY mf.parent_id IS NOT NULL ASC, mf.sort_order ASC, mf.name ASC, mf.id DESC "; $stmt = $pdo->prepare($sql); $stmt->execute($params); $folders = $stmt->fetchAll(); /* ----------------------------------------------------------- Stats ----------------------------------------------------------- */ $totalFolders = (int)$pdo->query(" SELECT COUNT(*) FROM media_folders ")->fetchColumn(); $activeFolders = (int)$pdo->query(" SELECT COUNT(*) FROM media_folders WHERE status = 1 ")->fetchColumn(); $inactiveFolders = $totalFolders - $activeFolders; $rootFolders = (int)$pdo->query(" SELECT COUNT(*) FROM media_folders WHERE parent_id IS NULL ")->fetchColumn(); $subFolders = $totalFolders - $rootFolders; /* ----------------------------------------------------------- Edit values ----------------------------------------------------------- */ $formId = $editFolder['id'] ?? 0; $formParentId = $editFolder['parent_id'] ?? null; $formName = $editFolder['name'] ?? ''; $formSlug = $editFolder['slug'] ?? ''; $formSort = $editFolder['sort_order'] ?? 0; $formStatus = isset($editFolder['status']) ? (int)$editFolder['status'] : 1; $pageTitle = $editId > 0 ? 'Edit Media Folder' : 'Media Folders'; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title><?= e($pageTitle) ?> | Pure Chemical Admin</title> <meta name="theme-color" content="#07111f"> <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 { --pc-primary: #10b981; --pc-primary-dark: #059669; --pc-primary-soft: rgba(16, 185, 129, .10); --pc-blue: #2563eb; --pc-red: #ef4444; --pc-orange: #f59e0b; --pc-dark: #07111f; --pc-dark-2: #0d1726; --pc-bg: #f4f7fb; --pc-card: #ffffff; --pc-text: #172033; --pc-muted: #718096; --pc-border: #e6ebf2; --pc-radius: 18px; --pc-shadow: 0 16px 45px rgba(15, 23, 42, .07); } * { box-sizing: border-box; } html { scroll-behavior: smooth; } body { margin: 0; background: var(--pc-bg); color: var(--pc-text); font-family: "Inter", sans-serif; font-size: 14px; } button, input, select { font-family: inherit; } a { text-decoration: none; } /* ---------------------------------------------------- MAIN ---------------------------------------------------- */ .pc-main { margin-left: 280px; min-height: 100vh; } /* ---------------------------------------------------- TOPBAR ---------------------------------------------------- */ .pc-topbar { position: sticky; top: 0; z-index: 1000; min-height: 76px; padding: 14px 30px; background: rgba(255, 255, 255, .88); backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px); border-bottom: 1px solid rgba(226, 232, 240, .85); display: flex; align-items: center; justify-content: space-between; gap: 20px; } .pc-topbar-left { min-width: 0; } .pc-breadcrumb { display: flex; align-items: center; gap: 8px; color: var(--pc-muted); font-size: 12px; margin-bottom: 5px; } .pc-breadcrumb i { font-size: 10px; } .pc-page-title { margin: 0; font-size: 22px; line-height: 1.2; font-weight: 800; letter-spacing: -.5px; color: var(--pc-dark); } .pc-topbar-right { display: flex; align-items: center; gap: 10px; } .pc-top-btn { height: 42px; padding: 0 15px; border-radius: 12px; border: 1px solid var(--pc-border); background: #fff; color: var(--pc-text); display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-weight: 700; font-size: 13px; transition: .2s ease; } .pc-top-btn:hover { border-color: rgba(16, 185, 129, .35); color: var(--pc-primary-dark); transform: translateY(-1px); } .pc-top-btn.primary { color: #fff; background: linear-gradient( 135deg, var(--pc-primary), var(--pc-primary-dark) ); border-color: transparent; box-shadow: 0 9px 22px rgba(16, 185, 129, .20); } .pc-top-btn.primary:hover { color: #fff; transform: translateY(-2px); box-shadow: 0 13px 28px rgba(16, 185, 129, .28); } /* ---------------------------------------------------- CONTENT ---------------------------------------------------- */ .pc-content { padding: 30px; } .pc-alert { border-radius: 14px; padding: 14px 16px; margin-bottom: 22px; display: flex; align-items: flex-start; gap: 11px; font-size: 13px; font-weight: 600; } .pc-alert.success { background: #ecfdf5; color: #047857; border: 1px solid #a7f3d0; } .pc-alert.error { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; } .pc-alert i { margin-top: 2px; } /* ---------------------------------------------------- HERO ---------------------------------------------------- */ .pc-hero { position: relative; overflow: hidden; background: radial-gradient( circle at 90% 20%, rgba(16, 185, 129, .18), transparent 35% ), linear-gradient( 135deg, #07111f 0%, #0d1b2d 60%, #10263a 100% ); border-radius: 24px; padding: 30px; color: #fff; box-shadow: var(--pc-shadow); margin-bottom: 24px; } .pc-hero:after { content: ""; position: absolute; width: 190px; height: 190px; right: -70px; bottom: -90px; border-radius: 50%; border: 1px solid rgba(255,255,255,.08); box-shadow: 0 0 0 28px rgba(255,255,255,.025), 0 0 0 56px rgba(255,255,255,.02); } .pc-hero-inner { position: relative; z-index: 2; display: flex; justify-content: space-between; align-items: center; gap: 30px; } .pc-hero-copy { max-width: 680px; } .pc-eyebrow { display: inline-flex; align-items: center; gap: 8px; padding: 7px 11px; border-radius: 999px; background: rgba(16, 185, 129, .13); border: 1px solid rgba(52, 211, 153, .22); color: #a7f3d0; font-size: 11px; font-weight: 800; letter-spacing: .5px; text-transform: uppercase; } .pc-hero h2 { margin: 14px 0 8px; font-size: 28px; line-height: 1.18; letter-spacing: -.8px; } .pc-hero p { margin: 0; color: rgba(255,255,255,.68); line-height: 1.7; max-width: 650px; } .pc-hero-icon { width: 86px; height: 86px; border-radius: 24px; display: flex; align-items: center; justify-content: center; background: rgba(255,255,255,.07); border: 1px solid rgba(255,255,255,.10); font-size: 35px; color: #6ee7b7; flex-shrink: 0; } /* ---------------------------------------------------- STATS ---------------------------------------------------- */ .pc-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 24px; } .pc-stat { background: var(--pc-card); border: 1px solid var(--pc-border); border-radius: var(--pc-radius); padding: 18px; display: flex; align-items: center; gap: 14px; box-shadow: 0 8px 30px rgba(15, 23, 42, .04); } .pc-stat-icon { width: 46px; height: 46px; flex: 0 0 46px; border-radius: 14px; display: flex; align-items: center; justify-content: center; font-size: 18px; } .pc-stat:nth-child(1) .pc-stat-icon { background: #ecfdf5; color: #059669; } .pc-stat:nth-child(2) .pc-stat-icon { background: #eff6ff; color: #2563eb; } .pc-stat:nth-child(3) .pc-stat-icon { background: #fff7ed; color: #ea580c; } .pc-stat:nth-child(4) .pc-stat-icon { background: #f5f3ff; color: #7c3aed; } .pc-stat-label { color: var(--pc-muted); font-size: 12px; font-weight: 600; margin-bottom: 3px; } .pc-stat-value { font-size: 23px; line-height: 1.1; font-weight: 800; color: var(--pc-dark); } /* ---------------------------------------------------- GRID ---------------------------------------------------- */ .pc-grid { display: grid; grid-template-columns: minmax(330px, .85fr) minmax(0, 1.75fr); gap: 22px; align-items: start; } .pc-card { background: var(--pc-card); border: 1px solid var(--pc-border); border-radius: var(--pc-radius); box-shadow: var(--pc-shadow); overflow: hidden; } .pc-card-head { padding: 20px 22px; border-bottom: 1px solid var(--pc-border); display: flex; justify-content: space-between; align-items: center; gap: 14px; } .pc-card-title { margin: 0; font-size: 16px; font-weight: 800; color: var(--pc-dark); } .pc-card-subtitle { margin: 4px 0 0; color: var(--pc-muted); font-size: 12px; line-height: 1.5; } .pc-card-body { padding: 22px; } /* ---------------------------------------------------- FORM ---------------------------------------------------- */ .pc-form-group { margin-bottom: 17px; } .pc-label { display: block; margin-bottom: 8px; color: #334155; font-size: 12px; font-weight: 800; } .pc-label span { color: #ef4444; } .pc-input, .pc-select { width: 100%; height: 46px; border: 1px solid #dfe5ed; border-radius: 12px; background: #fff; color: var(--pc-text); padding: 0 13px; outline: none; transition: .2s ease; } .pc-input:focus, .pc-select:focus { border-color: rgba(16, 185, 129, .60); box-shadow: 0 0 0 4px rgba(16, 185, 129, .09); } .pc-input::placeholder { color: #a0aec0; } .pc-help { margin-top: 6px; color: #94a3b8; font-size: 11px; line-height: 1.5; } .pc-form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; } .pc-status-box { height: 46px; display: flex; align-items: center; justify-content: space-between; padding: 0 13px; border: 1px solid #dfe5ed; border-radius: 12px; } .pc-status-label { display: flex; align-items: center; gap: 9px; font-size: 12px; font-weight: 700; } .pc-switch { position: relative; width: 42px; height: 23px; flex-shrink: 0; } .pc-switch input { display: none; } .pc-switch-slider { position: absolute; inset: 0; background: #cbd5e1; border-radius: 999px; cursor: pointer; transition: .2s ease; } .pc-switch-slider:before { content: ""; position: absolute; width: 17px; height: 17px; left: 3px; top: 3px; background: #fff; border-radius: 50%; box-shadow: 0 2px 5px rgba(0,0,0,.16); transition: .2s ease; } .pc-switch input:checked + .pc-switch-slider { background: var(--pc-primary); } .pc-switch input:checked + .pc-switch-slider:before { transform: translateX(19px); } .pc-form-actions { display: flex; align-items: center; gap: 9px; margin-top: 22px; } .pc-btn { min-height: 43px; padding: 0 15px; border-radius: 11px; border: 1px solid transparent; display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-size: 12px; font-weight: 800; cursor: pointer; transition: .2s ease; } .pc-btn-primary { color: #fff; background: linear-gradient( 135deg, var(--pc-primary), var(--pc-primary-dark) ); box-shadow: 0 8px 18px rgba(16,185,129,.18); } .pc-btn-primary:hover { transform: translateY(-1px); box-shadow: 0 11px 22px rgba(16,185,129,.25); } .pc-btn-light { background: #fff; color: #475569; border-color: var(--pc-border); } .pc-btn-light:hover { border-color: #cbd5e1; background: #f8fafc; } .pc-btn-danger { background: #fff; color: #dc2626; border-color: #fecaca; } .pc-btn-danger:hover { background: #fef2f2; } /* ---------------------------------------------------- TOOLBAR ---------------------------------------------------- */ .pc-toolbar { padding: 15px 18px; background: #fbfcfe; border-bottom: 1px solid var(--pc-border); display: flex; align-items: center; gap: 9px; flex-wrap: wrap; } .pc-search { flex: 1 1 220px; position: relative; } .pc-search i { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: #94a3b8; font-size: 13px; } .pc-search input { width: 100%; height: 41px; border: 1px solid #dfe5ed; border-radius: 10px; padding: 0 12px 0 36px; outline: none; } .pc-search input:focus { border-color: rgba(16,185,129,.5); box-shadow: 0 0 0 3px rgba(16,185,129,.08); } .pc-filter { height: 41px; border: 1px solid #dfe5ed; border-radius: 10px; background: #fff; color: #475569; padding: 0 11px; outline: none; font-size: 12px; font-weight: 600; } .pc-filter:focus { border-color: rgba(16,185,129,.5); } /* ---------------------------------------------------- TABLE ---------------------------------------------------- */ .pc-table-wrap { width: 100%; overflow-x: auto; } .pc-table { width: 100%; min-width: 760px; border-collapse: collapse; } .pc-table th { padding: 13px 17px; background: #fbfcfe; border-bottom: 1px solid var(--pc-border); text-align: left; color: #64748b; font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: .6px; white-space: nowrap; } .pc-table td { padding: 15px 17px; border-bottom: 1px solid #edf1f5; vertical-align: middle; } .pc-table tbody tr { transition: .15s ease; } .pc-table tbody tr:hover { background: #fbfffd; } .pc-folder { display: flex; align-items: center; gap: 11px; min-width: 210px; } .pc-folder-icon { width: 40px; height: 40px; border-radius: 11px; display: flex; align-items: center; justify-content: center; background: #ecfdf5; color: #059669; flex-shrink: 0; } .pc-folder-name { color: #1e293b; font-size: 13px; font-weight: 800; word-break: break-word; } .pc-folder-slug { color: #94a3b8; font-size: 10px; margin-top: 3px; } .pc-parent { display: inline-flex; align-items: center; gap: 6px; padding: 6px 9px; border-radius: 8px; background: #f8fafc; color: #64748b; font-size: 11px; font-weight: 700; } .pc-root { background: #eff6ff; color: #2563eb; } .pc-order { display: inline-flex; align-items: center; justify-content: center; min-width: 30px; height: 28px; padding: 0 7px; border-radius: 8px; background: #f8fafc; color: #475569; font-size: 11px; font-weight: 800; } .pc-count { color: #64748b; font-size: 11px; font-weight: 700; } .pc-badge { display: inline-flex; align-items: center; gap: 6px; padding: 6px 9px; border-radius: 999px; font-size: 10px; font-weight: 800; } .pc-badge.active { background: #ecfdf5; color: #047857; } .pc-badge.inactive { background: #fef2f2; color: #b91c1c; } .pc-actions { display: flex; align-items: center; justify-content: flex-end; gap: 6px; } .pc-action { width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center; border-radius: 9px; background: #fff; border: 1px solid #e2e8f0; color: #64748b; cursor: pointer; transition: .18s ease; } .pc-action:hover { transform: translateY(-1px); } .pc-action.edit:hover { color: #2563eb; border-color: #bfdbfe; background: #eff6ff; } .pc-action.status:hover { color: #059669; border-color: #a7f3d0; background: #ecfdf5; } .pc-action.delete:hover { color: #dc2626; border-color: #fecaca; background: #fef2f2; } /* ---------------------------------------------------- EMPTY ---------------------------------------------------- */ .pc-empty { text-align: center; padding: 55px 25px; } .pc-empty-icon { width: 64px; height: 64px; margin: 0 auto 15px; border-radius: 18px; display: flex; align-items: center; justify-content: center; background: #ecfdf5; color: #059669; font-size: 25px; } .pc-empty h3 { margin: 0 0 7px; font-size: 15px; color: #1e293b; } .pc-empty p { margin: 0; color: #94a3b8; font-size: 12px; } /* ---------------------------------------------------- EDIT MODE ---------------------------------------------------- */ .pc-edit-note { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; background: #eff6ff; color: #1d4ed8; border: 1px solid #bfdbfe; font-size: 11px; font-weight: 700; margin-bottom: 17px; } /* ---------------------------------------------------- MOBILE ---------------------------------------------------- */ @media (max-width: 1200px) { .pc-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); } .pc-grid { grid-template-columns: 1fr; } } @media (max-width: 1100px) { .pc-main { margin-left: 0; } .pc-content { padding: 22px; } .pc-topbar { padding: 13px 22px; } } @media (max-width: 700px) { .pc-topbar { min-height: 68px; } .pc-page-title { font-size: 18px; } .pc-breadcrumb { display: none; } .pc-topbar-right .pc-top-btn span { display: none; } .pc-top-btn { width: 40px; padding: 0; } .pc-content { padding: 15px; } .pc-hero { padding: 22px; border-radius: 19px; } .pc-hero-inner { align-items: flex-start; } .pc-hero h2 { font-size: 22px; } .pc-hero-icon { display: none; } .pc-stats { grid-template-columns: 1fr 1fr; gap: 10px; } .pc-stat { padding: 13px; } .pc-stat-icon { width: 39px; height: 39px; flex-basis: 39px; } .pc-stat-value { font-size: 18px; } .pc-card-head, .pc-card-body { padding: 17px; } .pc-form-row { grid-template-columns: 1fr; } } @media (max-width: 470px) { .pc-stats { grid-template-columns: 1fr; } .pc-hero { margin-bottom: 15px; } .pc-topbar { padding-left: 15px; padding-right: 15px; } .pc-card { border-radius: 15px; } .pc-form-actions { flex-direction: column; align-items: stretch; } .pc-form-actions .pc-btn { width: 100%; } } </style> </head> <body> <?php /* |-------------------------------------------------------------------------- | Sidebar |-------------------------------------------------------------------------- | | User requested sidebar separately. | No admin/header.php is included here. | */ require_once __DIR__ . '/sidebar.php'; ?> <div class="pc-main"> <!-- TOPBAR --> <header class="pc-topbar"> <div class="pc-topbar-left"> <div class="pc-breadcrumb"> <span>Admin</span> <i class="fa-solid fa-chevron-right"></i> <span>Media</span> <i class="fa-solid fa-chevron-right"></i> <span>Folders</span> </div> <h1 class="pc-page-title"> Media Folders </h1> </div> <div class="pc-topbar-right"> <?php if ($editId > 0): ?> <a href="media_folders.php" class="pc-top-btn" > <i class="fa-solid fa-arrow-left"></i> <span>Back</span> </a> <?php endif; ?> <a href="media.php" class="pc-top-btn primary" > <i class="fa-solid fa-photo-film"></i> <span>Media Library</span> </a> </div> </header> <main class="pc-content"> <!-- FLASH --> <?php if ($flashSuccess): ?> <div class="pc-alert success"> <i class="fa-solid fa-circle-check"></i> <div> <?= e($flashSuccess) ?> </div> </div> <?php endif; ?> <?php if ($flashError): ?> <div class="pc-alert error"> <i class="fa-solid fa-circle-exclamation"></i> <div> <?= e($flashError) ?> </div> </div> <?php endif; ?> <!-- HERO --> <section class="pc-hero"> <div class="pc-hero-inner"> <div class="pc-hero-copy"> <span class="pc-eyebrow"> <i class="fa-solid fa-folder-tree"></i> Media Management </span> <h2> Organize your media library </h2> <p> Create a clean folder hierarchy for product images, documents, banners and other website media. Folder structure is stored directly in your existing <strong>media_folders</strong> table. </p> </div> <div class="pc-hero-icon"> <i class="fa-solid fa-folder-tree"></i> </div> </div> </section> <!-- STATS --> <section class="pc-stats"> <div class="pc-stat"> <div class="pc-stat-icon"> <i class="fa-solid fa-folder"></i> </div> <div> <div class="pc-stat-label"> Total Folders </div> <div class="pc-stat-value"> <?= number_format($totalFolders) ?> </div> </div> </div> <div class="pc-stat"> <div class="pc-stat-icon"> <i class="fa-solid fa-circle-check"></i> </div> <div> <div class="pc-stat-label"> Active </div> <div class="pc-stat-value"> <?= number_format($activeFolders) ?> </div> </div> </div> <div class="pc-stat"> <div class="pc-stat-icon"> <i class="fa-solid fa-circle-pause"></i> </div> <div> <div class="pc-stat-label"> Inactive </div> <div class="pc-stat-value"> <?= number_format($inactiveFolders) ?> </div> </div> </div> <div class="pc-stat"> <div class="pc-stat-icon"> <i class="fa-solid fa-sitemap"></i> </div> <div> <div class="pc-stat-label"> Root / Sub </div> <div class="pc-stat-value"> <?= number_format($rootFolders) ?> / <?= number_format($subFolders) ?> </div> </div> </div> </section> <!-- MAIN GRID --> <section class="pc-grid"> <!-- FORM CARD --> <div class="pc-card"> <div class="pc-card-head"> <div> <h2 class="pc-card-title"> <?= $editId > 0 ? 'Edit Folder' : 'Create Folder' ?> </h2> <p class="pc-card-subtitle"> <?= $editId > 0 ? 'Update the selected media folder.' : 'Add a new folder to your media library.' ?> </p> </div> <i class="fa-solid <?= $editId > 0 ? 'fa-pen-to-square' : 'fa-folder-plus' ?>" style="color:#10b981;font-size:20px;" ></i> </div> <div class="pc-card-body"> <?php if ($editId > 0): ?> <div class="pc-edit-note"> <i class="fa-solid fa-pen"></i> Editing: <strong><?= e($formName) ?></strong> </div> <?php endif; ?> <form method="POST" action="media_folders.php" autocomplete="off" > <?= csrf_field() ?> <input type="hidden" name="action" value="save" > <input type="hidden" name="id" value="<?= (int)$formId ?>" > <!-- NAME --> <div class="pc-form-group"> <label class="pc-label"> Folder Name <span>*</span> </label> <input type="text" name="name" id="folderName" class="pc-input" value="<?= e($formName) ?>" placeholder="e.g. Product Images" maxlength="150" required > <div class="pc-help"> Use a clear name that identifies the purpose of this media folder. </div> </div> <!-- SLUG --> <div class="pc-form-group"> <label class="pc-label"> Slug <span>*</span> </label> <input type="text" name="slug" id="folderSlug" class="pc-input" value="<?= e($formSlug) ?>" placeholder="product-images" maxlength="180" required > <div class="pc-help"> Lowercase URL-friendly identifier. If left unchanged, it can be generated from the name. </div> </div> <!-- PARENT --> <div class="pc-form-group"> <label class="pc-label"> Parent Folder </label> <select name="parent_id" class="pc-select" > <option value=""> — Root Folder — </option> <?php /* * Render all folders recursively. * Current folder is excluded during edit. */ ?> <?php ob_start(); render_folder_options( $childrenMap, 0, 0, $editId ); $optionsHtml = ob_get_clean(); if ($formParentId !== null) { $optionsHtml = str_replace( 'value="' . (int)$formParentId . '"', 'value="' . (int)$formParentId . '" selected', $optionsHtml ); } echo $optionsHtml; ?> </select> <div class="pc-help"> Select a parent to create a nested folder. </div> </div> <!-- SORT + STATUS --> <div class="pc-form-row"> <div class="pc-form-group"> <label class="pc-label"> Sort Order </label> <input type="number" name="sort_order" class="pc-input" value="<?= (int)$formSort ?>" min="0" step="1" > <div class="pc-help"> Lower numbers appear first. </div> </div> <div class="pc-form-group"> <label class="pc-label"> Status </label> <div class="pc-status-box"> <div class="pc-status-label"> <i class="fa-solid fa-circle" style="font-size:8px;color:<?= $formStatus ? '#10b981' : '#94a3b8' ?>;" ></i> <span> <?= $formStatus ? 'Active' : 'Inactive' ?> </span> </div> <label class="pc-switch"> <input type="checkbox" name="status" value="1" <?= $formStatus === 1 ? 'checked' : '' ?> > <span class="pc-switch-slider"></span> </label> </div> </div> </div> <!-- ACTIONS --> <div class="pc-form-actions"> <button type="submit" class="pc-btn pc-btn-primary" > <i class="fa-solid <?= $editId > 0 ? 'fa-floppy-disk' : 'fa-plus' ?>"></i> <?= $editId > 0 ? 'Update Folder' : 'Create Folder' ?> </button> <?php if ($editId > 0): ?> <a href="media_folders.php" class="pc-btn pc-btn-light" > <i class="fa-solid fa-xmark"></i> Cancel </a> <?php else: ?> <button type="reset" class="pc-btn pc-btn-light" id="resetFolderForm" > <i class="fa-solid fa-rotate-left"></i> Reset </button> <?php endif; ?> </div> </form> </div> </div> <!-- LIST CARD --> <div class="pc-card"> <div class="pc-card-head"> <div> <h2 class="pc-card-title"> Folder Structure </h2> <p class="pc-card-subtitle"> Manage existing media folders and hierarchy. </p> </div> <span style=" display:inline-flex; align-items:center; gap:6px; padding:7px 10px; background:#ecfdf5; color:#047857; border-radius:999px; font-size:10px; font-weight:800; " > <i class="fa-solid fa-database"></i> <?= number_format(count($folders)) ?> </span> </div> <!-- FILTER --> <form method="GET" action="media_folders.php" class="pc-toolbar" > <div class="pc-search"> <i class="fa-solid fa-magnifying-glass"></i> <input type="text" name="q" value="<?= e($search) ?>" placeholder="Search folder name or slug..." > </div> <select name="parent" class="pc-filter" > <option value=""> All Parents </option> <option value="0" <?= $parentFilter === 0 ? 'selected' : '' ?> > Root Folders </option> <?php foreach ($allFolders as $pf): ?> <?php if ($editId > 0 && (int)$pf['id'] === $editId) continue; ?> <option value="<?= (int)$pf['id'] ?>" <?= $parentFilter === (int)$pf['id'] ? 'selected' : '' ?> > <?= e($pf['name']) ?> </option> <?php endforeach; ?> </select> <select name="status" class="pc-filter" > <option value=""> All Status </option> <option value="1" <?= $statusFilter === '1' ? 'selected' : '' ?> > Active </option> <option value="0" <?= $statusFilter === '0' ? 'selected' : '' ?> > Inactive </option> </select> <button type="submit" class="pc-btn pc-btn-primary" style="min-height:41px;" > <i class="fa-solid fa-filter"></i> Filter </button> <?php if ( $search !== '' || $statusFilter !== '' || $parentFilter !== null ): ?> <a href="media_folders.php" class="pc-btn pc-btn-light" style="min-height:41px;" title="Clear filters" > <i class="fa-solid fa-xmark"></i> </a> <?php endif; ?> </form> <!-- TABLE --> <?php if (!empty($folders)): ?> <div class="pc-table-wrap"> <table class="pc-table"> <thead> <tr> <th> Folder </th> <th> Parent </th> <th> Children </th> <th> Order </th> <th> Status </th> <th style="text-align:right;"> Actions </th> </tr> </thead> <tbody> <?php foreach ($folders as $folder): ?> <tr> <!-- FOLDER --> <td> <div class="pc-folder"> <div class="pc-folder-icon"> <i class="fa-solid fa-folder"></i> </div> <div> <div class="pc-folder-name"> <?= e($folder['name']) ?> </div> <div class="pc-folder-slug"> /<?= e($folder['slug']) ?> </div> </div> </div> </td> <!-- PARENT --> <td> <?php if ($folder['parent_id'] !== null): ?> <span class="pc-parent"> <i class="fa-solid fa-folder-tree"></i> <?= e( $folder['parent_name'] ?: 'Unknown' ) ?> </span> <?php else: ?> <span class="pc-parent pc-root"> <i class="fa-solid fa-house"></i> Root </span> <?php endif; ?> </td> <!-- CHILDREN --> <td> <span class="pc-count"> <i class="fa-solid fa-code-branch" style="margin-right:4px;" ></i> <?= number_format( (int)$folder['child_count'] ) ?> </span> </td> <!-- SORT --> <td> <span class="pc-order"> <?= (int)$folder['sort_order'] ?> </span> </td> <!-- STATUS --> <td> <?php if ((int)$folder['status'] === 1): ?> <span class="pc-badge active"> <i class="fa-solid fa-circle"></i> Active </span> <?php else: ?> <span class="pc-badge inactive"> <i class="fa-solid fa-circle"></i> Inactive </span> <?php endif; ?> </td> <!-- ACTIONS --> <td> <div class="pc-actions"> <!-- EDIT --> <a href="media_folders.php?edit=<?= (int)$folder['id'] ?>" class="pc-action edit" title="Edit folder" > <i class="fa-solid fa-pen"></i> </a> <!-- STATUS --> <form method="POST" action="media_folders.php" style="display:inline;" > <?= csrf_field() ?> <input type="hidden" name="action" value="toggle_status" > <input type="hidden" name="id" value="<?= (int)$folder['id'] ?>" > <button type="submit" class="pc-action status" title="<?= (int)$folder['status'] === 1 ? 'Deactivate' : 'Activate' ?>" > <i class="fa-solid <?= (int)$folder['status'] === 1 ? 'fa-toggle-on' : 'fa-toggle-off' ?>"></i> </button> </form> <!-- DELETE --> <form method="POST" action="media_folders.php" style="display:inline;" onsubmit="return confirmDeleteFolder( <?= (int)$folder['id'] ?>, <?= htmlspecialchars( json_encode( $folder['name'], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ), ENT_QUOTES, 'UTF-8' ) ?> );" > <?= csrf_field() ?> <input type="hidden" name="action" value="delete" > <input type="hidden" name="id" value="<?= (int)$folder['id'] ?>" > <button type="submit" class="pc-action delete" title="Delete folder" > <i class="fa-solid fa-trash"></i> </button> </form> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php else: ?> <div class="pc-empty"> <div class="pc-empty-icon"> <i class="fa-solid fa-folder-open"></i> </div> <h3> No media folders found </h3> <p> <?= $search !== '' ? 'Try another search term or clear the filters.' : 'Create your first media folder using the form.' ?> </p> </div> <?php endif; ?> </div> </section> </main> </div> <script> (function () { const nameInput = document.getElementById('folderName'); const slugInput = document.getElementById('folderSlug'); if (!nameInput || !slugInput) { return; } let slugManuallyChanged = <?= $editId > 0 ? 'true' : 'false' ?>; slugInput.addEventListener('input', function () { slugManuallyChanged = true; }); nameInput.addEventListener('input', function () { if (slugManuallyChanged) { return; } let value = this.value .toLowerCase() .trim(); value = value .normalize('NFD') .replace(/[\u0300-\u036f]/g, ''); value = value .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); slugInput.value = value; }); })(); function confirmDeleteFolder(id, name) { return confirm( 'Delete "' + name + '"?\n\n' + 'This action cannot be undone.\n' + 'Folders containing sub-folders cannot be deleted.' ); } /* Reset form */ const resetButton = document.getElementById('resetFolderForm'); if (resetButton) { resetButton.addEventListener('click', function () { setTimeout(function () { const nameInput = document.getElementById('folderName'); const slugInput = document.getElementById('folderSlug'); if (nameInput) { nameInput.value = ''; } if (slugInput) { slugInput.value = ''; } }, 0); }); } /* Auto-hide alerts */ setTimeout(function () { document.querySelectorAll('.pc-alert').forEach(function (alert) { alert.style.transition = 'opacity .3s ease, transform .3s ease'; alert.style.opacity = '0'; alert.style.transform = 'translateY(-5px)'; setTimeout(function () { alert.remove(); }, 350); }); }, 4500); </script> </body> </html>
Save
Cancel