147 lines
7.1 KiB
PHP
147 lines
7.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/app.php';
|
|
$user = require_auth();
|
|
|
|
$pageTitle = current_lang() === 'ar' ? 'الملف الشخصي' : 'Profile';
|
|
$activeNav = 'profile';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'update_profile') {
|
|
$name_ar = trim($_POST['name_ar'] ?? '');
|
|
$name_en = trim($_POST['name_en'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($name_ar) || empty($name_en)) {
|
|
set_flash('danger', tr('الرجاء إدخال الاسم باللغتين', 'Please provide names in both languages'));
|
|
} else {
|
|
$update_sql = "UPDATE users SET name_ar = ?, name_en = ?";
|
|
$params = [$name_ar, $name_en];
|
|
|
|
// Handle avatar upload
|
|
$avatarPath = $user['avatar'] ?? null;
|
|
if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] === UPLOAD_ERR_OK) {
|
|
$uploadDir = __DIR__ . '/assets/images/users/';
|
|
if (!is_dir($uploadDir)) {
|
|
mkdir($uploadDir, 0775, true);
|
|
}
|
|
|
|
$fileTmpPath = $_FILES['avatar']['tmp_name'];
|
|
$fileName = $_FILES['avatar']['name'];
|
|
$fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
|
|
$allowedfileExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
|
|
|
if (in_array($fileExtension, $allowedfileExtensions)) {
|
|
$newFileName = md5(time() . $fileName) . '.' . $fileExtension;
|
|
$dest_path = $uploadDir . $newFileName;
|
|
|
|
if (move_uploaded_file($fileTmpPath, $dest_path)) {
|
|
$avatarPath = 'assets/images/users/' . $newFileName;
|
|
$update_sql .= ", avatar = ?";
|
|
$params[] = $avatarPath;
|
|
}
|
|
} else {
|
|
set_flash('danger', tr('نوع الملف غير مدعوم للصورة الشخصية.', 'Unsupported file type for profile picture.'));
|
|
}
|
|
}
|
|
|
|
if (!empty($password)) {
|
|
$update_sql .= ", password = ?";
|
|
$params[] = password_hash($password, PASSWORD_DEFAULT);
|
|
}
|
|
|
|
$update_sql .= " WHERE id = ?";
|
|
$params[] = $user['id'];
|
|
|
|
$stmt = db()->prepare($update_sql);
|
|
if ($stmt->execute($params)) {
|
|
// Update session data
|
|
$_SESSION['auth_user']['name_ar'] = $name_ar;
|
|
$_SESSION['auth_user']['name_en'] = $name_en;
|
|
if (isset($avatarPath)) {
|
|
$_SESSION['auth_user']['avatar'] = $avatarPath;
|
|
}
|
|
set_flash('success', tr('تم تحديث الملف الشخصي بنجاح', 'Profile updated successfully'));
|
|
redirect_to('profile.php');
|
|
} else {
|
|
set_flash('danger', tr('حدث خطأ أثناء التحديث', 'Error updating profile'));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Refresh user data from session just in case it was updated
|
|
$user = $_SESSION['auth_user'];
|
|
|
|
require_once __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card shadow-sm border-0 rounded-4">
|
|
<div class="card-header bg-white text-center py-4 border-0">
|
|
<div class="mb-3">
|
|
<?php if (!empty($user['avatar'])): ?>
|
|
<img src="<?= h($user['avatar']) ?>" alt="Profile Picture" class="rounded-circle shadow-sm" style="width: 120px; height: 120px; object-fit: cover; border: 3px solid #fff;">
|
|
<?php else: ?>
|
|
<i class="bi bi-person-circle text-primary" style="font-size: 4rem;"></i>
|
|
<?php endif; ?>
|
|
</div>
|
|
<h4 class="mb-0 fw-bold"><?= h($pageTitle) ?></h4>
|
|
<p class="text-muted small mt-1"><?= h(role_label($user['role'])) ?> · <?= h(branch_label($user['branch_code'])) ?></p>
|
|
</div>
|
|
<div class="card-body p-4 pt-0">
|
|
<form method="post" enctype="multipart/form-data">
|
|
<input type="hidden" name="action" value="update_profile">
|
|
|
|
<div class="mb-4 text-center">
|
|
<label for="avatarInput" class="btn btn-sm btn-outline-primary rounded-pill px-3">
|
|
<i class="bi bi-camera me-1"></i> <?= h(tr('تغيير الصورة الشخصية', 'Change Profile Picture')) ?>
|
|
</label>
|
|
<input type="file" id="avatarInput" name="avatar" class="d-none" accept="image/*" onchange="previewAvatar(this)">
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label text-muted small fw-bold"><?= h(tr('اسم المستخدم', 'Username')) ?></label>
|
|
<input type="text" class="form-control bg-light" value="<?= h($user['username']) ?>" readonly>
|
|
</div>
|
|
|
|
<div class="row mb-3">
|
|
<div class="col-md-6">
|
|
<label class="form-label text-muted small fw-bold"><?= h(tr('الاسم (عربي) *', 'Name (AR) *')) ?></label>
|
|
<input type="text" name="name_ar" class="form-control" value="<?= h($user['name_ar']) ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mt-3 mt-md-0">
|
|
<label class="form-label text-muted small fw-bold"><?= h(tr('الاسم (إنجليزي) *', 'Name (EN) *')) ?></label>
|
|
<input type="text" name="name_en" class="form-control" value="<?= h($user['name_en']) ?>" required>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<label class="form-label text-muted small fw-bold"><?= h(tr('كلمة المرور الجديدة', 'New Password')) ?></label>
|
|
<input type="password" name="password" class="form-control" placeholder="<?= h(tr('اتركه فارغاً إذا لم ترغب بالتغيير', 'Leave blank to keep unchanged')) ?>">
|
|
</div>
|
|
|
|
<div class="d-grid mt-4">
|
|
<button type="submit" class="btn btn-primary py-2 fw-bold">
|
|
<i class="bi bi-save me-1"></i> <?= h(tr('حفظ التغييرات', 'Save Changes')) ?>
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function previewAvatar(input) {
|
|
if (input.files && input.files[0]) {
|
|
// You could add a simple JS preview here if desired
|
|
// For now, let's just submit or show a quick indication
|
|
const fileName = input.files[0].name;
|
|
const label = document.querySelector('label[for="avatarInput"]');
|
|
label.innerHTML = '<i class="bi bi-check-circle me-1"></i> ' + fileName;
|
|
label.classList.replace('btn-outline-primary', 'btn-success');
|
|
label.classList.add('text-white');
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|