diff --git a/admin.php b/admin.php index a8df133..54c4c17 100644 --- a/admin.php +++ b/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();
| Name | +Actions | +
|---|---|
| No categories. | |
|
+ = h($cat['name_en']) ?> + = h($cat['name_ar']) ?> + |
+
+ |
+
| Name | +Parent | +Actions | +
|---|---|---|
| No subcategories. | ||
|
+ = h($sub['name_en']) ?> + = h($sub['name_ar']) ?> + |
+ = h($parentName) ?> | +
+ |
+