Files
Ecobuddy/Views/index.phtml
2025-04-22 01:17:48 +01:00

121 lines
4.9 KiB
PHTML
Executable File

<?php
/**
* Main index view for the EcoBuddy application
*
* This file serves as the main view for the application, displaying
* a table of facilities with various actions depending on the user's
* access level. It includes modals for creating, updating, deleting,
* and viewing statuses of facilities.
*
* The table is populated dynamically using JavaScript, with the data
* stored in sessionStorage.
*/
require('template/header.phtml')
?>
<div class="row">
<div class="col-12 p-0" id="facilityContent">
<!-- Main content -->
<div class="card shadow-sm border-0 rounded-3">
<!-- Title and add button (admins only) -->
<div class="card-header bg-light py-3">
<div class="d-flex justify-content-between align-items-center">
<div class="d-flex align-items-center">
<!-- Search and filter controls -->
<div class="d-flex flex-column flex-lg-row search-controls mx-auto">
<form class="d-flex flex-column flex-lg-row gap-2 w-100" role="search" action="" method="POST">
<div class="input-group flex-grow-1">
<span class="input-group-text bg-light border-end-0">
<i class="bi bi-search text-success"></i>
</span>
<input class="form-control border-start-0" id="searchInput" type="search" name="filter" placeholder="Search..." aria-label="Search">
</div>
</form>
</div>
</div>
<!-- Admin-only buttons -->
<div id="adminButtons" style="display: none;">
<!-- Add new facility button (admin only) -->
<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#createModal">
<i class="bi bi-plus-circle me-1"></i>Add New Facility
</button>
</div>
</div>
</div>
<!-- Pagination controls -->
<div class="card-footer bg-white py-2">
<?php require('template/pagination.phtml');?>
</div>
<!-- Facilities table -->
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover align-middle mb-0" id="facilityTable">
<thead class="table-light">
<tr id="tableHeaderRow">
<!-- Table headers will be dynamically populated by JavaScript -->
</tr>
</thead>
<tbody class="border-top-0">
<!-- Table content will be dynamically populated by JavaScript -->
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Include modal templates -->
<?php require('template/createModal.phtml') ?>
<?php require('template/updateModal.phtml') ?>
<?php require('template/deleteModal.phtml') ?>
<?php require('template/statusModal.phtml') ?>
<!-- Regular user view (no admin buttons) -->
<div id="regularUserView"></div>
<script>
// Function to update UI based on user role
async function updateRoleBasedUI() {
const adminButtons = document.getElementById('adminButtons');
const regularUserView = document.getElementById('regularUserView');
const tableHeaderRow = document.getElementById('tableHeaderRow');
// Validate authentication with server first
let isAdmin = false;
if (auth.isAuthenticated()) {
try {
// This will validate the token with the server and handle refresh if needed
const isValid = await auth.validateOnLoad();
if (isValid) {
isAdmin = auth.isAdmin();
}
} catch (error) {
console.error('Error validating authentication:', error);
isAdmin = false;
}
}
// Show/hide admin buttons
if (adminButtons) {
adminButtons.style.display = isAdmin ? 'block' : 'none';
}
if (regularUserView) {
regularUserView.style.display = isAdmin ? 'none' : 'block';
}
}
// Update UI when the page loads
document.addEventListener('DOMContentLoaded', updateRoleBasedUI);
// Also update when auth state changes
window.addEventListener('storage', function(e) {
if (e.key === 'token' || e.key === 'user') {
updateRoleBasedUI();
}
});
</script>
<?php require('template/footer.phtml');?>