38929-vm/includes/header.php
2026-03-10 08:24:46 +00:00

436 lines
20 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/lang.php';
// Authentication check (simple for now)
if (!isset($_SESSION['user_id']) && basename($_SERVER['PHP_SELF']) !== 'login.php') {
header('Location: login.php');
exit;
}
$current_user_id = $_SESSION['user_id'] ?? null;
$current_branch = $_SESSION['branch_id'] ?? null;
$current_role = $_SESSION['role'] ?? 'cashier';
// Branch switching logic
if (isset($_GET['switch_branch']) && $current_user_id) {
$new_branch_id = $_GET['switch_branch'];
if ($new_branch_id === 'all' && $current_role === 'super_admin') {
$_SESSION['branch_id'] = 'all';
$_SESSION['branch_name'] = __('all_branches');
// Redirect back to same page without the switch_branch param but keeping others
$params = $_GET;
unset($params['switch_branch']);
$query = count($params) > 0 ? '?' . http_build_query($params) : '';
$url = strtok($_SERVER["REQUEST_URI"], '?') . $query;
header("Location: $url");
exit;
}
$new_branch_id = (int)$new_branch_id;
// Verify user has access to this branch (or is super_admin)
if ($current_role === 'super_admin') {
$stmt = db()->prepare("SELECT * FROM branches WHERE id = ?");
$stmt->execute([$new_branch_id]);
} else {
$stmt = db()->prepare("SELECT b.* FROM user_branches ub JOIN branches b ON ub.branch_id = b.id WHERE ub.user_id = ? AND ub.branch_id = ?");
$stmt->execute([$current_user_id, $new_branch_id]);
}
$branch = $stmt->fetch();
if ($branch) {
$_SESSION['branch_id'] = $branch['id'];
$_SESSION['branch_name'] = $lang === 'ar' ? ($branch['name_ar'] ?: $branch['name_en']) : $branch['name_en'];
// Redirect back to same page without the switch_branch param but keeping others
$params = $_GET;
unset($params['switch_branch']);
$query = count($params) > 0 ? '?' . http_build_query($params) : '';
$url = strtok($_SERVER["REQUEST_URI"], '?') . $query;
header("Location: $url");
exit;
}
}
// Fetch user branches
$user_branches = [];
if ($current_user_id) {
if ($current_role === 'super_admin') {
$user_branches = db()->query("SELECT * FROM branches")->fetchAll();
} else {
$stmt = db()->prepare("SELECT b.* FROM user_branches ub JOIN branches b ON ub.branch_id = b.id WHERE ub.user_id = ?");
$stmt->execute([$current_user_id]);
$user_branches = $stmt->fetchAll();
}
}
// Ensure branch_name is set if not already
if (!isset($_SESSION['branch_name']) || empty($_SESSION['branch_name'])) {
if (isset($_SESSION['branch_id'])) {
if ($_SESSION['branch_id'] === 'all') {
$_SESSION['branch_name'] = __('all_branches');
} else {
$stmt = db()->prepare("SELECT * FROM branches WHERE id = ?");
$stmt->execute([$_SESSION['branch_id']]);
$b = $stmt->fetch();
if ($b) {
$_SESSION['branch_name'] = $lang === 'ar' ? ($b['name_ar'] ?: $b['name_en']) : $b['name_en'];
}
}
}
}
// Fetch Global Company Info
$stmt = db()->query("SELECT * FROM companies LIMIT 1");
$company_info = $stmt->fetch();
// Fetch Current User Info (for profile picture)
$current_user_data = null;
if ($current_user_id) {
$stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$current_user_id]);
$current_user_data = $stmt->fetch();
}
// Global Permission Check for current page
$current_page = basename($_SERVER['PHP_SELF']);
if ($current_user_id && $current_page !== 'login.php' && $current_page !== 'logout.php') {
if (!has_permission('view', $current_page)) {
if ($current_page !== 'admin.php' && has_permission('view', 'admin.php')) {
header('Location: admin.php');
exit;
} elseif ($current_page !== 'profile.php' && has_permission('view', 'profile.php')) {
header('Location: profile.php');
exit;
} elseif ($current_page !== 'admin.php' && $current_page !== 'profile.php') {
die('Access Denied. You do not have permission to view this page.');
}
}
}
?>
<!doctype html>
<html lang="<?= $lang ?>" dir="<?= is_rtl() ? 'rtl' : 'ltr' ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= htmlspecialchars(is_arabic() ? $company_info['name_ar'] : $company_info['name_en']) ?> - <?= __($title ?? 'dashboard') ?></title>
<?php if ($company_info['favicon']): ?>
<link rel="icon" type="image/x-icon" href="<?= $company_info['favicon'] ?>">
<?php endif; ?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.<?= is_rtl() ? 'rtl.' : '' ?>min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&family=Cairo:wght@400;600;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', <?= is_rtl() ? "'Cairo'," : '' ?> sans-serif;
background-color: #f8f9fa;
}
.sidebar {
min-height: 100vh;
background: #212529;
color: #fff;
padding-top: 1rem;
}
.sidebar .nav-link {
color: rgba(255,255,255,.75);
margin-bottom: 0.5rem;
padding: 0.75rem 1.5rem;
border-radius: 12px;
display: flex;
align-items: center;
}
.sidebar .nav-link i {
margin-right: 0.75rem;
<?= is_rtl() ? 'margin-left: 0.75rem; margin-right: 0;' : '' ?>
font-size: 1.25rem;
}
.sidebar .nav-link:hover, .sidebar .nav-link.active {
color: #fff;
background: rgba(255,255,255,.1);
}
.sidebar .nav-link.active {
background: var(--bs-primary);
}
.main-content {
padding: 2rem;
}
.card {
border: none;
border-radius: 15px;
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
}
.lang-switch {
font-size: 0.9rem;
}
.user-avatar-sm {
width: 32px;
height: 32px;
object-fit: cover;
border-radius: 50%;
}
.dropdown-item i {
margin-right: 0.5rem;
<?= is_rtl() ? 'margin-left: 0.5rem; margin-right: 0;' : '' ?>
}
.user-dropdown-btn {
background-color: white;
border: 1px solid #dee2e6;
border-radius: 50rem;
padding: 0.25rem 1rem;
display: flex;
align-items: center;
cursor: pointer;
transition: all 0.2s;
}
.user-dropdown-btn:hover {
background-color: #f8f9fa;
border-color: #ced4da;
}
/* Ensure dropdowns are above everything */
.dropdown-menu {
z-index: 9999 !important;
}
.dropdown-toggle {
cursor: pointer;
}
.whatsapp-toggle-btn {
transition: all 0.3s;
border: 2px solid #e9ecef;
background: #fff;
}
.whatsapp-toggle-btn.active {
border-color: #25D366;
color: #25D366;
background: rgba(37, 211, 102, 0.1);
}
.whatsapp-toggle-btn:not(.active) {
color: #6c757d;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<!-- Sidebar -->
<nav class="col-md-3 col-lg-2 d-md-block sidebar collapse no-print">
<div class="position-sticky">
<div class="px-4 mb-4 mt-2 text-center">
<?php if ($company_info['logo']): ?>
<img src="<?= $company_info['logo'] ?>" alt="Logo" class="img-fluid mb-2" style="max-height: 60px;">
<?php else: ?>
<h5 class="fw-bold"><?= htmlspecialchars(is_arabic() ? $company_info['name_ar'] : $company_info['name_en']) ?></h5>
<?php endif; ?>
</div>
<ul class="nav flex-column px-3">
<?php if (has_permission('view', 'admin.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'admin.php' ? 'active' : '' ?>" href="admin.php">
<i class="bi bi-speedometer2"></i>
<?= __('dashboard') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'pos.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'pos.php' ? 'active' : '' ?>" href="pos.php">
<i class="bi bi-pc-display-horizontal"></i>
<?= __('pos') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'orders.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'orders.php' ? 'active' : '' ?>" href="orders.php">
<i class="bi bi-cart"></i>
<?= __('orders') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'lab.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'lab.php' ? 'active' : '' ?>" href="lab.php">
<i class="bi bi-box-seam"></i>
<?= __('lab') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'customers.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'customers.php' ? 'active' : '' ?>" href="customers.php">
<i class="bi bi-people"></i>
<?= __('customers') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'reports.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'reports.php' ? 'active' : '' ?>" href="reports.php">
<i class="bi bi-file-earmark-bar-graph"></i>
<?= __('reports') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'ratings.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'ratings.php' ? 'active' : '' ?>" href="ratings.php">
<i class="bi bi-star"></i>
<?= __('ratings') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'items.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'items.php' ? 'active' : '' ?>" href="items.php">
<i class="bi bi-tags"></i>
<?= __('items') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'branches.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'branches.php' ? 'active' : '' ?>" href="branches.php">
<i class="bi bi-shop"></i>
<?= __('branches') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'users.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'users.php' ? 'active' : '' ?>" href="users.php">
<i class="bi bi-person-badge"></i>
<?= __('users') ?>
</a>
</li>
<?php endif; ?>
<hr class="mx-3 my-2 text-secondary">
<?php if (has_permission('view', 'company_profile.php')): ?>
<li class="nav-item">
<a class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'company_profile.php' ? 'active' : '' ?>" href="company_profile.php">
<i class="bi bi-gear"></i>
<?= __('company_profile') ?>
</a>
</li>
<?php endif; ?>
</ul>
<hr class="mx-3 my-4">
<div class="px-3">
<a href="logout.php" class="nav-link text-danger">
<i class="bi bi-box-arrow-right"></i>
<?= __('logout') ?>
</a>
</div>
</div>
</nav>
<!-- Main Content -->
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
<header class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom no-print">
<h1 class="h2"><?= __($title ?? 'dashboard') ?></h1>
<div class="mb-2 mb-md-0">
<div class="d-flex align-items-center">
<?php if (count($user_branches) > 1 || $current_role === 'super_admin'): ?>
<div class="dropdown me-2">
<button class="btn btn-sm btn-outline-primary dropdown-toggle rounded-pill px-3" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-shop me-1"></i> <?= $_SESSION['branch_name'] ?? '' ?>
</button>
<ul class="dropdown-menu shadow border-0 mt-2">
<?php if ($current_role === 'super_admin'): ?>
<li>
<a class="dropdown-item <?= ($_SESSION['branch_id'] ?? '') === 'all' ? 'active' : '' ?>" href="<?= branch_url('all') ?>">
<?= __('all_branches') ?>
</a>
</li>
<li><hr class="dropdown-divider"></li>
<?php endif; ?>
<?php foreach ($user_branches as $ub): ?>
<li>
<a class="dropdown-item <?= $ub['id'] == ($_SESSION['branch_id'] ?? '') ? 'active' : '' ?>" href="<?= branch_url($ub['id']) ?>">
<?= $lang === 'ar' ? ($ub['name_ar'] ?: $ub['name_en']) : $ub['name_en'] ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php else: ?>
<span class="badge bg-primary px-3 py-2 me-2"><?= $_SESSION['branch_name'] ?? '' ?></span>
<?php endif; ?>
<!-- Language Switch -->
<div class="dropdown me-2">
<button class="btn btn-sm btn-outline-secondary dropdown-toggle rounded-pill px-3" type="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-translate me-1"></i> <?= $lang === 'ar' ? 'العربية' : 'English' ?>
</button>
<ul class="dropdown-menu shadow border-0 mt-2">
<li>
<a class="dropdown-item <?= $lang === 'en' ? 'active' : '' ?>" href="<?= lang_url('en') ?>">
English
</a>
</li>
<li>
<a class="dropdown-item <?= $lang === 'ar' ? 'active' : '' ?>" href="<?= lang_url('ar') ?>">
العربية
</a>
</li>
</ul>
</div>
<div class="dropdown">
<button class="user-dropdown-btn dropdown-toggle" type="button" id="userDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<?php if ($current_user_data['profile_picture'] ?? null): ?>
<img src="<?= $current_user_data['profile_picture'] ?>" alt="Profile" class="user-avatar-sm me-2">
<?php else: ?>
<i class="bi bi-person-circle fs-5 me-2 text-secondary"></i>
<?php endif; ?>
<span class="small fw-bold d-none d-sm-inline"><?= $_SESSION['full_name'] ?? '' ?></span>
</button>
<ul class="dropdown-menu dropdown-menu-end shadow border-0 mt-2" aria-labelledby="userDropdown" style="border-radius: 12px;">
<?php if (has_permission('view', 'profile.php')): ?>
<li>
<a class="dropdown-item py-2" href="profile.php">
<i class="bi bi-person-circle"></i> <?= __('user_profile') ?>
</a>
</li>
<?php endif; ?>
<?php if (has_permission('view', 'company_profile.php')): ?>
<li>
<a class="dropdown-item py-2" href="company_profile.php">
<i class="bi bi-gear"></i> <?= __('company_profile') ?>
</a>
</li>
<?php endif; ?>
<li><hr class="dropdown-divider"></li>
<li>
<a class="dropdown-item py-2 text-danger" href="logout.php">
<i class="bi bi-box-arrow-right"></i> <?= __('logout') ?>
</a>
</li>
</ul>
</div>
</div>
</div>
</header>