252 lines
12 KiB
PHP
252 lines
12 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/includes/app.php';
|
|
|
|
// Handle POST actions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
if ($action === 'create') {
|
|
$name = clean_text($_POST['name'] ?? '', 255);
|
|
$description = clean_text($_POST['description'] ?? '', 1000);
|
|
$status = in_array($_POST['status'] ?? '', ['enabled', 'disabled']) ? $_POST['status'] : 'enabled';
|
|
|
|
if ($name !== '') {
|
|
$stmt = db()->prepare('INSERT INTO subjects (name, description, status) VALUES (?, ?, ?)');
|
|
$stmt->execute([$name, $description, $status]);
|
|
set_flash('success', 'تمت إضافة المادة بنجاح.');
|
|
} else {
|
|
set_flash('error', 'اسم المادة مطلوب.');
|
|
}
|
|
header('Location: subjects.php');
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'edit') {
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
$name = clean_text($_POST['name'] ?? '', 255);
|
|
$description = clean_text($_POST['description'] ?? '', 1000);
|
|
$status = in_array($_POST['status'] ?? '', ['enabled', 'disabled']) ? $_POST['status'] : 'enabled';
|
|
|
|
if ($id > 0 && $name !== '') {
|
|
$stmt = db()->prepare('UPDATE subjects SET name = ?, description = ?, status = ? WHERE id = ?');
|
|
$stmt->execute([$name, $description, $status, $id]);
|
|
set_flash('success', 'تم تحديث المادة بنجاح.');
|
|
} else {
|
|
set_flash('error', 'تأكد من إدخال اسم المادة.');
|
|
}
|
|
header('Location: subjects.php');
|
|
exit;
|
|
}
|
|
|
|
if ($action === 'delete') {
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
if ($id > 0) {
|
|
$stmt = db()->prepare('DELETE FROM subjects WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
set_flash('success', 'تم حذف المادة بنجاح.');
|
|
}
|
|
header('Location: subjects.php');
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Read list
|
|
$search = clean_text($_GET['search'] ?? '', 255);
|
|
$query = 'SELECT * FROM subjects';
|
|
$params = [];
|
|
if ($search !== '') {
|
|
$query .= ' WHERE name LIKE ? OR description LIKE ?';
|
|
$params[] = "%$search%";
|
|
$params[] = "%$search%";
|
|
}
|
|
$query .= ' ORDER BY id DESC';
|
|
$stmt = db()->prepare($query);
|
|
$stmt->execute($params);
|
|
$subjects = $stmt->fetchAll();
|
|
|
|
$flash = consume_flash();
|
|
render_page_start('المواد الدراسية', 'subjects', 'إدارة المواد الدراسية الخاصة بالمراكز');
|
|
render_flash($flash);
|
|
?>
|
|
<section class="py-4 py-lg-5">
|
|
<div class="container-xxl">
|
|
<div class="row g-4 align-items-start">
|
|
<div class="col-lg-3">
|
|
<?php require __DIR__ . '/includes/sidebar.php'; ?>
|
|
</div>
|
|
<div class="col-lg-9">
|
|
<div class="app-card mb-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-3">
|
|
<div class="section-title mb-0">إدارة المواد الدراسية</div>
|
|
<button class="btn btn-dark" data-bs-toggle="modal" data-bs-target="#createModal">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="me-1" viewBox="0 0 16 16">
|
|
<path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/>
|
|
</svg>
|
|
إضافة مادة
|
|
</button>
|
|
</div>
|
|
|
|
<!-- Search Bar -->
|
|
<form method="GET" action="subjects.php" class="mb-4">
|
|
<div class="input-group">
|
|
<input type="text" name="search" class="form-control" placeholder="ابحث باسم المادة أو الوصف..." value="<?= e($search) ?>">
|
|
<button class="btn btn-outline-secondary" type="submit">بحث</button>
|
|
<?php if ($search !== ''): ?>
|
|
<a href="subjects.php" class="btn btn-outline-danger">إلغاء</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</form>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table app-table align-middle">
|
|
<thead>
|
|
<tr>
|
|
<th>#</th>
|
|
<th>الاسم</th>
|
|
<th>الوصف</th>
|
|
<th>الحالة</th>
|
|
<th>الإجراءات</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (count($subjects) === 0): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center py-4 text-muted">لا توجد مواد مسجلة أو لم يتم العثور على نتائج.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($subjects as $subject): ?>
|
|
<tr>
|
|
<td><?= e((string)$subject['id']) ?></td>
|
|
<td class="fw-semibold"><?= e($subject['name']) ?></td>
|
|
<td class="text-muted" style="max-width: 250px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" title="<?= e($subject['description']) ?>"><?= e($subject['description'] ?: '—') ?></td>
|
|
<td>
|
|
<?php if ($subject['status'] === 'enabled'): ?>
|
|
<span class="badge bg-success-subtle text-success">مفعل</span>
|
|
<?php else: ?>
|
|
<span class="badge bg-secondary-subtle text-secondary">معطل</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<div class="d-flex gap-2">
|
|
<!-- Edit btn -->
|
|
<button class="btn btn-sm btn-outline-secondary" title="تعديل"
|
|
data-bs-toggle="modal" data-bs-target="#editModal"
|
|
data-id="<?= e((string)$subject['id']) ?>"
|
|
data-name="<?= e($subject['name']) ?>"
|
|
data-description="<?= e($subject['description']) ?>"
|
|
data-status="<?= e($subject['status']) ?>"
|
|
onclick="fillEditModal(this)">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" 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>
|
|
<!-- Delete btn -->
|
|
<form method="POST" action="subjects.php" onsubmit="return confirm('هل أنت متأكد من حذف هذه المادة؟');">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="id" value="<?= e((string)$subject['id']) ?>">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger" title="حذف">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" 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>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<!-- Create Modal -->
|
|
<div class="modal fade" id="createModal" tabindex="-1" aria-labelledby="createModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="POST" action="subjects.php">
|
|
<input type="hidden" name="action" value="create">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="createModalLabel">إضافة مادة دراسية</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">اسم المادة</label>
|
|
<input type="text" name="name" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">الوصف</label>
|
|
<textarea name="description" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">الحالة</label>
|
|
<select name="status" class="form-select">
|
|
<option value="enabled">مفعل</option>
|
|
<option value="disabled">معطل</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
|
|
<button type="submit" class="btn btn-dark">إضافة</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Modal -->
|
|
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="POST" action="subjects.php">
|
|
<input type="hidden" name="action" value="edit">
|
|
<input type="hidden" name="id" id="edit_id">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="editModalLabel">تعديل مادة دراسية</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">اسم المادة</label>
|
|
<input type="text" name="name" id="edit_name" class="form-control" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">الوصف</label>
|
|
<textarea name="description" id="edit_description" class="form-control" rows="3"></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">الحالة</label>
|
|
<select name="status" id="edit_status" class="form-select">
|
|
<option value="enabled">مفعل</option>
|
|
<option value="disabled">معطل</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
|
|
<button type="submit" class="btn btn-dark">حفظ التعديلات</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function fillEditModal(btn) {
|
|
document.getElementById('edit_id').value = btn.getAttribute('data-id');
|
|
document.getElementById('edit_name').value = btn.getAttribute('data-name');
|
|
document.getElementById('edit_description').value = btn.getAttribute('data-description');
|
|
document.getElementById('edit_status').value = btn.getAttribute('data-status');
|
|
}
|
|
</script>
|
|
|
|
<?php render_page_end(); ?>
|