38929-vm/profile.php
2026-03-03 05:22:06 +00:00

242 lines
14 KiB
PHP

<?php
$title = 'user_profile';
require_once __DIR__ . '/includes/header.php';
$success = '';
$error = '';
// Get user data
$stmt = db()->prepare("SELECT u.*, b.name_en as branch_name_en, b.name_ar as branch_name_ar
FROM users u
LEFT JOIN branches b ON u.branch_id = b.id
WHERE u.id = ?");
$stmt->execute([$current_user_id]);
$user = $stmt->fetch();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$full_name_en = $_POST['full_name_en'] ?? '';
$full_name_ar = $_POST['full_name_ar'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
// Handle Profile Picture Upload
$profile_picture = $user['profile_picture'];
if (isset($_FILES['profile_picture']) && $_FILES['profile_picture']['error'] === UPLOAD_ERR_OK) {
$ext = pathinfo($_FILES['profile_picture']['name'], PATHINFO_EXTENSION);
$filename = 'user_' . $current_user_id . '_' . time() . '.' . $ext;
$target = 'assets/images/users/' . $filename;
if (!is_dir('assets/images/users/')) {
mkdir('assets/images/users/', 0775, true);
}
if (move_uploaded_file($_FILES['profile_picture']['tmp_name'], $target)) {
$profile_picture = $target;
}
}
if ($password !== '' && $password !== $confirm_password) {
$error = 'Passwords do not match';
} else {
try {
if ($password !== '') {
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = db()->prepare("UPDATE users SET full_name_en = ?, full_name_ar = ?, email = ?, profile_picture = ?, password_hash = ? WHERE id = ?");
$stmt->execute([$full_name_en, $full_name_ar, $email, $profile_picture, $password_hash, $current_user_id]);
} else {
$stmt = db()->prepare("UPDATE users SET full_name_en = ?, full_name_ar = ?, email = ?, profile_picture = ? WHERE id = ?");
$stmt->execute([$full_name_en, $full_name_ar, $email, $profile_picture, $current_user_id]);
}
// Update Session
$_SESSION['full_name'] = is_arabic() ? ($full_name_ar ?: $full_name_en) : $full_name_en;
$success = __('success_update');
// Refresh data
$stmt = db()->prepare("SELECT u.*, b.name_en as branch_name_en, b.name_ar as branch_name_ar
FROM users u
LEFT JOIN branches b ON u.branch_id = b.id
WHERE u.id = ?");
$stmt->execute([$current_user_id]);
$user = $stmt->fetch();
// Refresh header data
$current_user_data = $user;
} catch (Exception $e) {
$error = __('error_update') . ' ' . $e->getMessage();
}
}
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="h3 mb-0"><?= __('user_profile') ?></h2>
<button type="button" class="btn btn-primary px-4 fw-bold shadow-sm" style="border-radius: 12px;" data-bs-toggle="modal" data-bs-target="#editProfileModal">
<i class="bi bi-pencil-square me-2"></i> <?= __('edit_profile') ?? 'Edit Profile' ?>
</button>
</div>
<?php if ($success): ?>
<div class="alert alert-success alert-dismissible fade show rounded-4 border-0 shadow-sm" role="alert">
<i class="bi bi-check-circle-fill me-2"></i> <?= $success ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger alert-dismissible fade show rounded-4 border-0 shadow-sm" role="alert">
<i class="bi bi-exclamation-triangle-fill me-2"></i> <?= $error ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="row">
<div class="col-lg-4 mb-4">
<div class="card text-center p-4 border-0 shadow-sm h-100" style="border-radius: 20px;">
<div class="mb-4">
<?php if ($user['profile_picture']): ?>
<img src="<?= $user['profile_picture'] ?>?v=<?= time() ?>" alt="Profile Picture" class="rounded-circle border p-2 shadow-sm" style="width: 180px; height: 180px; object-fit: cover; background-color: #fff;">
<?php else: ?>
<div class="rounded-circle bg-light d-flex align-items-center justify-content-center mx-auto text-primary border shadow-sm" style="width: 180px; height: 180px; font-size: 4.5rem; background-color: #fff !important;">
<i class="bi bi-person"></i>
</div>
<?php endif; ?>
</div>
<h4 class="fw-bold mb-1"><?= htmlspecialchars($lang === 'ar' ? ($user['full_name_ar'] ?: $user['full_name_en']) : $user['full_name_en']) ?></h4>
<p class="text-muted mb-3"><?= htmlspecialchars($user['email']) ?></p>
<div class="d-flex justify-content-center gap-2">
<span class="badge bg-soft-primary text-primary px-3 py-2" style="border-radius: 8px;"><?= ucfirst(str_replace('_', ' ', $user['role'])) ?></span>
<?php if ($user['branch_name_en']): ?>
<span class="badge bg-soft-info text-info px-3 py-2" style="border-radius: 8px;">
<i class="bi bi-shop me-1"></i> <?= $lang === 'ar' ? ($user['branch_name_ar'] ?: $user['branch_name_en']) : $user['branch_name_en'] ?>
</span>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-lg-8">
<div class="card p-4 border-0 shadow-sm h-100" style="border-radius: 20px;">
<h5 class="fw-bold mb-4 pb-2 border-bottom"><?= __('personal_information') ?? 'Personal Information' ?></h5>
<div class="row g-4">
<div class="col-md-6">
<label class="text-muted small d-block mb-1"><?= __('full_name_en') ?></label>
<div class="fw-bold fs-5"><?= htmlspecialchars($user['full_name_en'] ?? '-') ?></div>
</div>
<div class="col-md-6">
<label class="text-muted small d-block mb-1"><?= __('full_name_ar') ?></label>
<div class="fw-bold fs-5"><?= htmlspecialchars($user['full_name_ar'] ?? '-') ?></div>
</div>
<div class="col-md-6">
<label class="text-muted small d-block mb-1"><?= __('email') ?></label>
<div class="fw-bold fs-5"><?= htmlspecialchars($user['email'] ?? '-') ?></div>
</div>
<div class="col-md-6">
<label class="text-muted small d-block mb-1"><?= __('username') ?></label>
<div class="fw-bold fs-5"><?= htmlspecialchars($user['username'] ?? '-') ?></div>
</div>
<div class="col-md-6">
<label class="text-muted small d-block mb-1"><?= __('role') ?></label>
<div class="fw-bold fs-5"><?= ucfirst(str_replace('_', ' ', $user['role'])) ?></div>
</div>
<div class="col-md-6">
<label class="text-muted small d-block mb-1"><?= __('branch') ?></label>
<div class="fw-bold fs-5"><?= $lang === 'ar' ? ($user['branch_name_ar'] ?: $user['branch_name_en']) : ($user['branch_name_en'] ?: '-') ?></div>
</div>
</div>
</div>
</div>
</div>
<!-- Edit Profile Modal -->
<div class="modal fade" id="editProfileModal" tabindex="-1" aria-labelledby="editProfileModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content border-0 shadow" style="border-radius: 25px;">
<form action="profile.php" method="POST" enctype="multipart/form-data">
<div class="modal-header border-0 p-4 pb-0">
<h5 class="modal-title fw-bold" id="editProfileModalLabel"><?= __('update_profile') ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body p-4">
<div class="row g-4">
<div class="col-md-12 text-center mb-2">
<div class="position-relative d-inline-block">
<?php if ($user['profile_picture']): ?>
<img id="modalPreview" src="<?= $user['profile_picture'] ?>?v=<?= time() ?>" alt="Profile Picture" class="rounded-circle border p-1 shadow-sm" style="width: 120px; height: 120px; object-fit: cover; background-color: #fff;">
<?php else: ?>
<div id="modalPlaceholder" class="rounded-circle bg-light d-flex align-items-center justify-content-center mx-auto text-primary border shadow-sm" style="width: 120px; height: 120px; font-size: 3rem; background-color: #fff !important;">
<i class="bi bi-person"></i>
</div>
<img id="modalPreview" src="" alt="Profile Picture" class="rounded-circle border p-1 shadow-sm d-none" style="width: 120px; height: 120px; object-fit: cover; background-color: #fff;">
<?php endif; ?>
<label class="btn btn-sm btn-primary rounded-circle position-absolute bottom-0 end-0 p-2 shadow" style="cursor: pointer;">
<i class="bi bi-camera-fill"></i>
<input type="file" name="profile_picture" class="d-none" accept="image/*" onchange="previewImage(this)">
</label>
</div>
<div class="mt-2 small text-muted"><?= __('profile_picture') ?></div>
</div>
<div class="col-md-6">
<label class="form-label fw-bold small"><?= __('full_name_en') ?></label>
<input type="text" name="full_name_en" class="form-control" value="<?= htmlspecialchars($user['full_name_en'] ?? '') ?>" required style="border-radius: 12px;">
</div>
<div class="col-md-6">
<label class="form-label fw-bold small"><?= __('full_name_ar') ?></label>
<input type="text" name="full_name_ar" class="form-control" value="<?= htmlspecialchars($user['full_name_ar'] ?? '') ?>" required style="border-radius: 12px;">
</div>
<div class="col-md-6">
<label class="form-label fw-bold small"><?= __('email') ?></label>
<input type="email" name="email" class="form-control" value="<?= htmlspecialchars($user['email'] ?? '') ?>" required style="border-radius: 12px;">
</div>
<div class="col-md-6">
<label class="form-label fw-bold small"><?= __('username') ?></label>
<input type="text" class="form-control bg-light" value="<?= htmlspecialchars($user['username']) ?>" disabled style="border-radius: 12px;">
</div>
<div class="col-md-12">
<hr class="my-2">
<h6 class="fw-bold mb-3"><?= __('change_password') ?></h6>
</div>
<div class="col-md-6">
<label class="form-label fw-bold small"><?= __('password') ?></label>
<input type="password" name="password" class="form-control" autocomplete="new-password" style="border-radius: 12px;" placeholder="<?= __('leave_blank_to_keep') ?? 'Leave blank to keep current' ?>">
</div>
<div class="col-md-6">
<label class="form-label fw-bold small"><?= __('confirm_password') ?></label>
<input type="password" name="confirm_password" class="form-control" autocomplete="new-password" style="border-radius: 12px;" placeholder="<?= __('confirm_new_password') ?? 'Confirm new password' ?>">
</div>
</div>
</div>
<div class="modal-footer border-0 p-4 pt-0">
<button type="button" class="btn btn-light px-4 fw-bold" data-bs-dismiss="modal" style="border-radius: 12px;"><?= __('cancel') ?></button>
<button type="submit" class="btn btn-primary px-5 fw-bold shadow-sm" style="border-radius: 12px;"><?= __('save_changes') ?></button>
</div>
</form>
</div>
</div>
</div>
<style>
.bg-soft-primary { background-color: rgba(13, 110, 253, 0.1); }
.bg-soft-info { background-color: rgba(13, 202, 240, 0.1); }
</style>
<script>
function previewImage(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
const preview = document.getElementById('modalPreview');
const placeholder = document.getElementById('modalPlaceholder');
preview.src = e.target.result;
preview.classList.remove('d-none');
if (placeholder) placeholder.classList.add('d-none');
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
<?php require_once __DIR__ . '/includes/footer.php'; ?>