📁 File Manager
Editing:
/home/wvl7mof7402e/public_html/chem/admin/documents.php
<?php /** * Pure Chemical Admin * Documents Manager * * File: * admin/documents.php * * Existing tables used: * documents * categories * 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'; } /* ========================================================= AUTH ========================================================= */ if (function_exists('requireAdmin')) { requireAdmin(); } /* ========================================================= HELPERS ========================================================= */ if (!function_exists('e')) { function e($value) { return htmlspecialchars( (string)$value, ENT_QUOTES, 'UTF-8' ); } } function doc_csrf_field() { if (function_exists('csrf_field')) { return csrf_field(); } if (empty($_SESSION['_csrf'])) { $_SESSION['_csrf'] = bin2hex( random_bytes(32) ); } return '<input type="hidden" name="_csrf" value="' . e($_SESSION['_csrf']) . '">'; } function doc_verify_csrf() { if (function_exists('csrf_verify')) { csrf_verify(); return; } $token = $_POST['_csrf'] ?? ''; if ( empty($_SESSION['_csrf']) || empty($token) || !hash_equals( $_SESSION['_csrf'], $token ) ) { http_response_code(419); exit('Invalid CSRF token.'); } } function doc_flash($type, $message) { $_SESSION['documents_flash'] = [ 'type' => $type, 'message' => $message ]; } function doc_get_flash() { $flash = $_SESSION['documents_flash'] ?? null; unset($_SESSION['documents_flash']); return $flash; } function doc_redirect($url) { header('Location: ' . $url); exit; } function doc_table_exists(PDO $pdo, $table) { try { $stmt = $pdo->prepare(" SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = ? "); $stmt->execute([$table]); return (bool)$stmt->fetchColumn(); } catch (Throwable $e) { return false; } } function doc_column_exists( PDO $pdo, $table, $column ) { try { $stmt = $pdo->prepare(" SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ? "); $stmt->execute([ $table, $column ]); return (bool)$stmt->fetchColumn(); } catch (Throwable $e) { return false; } } function doc_detect_column( PDO $pdo, $table, array $candidates ) { foreach ($candidates as $column) { if ( doc_column_exists( $pdo, $table, $column ) ) { return $column; } } return null; } function doc_slugify($value) { $value = trim($value); $value = strtolower($value); $value = preg_replace( '/[^a-z0-9]+/', '-', $value ); $value = trim( $value, '-' ); return $value; } function doc_date_display($date) { if (!$date) { return '—'; } $time = strtotime($date); if (!$time) { return e($date); } return date( 'd M Y', $time ); } function doc_date_input($date) { if (!$date) { return ''; } $time = strtotime($date); if (!$time) { return ''; } return date( 'Y-m-d', $time ); } function doc_format_size($bytes) { if (!$bytes || !is_numeric($bytes)) { return ''; } $bytes = (float)$bytes; if ($bytes < 1024) { return round($bytes) . ' B'; } if ($bytes < 1048576) { return round($bytes / 1024, 1) . ' KB'; } if ($bytes < 1073741824) { return round($bytes / 1048576, 1) . ' MB'; } return round( $bytes / 1073741824, 1 ) . ' GB'; } /* ========================================================= TABLE CHECK ========================================================= */ if (!doc_table_exists($pdo, 'documents')) { http_response_code(500); exit( 'documents table not found.' ); } $hasCategories = doc_table_exists( $pdo, 'categories' ); $hasMedia = doc_table_exists( $pdo, 'media' ); /* ========================================================= MEDIA COLUMN DETECTION ========================================================= */ $mediaUrlColumn = null; $mediaNameColumn = null; $mediaTitleColumn = null; $mediaMimeColumn = null; $mediaSizeColumn = null; if ($hasMedia) { $mediaUrlColumn = doc_detect_column( $pdo, 'media', [ 'url', 'file_url', 'path', 'file_path', 'media_url', 'src' ] ); $mediaNameColumn = doc_detect_column( $pdo, 'media', [ 'file_name', 'filename', 'name', 'original_name' ] ); $mediaTitleColumn = doc_detect_column( $pdo, 'media', [ 'title', 'name', 'file_name', 'filename' ] ); $mediaMimeColumn = doc_detect_column( $pdo, 'media', [ 'mime_type', 'mime', 'file_type', 'type' ] ); $mediaSizeColumn = doc_detect_column( $pdo, 'media', [ 'file_size', 'size', 'bytes' ] ); } /* ========================================================= CATEGORIES ========================================================= */ $categories = []; if ($hasCategories) { try { $categoryStmt = $pdo->query(" SELECT id, name, slug FROM categories ORDER BY name ASC, id ASC "); $categories = $categoryStmt->fetchAll(); } catch (Throwable $e) { $categories = []; } } /* ========================================================= MEDIA ========================================================= */ $mediaItems = []; if ( $hasMedia && $mediaNameColumn ) { try { $mediaSelect = " SELECT id, `" . $mediaNameColumn . "` AS media_name "; if ($mediaTitleColumn) { $mediaSelect .= ", `" . $mediaTitleColumn . "` AS media_title "; } if ($mediaUrlColumn) { $mediaSelect .= ", `" . $mediaUrlColumn . "` AS media_url "; } if ($mediaMimeColumn) { $mediaSelect .= ", `" . $mediaMimeColumn . "` AS media_mime "; } if ($mediaSizeColumn) { $mediaSelect .= ", `" . $mediaSizeColumn . "` AS media_size "; } $mediaSelect .= " FROM media ORDER BY id DESC LIMIT 500 "; $mediaStmt = $pdo->query( $mediaSelect ); $mediaItems = $mediaStmt->fetchAll(); } catch (Throwable $e) { $mediaItems = []; } } /* ========================================================= POST ACTIONS ========================================================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { doc_verify_csrf(); $action = $_POST['action'] ?? ''; /* ===================================================== SAVE DOCUMENT ===================================================== */ if ($action === 'save') { $id = (int)($_POST['id'] ?? 0); $categoryId = (int)($_POST['category_id'] ?? 0); $mediaId = (int)($_POST['media_id'] ?? 0); $title = trim($_POST['title'] ?? ''); $slug = trim($_POST['slug'] ?? ''); $documentType = trim( $_POST['document_type'] ?? '' ); $documentNumber = trim( $_POST['document_number'] ?? '' ); $version = trim( $_POST['version'] ?? '' ); $description = trim( $_POST['description'] ?? '' ); $validFrom = trim( $_POST['valid_from'] ?? '' ); $validUntil = trim( $_POST['valid_until'] ?? '' ); $status = isset($_POST['status']) ? 1 : 0; $sortOrder = max( 0, (int)( $_POST['sort_order'] ?? 0 ) ); /* ------------------------------------------------- VALIDATION ------------------------------------------------- */ if ($title === '') { doc_flash( 'error', 'Document title is required.' ); doc_redirect( 'documents.php' . ($id ? '?edit=' . $id : '?action=add') ); } if ($mediaId <= 0) { doc_flash( 'error', 'Please select a media/file for this document.' ); doc_redirect( 'documents.php' . ($id ? '?edit=' . $id : '?action=add') ); } if ($slug === '') { $slug = doc_slugify($title); } else { $slug = doc_slugify($slug); } if ($slug === '') { doc_flash( 'error', 'A valid document slug is required.' ); doc_redirect( 'documents.php' . ($id ? '?edit=' . $id : '?action=add') ); } if ( $validFrom !== '' && $validUntil !== '' && $validFrom > $validUntil ) { doc_flash( 'error', 'Valid Until date cannot be earlier than Valid From date.' ); doc_redirect( 'documents.php' . ($id ? '?edit=' . $id : '?action=add') ); } /* ------------------------------------------------- CATEGORY VALIDATION ------------------------------------------------- */ if ( $categoryId > 0 && $hasCategories ) { $stmt = $pdo->prepare(" SELECT id FROM categories WHERE id = ? LIMIT 1 "); $stmt->execute([ $categoryId ]); if (!$stmt->fetchColumn()) { doc_flash( 'error', 'Selected category does not exist.' ); doc_redirect( 'documents.php' . ($id ? '?edit=' . $id : '?action=add') ); } } else { $categoryId = null; } /* ------------------------------------------------- MEDIA VALIDATION ------------------------------------------------- */ if ($hasMedia) { $stmt = $pdo->prepare(" SELECT id FROM media WHERE id = ? LIMIT 1 "); $stmt->execute([ $mediaId ]); if (!$stmt->fetchColumn()) { doc_flash( 'error', 'Selected media file does not exist.' ); doc_redirect( 'documents.php' . ($id ? '?edit=' . $id : '?action=add') ); } } else { doc_flash( 'error', 'Media table is required to create a document.' ); doc_redirect( 'documents.php' ); } /* ------------------------------------------------- SLUG UNIQUE ------------------------------------------------- */ try { if ($id > 0) { $stmt = $pdo->prepare(" SELECT id FROM documents WHERE slug = ? AND id <> ? LIMIT 1 "); $stmt->execute([ $slug, $id ]); } else { $stmt = $pdo->prepare(" SELECT id FROM documents WHERE slug = ? LIMIT 1 "); $stmt->execute([ $slug ]); } if ($stmt->fetchColumn()) { doc_flash( 'error', 'This document slug already exists.' ); doc_redirect( 'documents.php' . ($id ? '?edit=' . $id : '?action=add') ); } /* --------------------------------------------- SAVE --------------------------------------------- */ if ($id > 0) { $stmt = $pdo->prepare(" UPDATE documents SET category_id = ?, media_id = ?, title = ?, slug = ?, document_type = ?, document_number = ?, version = ?, description = ?, valid_from = ?, valid_until = ?, status = ?, sort_order = ? WHERE id = ? "); $stmt->execute([ $categoryId, $mediaId, $title, $slug, $documentType !== '' ? $documentType : null, $documentNumber !== '' ? $documentNumber : null, $version !== '' ? $version : null, $description !== '' ? $description : null, $validFrom !== '' ? $validFrom : null, $validUntil !== '' ? $validUntil : null, $status, $sortOrder, $id ]); doc_flash( 'success', 'Document updated successfully.' ); } else { $stmt = $pdo->prepare(" INSERT INTO documents ( category_id, media_id, title, slug, document_type, document_number, version, description, valid_from, valid_until, status, sort_order ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) "); $stmt->execute([ $categoryId, $mediaId, $title, $slug, $documentType !== '' ? $documentType : null, $documentNumber !== '' ? $documentNumber : null, $version !== '' ? $version : null, $description !== '' ? $description : null, $validFrom !== '' ? $validFrom : null, $validUntil !== '' ? $validUntil : null, $status, $sortOrder ]); doc_flash( 'success', 'Document created successfully.' ); } } catch (Throwable $e) { error_log( 'Documents save error: ' . $e->getMessage() ); doc_flash( 'error', 'Unable to save document. Please try again.' ); } doc_redirect( 'documents.php' ); } /* ===================================================== DELETE ===================================================== */ if ($action === 'delete') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { doc_flash( 'error', 'Invalid document selected.' ); doc_redirect( 'documents.php' ); } try { $stmt = $pdo->prepare(" SELECT id FROM documents WHERE id = ? LIMIT 1 "); $stmt->execute([ $id ]); if (!$stmt->fetchColumn()) { doc_flash( 'error', 'Document not found.' ); doc_redirect( 'documents.php' ); } /* * Important: * We delete the document record only. * The media file itself is NOT deleted because * media may be used by other records. */ $stmt = $pdo->prepare(" DELETE FROM documents WHERE id = ? "); $stmt->execute([ $id ]); doc_flash( 'success', 'Document deleted successfully.' ); } catch (Throwable $e) { error_log( 'Documents delete error: ' . $e->getMessage() ); doc_flash( 'error', 'Unable to delete document. It may be used by another record.' ); } doc_redirect( 'documents.php' ); } /* ===================================================== QUICK STATUS ===================================================== */ if ($action === 'toggle_status') { $id = (int)($_POST['id'] ?? 0); if ($id <= 0) { doc_flash( 'error', 'Invalid document selected.' ); doc_redirect( 'documents.php' ); } try { $stmt = $pdo->prepare(" UPDATE documents SET status = CASE WHEN status = 1 THEN 0 ELSE 1 END WHERE id = ? "); $stmt->execute([ $id ]); doc_flash( 'success', 'Document status updated.' ); } catch (Throwable $e) { error_log( 'Document status error: ' . $e->getMessage() ); doc_flash( 'error', 'Unable to update document status.' ); } doc_redirect( 'documents.php' ); } } /* ========================================================= EDIT RECORD ========================================================= */ $editId = (int)($_GET['edit'] ?? 0); $editDocument = null; if ($editId > 0) { try { $stmt = $pdo->prepare(" SELECT * FROM documents WHERE id = ? LIMIT 1 "); $stmt->execute([ $editId ]); $editDocument = $stmt->fetch(); if (!$editDocument) { doc_flash( 'error', 'Document not found.' ); doc_redirect( 'documents.php' ); } } catch (Throwable $e) { doc_flash( 'error', 'Unable to load document.' ); doc_redirect( 'documents.php' ); } } $isAdd = isset($_GET['action']) && $_GET['action'] === 'add'; $isFormOpen = $isAdd || $editDocument !== null; /* ========================================================= FILTERS ========================================================= */ $q = trim($_GET['q'] ?? ''); $filterCategory = (int)( $_GET['category_id'] ?? 0 ); $filterType = trim( $_GET['document_type'] ?? '' ); $filterStatus = $_GET['status'] ?? ''; $page = max( 1, (int)( $_GET['page'] ?? 1 ) ); $perPage = 15; /* ========================================================= FILTER WHERE ========================================================= */ $where = []; $params = []; if ($q !== '') { $where[] = " ( d.title LIKE ? OR d.slug LIKE ? OR d.document_number LIKE ? OR d.version LIKE ? ) "; $like = '%' . $q . '%'; $params[] = $like; $params[] = $like; $params[] = $like; $params[] = $like; } if ($filterCategory > 0) { $where[] = "d.category_id = ?"; $params[] = $filterCategory; } if ($filterType !== '') { $where[] = "d.document_type = ?"; $params[] = $filterType; } if ( $filterStatus !== '' && in_array( $filterStatus, ['1', '0'], true ) ) { $where[] = "d.status = ?"; $params[] = (int)$filterStatus; } $whereSql = ''; if (!empty($where)) { $whereSql = 'WHERE ' . implode( ' AND ', $where ); } /* ========================================================= DOCUMENT TYPES ========================================================= */ $documentTypes = []; try { $typeStmt = $pdo->query(" SELECT DISTINCT document_type FROM documents WHERE document_type IS NOT NULL AND document_type <> '' ORDER BY document_type ASC "); $documentTypes = $typeStmt->fetchAll( PDO::FETCH_COLUMN ); } catch (Throwable $e) { $documentTypes = []; } /* ========================================================= TOTAL ========================================================= */ $totalRows = 0; try { $countSql = " SELECT COUNT(*) FROM documents d $whereSql "; $countStmt = $pdo->prepare( $countSql ); $countStmt->execute( $params ); $totalRows = (int)$countStmt->fetchColumn(); } catch (Throwable $e) { $totalRows = 0; } $totalPages = max( 1, (int)ceil( $totalRows / $perPage ) ); if ($page > $totalPages) { $page = $totalPages; } $offset = ($page - 1) * $perPage; /* ========================================================= DOCUMENT LIST ========================================================= */ $documents = []; try { if ($hasCategories) { $listSql = " SELECT d.*, c.name AS category_name, c.slug AS category_slug FROM documents d LEFT JOIN categories c ON c.id = d.category_id $whereSql ORDER BY d.sort_order ASC, d.id DESC LIMIT $perPage OFFSET $offset "; } else { $listSql = " SELECT d.* FROM documents d $whereSql ORDER BY d.sort_order ASC, d.id DESC LIMIT $perPage OFFSET $offset "; } $stmt = $pdo->prepare( $listSql ); $stmt->execute( $params ); $documents = $stmt->fetchAll(); } catch (Throwable $e) { error_log( 'Documents list error: ' . $e->getMessage() ); $documents = []; } /* ========================================================= STATS ========================================================= */ $stats = [ 'total' => 0, 'active' => 0, 'inactive' => 0, 'types' => 0 ]; try { $statsStmt = $pdo->query(" SELECT COUNT(*) AS total, SUM(status = 1) AS active_count, SUM(status = 0) AS inactive_count, COUNT( DISTINCT NULLIF( document_type, '' ) ) AS type_count FROM documents "); $statsRow = $statsStmt->fetch(); if ($statsRow) { $stats['total'] = (int)$statsRow['total']; $stats['active'] = (int)$statsRow['active_count']; $stats['inactive'] = (int)$statsRow['inactive_count']; $stats['types'] = (int)$statsRow['type_count']; } } catch (Throwable $e) { // Keep defaults. } /* ========================================================= FLASH ========================================================= */ $flash = doc_get_flash(); /* ========================================================= PAGINATION URL ========================================================= */ function doc_page_url($page) { $query = $_GET; $query['page'] = $page; return 'documents.php?' . http_build_query( $query ); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <title> Documents | Pure Chemical Admin </title> <link rel="preconnect" href="https://fonts.googleapis.com" > <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin > <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet" > <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" > <style> :root { --primary: #0f766e; --primary-dark: #115e59; --primary-soft: #ecfdf5; --dark: #0f172a; --text: #334155; --muted: #64748b; --border: #e2e8f0; --bg: #f8fafc; --white: #ffffff; --green: #059669; --green-soft: #ecfdf5; --red: #dc2626; --red-soft: #fef2f2; --blue: #2563eb; --blue-soft: #eff6ff; --orange: #d97706; --orange-soft: #fffbeb; --purple: #7c3aed; --purple-soft: #f5f3ff; --shadow-sm: 0 1px 2px rgba(15,23,42,.04); --shadow: 0 12px 35px rgba(15,23,42,.07); --radius: 18px; } * { box-sizing: border-box; } body { margin: 0; font-family: "Inter", sans-serif; background: var(--bg); color: var(--dark); } a { color: inherit; text-decoration: none; } button, input, select, textarea { font: inherit; } /* ========================================= PAGE ========================================= */ .page { min-height: 100vh; margin-left: 280px; } .main { max-width: 1800px; padding: 25px 28px 45px; } /* ========================================= TOPBAR ========================================= */ .topbar { position: sticky; top: 0; z-index: 100; height: 76px; padding: 0 28px; display: flex; align-items: center; justify-content: space-between; background: rgba(255,255,255,.95); backdrop-filter: blur(18px); border-bottom: 1px solid var(--border); box-shadow: 0 4px 20px rgba(15,23,42,.04); } .topbar-left { display: flex; align-items: center; gap: 14px; } .page-icon { width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; border-radius: 13px; color: var(--primary); background: var(--primary-soft); font-size: 18px; } .topbar-title h1 { margin: 0; font-size: 18px; font-weight: 800; letter-spacing: -.3px; } .breadcrumb { display: flex; align-items: center; gap: 7px; margin-top: 3px; color: var(--muted); font-size: 11px; } .breadcrumb i { font-size: 8px; } .topbar-right { display: flex; align-items: center; gap: 10px; } .admin-chip { display: flex; align-items: center; gap: 9px; padding: 7px 12px 7px 7px; border: 1px solid var(--border); border-radius: 12px; background: #fff; font-size: 12px; font-weight: 700; } .admin-avatar { width: 31px; height: 31px; display: flex; align-items: center; justify-content: center; border-radius: 9px; color: #fff; background: var(--primary); font-size: 12px; } /* ========================================= HEADER ========================================= */ .page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 22px; } .page-header h2 { margin: 0; font-size: 28px; font-weight: 800; letter-spacing: -.7px; } .page-header p { margin: 7px 0 0; color: var(--muted); font-size: 13px; } .header-actions { display: flex; gap: 9px; } /* ========================================= BUTTONS ========================================= */ .btn { min-height: 42px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 0 15px; border: 0; border-radius: 11px; cursor: pointer; font-size: 12px; font-weight: 800; transition: .2s ease; } .btn:hover { transform: translateY(-1px); } .btn-primary { color: #fff; background: var(--primary); box-shadow: 0 8px 20px rgba(15,118,110,.18); } .btn-primary:hover { background: var(--primary-dark); } .btn-light { color: var(--text); background: #fff; border: 1px solid var(--border); } .btn-danger { color: #fff; background: var(--red); } .btn-sm { min-height: 35px; padding: 0 11px; font-size: 11px; } /* ========================================= ALERT ========================================= */ .alert { display: flex; align-items: flex-start; gap: 11px; padding: 13px 15px; margin-bottom: 20px; border-radius: 13px; font-size: 12px; font-weight: 600; } .alert-success { color: #065f46; background: var(--green-soft); border: 1px solid #a7f3d0; } .alert-error { color: #991b1b; background: var(--red-soft); border: 1px solid #fecaca; } /* ========================================= STATS ========================================= */ .stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 15px; margin-bottom: 20px; } .stat-card { position: relative; padding: 18px; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); overflow: hidden; } .stat-card::after { content: ""; position: absolute; width: 75px; height: 75px; right: -30px; bottom: -30px; border-radius: 50%; background: rgba(15,118,110,.05); } .stat-icon { width: 39px; height: 39px; display: flex; align-items: center; justify-content: center; margin-bottom: 14px; border-radius: 11px; font-size: 15px; } .stat-card:nth-child(1) .stat-icon { color: var(--primary); background: var(--primary-soft); } .stat-card:nth-child(2) .stat-icon { color: var(--green); background: var(--green-soft); } .stat-card:nth-child(3) .stat-icon { color: var(--red); background: var(--red-soft); } .stat-card:nth-child(4) .stat-icon { color: var(--purple); background: var(--purple-soft); } .stat-label { color: var(--muted); font-size: 11px; font-weight: 700; } .stat-value { margin-top: 4px; font-size: 25px; font-weight: 800; letter-spacing: -.5px; } /* ========================================= FILTER ========================================= */ .filter-card { margin-bottom: 18px; padding: 16px; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); } .filters { display: grid; grid-template-columns: 1.5fr 1fr 1fr .8fr auto; gap: 9px; } .input-wrap { position: relative; } .input-wrap i { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: #94a3b8; font-size: 12px; } .input, .select, .textarea { width: 100%; border: 1px solid var(--border); border-radius: 10px; outline: none; color: var(--dark); background: #fff; transition: .2s ease; } .input, .select { height: 43px; padding: 0 12px; font-size: 12px; } .input.with-icon { padding-left: 37px; } .textarea { min-height: 120px; padding: 12px; resize: vertical; font-size: 12px; line-height: 1.6; } .input:focus, .select:focus, .textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(15,118,110,.08); } .filter-actions { display: flex; gap: 7px; } /* ========================================= TABLE ========================================= */ .table-card { overflow: hidden; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); } .table-head { padding: 17px 20px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); } .table-title { display: flex; align-items: center; gap: 9px; } .table-title h3 { margin: 0; font-size: 14px; font-weight: 800; } .record-count { padding: 4px 8px; color: var(--primary); background: var(--primary-soft); border-radius: 20px; font-size: 10px; font-weight: 800; } .table-wrap { width: 100%; overflow-x: auto; } table { width: 100%; min-width: 1200px; border-collapse: collapse; } th { padding: 12px 17px; text-align: left; background: #f8fafc; color: #64748b; font-size: 9px; font-weight: 800; text-transform: uppercase; letter-spacing: .7px; border-bottom: 1px solid var(--border); } td { padding: 14px 17px; color: var(--text); font-size: 12px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; } tr:last-child td { border-bottom: 0; } tbody tr { transition: .15s ease; } tbody tr:hover { background: #fbfefd; } .doc-id { color: var(--primary); font-weight: 800; } .doc-title { max-width: 240px; color: var(--dark); font-size: 12px; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .doc-slug { max-width: 240px; margin-top: 4px; color: #94a3b8; font-size: 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .category-name { color: var(--dark); font-weight: 700; } .muted { color: var(--muted); font-size: 11px; } .type-badge { display: inline-flex; padding: 5px 8px; border-radius: 7px; color: var(--purple); background: var(--purple-soft); font-size: 10px; font-weight: 800; } .version { display: inline-flex; padding: 4px 7px; border-radius: 6px; color: var(--blue); background: var(--blue-soft); font-size: 10px; font-weight: 800; } /* ========================================= STATUS ========================================= */ .status { display: inline-flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 7px; font-size: 10px; font-weight: 800; } .status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; } .status-active { color: #047857; background: var(--green-soft); } .status-active::before { background: #059669; } .status-inactive { color: #b91c1c; background: var(--red-soft); } .status-inactive::before { background: #dc2626; } /* ========================================= ACTIONS ========================================= */ .actions { display: flex; align-items: center; gap: 6px; } .icon-btn { width: 33px; height: 33px; display: inline-flex; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 9px; color: #475569; background: #fff; cursor: pointer; transition: .2s ease; } .icon-btn:hover { color: var(--primary); background: var(--primary-soft); border-color: #99f6e4; } .icon-btn.danger:hover { color: var(--red); background: var(--red-soft); border-color: #fecaca; } /* ========================================= EMPTY ========================================= */ .empty-state { padding: 65px 20px; text-align: center; } .empty-icon { width: 64px; height: 64px; display: flex; align-items: center; justify-content: center; margin: 0 auto 15px; border-radius: 18px; color: var(--primary); background: var(--primary-soft); font-size: 24px; } .empty-state h3 { margin: 0; font-size: 15px; font-weight: 800; } .empty-state p { max-width: 450px; margin: 8px auto 0; color: var(--muted); font-size: 12px; line-height: 1.6; } /* ========================================= PAGINATION ========================================= */ .pagination { padding: 15px 18px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-top: 1px solid var(--border); } .pagination-info { color: var(--muted); font-size: 11px; } .pagination-links { display: flex; gap: 5px; } .page-link { min-width: 33px; height: 33px; display: inline-flex; align-items: center; justify-content: center; padding: 0 8px; color: #475569; background: #fff; border: 1px solid var(--border); border-radius: 8px; font-size: 11px; font-weight: 700; } .page-link.active { color: #fff; background: var(--primary); border-color: var(--primary); } .page-link.disabled { opacity: .4; pointer-events: none; } /* ========================================= MODAL (FIXED FOR SIDEBAR OVERLAP) ========================================= */ .modal-backdrop { position: fixed; inset: 0; z-index: 99999; /* Updated high z-index to stay above sidebar */ display: none; align-items: center; justify-content: center; padding: 20px; background: rgba(15,23,42,.65); backdrop-filter: blur(5px); } .modal-backdrop.show { display: flex; } .modal { width: 100%; max-width: 850px; max-height: 92vh; overflow: hidden; background: #fff; border-radius: 22px; box-shadow: 0 30px 90px rgba(15,23,42,.25); animation: modalIn .2s ease; } @keyframes modalIn { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } } .modal-head { padding: 18px 20px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); } .modal-title { display: flex; align-items: center; gap: 11px; } .modal-title-icon { width: 39px; height: 39px; display: flex; align-items: center; justify-content: center; border-radius: 11px; color: var(--primary); background: var(--primary-soft); } .modal-title h3 { margin: 0; font-size: 15px; font-weight: 800; } .modal-title p { margin: 3px 0 0; color: var(--muted); font-size: 10px; } .close-modal { width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 9px; background: #fff; color: #64748b; cursor: pointer; } .modal-body { max-height: calc(92vh - 135px); overflow-y: auto; padding: 20px; } /* ========================================= FORM ========================================= */ .form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 15px; } .field.full { grid-column: 1 / -1; } .label { display: block; margin-bottom: 7px; color: #334155; font-size: 11px; font-weight: 800; } .required { color: var(--red); } .help { margin-top: 5px; color: #94a3b8; font-size: 10px; line-height: 1.5; } .checkbox-row { display: flex; align-items: center; gap: 10px; min-height: 43px; padding: 0 12px; border: 1px solid var(--border); border-radius: 10px; background: #fff; } .checkbox-row input { width: 16px; height: 16px; accent-color: var(--primary); } .checkbox-row label { color: var(--text); font-size: 12px; font-weight: 700; cursor: pointer; } .media-info { display: none; margin-top: 8px; padding: 9px 11px; border-radius: 9px; color: var(--muted); background: #f8fafc; font-size: 10px; } .modal-footer { padding: 14px 20px; display: flex; align-items: center; justify-content: space-between; gap: 10px; border-top: 1px solid var(--border); background: #fbfdff; } .footer-left { color: var(--muted); font-size: 10px; } .footer-actions { display: flex; gap: 8px; } /* ========================================= RESPONSIVE ========================================= */ @media (max-width: 1250px) { .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .filters { grid-template-columns: 1fr 1fr; } .filter-actions { grid-column: 1 / -1; } } @media (max-width: 1100px) { .page { margin-left: 0; } .main { padding: 20px; } } @media (max-width: 760px) { .topbar { height: auto; min-height: 70px; padding: 12px 16px; } .topbar-right { display: none; } .main { padding: 18px 14px 30px; } .page-header { align-items: flex-start; flex-direction: column; } .page-header h2 { font-size: 23px; } .header-actions { width: 100%; } .header-actions .btn { width: 100%; } .filters { grid-template-columns: 1fr; } .filter-actions { grid-column: auto; } .form-grid { grid-template-columns: 1fr; } .field.full { grid-column: auto; } .modal-footer { align-items: flex-start; flex-direction: column; } .footer-actions { width: 100%; } .footer-actions .btn { flex: 1; } .pagination { align-items: flex-start; flex-direction: column; } } @media (max-width: 480px) { .stats-grid { grid-template-columns: 1fr; } .page-icon { width: 38px; height: 38px; } .topbar-title h1 { font-size: 15px; } } </style> </head> <body> <?php /* |-------------------------------------------------------------------------- | SIDEBAR |-------------------------------------------------------------------------- | Sidebar intentionally included separately. | admin/header.php is NOT included. |-------------------------------------------------------------------------- */ require_once __DIR__ . '/sidebar.php'; ?> <div class="page"> <!-- ================================================= TOPBAR ================================================== --> <header class="topbar"> <div class="topbar-left"> <div class="page-icon"> <i class="fa-solid fa-file-lines"></i> </div> <div class="topbar-title"> <h1> Documents </h1> <div class="breadcrumb"> <span> Admin </span> <i class="fa-solid fa-chevron-right"></i> <span> Content </span> <i class="fa-solid fa-chevron-right"></i> <strong> Documents </strong> </div> </div> </div> <div class="topbar-right"> <div class="admin-chip"> <div class="admin-avatar"> <i class="fa-solid fa-user"></i> </div> <span> <?php echo e( $_SESSION['ADMIN_NAME'] ?? $_SESSION['admin_name'] ?? 'Administrator' ); ?> </span> </div> </div> </header> <!-- ================================================= MAIN ================================================== --> <main class="main"> <!-- PAGE HEADER --> <div class="page-header"> <div> <h2> Documents </h2> <p> Product aur website documents ko manage karein. </p> </div> <div class="header-actions"> <a href="documents.php?action=add" class="btn btn-primary" > <i class="fa-solid fa-plus"></i> Add Document </a> </div> </div> <!-- ALERT --> <?php if ($flash): ?> <div class="alert <?php echo $flash['type'] === 'success' ? 'alert-success' : 'alert-error'; ?>" > <i class="fa-solid <?php echo $flash['type'] === 'success' ? 'fa-circle-check' : 'fa-circle-exclamation'; ?>"></i> <div> <?php echo e( $flash['message'] ); ?> </div> </div> <?php endif; ?> <!-- ================================================= STATS ================================================== --> <section class="stats-grid"> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-file-lines"></i> </div> <div class="stat-label"> Total Documents </div> <div class="stat-value"> <?php echo number_format( $stats['total'] ); ?> </div> </div> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-circle-check"></i> </div> <div class="stat-label"> Active </div> <div class="stat-value"> <?php echo number_format( $stats['active'] ); ?> </div> </div> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-circle-xmark"></i> </div> <div class="stat-label"> Inactive </div> <div class="stat-value"> <?php echo number_format( $stats['inactive'] ); ?> </div> </div> <div class="stat-card"> <div class="stat-icon"> <i class="fa-solid fa-layer-group"></i> </div> <div class="stat-label"> Document Types </div> <div class="stat-value"> <?php echo number_format( $stats['types'] ); ?> </div> </div> </section> <!-- ================================================= FILTERS ================================================== --> <section class="filter-card"> <form method="get" action="documents.php" > <div class="filters"> <div class="input-wrap"> <i class="fa-solid fa-magnifying-glass"></i> <input type="text" name="q" class="input with-icon" value="<?php echo e($q); ?>" placeholder="Search title, slug, document number..." > </div> <select name="category_id" class="select" > <option value=""> All Categories </option> <?php foreach ( $categories as $category ): ?> <option value="<?php echo (int)$category['id']; ?>" <?php echo $filterCategory === (int)$category['id'] ? 'selected' : ''; ?> > <?php echo e( $category['name'] ); ?> </option> <?php endforeach; ?> </select> <select name="document_type" class="select" > <option value=""> All Types </option> <?php foreach ( $documentTypes as $type ): ?> <option value="<?php echo e($type); ?>" <?php echo $filterType === $type ? 'selected' : ''; ?> > <?php echo e($type); ?> </option> <?php endforeach; ?> </select> <select name="status" class="select" > <option value=""> All Status </option> <option value="1" <?php echo $filterStatus === '1' ? 'selected' : ''; ?> > Active </option> <option value="0" <?php echo $filterStatus === '0' ? 'selected' : ''; ?> > Inactive </option> </select> <div class="filter-actions"> <button type="submit" class="btn btn-primary" > <i class="fa-solid fa-filter"></i> Filter </button> <a href="documents.php" class="btn btn-light" > <i class="fa-solid fa-rotate-left"></i> </a> </div> </div> </form> </section> <!-- ================================================= TABLE ================================================== --> <section class="table-card"> <div class="table-head"> <div class="table-title"> <h3> All Documents </h3> <span class="record-count"> <?php echo number_format( $totalRows ); ?> </span> </div> <div class="muted"> Page <?php echo $page; ?> of <?php echo $totalPages; ?> </div> </div> <?php if (!empty($documents)): ?> <div class="table-wrap"> <table> <thead> <tr> <th> ID </th> <th> Document </th> <th> Category </th> <th> Type </th> <th> Number / Version </th> <th> Validity </th> <th> Status </th> <th> Sort </th> <th> Actions </th> </tr> </thead> <tbody> <?php foreach ( $documents as $document ): ?> <tr> <td> <div class="doc-id"> # <?php echo (int)$document['id']; ?> </div> </td> <td> <div class="doc-title" title="<?php echo e( $document['title'] ); ?>" > <?php echo e( $document['title'] ); ?> </div> <?php if ( !empty( $document['slug'] ) ): ?> <div class="doc-slug" title="<?php echo e( $document['slug'] ); ?>" > /<?php echo e( $document['slug'] ); ?> </div> <?php endif; ?> </td> <td> <?php if ( !empty( $document['category_name'] ) ): ?> <div class="category-name"> <?php echo e( $document[ 'category_name' ] ); ?> </div> <?php else: ?> <span class="muted"> Uncategorized </span> <?php endif; ?> </td> <td> <?php if ( !empty( $document[ 'document_type' ] ) ): ?> <span class="type-badge"> <?php echo e( $document[ 'document_type' ] ); ?> </span> <?php else: ?> <span class="muted"> — </span> <?php endif; ?> </td> <td> <?php if ( !empty( $document[ 'document_number' ] ) ): ?> <div class="muted"> <?php echo e( $document[ 'document_number' ] ); ?> </div> <?php endif; ?> <?php if ( !empty( $document['version'] ) ): ?> <div style="margin-top:5px;"> <span class="version"> v<?php echo e( $document[ 'version' ] ); ?> </span> </div> <?php endif; ?> <?php if ( empty( $document[ 'document_number' ] ) && empty( $document['version'] ) ): ?> <span class="muted"> — </span> <?php endif; ?> </td> <td> <div class="muted"> <?php echo doc_date_display( $document[ 'valid_from' ] ?? null ); ?> <span> → </span> <?php echo doc_date_display( $document[ 'valid_until' ] ?? null ); ?> </div> </td> <td> <?php if ( (int)$document['status'] === 1 ): ?> <span class="status status-active" > Active </span> <?php else: ?> <span class="status status-inactive" > Inactive </span> <?php endif; ?> </td> <td> <span class="muted"> <?php echo (int)$document[ 'sort_order' ]; ?> </span> </td> <td> <div class="actions"> <!-- EDIT --> <a href="documents.php?edit=<?php echo (int)$document['id']; ?>" class="icon-btn" title="Edit Document" > <i class="fa-regular fa-pen-to-square"></i> </a> <!-- STATUS --> <form method="post" style="display:inline;" > <?php echo doc_csrf_field(); ?> <input type="hidden" name="action" value="toggle_status" > <input type="hidden" name="id" value="<?php echo (int)$document['id']; ?>" > <button type="submit" class="icon-btn" title="Toggle Status" > <i class="fa-solid fa-power-off"></i> </button> </form> <!-- DELETE --> <button type="button" class="icon-btn danger" title="Delete Document" onclick="openDeleteModal( <?php echo (int)$document['id']; ?>, '<?php echo e( $document['title'] ); ?>' )" > <i class="fa-regular fa-trash-can"></i> </button> </div> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <!-- PAGINATION --> <?php if ( $totalPages > 1 ): ?> <div class="pagination"> <div class="pagination-info"> Showing <strong> <?php echo $offset + 1; ?> </strong> - <strong> <?php echo min( $offset + $perPage, $totalRows ); ?> </strong> of <strong> <?php echo number_format( $totalRows ); ?> </strong> </div> <div class="pagination-links"> <a href="<?php echo $page > 1 ? e( doc_page_url( $page - 1 ) ) : '#'; ?>" class="page-link <?php echo $page <= 1 ? 'disabled' : ''; ?>" > <i class="fa-solid fa-chevron-left"></i> </a> <?php $startPage = max( 1, $page - 2 ); $endPage = min( $totalPages, $page + 2 ); for ( $p = $startPage; $p <= $endPage; $p++ ): ?> <a href="<?php echo e( doc_page_url( $p ) ); ?>" class="page-link <?php echo $p === $page ? 'active' : ''; ?>" > <?php echo $p; ?> </a> <?php endfor; ?> <a href="<?php echo $page < $totalPages ? e( doc_page_url( $page + 1 ) ) : '#'; ?>" class="page-link <?php echo $page >= $totalPages ? 'disabled' : ''; ?>" > <i class="fa-solid fa-chevron-right"></i> </a> </div> </div> <?php endif; ?> <?php else: ?> <div class="empty-state"> <div class="empty-icon"> <i class="fa-regular fa-file-lines"></i> </div> <h3> No documents found </h3> <p> Abhi database mein koi document available nahi hai ya selected filters ke according koi result nahi mila. </p> <div style="margin-top:18px;"> <a href="documents.php?action=add" class="btn btn-primary" > <i class="fa-solid fa-plus"></i> Add First Document </a> </div> </div> <?php endif; ?> </section> </main> </div> <!-- ===================================================== ADD / EDIT MODAL ====================================================== --> <?php if ($isFormOpen): ?> <?php $formData = $editDocument ?: [ 'id' => 0, 'category_id' => '', 'media_id' => '', 'title' => '', 'slug' => '', 'document_type' => '', 'document_number' => '', 'version' => '', 'description' => '', 'valid_from' => '', 'valid_until' => '', 'status' => 1, 'sort_order' => 0 ]; ?> <div class="modal-backdrop show" id="documentModal" > <div class="modal" onclick="event.stopPropagation()" > <div class="modal-head"> <div class="modal-title"> <div class="modal-title-icon"> <i class="fa-solid fa-file-pen"></i> </div> <div> <h3> <?php echo $editDocument ? 'Edit Document' : 'Add Document'; ?> </h3> <p> Document information configure karein. </p> </div> </div> <button type="button" class="close-modal" onclick="closeDocumentModal()" > <i class="fa-solid fa-xmark"></i> </button> </div> <form method="post" action="documents.php" > <?php echo doc_csrf_field(); ?> <input type="hidden" name="action" value="save" > <input type="hidden" name="id" value="<?php echo (int)$formData['id']; ?>" > <div class="modal-body"> <div class="form-grid"> <!-- TITLE --> <div class="field full"> <label class="label"> Document Title <span class="required"> * </span> </label> <input type="text" name="title" id="documentTitle" class="input" maxlength="255" required value="<?php echo e( $formData['title'] ); ?>" placeholder="Enter document title" oninput="generateDocumentSlug()" > </div> <!-- SLUG --> <div class="field"> <label class="label"> Slug </label> <input type="text" name="slug" id="documentSlug" class="input" maxlength="255" value="<?php echo e( $formData['slug'] ); ?>" placeholder="document-slug" > <div class="help"> URL-friendly unique slug. </div> </div> <!-- CATEGORY --> <div class="field"> <label class="label"> Category </label> <select name="category_id" class="select" > <option value=""> No Category </option> <?php foreach ( $categories as $category ): ?> <option value="<?php echo (int)$category['id']; ?>" <?php echo (int)( $formData[ 'category_id' ] ?? 0 ) === (int)$category['id'] ? 'selected' : ''; ?> > <?php echo e( $category['name'] ); ?> </option> <?php endforeach; ?> </select> </div> <!-- MEDIA --> <div class="field full"> <label class="label"> Document File / Media <span class="required"> * </span> </label> <?php if ( $hasMedia && !empty($mediaItems) ): ?> <select name="media_id" id="mediaSelect" class="select" required > <option value=""> Select document file </option> <?php foreach ( $mediaItems as $media ): ?> <option value="<?php echo (int)$media['id']; ?>" data-url="<?php echo e( $media['media_url'] ?? '' ); ?>" data-mime="<?php echo e( $media['media_mime'] ?? '' ); ?>" data-size="<?php echo e( $media['media_size'] ?? '' ); ?>" <?php echo (int)( $formData[ 'media_id' ] ?? 0 ) === (int)$media['id'] ? 'selected' : ''; ?> > <?php echo e( $media['media_title'] ?? $media['media_name'] ?? ( 'Media #' . $media['id'] ) ); ?> — #<?php echo (int)$media['id']; ?> </option> <?php endforeach; ?> </select> <div class="media-info" id="mediaInfo" ></div> <div class="help"> Existing media table se document file select karein. </div> <?php else: ?> <div style=" padding:13px; border-radius:10px; background:#fff7ed; border:1px solid #fed7aa; color:#9a3412; font-size:11px; line-height:1.5; " > <i class="fa-solid fa-triangle-exclamation"></i> Media table mein selectable records nahi mile. Pehle media library mein document upload karein. </div> <input type="hidden" name="media_id" value="<?php echo (int)( $formData['media_id'] ?? 0 ); ?>" > <?php endif; ?> </div> <!-- DOCUMENT TYPE --> <div class="field"> <label class="label"> Document Type </label> <input type="text" name="document_type" class="input" maxlength="100" value="<?php echo e( $formData[ 'document_type' ] ); ?>" placeholder="e.g. SDS, TDS, COA" > <?php if ( !empty($documentTypes) ): ?> <div class="help"> Existing types: <?php echo e( implode( ', ', array_slice( $documentTypes, 0, 6 ) ) ); ?> </div> <?php endif; ?> </div> <!-- DOCUMENT NUMBER --> <div class="field"> <label class="label"> Document Number </label> <input type="text" name="document_number" class="input" maxlength="150" value="<?php echo e( $formData[ 'document_number' ] ); ?>" placeholder="Document reference number" > </div> <!-- VERSION --> <div class="field"> <label class="label"> Version </label> <input type="text" name="version" class="input" maxlength="50" value="<?php echo e( $formData[ 'version' ] ); ?>" placeholder="e.g. 1.0" > </div> <!-- SORT --> <div class="field"> <label class="label"> Sort Order </label> <input type="number" name="sort_order" class="input" min="0" value="<?php echo (int)( $formData[ 'sort_order' ] ); ?>" placeholder="0" > <div class="help"> Lower value appears first. </div> </div> <!-- VALID FROM --> <div class="field"> <label class="label"> Valid From </label> <input type="date" name="valid_from" class="input" value="<?php echo e( doc_date_input( $formData[ 'valid_from' ] ) ); ?>" > </div> <!-- VALID UNTIL --> <div class="field"> <label class="label"> Valid Until </label> <input type="date" name="valid_until" class="input" value="<?php echo e( doc_date_input( $formData[ 'valid_until' ] ) ); ?>" > </div> <!-- DESCRIPTION --> <div class="field full"> <label class="label"> Description </label> <textarea name="description" class="textarea" placeholder="Document description..." ><?php echo e( $formData[ 'description' ] ); ?></textarea> </div> <!-- STATUS --> <div class="field full"> <div class="checkbox-row"> <input type="checkbox" name="status" id="documentStatus" value="1" <?php echo (int)( $formData['status'] ) === 1 ? 'checked' : ''; ?> > <label for="documentStatus" > Document is Active </label> </div> </div> </div> </div> <div class="modal-footer"> <div class="footer-left"> <i class="fa-solid fa-circle-info"></i> Fields marked with <span style="color:#dc2626;"> * </span> are required. </div> <div class="footer-actions"> <button type="button" class="btn btn-light" onclick="closeDocumentModal()" > Cancel </button> <button type="submit" class="btn btn-primary" > <i class="fa-solid fa-check"></i> <?php echo $editDocument ? 'Update Document' : 'Save Document'; ?> </button> </div> </div> </form> </div> </div> <?php endif; ?> <!-- ===================================================== DELETE MODAL ====================================================== --> <div class="modal-backdrop" id="deleteModal" > <div class="modal" onclick="event.stopPropagation()" style="max-width:440px;" > <div class="modal-head"> <div class="modal-title"> <div class="modal-title-icon" style=" color:#dc2626; background:#fef2f2; " > <i class="fa-regular fa-trash-can"></i> </div> <div> <h3> Delete Document </h3> <p> This action cannot be undone. </p> </div> </div> <button type="button" class="close-modal" onclick="closeDeleteModal()" > <i class="fa-solid fa-xmark"></i> </button> </div> <form method="post" action="documents.php" > <?php echo doc_csrf_field(); ?> <input type="hidden" name="action" value="delete" > <input type="hidden" name="id" id="deleteDocumentId" value="" > <div class="modal-body"> <div style=" padding:14px; border-radius:12px; background:#fef2f2; border:1px solid #fecaca; color:#991b1b; font-size:12px; line-height:1.6; " > <i class="fa-solid fa-triangle-exclamation" style="margin-right:5px;" ></i> Are you sure you want to permanently delete <strong id="deleteDocumentName"> this document </strong>? <br><br> Document record delete hoga. Linked media file ko delete nahi kiya jayega. </div> </div> <div class="modal-footer"> <div></div> <div class="footer-actions"> <button type="button" class="btn btn-light" onclick="closeDeleteModal()" > Cancel </button> <button type="submit" class="btn btn-danger" > <i class="fa-regular fa-trash-can"></i> Delete </button> </div> </div> </form> </div> </div> <script> /* ===================================================== DOCUMENT MODAL ===================================================== */ function closeDocumentModal() { const modal = document.getElementById( 'documentModal' ); if (!modal) { return; } /* * If opened through edit/add URL, * return to clean documents page. */ window.location.href = 'documents.php'; } /* ===================================================== DELETE MODAL ===================================================== */ function openDeleteModal( id, title ) { const modal = document.getElementById( 'deleteModal' ); const idInput = document.getElementById( 'deleteDocumentId' ); const name = document.getElementById( 'deleteDocumentName' ); if (!modal || !idInput) { return; } idInput.value = id; if (name) { name.textContent = '"' + title + '"'; } modal.classList.add( 'show' ); } function closeDeleteModal() { const modal = document.getElementById( 'deleteModal' ); if (modal) { modal.classList.remove( 'show' ); } } /* ===================================================== SLUG GENERATOR ===================================================== */ let slugManuallyChanged = false; const slugInput = document.getElementById( 'documentSlug' ); if (slugInput) { slugInput.addEventListener( 'input', function() { slugManuallyChanged = true; } ); } function generateDocumentSlug() { const title = document.getElementById( 'documentTitle' ); const slug = document.getElementById( 'documentSlug' ); if (!title || !slug) { return; } /* * On add form, generate slug automatically. * On edit, user can still manually change it. */ if ( !slugManuallyChanged && !slug.value.trim() ) { let value = title.value .toLowerCase() .trim() .replace( /[^a-z0-9]+/g, '-' ) .replace( /^-+|-+$/g, '' ); slug.value = value; } } /* ===================================================== MEDIA INFO ===================================================== */ const mediaSelect = document.getElementById( 'mediaSelect' ); const mediaInfo = document.getElementById( 'mediaInfo' ); function updateMediaInfo() { if ( !mediaSelect || !mediaInfo ) { return; } const option = mediaSelect.options[ mediaSelect.selectedIndex ]; if ( !option || !option.value ) { mediaInfo.style.display = 'none'; mediaInfo.innerHTML = ''; return; } const url = option.getAttribute( 'data-url' ) || ''; const mime = option.getAttribute( 'data-mime' ) || ''; const size = option.getAttribute( 'data-size' ) || ''; let html = '<strong>Selected:</strong> ' + option.textContent.trim(); if (mime) { html += ' • ' + mime; } if (size) { html += ' • ' + size + ' bytes'; } if (url) { html += '<br><span style="color:#0f766e;">' + url + '</span>'; } mediaInfo.innerHTML = html; mediaInfo.style.display = 'block'; } if (mediaSelect) { mediaSelect.addEventListener( 'change', updateMediaInfo ); updateMediaInfo(); } /* ===================================================== ESC KEY ===================================================== */ document.addEventListener( 'keydown', function(event) { if ( event.key !== 'Escape' ) { return; } closeDeleteModal(); const documentModal = document.getElementById( 'documentModal' ); if (documentModal) { window.location.href = 'documents.php'; } } ); /* ===================================================== DELETE BACKDROP ===================================================== */ const deleteModal = document.getElementById( 'deleteModal' ); if (deleteModal) { deleteModal.addEventListener( 'click', function(event) { if ( event.target === deleteModal ) { closeDeleteModal(); } } ); } </script> </body> </html>
Save
Cancel