252 lines
11 KiB
PHP
252 lines
11 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') {
|
|
if ($action === 'delete' && $id > 0) {
|
|
$stmt = db()->prepare("DELETE FROM subjects WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
header('Location: ' . app_url('admin.php', ['page' => 'subjects']));
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'edit' || $action === 'add') {
|
|
$slug = $_POST['slug'] ?? '';
|
|
$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'] ?? '';
|
|
$teacher_en = $_POST['teacher_en'] ?? '';
|
|
$teacher_ar = $_POST['teacher_ar'] ?? '';
|
|
$meet_url = $_POST['meet_url'] ?? '';
|
|
|
|
if ($action === 'edit' && $id > 0) {
|
|
$stmt = db()->prepare("UPDATE subjects SET slug=?, class_id=?, title_en=?, title_ar=?, summary_en=?, summary_ar=?, teacher_en=?, teacher_ar=?, meet_url=? WHERE id=?");
|
|
$stmt->execute([$slug, $class_id, $title_en, $title_ar, $summary_en, $summary_ar, $teacher_en, $teacher_ar, $meet_url, $id]);
|
|
} else {
|
|
$stmt = db()->prepare("INSERT INTO subjects (slug, class_id, title_en, title_ar, summary_en, summary_ar, teacher_en, teacher_ar, meet_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$slug, $class_id, $title_en, $title_ar, $summary_en, $summary_ar, $teacher_en, $teacher_ar, $meet_url]);
|
|
}
|
|
header('Location: ' . app_url('admin.php', ['page' => 'subjects']));
|
|
exit;
|
|
}
|
|
}
|
|
|
|
if ($action === 'edit' || $action === 'add'):
|
|
$item = ['slug'=>'', 'class_id'=>null, 'title_en'=>'', 'title_ar'=>'', 'summary_en'=>'', 'summary_ar'=>'', 'teacher_en'=>'', 'teacher_ar'=>'', 'meet_url'=>''];
|
|
if ($action === 'edit' && $id > 0) {
|
|
$stmt = db()->prepare("SELECT * FROM subjects 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 Subject', 'تعديل المادة') : t('Add Subject', 'إضافة مادة')) ?></h1>
|
|
</div>
|
|
</div>
|
|
<div class="panel-card" style="max-width: 800px;">
|
|
<form method="post" action="<?= h(app_url('admin.php', ['page'=>'subjects', 'action'=>$action, 'id'=>$id])) ?>">
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Slug</label>
|
|
<input type="text" name="slug" class="form-control" value="<?= h($item['slug']) ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Class', 'الصف')) ?></label>
|
|
<select name="class_id" class="form-control">
|
|
<option value=""><?= h(t('-- Select Class --', '-- اختر الصف --')) ?></option>
|
|
<?php foreach($all_classes as $c): ?>
|
|
<option value="<?= $c['id'] ?>" <?= $item['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">Title (EN)</label>
|
|
<input type="text" name="title_en" class="form-control" value="<?= h($item['title_en']) ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Title (AR)</label>
|
|
<input type="text" name="title_ar" class="form-control" value="<?= h($item['title_ar']) ?>" required>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Summary (EN)</label>
|
|
<textarea name="summary_en" class="form-control" rows="2"><?= h($item['summary_en']) ?></textarea>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Summary (AR)</label>
|
|
<textarea name="summary_ar" class="form-control" rows="2"><?= h($item['summary_ar']) ?></textarea>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Teacher (EN)</label>
|
|
<input type="text" name="teacher_en" class="form-control" value="<?= h($item['teacher_en']) ?>">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Teacher (AR)</label>
|
|
<input type="text" name="teacher_ar" class="form-control" value="<?= h($item['teacher_ar']) ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label class="form-label">Meet URL</label>
|
|
<input type="url" name="meet_url" class="form-control" value="<?= h($item['meet_url']) ?>">
|
|
</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'=>'subjects'])) ?>" class="btn btn-outline-secondary"><?= h(t('Cancel', 'إلغاء')) ?></a>
|
|
</form>
|
|
</div>
|
|
|
|
<?php else: // 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>
|
|
<a href="<?= h(app_url('admin.php', ['page'=>'subjects', 'action'=>'add'])) ?>" class="btn btn-primary" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Subject', 'إضافة مادة')) ?></a>
|
|
</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 title...', 'بحث في العنوان...')) ?>" 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', 'المادة')) ?></th>
|
|
<th><?= h(t('Class', 'الصف')) ?></th>
|
|
<th><?= h(t('Teacher', 'المعلم')) ?></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>
|
|
<div class="small text-secondary"><?= h($row['slug']) ?></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>
|
|
<?= h(current_lang() === 'ar' ? $row['teacher_ar'] : $row['teacher_en']) ?>
|
|
</td>
|
|
<td>
|
|
<a href="<?= h(app_url('admin.php', ['page'=>'subjects', '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'=>'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"><?= h(t('Delete', 'حذف')) ?></button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?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; ?>
|
|
|
|
<?php endif; ?>
|