39301-vm/includes/admin_layout.php
2026-04-09 17:56:09 +00:00

157 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/library.php';
function admin_render_header(string $title, string $activePage = 'dashboard'): void {
library_bootstrap();
$lang = library_get_language();
$dir = $lang === 'ar' ? 'rtl' : 'ltr';
$isRtl = $lang === 'ar';
// Get flashes and clear them
$flashes = [];
if (isset($_SESSION['library_flash'])) {
$flashes = $_SESSION['library_flash'];
unset($_SESSION['library_flash']);
}
?>
<!doctype html>
<html lang="<?= $lang ?>" dir="<?= $dir ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= h($title) ?> · <?= library_trans('admin_panel') ?></title>
<?php if ($isRtl): ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" integrity="sha384-dpuaG1suU0eT09tx5plTaGMLBsfDLzUCCUXOY2j/LSvXYuG6Bqs43ALlhIqAJVRb" crossorigin="anonymous">
<?php else: ?>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<?php endif; ?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<style>
.admin-sidebar {
width: 260px;
height: 100vh;
position: sticky;
top: 0;
background: #fff;
border-<?= $isRtl ? 'left' : 'right' ?>: 1px solid #dee2e6;
padding: 20px;
}
.nav-link {
color: #333;
margin-bottom: 0.25rem;
}
.nav-link:hover {
background-color: #f8f9fa;
}
.nav-link.active {
background-color: #e9ecef;
color: #0d6efd !important;
font-weight: 500;
}
</style>
</head>
<body class="bg-light">
<div class="d-flex">
<aside class="admin-sidebar d-flex flex-column">
<div class="d-flex align-items-center justify-content-between mb-4">
<h4 class="mb-0 text-primary fw-bold"><i class="bi bi-grid-fill me-2"></i><?= library_trans('admin_panel') ?></h4>
</div>
<div class="mb-3">
<a href="?lang=<?= library_trans('switch_lang_code') ?>" class="btn btn-sm btn-outline-secondary w-100">
<i class="bi bi-translate me-2"></i><?= library_trans('switch_lang') ?>
</a>
</div>
<nav class="nav flex-column flex-grow-1">
<a class="nav-link rounded <?= $activePage === 'dashboard' ? 'active' : '' ?>" href="/admin.php">
<i class="bi bi-speedometer2 me-2"></i> <?= library_trans('dashboard') ?>
</a>
<a class="nav-link rounded <?= $activePage === 'documents' ? 'active' : '' ?>" href="/admin_documents.php">
<i class="bi bi-folder2-open me-2"></i> <?= library_trans('material_entry') ?>
</a>
<div class="my-2 border-top"></div>
<a class="nav-link rounded <?= $activePage === 'categories' ? 'active' : '' ?>" href="/admin_categories.php">
<i class="bi bi-tags me-2"></i> <?= library_trans('categories') ?>
</a>
<a class="nav-link rounded <?= $activePage === 'subcategories' ? 'active' : '' ?>" href="/admin_subcategories.php">
<i class="bi bi-diagram-3 me-2"></i> <?= library_trans('subcategories') ?>
</a>
<a class="nav-link rounded <?= $activePage === 'types' ? 'active' : '' ?>" href="/admin_types.php">
<i class="bi bi-file-earmark-text me-2"></i> <?= library_trans('types') ?>
</a>
</nav>
<div class="mt-auto pt-3 border-top">
<a class="nav-link text-secondary rounded" href="/index.php">
<i class="bi bi-box-arrow-right me-2"></i> <?= library_trans('return_to_site') ?>
</a>
</div>
</aside>
<main class="flex-grow-1 p-4" style="min-width: 0;">
<header class="mb-4 border-bottom pb-2">
<h2 class="h3"><?= h($title) ?></h2>
</header>
<?php foreach ($flashes as $flash): ?>
<div class="alert alert-<?= $flash['type'] === 'error' ? 'danger' : 'success' ?> alert-dismissible fade show shadow-sm" role="alert">
<?= h($flash['message']) ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endforeach; ?>
<?php
}
function admin_render_footer(): void {
?>
</main>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Shared Admin JS - Translation Helper
async function translateText(sourceId, targetId, targetLang) {
const source = document.getElementById(sourceId);
const target = document.getElementById(targetId);
const text = source.value.trim();
if (!text) {
alert(<?= json_encode(library_trans('translation_enter_text')) ?>);
return;
}
const originalPlaceholder = target.placeholder;
target.placeholder = <?= json_encode(library_trans('translating')) ?>;
const originalOpacity = target.style.opacity;
target.style.opacity = '0.7';
try {
const response = await fetch('/api/translate.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: text, target_lang: targetLang })
});
if (!response.ok) throw new Error(<?= json_encode(library_trans('translation_failed')) ?>);
const data = await response.json();
if (data.translation) {
target.value = data.translation;
} else if (data.error) {
alert(<?= json_encode(library_trans('translation_error_prefix')) ?> + ' ' + data.error);
}
} catch (e) {
console.error(e);
alert(<?= json_encode(library_trans('translation_failed')) ?>);
} finally {
target.placeholder = originalPlaceholder;
target.style.opacity = originalOpacity;
}
}
</script>
</body>
</html>
<?php
}
?>