add categories
This commit is contained in:
parent
d9a8b246f2
commit
f72c5f11b8
220
admin.php
220
admin.php
@ -12,6 +12,7 @@ $successMessage = '';
|
||||
// Handle POST request
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$action = $_POST['action'] ?? '';
|
||||
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
|
||||
|
||||
try {
|
||||
if ($action === 'upload_document') {
|
||||
@ -29,6 +30,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
library_set_flash('success', 'Category created successfully.');
|
||||
header('Location: /admin.php');
|
||||
exit;
|
||||
} elseif ($action === 'update_category') {
|
||||
$nameEn = trim($_POST['name_en'] ?? '');
|
||||
$nameAr = trim($_POST['name_ar'] ?? '');
|
||||
if (!$id || !$nameEn || !$nameAr) {
|
||||
throw new RuntimeException('ID, English name, and Arabic name are required.');
|
||||
}
|
||||
library_update_category($id, $nameEn, $nameAr);
|
||||
library_set_flash('success', 'Category updated successfully.');
|
||||
header('Location: /admin.php');
|
||||
exit;
|
||||
} elseif ($action === 'delete_category') {
|
||||
if (!$id) {
|
||||
throw new RuntimeException('Invalid Category ID.');
|
||||
}
|
||||
library_delete_category($id);
|
||||
library_set_flash('success', 'Category deleted successfully.');
|
||||
header('Location: /admin.php');
|
||||
exit;
|
||||
} elseif ($action === 'create_subcategory') {
|
||||
$catId = (int)($_POST['category_id'] ?? 0);
|
||||
$nameEn = trim($_POST['name_en'] ?? '');
|
||||
@ -40,6 +59,25 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
library_set_flash('success', 'Subcategory created successfully.');
|
||||
header('Location: /admin.php');
|
||||
exit;
|
||||
} elseif ($action === 'update_subcategory') {
|
||||
$catId = (int)($_POST['category_id'] ?? 0);
|
||||
$nameEn = trim($_POST['name_en'] ?? '');
|
||||
$nameAr = trim($_POST['name_ar'] ?? '');
|
||||
if (!$id || !$catId || !$nameEn || !$nameAr) {
|
||||
throw new RuntimeException('ID, Category, English name, and Arabic name are required.');
|
||||
}
|
||||
library_update_subcategory($id, $catId, $nameEn, $nameAr);
|
||||
library_set_flash('success', 'Subcategory updated successfully.');
|
||||
header('Location: /admin.php');
|
||||
exit;
|
||||
} elseif ($action === 'delete_subcategory') {
|
||||
if (!$id) {
|
||||
throw new RuntimeException('Invalid Subcategory ID.');
|
||||
}
|
||||
library_delete_subcategory($id);
|
||||
library_set_flash('success', 'Subcategory deleted successfully.');
|
||||
header('Location: /admin.php');
|
||||
exit;
|
||||
}
|
||||
} catch (Throwable $exception) {
|
||||
$errors[] = $exception->getMessage();
|
||||
@ -86,10 +124,10 @@ $flashes = library_get_flashes();
|
||||
<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">
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm text-start" onclick="openCreateCategoryModal()">
|
||||
+ New Category
|
||||
</button>
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm text-start" data-bs-toggle="modal" data-bs-target="#subcategoryModal">
|
||||
<button type="button" class="btn btn-outline-secondary btn-sm text-start" onclick="openCreateSubcategoryModal()">
|
||||
+ New Subcategory
|
||||
</button>
|
||||
</div>
|
||||
@ -109,8 +147,9 @@ $flashes = library_get_flashes();
|
||||
<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">
|
||||
<form method="post" action="/admin.php#catalog-manager" enctype="multipart/form-data" class="panel border p-4 bg-white rounded shadow-sm mb-5">
|
||||
<input type="hidden" name="action" value="upload_document">
|
||||
<h4 class="mb-3">Upload New Document</h4>
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Title (English)</label>
|
||||
@ -132,7 +171,7 @@ $flashes = library_get_flashes();
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<button class="btn btn-outline-secondary" type="button" data-bs-toggle="modal" data-bs-target="#categoryModal">+</button>
|
||||
<button class="btn btn-outline-secondary" type="button" onclick="openCreateCategoryModal()">+</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -142,7 +181,7 @@ $flashes = library_get_flashes();
|
||||
<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>
|
||||
<button class="btn btn-outline-secondary" type="button" onclick="openCreateSubcategoryModal()">+</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -175,8 +214,8 @@ $flashes = library_get_flashes();
|
||||
<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">
|
||||
<h3 class="mb-3">Recent Documents</h3>
|
||||
<div class="table-responsive bg-white border rounded mb-5">
|
||||
<table class="table mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
@ -220,6 +259,88 @@ $flashes = library_get_flashes();
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h3>Categories</h3>
|
||||
<button class="btn btn-sm btn-outline-primary" onclick="openCreateCategoryModal()">+ Add New</button>
|
||||
</div>
|
||||
<div class="table-responsive bg-white border rounded">
|
||||
<table class="table mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th class="text-end">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (empty($categories)): ?>
|
||||
<tr><td colspan="2" class="text-center py-4 text-muted">No categories.</td></tr>
|
||||
<?php else: ?>
|
||||
<?php foreach ($categories as $cat): ?>
|
||||
<tr>
|
||||
<td>
|
||||
<?= h($cat['name_en']) ?><br>
|
||||
<small class="text-muted"><?= h($cat['name_ar']) ?></small>
|
||||
</td>
|
||||
<td class="text-end">
|
||||
<button class="btn btn-sm btn-outline-secondary" onclick="openEditCategoryModal(<?= $cat['id'] ?>, '<?= h($cat['name_en']) ?>', '<?= h($cat['name_ar']) ?>')">Edit</button>
|
||||
<button class="btn btn-sm btn-outline-danger" onclick="deleteCategory(<?= $cat['id'] ?>)">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h3>Subcategories</h3>
|
||||
<button class="btn btn-sm btn-outline-primary" onclick="openCreateSubcategoryModal()">+ Add New</button>
|
||||
</div>
|
||||
<div class="table-responsive bg-white border rounded">
|
||||
<table class="table mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Parent</th>
|
||||
<th class="text-end">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (empty($allSubcategories)): ?>
|
||||
<tr><td colspan="3" class="text-center py-4 text-muted">No subcategories.</td></tr>
|
||||
<?php else: ?>
|
||||
<?php foreach ($allSubcategories as $sub):
|
||||
$parentName = 'Unknown';
|
||||
foreach ($categories as $c) {
|
||||
if ($c['id'] == $sub['category_id']) {
|
||||
$parentName = $c['name_en'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<tr>
|
||||
<td>
|
||||
<?= h($sub['name_en']) ?><br>
|
||||
<small class="text-muted"><?= h($sub['name_ar']) ?></small>
|
||||
</td>
|
||||
<td><small><?= h($parentName) ?></small></td>
|
||||
<td class="text-end">
|
||||
<button class="btn btn-sm btn-outline-secondary" onclick="openEditSubcategoryModal(<?= $sub['id'] ?>, <?= $sub['category_id'] ?>, '<?= h($sub['name_en']) ?>', '<?= h($sub['name_ar']) ?>')">Edit</button>
|
||||
<button class="btn btn-sm btn-outline-danger" onclick="deleteSubcategory(<?= $sub['id'] ?>)">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</main>
|
||||
</div>
|
||||
|
||||
@ -227,10 +348,11 @@ $flashes = library_get_flashes();
|
||||
<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">
|
||||
<form method="post" action="/admin.php" id="categoryForm">
|
||||
<input type="hidden" name="action" id="cat_action" value="create_category">
|
||||
<input type="hidden" name="id" id="cat_id" value="">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Add New Category</h5>
|
||||
<h5 class="modal-title" id="categoryModalTitle">Add New Category</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
@ -266,16 +388,17 @@ $flashes = library_get_flashes();
|
||||
<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">
|
||||
<form method="post" action="/admin.php" id="subcategoryForm">
|
||||
<input type="hidden" name="action" id="sub_action" value="create_subcategory">
|
||||
<input type="hidden" name="id" id="sub_id" value="">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Add New Subcategory</h5>
|
||||
<h5 class="modal-title" id="subcategoryModalTitle">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>
|
||||
<select class="form-select" name="category_id" id="sub_category_id" required>
|
||||
<option value="">Select...</option>
|
||||
<?php foreach ($categories as $cat): ?>
|
||||
<option value="<?= $cat['id'] ?>"><?= h($cat['name_en']) ?></option>
|
||||
@ -310,6 +433,12 @@ $flashes = library_get_flashes();
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Delete Confirmation Form (Hidden) -->
|
||||
<form method="post" action="/admin.php" id="deleteForm">
|
||||
<input type="hidden" name="action" id="deleteAction" value="">
|
||||
<input type="hidden" name="id" id="deleteId" value="">
|
||||
</form>
|
||||
|
||||
<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
|
||||
@ -317,6 +446,9 @@ $flashes = library_get_flashes();
|
||||
|
||||
const catSelect = document.getElementById('docCategorySelect');
|
||||
const subSelect = document.getElementById('docSubcategorySelect');
|
||||
|
||||
const categoryModal = new bootstrap.Modal(document.getElementById('categoryModal'));
|
||||
const subcategoryModal = new bootstrap.Modal(document.getElementById('subcategoryModal'));
|
||||
|
||||
function updateSubcategories() {
|
||||
const selectedCatId = catSelect.value;
|
||||
@ -332,10 +464,6 @@ $flashes = library_get_flashes();
|
||||
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);
|
||||
});
|
||||
@ -346,6 +474,61 @@ $flashes = library_get_flashes();
|
||||
updateSubcategories();
|
||||
}
|
||||
|
||||
// Modal Helpers
|
||||
function openCreateCategoryModal() {
|
||||
document.getElementById('categoryModalTitle').innerText = 'Add New Category';
|
||||
document.getElementById('cat_action').value = 'create_category';
|
||||
document.getElementById('cat_id').value = '';
|
||||
document.getElementById('cat_name_en').value = '';
|
||||
document.getElementById('cat_name_ar').value = '';
|
||||
categoryModal.show();
|
||||
}
|
||||
|
||||
function openEditCategoryModal(id, nameEn, nameAr) {
|
||||
document.getElementById('categoryModalTitle').innerText = 'Edit Category';
|
||||
document.getElementById('cat_action').value = 'update_category';
|
||||
document.getElementById('cat_id').value = id;
|
||||
document.getElementById('cat_name_en').value = nameEn;
|
||||
document.getElementById('cat_name_ar').value = nameAr;
|
||||
categoryModal.show();
|
||||
}
|
||||
|
||||
function deleteCategory(id) {
|
||||
if (confirm('Are you sure you want to delete this category? All related subcategories will also be deleted.')) {
|
||||
document.getElementById('deleteAction').value = 'delete_category';
|
||||
document.getElementById('deleteId').value = id;
|
||||
document.getElementById('deleteForm').submit();
|
||||
}
|
||||
}
|
||||
|
||||
function openCreateSubcategoryModal() {
|
||||
document.getElementById('subcategoryModalTitle').innerText = 'Add New Subcategory';
|
||||
document.getElementById('sub_action').value = 'create_subcategory';
|
||||
document.getElementById('sub_id').value = '';
|
||||
document.getElementById('sub_category_id').value = '';
|
||||
document.getElementById('sub_name_en').value = '';
|
||||
document.getElementById('sub_name_ar').value = '';
|
||||
subcategoryModal.show();
|
||||
}
|
||||
|
||||
function openEditSubcategoryModal(id, catId, nameEn, nameAr) {
|
||||
document.getElementById('subcategoryModalTitle').innerText = 'Edit Subcategory';
|
||||
document.getElementById('sub_action').value = 'update_subcategory';
|
||||
document.getElementById('sub_id').value = id;
|
||||
document.getElementById('sub_category_id').value = catId;
|
||||
document.getElementById('sub_name_en').value = nameEn;
|
||||
document.getElementById('sub_name_ar').value = nameAr;
|
||||
subcategoryModal.show();
|
||||
}
|
||||
|
||||
function deleteSubcategory(id) {
|
||||
if (confirm('Are you sure you want to delete this subcategory?')) {
|
||||
document.getElementById('deleteAction').value = 'delete_subcategory';
|
||||
document.getElementById('deleteId').value = id;
|
||||
document.getElementById('deleteForm').submit();
|
||||
}
|
||||
}
|
||||
|
||||
async function translateText(sourceId, targetId, lang) {
|
||||
const source = document.getElementById(sourceId);
|
||||
const target = document.getElementById(targetId);
|
||||
@ -356,7 +539,6 @@ $flashes = library_get_flashes();
|
||||
return;
|
||||
}
|
||||
|
||||
// Show loading state
|
||||
const originalPlaceholder = target.placeholder;
|
||||
target.placeholder = 'Translating...';
|
||||
const originalOpacity = target.style.opacity;
|
||||
|
||||
@ -22,18 +22,19 @@ if (!$text || !$targetLang) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$prompt = "Translate the following text to {$targetLang}. Provide ONLY the translation, without any additional text, quotes, or explanations.";
|
||||
|
||||
$response = LocalAIApi::createResponse([
|
||||
'model' => 'gemini-2.0-flash-001', // Using a fast model if available, or default
|
||||
'input' => [
|
||||
['role' => 'system', 'content' => 'You are a helpful translator. Translate the user input accurately. Output only the translated text.'],
|
||||
['role' => 'user', 'content' => $text],
|
||||
['role' => 'user', 'content' => "Translate the following text to {$targetLang}: \"{$text}\""],
|
||||
],
|
||||
], [
|
||||
'poll_interval' => 2,
|
||||
'poll_timeout' => 30
|
||||
]);
|
||||
|
||||
if (empty($response['success'])) {
|
||||
http_response_code(500);
|
||||
error_log("AI Translation Failed: " . json_encode($response));
|
||||
echo json_encode(['error' => 'AI translation failed', 'details' => $response['error'] ?? 'Unknown error']);
|
||||
exit;
|
||||
}
|
||||
@ -41,6 +42,9 @@ if (empty($response['success'])) {
|
||||
$translatedText = LocalAIApi::extractText($response);
|
||||
|
||||
// Clean up any accidental quotes if the model adds them despite instructions
|
||||
$translatedText = trim($translatedText, " \t\n\r\0\x0B\"'");
|
||||
$translatedText = trim($translatedText, "
|
||||
|
||||
|
||||
'" );
|
||||
|
||||
echo json_encode(['translation' => $translatedText]);
|
||||
|
||||
@ -190,6 +190,34 @@ function library_get_subcategory_by_id(int $id): ?array
|
||||
return $stmt->fetch() ?: null;
|
||||
}
|
||||
|
||||
function library_update_category(int $id, string $nameEn, string $nameAr): void
|
||||
{
|
||||
library_bootstrap();
|
||||
$stmt = db()->prepare('UPDATE library_categories SET name_en = ?, name_ar = ? WHERE id = ?');
|
||||
$stmt->execute([$nameEn, $nameAr, $id]);
|
||||
}
|
||||
|
||||
function library_delete_category(int $id): void
|
||||
{
|
||||
library_bootstrap();
|
||||
$stmt = db()->prepare('DELETE FROM library_categories WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
}
|
||||
|
||||
function library_update_subcategory(int $id, int $categoryId, string $nameEn, string $nameAr): void
|
||||
{
|
||||
library_bootstrap();
|
||||
$stmt = db()->prepare('UPDATE library_subcategories SET category_id = ?, name_en = ?, name_ar = ? WHERE id = ?');
|
||||
$stmt->execute([$categoryId, $nameEn, $nameAr, $id]);
|
||||
}
|
||||
|
||||
function library_delete_subcategory(int $id): void
|
||||
{
|
||||
library_bootstrap();
|
||||
$stmt = db()->prepare('DELETE FROM library_subcategories WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
}
|
||||
|
||||
// --- End Category Functions ---
|
||||
|
||||
function library_fetch_documents(bool $publicOnly = false, array $filters = []): array
|
||||
@ -361,4 +389,4 @@ function library_create_document(array $payload, array $file): int
|
||||
]);
|
||||
|
||||
return (int) db()->lastInsertId();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user