39496-vm/admin_teachers.php
2026-04-06 05:37:02 +00:00

283 lines
15 KiB
PHP

<?php
// admin_teachers.php
require_once __DIR__ . '/includes/app.php';
$action = $_GET['action'] ?? 'list';
$id = (int)($_GET['id'] ?? 0);
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 teachers WHERE id = ?");
$stmt->execute([$post_id]);
header('Location: ' . app_url('admin.php', ['page' => 'teachers']));
exit;
}
if ($post_action === 'edit' || $post_action === 'add') {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$phone = $_POST['phone'] ?? '';
$bio = $_POST['bio'] ?? '';
$raw_password = $_POST['password'] ?? '';
$photo_path = '';
$existing_password = '';
if ($post_action === 'edit' && $post_id > 0) {
$stmt = db()->prepare("SELECT photo_path, password FROM teachers WHERE id = ?");
$stmt->execute([$post_id]);
$existing = $stmt->fetch(PDO::FETCH_ASSOC);
if ($existing) {
$photo_path = $existing['photo_path'];
$existing_password = $existing['password'];
}
}
$upload_dir = __DIR__ . '/assets/images/uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0777, true);
}
if (!empty($_FILES['photo']['tmp_name'])) {
$filename = 'teacher_' . time() . '_' . basename($_FILES['photo']['name']);
$target = $upload_dir . $filename;
if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
$photo_path = 'assets/images/uploads/' . $filename;
}
}
$final_password = '';
if ($post_action === 'add') {
$final_password = $raw_password ? password_hash($raw_password, PASSWORD_DEFAULT) : '';
} else {
if ($raw_password) {
$final_password = password_hash($raw_password, PASSWORD_DEFAULT);
} else {
$final_password = $existing_password;
}
}
if ($post_action === 'edit' && $post_id > 0) {
$stmt = db()->prepare("UPDATE teachers SET name=?, email=?, phone=?, bio=?, photo_path=?, password=? WHERE id=?");
$stmt->execute([$name, $email, $phone, $bio, $photo_path, $final_password, $post_id]);
} else {
$stmt = db()->prepare("INSERT INTO teachers (name, email, phone, bio, photo_path, password) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$name, $email, $phone, $bio, $photo_path, $final_password]);
}
header('Location: ' . app_url('admin.php', ['page' => 'teachers']));
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 LIKE ? OR email LIKE ? OR phone LIKE ?";
$params[] = "%$search%";
$params[] = "%$search%";
$params[] = "%$search%";
}
$total_stmt = db()->prepare("SELECT COUNT(*) FROM teachers $where");
$total_stmt->execute($params);
$total = $total_stmt->fetchColumn();
$pages = ceil($total / $limit);
$stmt = db()->prepare("SELECT * FROM teachers $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('Teachers', 'المعلمون')) ?></h1>
</div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addTeacherModal" style="background-color: var(--accent); border-color: var(--accent);">+ <?= h(t('Add Teacher', 'إضافة معلم')) ?></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="teachers">
<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'=>'teachers'])) ?>" 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><?= h(t('Photo', 'الصورة')) ?></th>
<th><?= h(t('Name', 'الاسم')) ?></th>
<th><?= h(t('Contact', 'جهة الاتصال')) ?></th>
<th><?= h(t('Actions', 'إجراءات')) ?></th>
</tr>
</thead>
<tbody>
<?php foreach($items as $row): ?>
<tr>
<td>
<?php if(!empty($row['photo_path'])): ?>
<img src="<?= h(asset_url($row['photo_path'])) ?>" alt="" style="width: 40px; height: 40px; border-radius: 50%; object-fit: cover;">
<?php else: ?>
<div class="bg-light text-secondary d-flex align-items-center justify-content-center" style="width: 40px; height: 40px; border-radius: 50%;">
<?= h(strtoupper(substr($row['name'], 0, 1))) ?>
</div>
<?php endif; ?>
</td>
<td>
<div class="fw-semibold"><?= h($row['name']) ?></div>
</td>
<td>
<?php if (!empty($row['email'])): ?><div><small><?= h($row['email']) ?></small></div><?php endif; ?>
<?php if (!empty($row['phone'])): ?><div><small><?= h($row['phone']) ?></small></div><?php endif; ?>
</td>
<td>
<button type="button" class="btn btn-sm btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editTeacherModal<?= $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'=>'teachers', '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 Teacher Modal -->
<div class="modal fade" id="editTeacherModal<?= $row['id'] ?>" tabindex="-1" aria-labelledby="editTeacherModalLabel<?= $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="editTeacherModalLabel<?= $row['id'] ?>"><?= h(t('Edit Teacher', 'تعديل المعلم')) ?></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" enctype="multipart/form-data" action="<?= h(app_url('admin.php', ['page'=>'teachers'])) ?>">
<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('Name', 'الاسم')) ?></label>
<input type="text" name="name" class="form-control" value="<?= h($row['name']) ?>" required>
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Email', 'البريد الإلكتروني')) ?></label>
<input type="email" name="email" class="form-control" value="<?= h($row['email'] ?? '') ?>">
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Phone', 'رقم الهاتف')) ?></label>
<input type="tel" name="phone" class="form-control" value="<?= h($row['phone'] ?? '') ?>" dir="ltr">
</div>
<div class="mb-3">
<label class="form-label">
<?= h(t('Password', 'كلمة المرور')) ?>
<small class="text-muted">(<?= h(t('Leave blank to keep current', 'اتركه فارغاً للاحتفاظ بكلمة المرور الحالية')) ?>)</small>
</label>
<input type="password" name="password" class="form-control" autocomplete="new-password">
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Bio', 'نبذة')) ?></label>
<textarea name="bio" class="form-control" rows="4"><?= h($row['bio'] ?? '') ?></textarea>
</div>
<div class="mb-4">
<label class="form-label"><?= h(t('Photo', 'الصورة')) ?></label>
<?php if (!empty($row['photo_path'])): ?>
<div class="mb-2">
<img src="<?= h(asset_url($row['photo_path'])) ?>" alt="Photo" style="height: 60px; border-radius: 4px; object-fit: cover;">
</div>
<?php endif; ?>
<input type="file" name="photo" class="form-control" accept="image/*">
</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 Teacher Modal -->
<?php endforeach; ?>
<?php if(!$items): ?>
<tr><td colspan="4" class="text-center text-secondary py-3"><?= h(t('No teachers 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'=>'teachers', 'p'=>$i, 'search'=>$search])) ?>"><?= $i ?></a>
</li>
<?php endfor; ?>
</ul>
</nav>
<?php endif; ?>
<!-- Add Teacher Modal -->
<div class="modal fade" id="addTeacherModal" tabindex="-1" aria-labelledby="addTeacherModalLabel" 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="addTeacherModalLabel"><?= h(t('Add Teacher', 'إضافة معلم')) ?></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" enctype="multipart/form-data" action="<?= h(app_url('admin.php', ['page'=>'teachers'])) ?>">
<input type="hidden" name="action" value="add">
<div class="mb-3">
<label class="form-label"><?= h(t('Name', 'الاسم')) ?></label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Email', 'البريد الإلكتروني')) ?></label>
<input type="email" name="email" class="form-control">
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Phone', 'رقم الهاتف')) ?></label>
<input type="tel" name="phone" class="form-control" dir="ltr">
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Password', 'كلمة المرور')) ?></label>
<input type="password" name="password" class="form-control" autocomplete="new-password" required>
</div>
<div class="mb-3">
<label class="form-label"><?= h(t('Bio', 'نبذة')) ?></label>
<textarea name="bio" class="form-control" rows="4"></textarea>
</div>
<div class="mb-4">
<label class="form-label"><?= h(t('Photo', 'الصورة')) ?></label>
<input type="file" name="photo" class="form-control" accept="image/*">
</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 Teacher Modal -->