230 lines
13 KiB
PHP
230 lines
13 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 modal is submitted, action is typically passed as a POST variable.
|
|
// However, delete still uses the URL action. Let's support both.
|
|
$post_action = $_POST['action'] ?? $action;
|
|
$post_id = (int)($_POST['id'] ?? $id);
|
|
|
|
if ($post_action === 'delete' && $post_id > 0) {
|
|
$stmt = db()->prepare("DELETE FROM classes WHERE id = ?");
|
|
$stmt->execute([$post_id]);
|
|
header('Location: ' . app_url('admin.php', ['page' => 'classes']));
|
|
exit;
|
|
}
|
|
|
|
if ($post_action === 'edit' || $post_action === 'add') {
|
|
$name_en = $_POST['name_en'] ?? '';
|
|
$name_ar = $_POST['name_ar'] ?? '';
|
|
$desc_en = $_POST['description_en'] ?? '';
|
|
$desc_ar = $_POST['description_ar'] ?? '';
|
|
$status = $_POST['status'] ?? 'active';
|
|
|
|
if ($post_action === 'edit' && $post_id > 0) {
|
|
$stmt = db()->prepare("UPDATE classes SET name_en=?, name_ar=?, description_en=?, description_ar=?, status=? WHERE id=?");
|
|
$stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status, $post_id]);
|
|
} else {
|
|
$stmt = db()->prepare("INSERT INTO classes (name_en, name_ar, description_en, description_ar, status) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$name_en, $name_ar, $desc_en, $desc_ar, $status]);
|
|
}
|
|
header('Location: ' . app_url('admin.php', ['page' => 'classes']));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// 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>
|
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addClassModal" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Class', 'إضافة صف')) ?></button>
|
|
</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('Status', 'الحالة')) ?></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>
|
|
<?php if (($row['status'] ?? 'active') === 'active'): ?>
|
|
<span class="badge bg-success"><?= h(t('Active', 'نشط')) ?></span>
|
|
<?php else: ?>
|
|
<span class="badge bg-secondary"><?= h(t('Inactive', 'غير نشط')) ?></span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td class="text-truncate" style="max-width: 200px;">
|
|
<?= h(current_lang() === 'ar' ? $row['description_ar'] : $row['description_en']) ?>
|
|
</td>
|
|
<td>
|
|
<button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editClassModal<?= $row['id'] ?>" title="<?= h(t('Edit', 'تعديل')) ?>">
|
|
<svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/></svg>
|
|
</button>
|
|
<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" title="<?= h(t('Delete', 'حذف')) ?>"><svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/><path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/></svg></button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- Edit Modal for Row <?= $row['id'] ?> -->
|
|
<div class="modal fade" id="editClassModal<?= $row['id'] ?>" tabindex="-1" aria-labelledby="editClassModalLabel<?= $row['id'] ?>" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header border-0 bg-dark-blue">
|
|
<h5 class="modal-title section-title" id="editClassModalLabel<?= $row['id'] ?>"><?= h(t('Edit Class', 'تعديل الصف')) ?></h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'classes'])) ?>">
|
|
<input type="hidden" name="action" value="edit">
|
|
<input type="hidden" name="id" value="<?= $row['id'] ?>">
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
|
|
<select name="status" class="form-select">
|
|
<option value="active" <?= ($row['status'] ?? 'active') === 'active' ? 'selected' : '' ?>><?= h(t('Active', 'نشط')) ?></option>
|
|
<option value="inactive" <?= ($row['status'] ?? 'active') === 'inactive' ? 'selected' : '' ?>><?= h(t('Inactive', 'غير نشط')) ?></option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Name (EN)', 'الاسم (إنجليزي)')) ?></label>
|
|
<input type="text" name="name_en" class="form-control" value="<?= h($row['name_en']) ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Name (AR)', 'الاسم (عربي)')) ?></label>
|
|
<input type="text" name="name_ar" class="form-control" value="<?= h($row['name_ar']) ?>" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Description (EN)', 'الوصف (إنجليزي)')) ?></label>
|
|
<textarea name="description_en" class="form-control"><?= h($row['description_en']) ?></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Description (AR)', 'الوصف (عربي)')) ?></label>
|
|
<textarea name="description_ar" class="form-control"><?= h($row['description_ar']) ?></textarea>
|
|
</div>
|
|
<div class="d-flex justify-content-end gap-2 mt-4">
|
|
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?= h(t('Cancel', 'إلغاء')) ?></button>
|
|
<button type="submit" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);"><?= h(t('Save Changes', 'حفظ التغييرات')) ?></button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<!-- End Edit Modal -->
|
|
|
|
<?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; ?>
|
|
|
|
<!-- Add Class Modal -->
|
|
<div class="modal fade" id="addClassModal" tabindex="-1" aria-labelledby="addClassModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header border-0 bg-dark-blue">
|
|
<h5 class="modal-title section-title" id="addClassModalLabel"><?= h(t('Add Class', 'إضافة صف')) ?></h5>
|
|
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'classes'])) ?>">
|
|
<input type="hidden" name="action" value="add">
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
|
|
<select name="status" class="form-select">
|
|
<option value="active"><?= h(t('Active', 'نشط')) ?></option>
|
|
<option value="inactive"><?= h(t('Inactive', 'غير نشط')) ?></option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Name (EN)', 'الاسم (إنجليزي)')) ?></label>
|
|
<input type="text" name="name_en" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Name (AR)', 'الاسم (عربي)')) ?></label>
|
|
<input type="text" name="name_ar" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Description (EN)', 'الوصف (إنجليزي)')) ?></label>
|
|
<textarea name="description_en" class="form-control"></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label"><?= h(t('Description (AR)', 'الوصف (عربي)')) ?></label>
|
|
<textarea name="description_ar" class="form-control"></textarea>
|
|
</div>
|
|
<div class="d-flex justify-content-end gap-2 mt-4">
|
|
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal"><?= h(t('Cancel', 'إلغاء')) ?></button>
|
|
<button type="submit" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);"><?= h(t('Save', 'حفظ')) ?></button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<!-- End Add Modal -->
|