298 lines
16 KiB
PHP
298 lines
16 KiB
PHP
<?php
|
|
// admin_subjects.php
|
|
require_once __DIR__ . '/includes/app.php';
|
|
|
|
$action = $_GET['action'] ?? 'list';
|
|
$id = (int)($_GET['id'] ?? 0);
|
|
|
|
// Fetch classes for dropdown and filter
|
|
$classes_stmt = db()->query("SELECT id, name_en, name_ar FROM classes ORDER BY id DESC");
|
|
$all_classes = $classes_stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$post_action = $_POST['action'] ?? $action;
|
|
$post_id = (int)($_POST['id'] ?? $id);
|
|
|
|
if ($post_action === 'delete' && $post_id > 0) {
|
|
$stmt = db()->prepare("DELETE FROM subjects WHERE id = ?");
|
|
$stmt->execute([$post_id]);
|
|
header('Location: ' . app_url('admin.php', ['page' => 'subjects']));
|
|
exit;
|
|
}
|
|
|
|
if ($post_action === 'edit' || $post_action === 'add') {
|
|
$class_id = !empty($_POST['class_id']) ? (int)$_POST['class_id'] : null;
|
|
$title_en = $_POST['title_en'] ?? '';
|
|
$title_ar = $_POST['title_ar'] ?? '';
|
|
$summary_en = $_POST['summary_en'] ?? '';
|
|
$summary_ar = $_POST['summary_ar'] ?? '';
|
|
|
|
if ($post_action === 'edit' && $post_id > 0) {
|
|
$stmt = db()->prepare("UPDATE subjects SET class_id=?, title_en=?, title_ar=?, summary_en=?, summary_ar=? WHERE id=?");
|
|
$stmt->execute([$class_id, $title_en, $title_ar, $summary_en, $summary_ar, $post_id]);
|
|
} else {
|
|
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $title_en)));
|
|
if (empty($slug)) $slug = 'subject';
|
|
$slug .= '-' . substr(md5(uniqid()), 0, 5);
|
|
|
|
$stmt = db()->prepare("INSERT INTO subjects (slug, class_id, title_en, title_ar, summary_en, summary_ar) VALUES (?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$slug, $class_id, $title_en, $title_ar, $summary_en, $summary_ar]);
|
|
}
|
|
header('Location: ' . app_url('admin.php', ['page' => 'subjects']));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// list view
|
|
$search = $_GET['search'] ?? '';
|
|
$filter_class = $_GET['class_id'] ?? '';
|
|
$page_num = max(1, (int)($_GET['p'] ?? 1));
|
|
$limit = 10;
|
|
$offset = ($page_num - 1) * $limit;
|
|
|
|
$where_clauses = [];
|
|
$params = [];
|
|
|
|
if ($search !== '') {
|
|
$where_clauses[] = "(s.title_en LIKE ? OR s.title_ar LIKE ?)";
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
}
|
|
if ($filter_class !== '') {
|
|
$where_clauses[] = "s.class_id = ?";
|
|
$params[] = $filter_class;
|
|
}
|
|
|
|
$where = "";
|
|
if (count($where_clauses) > 0) {
|
|
$where = "WHERE " . implode(" AND ", $where_clauses);
|
|
}
|
|
|
|
$total_stmt = db()->prepare("SELECT COUNT(*) FROM subjects s $where");
|
|
$total_stmt->execute($params);
|
|
$total = $total_stmt->fetchColumn();
|
|
$pages = ceil($total / $limit);
|
|
|
|
$stmt = db()->prepare("
|
|
SELECT s.*, c.name_en AS class_name_en, c.name_ar AS class_name_ar
|
|
FROM subjects s
|
|
LEFT JOIN classes c ON s.class_id = c.id
|
|
$where
|
|
ORDER BY s.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('Subjects', 'المواد')) ?></h1>
|
|
</div>
|
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addSubjectModal" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Subject', 'إضافة مادة')) ?></button>
|
|
</div>
|
|
|
|
<div class="panel-card mb-4">
|
|
<form method="get" class="d-flex gap-2 flex-wrap align-items-center">
|
|
<input type="hidden" name="page" value="subjects">
|
|
|
|
<input type="text" name="search" class="form-control w-auto" placeholder="<?= h(t('Search name...', 'بحث في الاسم...')) ?>" value="<?= h($search) ?>">
|
|
|
|
<select name="class_id" class="form-control w-auto">
|
|
<option value=""><?= h(t('All Classes', 'جميع الصفوف')) ?></option>
|
|
<?php foreach($all_classes as $c): ?>
|
|
<option value="<?= $c['id'] ?>" <?= $filter_class == $c['id'] ? 'selected' : '' ?>>
|
|
<?= h(current_lang() === 'ar' ? $c['name_ar'] : $c['name_en']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
|
|
<button type="submit" class="btn btn-outline-secondary"><?= h(t('Filter', 'تصفية')) ?></button>
|
|
<?php if ($search || $filter_class !== ''): ?>
|
|
<a href="<?= h(app_url('admin.php', ['page'=>'subjects'])) ?>" 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('Subject Name', 'اسم المادة')) ?></th>
|
|
<th><?= h(t('Details', 'تفاصيل')) ?></th>
|
|
<th><?= h(t('Class', 'الصف')) ?></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['title_ar'] : $row['title_en']) ?></div>
|
|
</td>
|
|
<td>
|
|
<div class="small text-secondary text-truncate" style="max-width: 250px;">
|
|
<?= h(current_lang() === 'ar' ? $row['summary_ar'] : $row['summary_en']) ?>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<?php if ($row['class_id']): ?>
|
|
<span class="badge bg-light text-dark border">
|
|
<?= h(current_lang() === 'ar' ? $row['class_name_ar'] : $row['class_name_en']) ?>
|
|
</span>
|
|
<?php else: ?>
|
|
<span class="text-muted">-</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editSubjectModal<?= $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'=>'subjects', '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 Subject Modal -->
|
|
<div class="modal fade" id="editSubjectModal<?= $row['id'] ?>" tabindex="-1" aria-labelledby="editSubjectModalLabel<?= $row['id'] ?>" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg 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="editSubjectModalLabel<?= $row['id'] ?>"><?= h(t('Edit Subject', 'تعديل المادة')) ?></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'=>'subjects'])) ?>">
|
|
<input type="hidden" name="action" value="edit">
|
|
<input type="hidden" name="id" value="<?= $row['id'] ?>">
|
|
|
|
<div class="row">
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label"><?= h(t('Class', 'الصف')) ?></label>
|
|
<select name="class_id" class="form-control" required>
|
|
<option value=""><?= h(t('-- Select Class --', '-- اختر الصف --')) ?></option>
|
|
<?php foreach($all_classes as $c): ?>
|
|
<option value="<?= $c['id'] ?>" <?= $row['class_id'] == $c['id'] ? 'selected' : '' ?>>
|
|
<?= h(current_lang() === 'ar' ? $c['name_ar'] : $c['name_en']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (English)', 'الاسم (بالانجليزية)')) ?></label>
|
|
<input type="text" name="title_en" class="form-control" value="<?= h($row['title_en']) ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (Arabic)', 'الاسم (بالعربية)')) ?></label>
|
|
<input type="text" name="title_ar" class="form-control" value="<?= h($row['title_ar']) ?>" required>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-4">
|
|
<label class="form-label"><?= h(t('Details (English)', 'تفاصيل (بالانجليزية)')) ?></label>
|
|
<textarea name="summary_en" class="form-control" rows="3"><?= h($row['summary_en']) ?></textarea>
|
|
</div>
|
|
<div class="col-md-6 mb-4">
|
|
<label class="form-label"><?= h(t('Details (Arabic)', 'تفاصيل (بالعربية)')) ?></label>
|
|
<textarea name="summary_ar" class="form-control" rows="3"><?= h($row['summary_ar']) ?></textarea>
|
|
</div>
|
|
</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 Subject Modal -->
|
|
|
|
<?php endforeach; ?>
|
|
<?php if(!$items): ?>
|
|
<tr><td colspan="5" class="text-center text-secondary py-3"><?= h(t('No subjects 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'=>'subjects', 'p'=>$i, 'search'=>$search, 'class_id'=>$filter_class])) ?>"><?= $i ?></a>
|
|
</li>
|
|
<?php endfor; ?>
|
|
</ul>
|
|
</nav>
|
|
<?php endif; ?>
|
|
|
|
<!-- Add Subject Modal -->
|
|
<div class="modal fade" id="addSubjectModal" tabindex="-1" aria-labelledby="addSubjectModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-lg 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="addSubjectModalLabel"><?= h(t('Add Subject', 'إضافة مادة')) ?></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'=>'subjects'])) ?>">
|
|
<input type="hidden" name="action" value="add">
|
|
|
|
<div class="row">
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label"><?= h(t('Class', 'الصف')) ?></label>
|
|
<select name="class_id" class="form-control" required>
|
|
<option value=""><?= h(t('-- Select Class --', '-- اختر الصف --')) ?></option>
|
|
<?php foreach($all_classes as $c): ?>
|
|
<option value="<?= $c['id'] ?>">
|
|
<?= h(current_lang() === 'ar' ? $c['name_ar'] : $c['name_en']) ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (English)', 'الاسم (بالانجليزية)')) ?></label>
|
|
<input type="text" name="title_en" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name (Arabic)', 'الاسم (بالعربية)')) ?></label>
|
|
<input type="text" name="title_ar" class="form-control" required>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-4">
|
|
<label class="form-label"><?= h(t('Details (English)', 'تفاصيل (بالانجليزية)')) ?></label>
|
|
<textarea name="summary_en" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
<div class="col-md-6 mb-4">
|
|
<label class="form-label"><?= h(t('Details (Arabic)', 'تفاصيل (بالعربية)')) ?></label>
|
|
<textarea name="summary_ar" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
</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 Subject Modal -->
|