prepare("UPDATE users SET first_name = ?, last_name = ?, email = ? WHERE id = ?");
$stmt->execute([$first_name, $last_name, $email, $user_id]);
$_SESSION['profile_message'] = 'اطلاعات شما با موفقیت بهروزرسانی شد.';
$_SESSION['profile_message_type'] = 'success';
$redirect_page = 'account';
} elseif ($action === 'update_password') {
$new_password = $_POST['new_password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
if (strlen($new_password) < 8) {
throw new Exception('رمز عبور جدید باید حداقل ۸ کاراکتر باشد.');
} elseif ($new_password !== $confirm_password) {
throw new Exception('رمزهای عبور جدید با هم مطابقت ندارند.');
}
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
$stmt->execute([$hashed_password, $user_id]);
$_SESSION['profile_message'] = 'رمز عبور شما با موفقیت تغییر کرد.';
$_SESSION['profile_message_type'] = 'success';
$redirect_page = 'account';
} elseif ($action === 'add_address') {
$province = trim($_POST['province'] ?? '');
$city = trim($_POST['city'] ?? '');
$address_line = trim($_POST['address_line'] ?? '');
$postal_code = trim($_POST['postal_code'] ?? '');
$is_default = isset($_POST['is_default']);
if (empty($province) || empty($city) || empty($address_line) || empty($postal_code)) {
throw new Exception('لطفاً تمام فیلدهای آدرس را پر کنید.');
}
$pdo->beginTransaction();
if ($is_default) {
$stmt = $pdo->prepare("UPDATE user_addresses SET is_default = 0 WHERE user_id = ?");
$stmt->execute([$user_id]);
}
$stmt = $pdo->prepare("INSERT INTO user_addresses (user_id, province, city, address_line, postal_code, is_default) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$user_id, $province, $city, $address_line, $postal_code, $is_default ? 1 : 0]);
$pdo->commit();
$_SESSION['profile_message'] = 'آدرس جدید با موفقیت اضافه شد.';
$_SESSION['profile_message_type'] = 'success';
$redirect_page = 'addresses';
} elseif ($action === 'delete_address') {
$address_id = $_POST['address_id'] ?? 0;
$stmt = $pdo->prepare("DELETE FROM user_addresses WHERE id = ? AND user_id = ?");
if (!$stmt->execute([$address_id, $user_id])) throw new Exception('خطا در حذف آدرس.');
$_SESSION['profile_message'] = 'آدرس با موفقیت حذف شد.';
$_SESSION['profile_message_type'] = 'success';
$redirect_page = 'addresses';
} elseif ($action === 'set_default_address') {
$address_id = $_POST['address_id'] ?? 0;
$pdo->beginTransaction();
$stmt1 = $pdo->prepare("UPDATE user_addresses SET is_default = 0 WHERE user_id = ?");
$stmt1->execute([$user_id]);
$stmt2 = $pdo->prepare("UPDATE user_addresses SET is_default = 1 WHERE id = ? AND user_id = ?");
$stmt2->execute([$address_id, $user_id]);
$pdo->commit();
$_SESSION['profile_message'] = 'آدرس پیشفرض با موفقیت تغییر کرد.';
$_SESSION['profile_message_type'] = 'success';
$redirect_page = 'addresses';
}
} catch (PDOException $e) {
if (isset($pdo) && $pdo->inTransaction()) {
$pdo->rollBack();
}
if ($e->errorInfo[1] == 1062) { // Duplicate entry
$_SESSION['profile_message'] = 'این ایمیل قبلاً ثبت شده است.';
} else {
$_SESSION['profile_message'] = 'یک خطای پایگاه داده رخ داد: ' . $e->getMessage();
}
$_SESSION['profile_message_type'] = 'danger';
} catch (Exception $e) {
$_SESSION['profile_message'] = $e->getMessage();
$_SESSION['profile_message_type'] = 'danger';
}
header('Location: profile.php?page=' . $redirect_page);
exit;
}
// Determine current page
$page = $_GET['page'] ?? 'dashboard';
$page_map = [
'dashboard' => 'داشبورد',
'orders' => 'سفارشات من',
'addresses' => 'آدرسهای من',
'account' => 'جزئیات حساب',
];
$page_title = $page_map[$page] ?? 'حساب کاربری';
// Retrieve flash message
if (isset($_SESSION['profile_message'])) {
$flash_message = $_SESSION['profile_message'];
$flash_message_type = $_SESSION['profile_message_type'];
unset($_SESSION['profile_message']);
unset($_SESSION['profile_message_type']);
}
// Fetch all necessary data
$stmt_user = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt_user->execute([$user_id]);
$user = $stmt_user->fetch(PDO::FETCH_ASSOC);
$stmt_orders = $pdo->prepare("SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC");
$stmt_orders->execute([$user_id]);
$orders = $stmt_orders->fetchAll(PDO::FETCH_ASSOC);
$stmt_addresses = $pdo->prepare("SELECT * FROM user_addresses WHERE user_id = ? ORDER BY is_default DESC, id DESC");
$stmt_addresses->execute([$user_id]);
$addresses = $stmt_addresses->fetchAll(PDO::FETCH_ASSOC);
$total_purchase_amount = array_reduce($orders, function ($sum, $order) {
return strtolower($order['status']) === 'completed' ? $sum + $order['total_amount'] : $sum;
}, 0);
?>
= htmlspecialchars($page_title) ?> - پنل کاربری
سلام، = htmlspecialchars($user['first_name'] ?? 'کاربر'); ?> عزیز!
به پنل کاربری خود خوش آمدید. از اینجا میتوانید آخرین سفارشات خود را مشاهده کرده و حساب خود را مدیریت کنید.
تعداد کل سفارشات
= count($orders); ?>
مجموع خرید (تکمیل شده)
= number_format($total_purchase_amount); ?> تومان
شما هنوز هیچ سفارشی ثبت نکردهاید.
| # |
تاریخ |
وضعیت |
مبلغ کل |
رهگیری |
| = htmlspecialchars($order['id']); ?> |
= jdate('d F Y', strtotime($order['created_at'])); ?> |
= htmlspecialchars($order['status']); ?>
|
= number_format($order['total_amount']); ?> تومان |
نمایش
|
شما هنوز هیچ آدرسی ثبت نکردهاید.
= htmlspecialchars(implode(', ', array_filter([$address['province'], $address['city'], $address['address_line'], "کدپستی: ".$address['postal_code']]))) ?>
پیشفرض
صفحه مورد نظر یافت نشد.