39496-vm/admin_classes.php
2026-04-06 04:21:26 +00:00

163 lines
7.2 KiB
PHP

<?php
// admin_classes.php
require_once __DIR__ . '/includes/app.php';
$action = $_GET['action'] ?? 'list';
$id = (int)($_GET['id'] ?? 0);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($action === 'delete' && $id > 0) {
$stmt = db()->prepare("DELETE FROM classes WHERE id = ?");
$stmt->execute([$id]);
header('Location: ' . app_url('admin.php', ['page' => 'classes']));
exit;
}
if ($action === 'edit' || $action === 'add') {
$name_en = $_POST['name_en'] ?? '';
$name_ar = $_POST['name_ar'] ?? '';
$desc_en = $_POST['description_en'] ?? '';
$desc_ar = $_POST['description_ar'] ?? '';
if ($action === 'edit' && $id > 0) {
$stmt = db()->prepare("UPDATE classes SET name_en=?, name_ar=?, description_en=?, description_ar=? WHERE id=?");
$stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $id]);
} else {
$stmt = db()->prepare("INSERT INTO classes (name_en, name_ar, description_en, description_ar) VALUES (?, ?, ?, ?)");
$stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar]);
}
header('Location: ' . app_url('admin.php', ['page' => 'classes']));
exit;
}
}
if ($action === 'edit' || $action === 'add'):
$item = ['name_en'=>'', 'name_ar'=>'', 'description_en'=>'', 'description_ar'=>''];
if ($action === 'edit' && $id > 0) {
$stmt = db()->prepare("SELECT * FROM classes WHERE id = ?");
$stmt->execute([$id]);
$item = $stmt->fetch(PDO::FETCH_ASSOC) ?: $item;
}
?>
<div class="section-header mb-4">
<div>
<h1 class="section-title mb-2"><?= h($action === 'edit' ? t('Edit Class', 'تعديل الصف') : t('Add Class', 'إضافة صف')) ?></h1>
</div>
</div>
<div class="panel-card" style="max-width: 600px;">
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'classes', 'action'=>$action, 'id'=>$id])) ?>">
<div class="mb-3">
<label class="form-label">Name (EN)</label>
<input type="text" name="name_en" class="form-control" value="<?= h($item['name_en']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Name (AR)</label>
<input type="text" name="name_ar" class="form-control" value="<?= h($item['name_ar']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Description (EN)</label>
<textarea name="description_en" class="form-control"><?= h($item['description_en']) ?></textarea>
</div>
<div class="mb-3">
<label class="form-label">Description (AR)</label>
<textarea name="description_ar" class="form-control"><?= h($item['description_ar']) ?></textarea>
</div>
<button type="submit" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);">
<?= h(t('Save', 'حفظ')) ?>
</button>
<a href="<?= h(app_url('admin.php', ['page'=>'classes'])) ?>" class="btn btn-outline-secondary"><?= h(t('Cancel', 'إلغاء')) ?></a>
</form>
</div>
<?php else: // list view
$search = $_GET['search'] ?? '';
$page_num = max(1, (int)($_GET['p'] ?? 1));
$limit = 10;
$offset = ($page_num - 1) * $limit;
$where = "";
$params = [];
if ($search !== '') {
$where = "WHERE name_en LIKE ? OR name_ar LIKE ?";
$params[] = "%$search%";
$params[] = "%$search%";
}
$total_stmt = db()->prepare("SELECT COUNT(*) FROM classes $where");
$total_stmt->execute($params);
$total = $total_stmt->fetchColumn();
$pages = ceil($total / $limit);
$stmt = db()->prepare("SELECT * FROM classes $where ORDER BY id DESC LIMIT $limit OFFSET $offset");
$stmt->execute($params);
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="section-header mb-4 d-flex justify-content-between align-items-center">
<div>
<h1 class="section-title mb-2"><?= h(t('Classes', 'الصفوف')) ?></h1>
</div>
<a href="<?= h(app_url('admin.php', ['page'=>'classes', 'action'=>'add'])) ?>" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Class', 'إضافة صف')) ?></a>
</div>
<div class="panel-card mb-4">
<form method="get" class="d-flex gap-2 align-items-center">
<input type="hidden" name="page" value="classes">
<input type="text" name="search" class="form-control w-auto" placeholder="<?= h(t('Search...', 'بحث...')) ?>" value="<?= h($search) ?>">
<button type="submit" class="btn btn-outline-secondary"><?= h(t('Filter', 'تصفية')) ?></button>
<?php if ($search): ?>
<a href="<?= h(app_url('admin.php', ['page'=>'classes'])) ?>" class="btn btn-link text-secondary text-decoration-none"><?= h(t('Clear', 'مسح')) ?></a>
<?php endif; ?>
</form>
</div>
<div class="panel-card">
<div class="table-responsive">
<table class="table align-middle dashboard-table mb-0">
<thead>
<tr>
<th>ID</th>
<th><?= h(t('Name', 'الاسم')) ?></th>
<th><?= h(t('Description', 'الوصف')) ?></th>
<th><?= h(t('Actions', 'إجراءات')) ?></th>
</tr>
</thead>
<tbody>
<?php foreach($items as $row): ?>
<tr>
<td><?= h((string)$row['id']) ?></td>
<td>
<div class="fw-semibold"><?= h(current_lang() === 'ar' ? $row['name_ar'] : $row['name_en']) ?></div>
</td>
<td class="text-truncate" style="max-width: 200px;">
<?= h(current_lang() === 'ar' ? $row['description_ar'] : $row['description_en']) ?>
</td>
<td>
<a href="<?= h(app_url('admin.php', ['page'=>'classes', 'action'=>'edit', 'id'=>$row['id']])) ?>" class="btn btn-sm btn-outline-primary"><?= h(t('Edit', 'تعديل')) ?></a>
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'classes', 'action'=>'delete', 'id'=>$row['id']])) ?>" class="d-inline" onsubmit="return confirm('<?= h(t('Are you sure?', 'هل أنت متأكد؟')) ?>');">
<button type="submit" class="btn btn-sm btn-outline-danger"><?= h(t('Delete', 'حذف')) ?></button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php if(!$items): ?>
<tr><td colspan="4" class="text-center text-secondary py-3"><?= h(t('No classes found.', 'لا توجد صفوف.')) ?></td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<?php if ($pages > 1): ?>
<nav class="mt-4">
<ul class="pagination justify-content-center">
<?php for($i=1; $i<=$pages; $i++): ?>
<li class="page-item <?= $i === $page_num ? 'active' : '' ?>">
<a class="page-link" href="<?= h(app_url('admin.php', ['page'=>'classes', 'p'=>$i, 'search'=>$search])) ?>"><?= $i ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>
<?php endif; ?>
<?php endif; ?>