321 lines
18 KiB
PHP
321 lines
18 KiB
PHP
<?php
|
|
// admin_teachers.php
|
|
require_once __DIR__ . '/includes/app.php';
|
|
require_once __DIR__ . '/includes/auth.php';
|
|
|
|
require_permission('teachers', 'view');
|
|
|
|
$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) {
|
|
require_permission('teachers', 'delete');
|
|
$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') {
|
|
require_permission('teachers', $post_action === 'add' ? 'add' : 'edit');
|
|
$name = $_POST['name'] ?? '';
|
|
$email = $_POST['email'] ?? '';
|
|
$phone = $_POST['phone'] ?? '';
|
|
$bio = $_POST['bio'] ?? '';
|
|
$status = $_POST['status'] ?? 'active';
|
|
$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=?, status=? WHERE id=?");
|
|
$stmt->execute([$name, $email, $phone, $bio, $photo_path, $final_password, $status, $post_id]);
|
|
} else {
|
|
$stmt = db()->prepare("INSERT INTO teachers (name, email, phone, bio, photo_path, password, status) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$name, $email, $phone, $bio, $photo_path, $final_password, $status]);
|
|
}
|
|
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>
|
|
<?php if (has_permission('teachers', 'add')): ?>
|
|
<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>
|
|
<?php endif; ?>
|
|
</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('Status', 'الحالة')) ?></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 (isset($row['status']) && $row['status'] === 'inactive'): ?>
|
|
<span class="badge bg-secondary"><?= h(t('Inactive', 'غير نشط')) ?></span>
|
|
<?php else: ?>
|
|
<span class="badge bg-success"><?= h(t('Active', 'نشط')) ?></span>
|
|
<?php endif; ?>
|
|
</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>
|
|
<?php if (has_permission('teachers', 'edit')): ?>
|
|
<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>
|
|
<?php endif; ?>
|
|
<?php if (has_permission('teachers', 'delete')): ?>
|
|
<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>
|
|
<?php endif; ?>
|
|
</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="row">
|
|
<div class="col-md-6 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="col-md-6 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="col-md-6 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="col-md-6 mb-3">
|
|
<label class="form-label">
|
|
<?= h(t('Password', 'كلمة المرور')) ?>
|
|
<small class="text-muted">(<?= h(t('Leave blank', 'اتركه فارغاً')) ?>)</small>
|
|
</label>
|
|
<input type="password" name="password" class="form-control" autocomplete="new-password">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Photo', 'الصورة')) ?></label>
|
|
<input type="file" name="photo" class="form-control" accept="image/*">
|
|
<?php if (!empty($row['photo_path'])): ?>
|
|
<div class="mt-2">
|
|
<img src="<?= h(asset_url($row['photo_path'])) ?>" alt="Photo" style="height: 40px; border-radius: 4px; object-fit: cover;">
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
|
|
<select name="status" class="form-select">
|
|
<option value="active" <?= (isset($row['status']) && $row['status'] === 'active') ? 'selected' : '' ?>><?= h(t('Active', 'نشط')) ?></option>
|
|
<option value="inactive" <?= (isset($row['status']) && $row['status'] === 'inactive') ? 'selected' : '' ?>><?= h(t('Inactive', 'غير نشط')) ?></option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label"><?= h(t('Bio', 'نبذة')) ?></label>
|
|
<textarea name="bio" class="form-control" rows="3"><?= h($row['bio'] ?? '') ?></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 Teacher Modal -->
|
|
|
|
<?php endforeach; ?>
|
|
<?php if(!$items): ?>
|
|
<tr><td colspan="5" 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="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Name', 'الاسم')) ?></label>
|
|
<input type="text" name="name" class="form-control" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Email', 'البريد الإلكتروني')) ?></label>
|
|
<input type="email" name="email" class="form-control">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Phone', 'رقم الهاتف')) ?></label>
|
|
<input type="tel" name="phone" class="form-control" dir="ltr">
|
|
</div>
|
|
<div class="col-md-6 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="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Photo', 'الصورة')) ?></label>
|
|
<input type="file" name="photo" class="form-control" accept="image/*">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label"><?= h(t('Status', 'الحالة')) ?></label>
|
|
<select name="status" class="form-select">
|
|
<option value="active" selected><?= h(t('Active', 'نشط')) ?></option>
|
|
<option value="inactive"><?= h(t('Inactive', 'غير نشط')) ?></option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label"><?= h(t('Bio', 'نبذة')) ?></label>
|
|
<textarea name="bio" 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 Teacher Modal -->
|