navigation.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * components/navigation.php
  4. *
  5. * Navigation component that replaces modX Personalize and Wayfinder snippets.
  6. * Handles authentication-based navigation display.
  7. *
  8. * Usage: include __DIR__.'/components/navigation.php';
  9. */
  10. require_once __DIR__.'/../lib/auth.php';
  11. // Start session if not already started
  12. if (session_status() === PHP_SESSION_NONE) {
  13. session_start();
  14. }
  15. // Determine navigation based on authentication status
  16. $isLoggedIn = isLoggedIn();
  17. // Navigation configuration
  18. $siteName = 'Crop Management Platform';
  19. if ($isLoggedIn) {
  20. // Logged-in user navigation
  21. $navItems = [
  22. [ 'href' => '/dashboard/dashboard.php', 'label' => 'Dashboard' ],
  23. [ 'href' => '/dashboard/crop-analysis/soil-analysis.php', 'label' => 'Soil Analysis' ],
  24. [ 'href' => '/dashboard/crop-analysis/soil-report.php', 'label' => 'Reports' ],
  25. [ 'href' => '/login/change-password.php', 'label' => 'Account' ],
  26. [ 'href' => '/login/logout.php', 'label' => 'Logout' ],
  27. ];
  28. $sidebarItems = [
  29. [ 'href' => '/dashboard/dashboard.php', 'label' => 'Home', 'icon' => 'fas fa-home' ],
  30. [
  31. 'label' => 'Soil Analysis',
  32. 'icon' => 'fas fa-seedling',
  33. 'children' => [
  34. [ 'href' => '/dashboard/crop-analysis/soil-test-data.php', 'label' => 'New Test' ],
  35. [ 'href' => '/dashboard/crop-analysis/soil-analysis.php', 'label' => 'View Results' ],
  36. [ 'href' => '/dashboard/crop-analysis/soil-report.php', 'label' => 'Reports' ],
  37. ]
  38. ],
  39. [ 'href' => '/dashboard/inbox.php', 'label' => 'Inbox', 'icon' => 'fas fa-inbox' ],
  40. [ 'href' => '/dashboard/planning-calendar.php', 'label' => 'Calendar', 'icon' => 'fas fa-calendar' ],
  41. [ 'href' => '/login/change-password.php', 'label' => 'Account', 'icon' => 'fas fa-user-cog' ],
  42. ];
  43. } else {
  44. // Guest navigation
  45. $navItems = [
  46. [ 'href' => '/', 'label' => 'Home' ],
  47. [ 'href' => '/login/login.php', 'label' => 'Login' ],
  48. [ 'href' => '/login/register.php', 'label' => 'Register' ],
  49. ];
  50. $sidebarItems = []; // No sidebar for guests
  51. }
  52. // Determine active item based on current URL
  53. $currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?? '';
  54. $activeItem = '';
  55. foreach ($navItems as $item) {
  56. if ($item['href'] === $currentPath) {
  57. $activeItem = $item['label'];
  58. break;
  59. }
  60. }
  61. // Function to render navbar
  62. function renderNavbar() {
  63. global $siteName, $navItems, $activeItem;
  64. ?>
  65. <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  66. <div class="container-fluid">
  67. <a class="navbar-brand" href="/"><?= htmlspecialchars($siteName, ENT_QUOTES, 'UTF-8') ?></a>
  68. <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#topNavbar" aria-controls="topNavbar" aria-expanded="false" aria-label="Toggle navigation">
  69. <span class="navbar-toggler-icon"></span>
  70. </button>
  71. <div class="collapse navbar-collapse" id="topNavbar">
  72. <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
  73. <?php foreach ($navItems as $item): ?>
  74. <li class="nav-item">
  75. <a class="nav-link<?= ($activeItem === $item['label'] ? ' active' : '') ?>" href="<?= htmlspecialchars($item['href'], ENT_QUOTES, 'UTF-8') ?>">
  76. <?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?>
  77. </a>
  78. </li>
  79. <?php endforeach; ?>
  80. </ul>
  81. </div>
  82. </div>
  83. </nav>
  84. <?php
  85. }
  86. // Function to render sidebar
  87. function renderSidebar() {
  88. global $sidebarItems, $activeItem;
  89. if (empty($sidebarItems)) {
  90. return; // No sidebar for guests
  91. }
  92. ?>
  93. <div class="sb-sidenav-menu">
  94. <div class="nav">
  95. <?php foreach ($sidebarItems as $item): ?>
  96. <?php if (isset($item['children'])): ?>
  97. <!-- Parent item with children -->
  98. <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#nav-<?= md5($item['label']) ?>" aria-expanded="false" aria-controls="nav-<?= md5($item['label']) ?>">
  99. <div class="sb-nav-link-icon"><i class="<?= htmlspecialchars($item['icon'], ENT_QUOTES, 'UTF-8') ?>"></i></div>
  100. <?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?>
  101. <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
  102. </a>
  103. <div class="collapse" id="nav-<?= md5($item['label']) ?>" aria-labelledby="heading-<?= md5($item['label']) ?>">
  104. <nav class="sb-sidenav-menu-nested nav">
  105. <?php foreach ($item['children'] as $child): ?>
  106. <a class="nav-link<?= ($activeItem === $child['label'] ? ' active' : '') ?>" href="<?= htmlspecialchars($child['href'], ENT_QUOTES, 'UTF-8') ?>">
  107. <?= htmlspecialchars($child['label'], ENT_QUOTES, 'UTF-8') ?>
  108. </a>
  109. <?php endforeach; ?>
  110. </nav>
  111. </div>
  112. <?php else: ?>
  113. <!-- Regular item -->
  114. <a class="nav-link<?= ($activeItem === $item['label'] ? ' active' : '') ?>" href="<?= htmlspecialchars($item['href'], ENT_QUOTES, 'UTF-8') ?>">
  115. <div class="sb-nav-link-icon"><i class="<?= htmlspecialchars($item['icon'], ENT_QUOTES, 'UTF-8') ?>"></i></div>
  116. <?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?>
  117. </a>
  118. <?php endif; ?>
  119. <?php endforeach; ?>
  120. </div>
  121. </div>
  122. <?php
  123. }
  124. // Auto-render based on context
  125. // This allows the component to be included and automatically render the appropriate navigation
  126. if (!isset($skipAutoRender) || !$skipAutoRender) {
  127. if ($isLoggedIn) {
  128. renderNavbar();
  129. } else {
  130. renderNavbar(); // Could render different navbar for guests if needed
  131. }
  132. }
  133. ?>