301 lines
14 KiB
PHP
301 lines
14 KiB
PHP
<?php
|
|
// Start session if not already started
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// Load configuration
|
|
$config_path = __DIR__ . '/../config.php';
|
|
if (!file_exists($config_path)) {
|
|
die("Configuration file not found: " . $config_path);
|
|
}
|
|
require_once $config_path;
|
|
|
|
// Function to sanitize output
|
|
function e($string) {
|
|
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
// Function to validate phone number
|
|
function validatePhone($phone) {
|
|
return preg_match('/^[0-9]{10,15}$/', $phone);
|
|
}
|
|
|
|
// Function to validate email
|
|
function validateEmail($email) {
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
}
|
|
|
|
// Get business profile
|
|
// Profil usaha
|
|
$query = mysqli_query($conn, "SELECT * FROM profil WHERE id_usaha = 1");
|
|
$profile = mysqli_fetch_assoc($query);
|
|
|
|
// Detect active page
|
|
$current_uri = $_SERVER['REQUEST_URI'];
|
|
?>
|
|
<header id="header" class="header fixed-top">
|
|
<div class="branding d-flex align-items-center">
|
|
<div class="container position-relative d-flex align-items-center justify-content-between">
|
|
<a href="/index.php" class="logo d-flex align-items-center">
|
|
<h1 class="sitename" id="nama_usaha"><?php echo e($profile['nama_usaha'] ?? 'Business Name'); ?></h1>
|
|
<span></span>
|
|
</a>
|
|
|
|
<nav id="navmenu" class="navmenu">
|
|
<ul>
|
|
<li><a href="/index.php#hero" class="<?= strpos($current_uri, 'index.php') !== false ? 'active' : '' ?>">Beranda</a></li>
|
|
<li><a href="/index.php#about" class="<?= strpos($current_uri, '#about') !== false ? 'active' : '' ?>">Tentang Kami</a></li>
|
|
<li><a href="/index.php#testimonials" class="<?= strpos($current_uri, '#testimonials') !== false ? 'active' : '' ?>">Testimoni</a></li>
|
|
<li><a href="/produk" class="<?= strpos($current_uri, '/produk') !== false ? 'active' : '' ?>">Produk</a></li>
|
|
|
|
<?php if (isset($_SESSION['status_login']) && $_SESSION['status_login'] === true && isset($_SESSION['user_global_pembeli'])): ?>
|
|
<!-- Profile Dropdown -->
|
|
<li class="nav-item dropdown pe-3">
|
|
<a class="nav-link nav-profile d-flex align-items-center pe-0" href="#" data-bs-toggle="dropdown">
|
|
Profile
|
|
<i class="bi bi-chevron-down fs-7 ms-1"></i>
|
|
</a>
|
|
<ul class="dropdown-menu dropdown-menu-start dropdown-menu-arrow profile">
|
|
<li class="dropdown-header">
|
|
<h6 style="color:black;"><?php echo e(substr($_SESSION['user_global_pembeli']->nama_pembeli, 0, 10)); ?></h6>
|
|
<span>Saldo : <?php echo e(number_format($_SESSION['user_global_pembeli']->saldo, 0, ',', '.')); ?></span>
|
|
</li>
|
|
<li><hr class="dropdown-divider"></li>
|
|
<li><a class="dropdown-item d-flex align-items-center" href="#" data-bs-toggle="modal" data-bs-target="#viewProfileModal"><i class="bi bi-person"></i> <span>Profil Saya</span></a></li>
|
|
<li><hr class="dropdown-divider"></li>
|
|
<li><a class="dropdown-item d-flex align-items-center" href="../pesanan/"><i class="bi bi-person"></i> <span>Pesanan Saya</span></a></li>
|
|
<li><hr class="dropdown-divider"></li>
|
|
<li><a class="dropdown-item d-flex align-items-center" href="#" id="logout"><i class="bi bi-box-arrow-right"></i> <span>Keluar</span></a></li>
|
|
</ul>
|
|
</li>
|
|
|
|
<!-- Cart Dropdown -->
|
|
<li class="nav-item dropdown pe-3">
|
|
<a class="nav-link nav-profile d-flex align-items-center pe-0" href="#" data-bs-toggle="dropdown">
|
|
Keranjang
|
|
<i class="bi bi-chevron-down fs-7 ms-1"></i>
|
|
</a>
|
|
<ul class="dropdown-menu dropdown-menu-start dropdown-menu-arrow cart">
|
|
<li class="dropdown-header">
|
|
<h6 style="color:black;">Keranjang</h6>
|
|
<span>Saldo: <?php echo e(number_format($_SESSION['user_global_pembeli']->saldo, 0, ',', '.')); ?></span>
|
|
</li>
|
|
<li><hr class="dropdown-divider"></li>
|
|
<li><a class="dropdown-item d-flex align-items-center" href="/keranjang"><i class="bi bi-cart"></i> <span>Lihat Keranjang</span></a></li>
|
|
<li><hr class="dropdown-divider"></li>
|
|
<li><a class="dropdown-item d-flex align-items-center" href="/checkout"><i class="bi bi-credit-card"></i> <span>Checkout</span></a></li>
|
|
</ul>
|
|
</li>
|
|
<?php else: ?>
|
|
<li><a href="/login" class="<?= strpos($current_uri, '/login') !== false ? 'active' : '' ?>">Masuk</a></li>
|
|
<?php endif; ?>
|
|
</ul>
|
|
<i class="mobile-nav-toggle d-xl-none bi bi-list"></i>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<!-- View Profile Modal -->
|
|
<div class="modal fade" id="viewProfileModal" tabindex="-1" aria-labelledby="viewProfileModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h1 class="modal-title fs-5" id="viewProfileModalLabel">Profil Pembeli</h1>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<?php if (isset($_SESSION['status_login']) && $_SESSION['status_login'] === true && isset($_SESSION['user_global_pembeli'])): ?>
|
|
<p><strong>Nama:</strong> <?php echo e($_SESSION['user_global_pembeli']->nama_pembeli); ?></p>
|
|
<p><strong>Email:</strong> <?php echo e($_SESSION['user_global_pembeli']->email); ?></p>
|
|
<p><strong>No. HP:</strong> <?php echo e($_SESSION['user_global_pembeli']->no_hp); ?></p>
|
|
<p><strong>Alamat:</strong> <?php echo e($_SESSION['user_global_pembeli']->alamat); ?></p>
|
|
<p><strong>Saldo:</strong> <?php echo e(number_format($_SESSION['user_global_pembeli']->saldo, 0, ',', '.')); ?></p>
|
|
<?php else: ?>
|
|
<p>Anda belum login.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
|
|
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#updateProfileModal">Ubah Profil</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Update Profile Modal -->
|
|
<div class="modal fade" id="updateProfileModal" tabindex="-1" aria-labelledby="updateProfileModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h1 class="modal-title fs-5" id="updateProfileModalLabel">Update Profil Pembeli</h1>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<?php if (isset($_SESSION['status_login']) && $_SESSION['status_login'] === true): ?>
|
|
<form id="updateProfileForm" action="" method="POST" class="needs-validation" novalidate>
|
|
<div class="mb-3">
|
|
<label for="nama_pembeli" class="form-label">Nama</label>
|
|
<input type="text" class="form-control" id="nama_pembeli" name="nama_pembeli"
|
|
value="<?php echo e($_SESSION['user_global_pembeli']->nama_pembeli); ?>" required>
|
|
<div class="invalid-feedback">Nama harus diisi</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email</label>
|
|
<input type="email" class="form-control" id="email" name="email"
|
|
value="<?php echo e($_SESSION['user_global_pembeli']->email); ?>" required>
|
|
<div class="invalid-feedback">Email tidak valid</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="no_hp" class="form-label">No. HP</label>
|
|
<input type="text" class="form-control" id="no_hp" name="no_hp"
|
|
value="<?php echo e($_SESSION['user_global_pembeli']->no_hp); ?>"
|
|
pattern="[0-9]{10,15}" required>
|
|
<div class="invalid-feedback">No. HP harus berupa angka (10-15 digit)</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="alamat" class="form-label">Alamat</label>
|
|
<input type="text" class="form-control" id="alamat" name="alamat"
|
|
value="<?php echo e($_SESSION['user_global_pembeli']->alamat); ?>" required>
|
|
<div class="invalid-feedback">Alamat harus diisi</div>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary" name="update">Simpan Perubahan</button>
|
|
</form>
|
|
<?php else: ?>
|
|
<p>Anda belum login.</p>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
// Handle profile update
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update'])) {
|
|
if (isset($_SESSION['status_login']) && $_SESSION['status_login'] === true) {
|
|
$id_pembeli = $_SESSION['id_pembeli'];
|
|
$nama_pembeli = trim($_POST['nama_pembeli']);
|
|
$email = trim($_POST['email']);
|
|
$no_hp = trim($_POST['no_hp']);
|
|
$alamat = trim($_POST['alamat']);
|
|
|
|
// Validate inputs
|
|
$errors = [];
|
|
if (empty($nama_pembeli)) $errors[] = "Nama tidak boleh kosong";
|
|
if (!validateEmail($email)) $errors[] = "Email tidak valid";
|
|
if (!validatePhone($no_hp)) $errors[] = "No. HP tidak valid";
|
|
if (empty($alamat)) $errors[] = "Alamat tidak boleh kosong";
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$stmt = $conn->prepare("UPDATE pembeli SET nama_pembeli=?, email=?, no_hp=?, alamat=? WHERE id_pembeli=?");
|
|
$stmt->bind_param("ssssi", $nama_pembeli, $email, $no_hp, $alamat, $id_pembeli);
|
|
|
|
if ($stmt->execute()) {
|
|
// Update session data
|
|
$_SESSION['user_global_pembeli']->nama_pembeli = $nama_pembeli;
|
|
$_SESSION['user_global_pembeli']->email = $email;
|
|
$_SESSION['user_global_pembeli']->no_hp = $no_hp;
|
|
$_SESSION['user_global_pembeli']->alamat = $alamat;
|
|
|
|
echo "<script>
|
|
Swal.fire({
|
|
title: 'Update Berhasil!',
|
|
text: 'Data profil Anda telah diperbarui.',
|
|
icon: 'success'
|
|
}).then((result) => {
|
|
window.location='../index.php';
|
|
});
|
|
</script>";
|
|
} else {
|
|
throw new Exception("Failed to update profile");
|
|
}
|
|
$stmt->close();
|
|
} catch (Exception $e) {
|
|
error_log("Error updating profile: " . $e->getMessage());
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Update Gagal!',
|
|
text: 'Terjadi kesalahan saat memperbarui data.',
|
|
});
|
|
</script>";
|
|
}
|
|
} else {
|
|
$errorMessage = implode("\\n", $errors);
|
|
echo "<script>
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: 'Validasi Gagal!',
|
|
text: '" . $errorMessage . "',
|
|
html: '" . str_replace("\\n", "<br>", $errorMessage) . "'
|
|
});
|
|
</script>";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!-- JavaScript -->
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
// Form validation
|
|
const forms = document.querySelectorAll('.needs-validation');
|
|
Array.from(forms).forEach(form => {
|
|
form.addEventListener('submit', event => {
|
|
if (!form.checkValidity()) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
form.classList.add('was-validated');
|
|
}, false);
|
|
});
|
|
|
|
// Phone number input validation
|
|
const phoneInput = document.getElementById('no_hp');
|
|
if (phoneInput) {
|
|
phoneInput.addEventListener('input', function(e) {
|
|
this.value = this.value.replace(/[^0-9]/g, '');
|
|
});
|
|
}
|
|
|
|
// Logout confirmation
|
|
document.getElementById('logout')?.addEventListener('click', function(e) {
|
|
e.preventDefault();
|
|
Swal.fire({
|
|
title: 'Apakah anda yakin?',
|
|
text: "Anda akan Keluar!",
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonColor: '#3085d6',
|
|
cancelButtonColor: '#d33',
|
|
confirmButtonText: 'Ya, Keluar Saja!'
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
window.location = '../logout.php';
|
|
}
|
|
});
|
|
});
|
|
|
|
// Auto update header
|
|
function fetchHeader() {
|
|
const timestamp = new Date().getTime();
|
|
fetch(`index.php?fetch_data=1&_=${timestamp}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
const namaUsaha = document.getElementById('nama_usaha');
|
|
if (namaUsaha) {
|
|
namaUsaha.innerText = data.nama_usaha;
|
|
document.title = data.nama_usaha + " - Landing Page";
|
|
}
|
|
})
|
|
.catch(error => console.error('Error fetching header:', error));
|
|
}
|
|
|
|
// Fetch header every 2 seconds
|
|
setInterval(fetchHeader, 2000);
|
|
fetchHeader();
|
|
});
|
|
</script>
|