39301-vm/admin_subcategories.php
2026-03-25 10:08:05 +00:00

240 lines
11 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/admin_layout.php';
library_bootstrap();
$errors = [];
// Handle POST request
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
try {
if ($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_subcategories.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_subcategories.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_subcategories.php');
exit;
}
} catch (Throwable $exception) {
$errors[] = $exception->getMessage();
}
}
// Search Logic
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$categories = library_get_categories();
$allSubcategories = library_get_subcategories(null, $search);
admin_render_header('Subcategories', 'subcategories');
?>
<!-- Page Content -->
<?php if ($errors): ?>
<div class="alert alert-danger"><?= h(implode(' ', $errors)) ?></div>
<?php endif; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
<p class="text-secondary mb-0">Manage document subcategories.</p>
<button class="btn btn-primary" onclick="openCreateSubcategoryModal()">
<i class="bi bi-plus-lg me-1"></i> Add New Subcategory
</button>
</div>
<!-- Search Bar -->
<div class="card shadow-sm border-0 mb-4">
<div class="card-body">
<form method="get" action="/admin_subcategories.php" class="row g-2 align-items-center">
<div class="col-auto flex-grow-1">
<input type="text" name="search" class="form-control" placeholder="Search subcategories..." value="<?= h($search) ?>">
</div>
<div class="col-auto">
<button type="submit" class="btn btn-outline-primary">
<i class="bi bi-search"></i> Search
</button>
<?php if ($search): ?>
<a href="/admin_subcategories.php" class="btn btn-outline-secondary">Clear</a>
<?php endif; ?>
</div>
</form>
</div>
</div>
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-light">
<tr>
<th class="ps-4">Name</th>
<th>Parent Category</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($allSubcategories)): ?>
<tr><td colspan="3" class="text-center py-5 text-muted">No subcategories found.</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 class="ps-4">
<div class="fw-medium text-dark"><?= h($sub['name_en']) ?></div>
<small class="text-muted"><?= h($sub['name_ar']) ?></small>
</td>
<td>
<span class="badge bg-light text-dark border"><?= h($parentName) ?></span>
</td>
<td class="text-end pe-4">
<button class="btn btn-sm btn-outline-secondary me-1"
data-id="<?= $sub['id'] ?>"
data-category-id="<?= $sub['category_id'] ?>"
data-name-en="<?= h($sub['name_en']) ?>"
data-name-ar="<?= h($sub['name_ar']) ?>"
onclick="openEditSubcategoryModal(this)">
<i class="bi bi-pencil"></i> Edit
</button>
<button class="btn btn-sm btn-outline-danger" onclick="deleteSubcategory(<?= $sub['id'] ?>)">
<i class="bi bi-trash"></i> Delete
</button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</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_subcategories.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" 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" id="sub_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>
<!-- Delete Confirmation Form -->
<form method="post" action="/admin_subcategories.php" id="deleteForm">
<input type="hidden" name="action" id="deleteAction" value="">
<input type="hidden" name="id" id="deleteId" value="">
</form>
<script>
let subcategoryModal;
document.addEventListener('DOMContentLoaded', function() {
subcategoryModal = new bootstrap.Modal(document.getElementById('subcategoryModal'));
});
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(btn) {
const id = btn.getAttribute('data-id');
const catId = btn.getAttribute('data-category-id');
const nameEn = btn.getAttribute('data-name-en');
const nameAr = btn.getAttribute('data-name-ar');
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();
}
}
</script>
<?php admin_render_footer(); ?>