390 lines
17 KiB
PHP
390 lines
17 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/includes/layout.php';
|
|
|
|
library_bootstrap();
|
|
|
|
$errors = [];
|
|
$successMessage = '';
|
|
|
|
// Handle POST request
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
try {
|
|
if ($action === 'upload_document') {
|
|
$documentId = library_create_document($_POST, $_FILES['document_file'] ?? []);
|
|
library_set_flash('success', 'Document uploaded successfully.');
|
|
header('Location: /admin.php?created=' . $documentId . '#catalog-manager');
|
|
exit;
|
|
} elseif ($action === 'create_category') {
|
|
$nameEn = trim($_POST['name_en'] ?? '');
|
|
$nameAr = trim($_POST['name_ar'] ?? '');
|
|
if (!$nameEn || !$nameAr) {
|
|
throw new RuntimeException('Both English and Arabic names are required for Category.');
|
|
}
|
|
library_create_category($nameEn, $nameAr);
|
|
library_set_flash('success', 'Category created successfully.');
|
|
header('Location: /admin.php');
|
|
exit;
|
|
} elseif ($action === 'create_subcategory') {
|
|
$catId = (int)($_POST['category_id'] ?? 0);
|
|
$nameEn = trim($_POST['name_en'] ?? '');
|
|
$nameAr = trim($_POST['name_ar'] ?? '');
|
|
if (!$catId || !$nameEn || !$nameAr) {
|
|
throw new RuntimeException('Category, English name, and Arabic name are required.');
|
|
}
|
|
library_create_subcategory($catId, $nameEn, $nameAr);
|
|
library_set_flash('success', 'Subcategory created successfully.');
|
|
header('Location: /admin.php');
|
|
exit;
|
|
}
|
|
} catch (Throwable $exception) {
|
|
$errors[] = $exception->getMessage();
|
|
}
|
|
}
|
|
|
|
$documents = library_fetch_documents(false, []);
|
|
$metrics = library_catalog_metrics();
|
|
$categories = library_get_categories();
|
|
// Fetch all subcategories to pass to JS for filtering
|
|
$allSubcategories = library_get_subcategories(null);
|
|
|
|
$flashes = library_get_flashes();
|
|
?>
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Admin Studio · Nabd Library</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
|
<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-right: 1px solid #dee2e6;
|
|
padding: 20px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body class="bg-light">
|
|
<div class="d-flex">
|
|
<aside class="admin-sidebar">
|
|
<h4 class="mb-4">Admin Dashboard</h4>
|
|
<nav class="nav flex-column gap-2">
|
|
<a class="nav-link text-dark bg-light rounded" href="/admin.php">Catalog Manager</a>
|
|
<a class="nav-link text-secondary" href="/index.php">Return to site</a>
|
|
</nav>
|
|
|
|
<hr class="my-4">
|
|
<h6 class="text-uppercase text-muted small">Metadata</h6>
|
|
<div class="d-grid gap-2">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm text-start" data-bs-toggle="modal" data-bs-target="#categoryModal">
|
|
+ New Category
|
|
</button>
|
|
<button type="button" class="btn btn-outline-secondary btn-sm text-start" data-bs-toggle="modal" data-bs-target="#subcategoryModal">
|
|
+ New Subcategory
|
|
</button>
|
|
</div>
|
|
</aside>
|
|
<main class="flex-grow-1 p-4">
|
|
<h1>Catalog Manager</h1>
|
|
<p class="text-secondary">Upload manuscripts and manage permissions.</p>
|
|
|
|
<?php foreach ($flashes as $flash): ?>
|
|
<div class="alert alert-<?= $flash['type'] === 'error' ? 'danger' : 'success' ?> alert-dismissible fade show" role="alert">
|
|
<?= h($flash['message']) ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<?php if ($errors): ?>
|
|
<div class="alert alert-danger"><?= h(implode(' ', $errors)) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="post" action="/admin.php#catalog-manager" enctype="multipart/form-data" class="panel border p-4 bg-white rounded shadow-sm">
|
|
<input type="hidden" name="action" value="upload_document">
|
|
<div class="row g-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label">Title (English)</label>
|
|
<input class="form-control" name="title_en" type="text" required>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<label class="form-label">Title (Arabic)</label>
|
|
<input class="form-control" name="title_ar" type="text" dir="rtl">
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<label class="form-label">Category</label>
|
|
<div class="input-group">
|
|
<select class="form-select" name="category_id" id="docCategorySelect" required>
|
|
<option value="">Select Category...</option>
|
|
<?php foreach ($categories as $cat): ?>
|
|
<option value="<?= $cat['id'] ?>">
|
|
<?= h($cat['name_en']) ?> / <?= h($cat['name_ar']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<button class="btn btn-outline-secondary" type="button" data-bs-toggle="modal" data-bs-target="#categoryModal">+</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<label class="form-label">Sub Category</label>
|
|
<div class="input-group">
|
|
<select class="form-select" name="subcategory_id" id="docSubcategorySelect">
|
|
<option value="">Select Category First</option>
|
|
</select>
|
|
<button class="btn btn-outline-secondary" type="button" data-bs-toggle="modal" data-bs-target="#subcategoryModal">+</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-12">
|
|
<label class="form-label">Visibility</label>
|
|
<select class="form-select" name="visibility">
|
|
<option value="public">Public</option>
|
|
<option value="private">Private</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-12">
|
|
<div class="form-check form-check-inline">
|
|
<input class="form-check-input" type="checkbox" name="allow_download" value="1">
|
|
<label class="form-check-label">Allow Download</label>
|
|
</div>
|
|
<div class="form-check form-check-inline">
|
|
<input class="form-check-input" type="checkbox" name="allow_print" value="1">
|
|
<label class="form-check-label">Allow Print</label>
|
|
</div>
|
|
<div class="form-check form-check-inline">
|
|
<input class="form-check-input" type="checkbox" name="allow_copy" value="1">
|
|
<label class="form-check-label">Allow Copy</label>
|
|
</div>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label">Document file</label>
|
|
<input class="form-control" name="document_file" type="file" required>
|
|
</div>
|
|
</div>
|
|
<button class="btn btn-primary mt-3" type="submit">Upload manuscript</button>
|
|
</form>
|
|
|
|
<h3 class="mt-5 mb-3">Recent Documents</h3>
|
|
<div class="table-responsive bg-white border rounded">
|
|
<table class="table mb-0">
|
|
<thead class="table-light">
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Title</th>
|
|
<th>Category</th>
|
|
<th>Visibility</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($documents)): ?>
|
|
<tr><td colspan="5" class="text-center py-4 text-muted">No documents found.</td></tr>
|
|
<?php else: ?>
|
|
<?php foreach (array_slice($documents, 0, 10) as $doc): ?>
|
|
<tr>
|
|
<td><?= $doc['id'] ?></td>
|
|
<td>
|
|
<?= h($doc['title_en']) ?><br>
|
|
<small class="text-muted"><?= h($doc['title_ar']) ?></small>
|
|
</td>
|
|
<td>
|
|
<?= h($doc['cat_en'] ?? $doc['category']) ?>
|
|
<?php if (!empty($doc['sub_en'])): ?>
|
|
<small class="text-muted"> > <?= h($doc['sub_en']) ?></small>
|
|
<?php elseif (!empty($doc['sub_category'])): ?>
|
|
<small class="text-muted"> > <?= h($doc['sub_category']) ?></small>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<span class="badge bg-<?= $doc['visibility'] === 'public' ? 'success' : 'secondary' ?>">
|
|
<?= h($doc['visibility']) ?>
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<a href="/document.php?id=<?= $doc['id'] ?>" class="btn btn-sm btn-outline-primary" target="_blank">View</a>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
|
|
<!-- Category Modal -->
|
|
<div class="modal fade" id="categoryModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="post" action="/admin.php">
|
|
<input type="hidden" name="action" value="create_category">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Add New Category</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">Name (English)</label>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" name="name_en" id="cat_name_en" required>
|
|
<button class="btn btn-outline-secondary" type="button" onclick="translateText('cat_name_en', 'cat_name_ar', 'Arabic')" title="Translate to Arabic">
|
|
<i class="bi bi-translate"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Name (Arabic)</label>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" name="name_ar" id="cat_name_ar" dir="rtl" required>
|
|
<button class="btn btn-outline-secondary" type="button" onclick="translateText('cat_name_ar', 'cat_name_en', 'English')" title="Translate to English">
|
|
<i class="bi bi-translate"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Subcategory Modal -->
|
|
<div class="modal fade" id="subcategoryModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="post" action="/admin.php">
|
|
<input type="hidden" name="action" value="create_subcategory">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Add New Subcategory</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">Parent Category</label>
|
|
<select class="form-select" name="category_id" required>
|
|
<option value="">Select...</option>
|
|
<?php foreach ($categories as $cat): ?>
|
|
<option value="<?= $cat['id'] ?>"><?= h($cat['name_en']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Name (English)</label>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" name="name_en" id="sub_name_en" required>
|
|
<button class="btn btn-outline-secondary" type="button" onclick="translateText('sub_name_en', 'sub_name_ar', 'Arabic')" title="Translate to Arabic">
|
|
<i class="bi bi-translate"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Name (Arabic)</label>
|
|
<div class="input-group">
|
|
<input type="text" class="form-control" name="name_ar" id="sub_name_ar" dir="rtl" required>
|
|
<button class="btn btn-outline-secondary" type="button" onclick="translateText('sub_name_ar', 'sub_name_en', 'English')" title="Translate to English">
|
|
<i class="bi bi-translate"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary">Save</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script>
|
|
// Pass PHP data to JS safely
|
|
const allSubcategories = <?= json_encode($allSubcategories) ?>;
|
|
|
|
const catSelect = document.getElementById('docCategorySelect');
|
|
const subSelect = document.getElementById('docSubcategorySelect');
|
|
|
|
function updateSubcategories() {
|
|
const selectedCatId = catSelect.value;
|
|
subSelect.innerHTML = '<option value="">Select Subcategory...</option>';
|
|
|
|
if (!selectedCatId) {
|
|
return;
|
|
}
|
|
|
|
// Loose equality check for ID comparison
|
|
const filtered = allSubcategories.filter(sub => sub.category_id == selectedCatId);
|
|
|
|
filtered.forEach(sub => {
|
|
const option = document.createElement('option');
|
|
option.value = sub.id;
|
|
// Use only English name for now in the dropdown as per "One language" request,
|
|
// or maybe keep bilingual if it helps selection.
|
|
// Let's keep it bilingual in the dropdown for clarity as before,
|
|
// since the user request was specifically about the modal form UI.
|
|
option.textContent = sub.name_en + ' / ' + sub.name_ar;
|
|
subSelect.appendChild(option);
|
|
});
|
|
}
|
|
|
|
if (catSelect && subSelect) {
|
|
catSelect.addEventListener('change', updateSubcategories);
|
|
updateSubcategories();
|
|
}
|
|
|
|
async function translateText(sourceId, targetId, lang) {
|
|
const source = document.getElementById(sourceId);
|
|
const target = document.getElementById(targetId);
|
|
const text = source.value.trim();
|
|
|
|
if (!text) {
|
|
alert('Please enter text to translate.');
|
|
return;
|
|
}
|
|
|
|
// Show loading state
|
|
const originalPlaceholder = target.placeholder;
|
|
target.placeholder = '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: lang })
|
|
});
|
|
|
|
if (!response.ok) throw new Error('Translation failed');
|
|
|
|
const data = await response.json();
|
|
if (data.translation) {
|
|
target.value = data.translation;
|
|
} else if (data.error) {
|
|
alert('Translation error: ' + data.error);
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
alert('Translation failed. Please try again.');
|
|
} finally {
|
|
target.placeholder = originalPlaceholder;
|
|
target.style.opacity = originalOpacity;
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|