Spaces:
Configuration error

lost-found-tracker-pro / storage.php
brucelloyd1's picture
You are a professional full-stack web developer assistant.
8df07b6 verified
Raw History Blame Contribute Delete
11.4 kB
```php
<?php
include 'includes/auth.php';
redirectIfNotAdmin();
include 'config/database.php';
$db = new Database();
$connection = $db->getConnection();
// Handle storage location actions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['add_location'])) {
$name = $_POST['name'];
$address = $_POST['address'];
$contactInfo = $_POST['contactInfo'];
$query = "INSERT INTO storage_locations (name, address, contactInfo) VALUES (:name, :address, :contactInfo)";
$stmt = $connection->prepare($query);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':address', $address);
$stmt->bindParam(':contactInfo', $contactInfo);
if ($stmt->execute()) {
$success = "Storage location added successfully!";
} else {
$error = "Error adding storage location.";
}
} elseif (isset($_POST['delete_location'])) {
$locationID = $_POST['locationID'];
// Check if location has items
$checkQuery = "SELECT COUNT(*) as itemCount FROM items WHERE storageLocationID = :locationID";
$checkStmt = $connection->prepare($checkQuery);
$checkStmt->bindParam(':locationID', $locationID);
$checkStmt->execute();
$result = $checkStmt->fetch(PDO::FETCH_ASSOC);
if ($result['itemCount'] > 0) {
$error = "Cannot delete storage location that has items assigned. Reassign items first.";
} else {
$deleteQuery = "DELETE FROM storage_locations WHERE locationID = :locationID";
$deleteStmt = $connection->prepare($deleteQuery);
$deleteStmt->bindParam(':locationID', $locationID);
if ($deleteStmt->execute()) {
$success = "Storage location deleted successfully!";
} else {
$error = "Error deleting storage location.";
}
}
}
}
// Get all storage locations
$locationsQuery = "SELECT * FROM storage_locations ORDER BY name";
$locationsStmt = $connection->prepare($locationsQuery);
$locationsStmt->execute();
$storageLocations = $locationsStmt->fetchAll(PDO::FETCH_ASSOC);
// Get items by storage location
$itemsQuery = "
SELECT i.*, sl.name as storageName, r.locationFound, u.name as finderName
FROM items i
LEFT JOIN storage_locations sl ON i.storageLocationID = sl.locationID
LEFT JOIN reports r ON i.reportID = r.reportID
LEFT JOIN users u ON r.userId = u.userId
WHERE i.storageLocationID IS NOT NULL
ORDER BY sl.name, i.createdAt DESC
";
$itemsStmt = $connection->prepare($itemsQuery);
$itemsStmt->execute();
$storedItems = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);
// Group items by storage location
$itemsByLocation = [];
foreach ($storedItems as $item) {
$locationID = $item['storageLocationID'];
if (!isset($itemsByLocation[$locationID])) {
$itemsByLocation[$locationID] = [
'location' => $item['storageName'],
'items' => []
];
}
$itemsByLocation[$locationID]['items'][] = $item;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Storage Management - Lost & Found</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<div class="container">
<nav>
<div class="logo">Lost & Found Pro</div>
<ul class="nav-links">
<li><a href="home.php">Home</a></li>
<li><a href="dashboard.php">Dashboard</a></li>
<li><a href="admin.php">Admin</a></li>
<li><a href="verifications.php">Verifications</a></li>
<li><a href="storage.php">Storage</a></li>
<li><a href="notifications.php">Notifications</a></li>
<li><a href="profile.php">Profile</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
</div>
</header>
<main class="container">
<div class="card">
<div class="card-header">
<h1>Storage Management</h1>
<p>Manage storage locations and view stored items</p>
</div>
<?php if (isset($success)): ?>
<div style="background: #D1FAE5; color: #065F46; padding: 1rem; border-radius: 5px; margin-bottom: 1rem;">
<?php echo $success; ?>
</div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div style="background: #FEE2E2; color: #DC2626; padding: 1rem; border-radius: 5px; margin-bottom: 1rem;">
<?php echo $error; ?>
</div>
<?php endif; ?>
<!-- Add Storage Location Form -->
<div class="mt-3">
<h3>Add New Storage Location</h3>
<form method="POST" class="row">
<div class="col-4">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Location Name" required>
</div>
</div>
<div class="col-4">
<div class="form-group">
<input type="text" name="address" class="form-control" placeholder="Address" required>
</div>
</div>
<div class="col-3">
<div class="form-group">
<input type="text" name="contactInfo" class="form-control" placeholder="Contact Info">
</div>
</div>
<div class="col-1">
<button type="submit" name="add_location" class="btn btn-success w-full">Add</button>
</div>
</form>
</div>
<!-- Storage Locations List -->
<div class="mt-4">
<h3>Storage Locations</h3>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Contact Info</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($storageLocations as $location): ?>
<tr>
<td><?php echo $location['name']; ?></td>
<td><?php echo $location['address']; ?></td>
<td><?php echo $location['contactInfo']; ?></td>
<td>
<form method="POST" style="display: inline;">
<input type="hidden" name="locationID" value="<?php echo $location['locationID']; ?>">
<button type="submit" name="delete_location" class="btn btn-danger btn-sm"
onclick="return confirm('Are you sure you want to delete this storage location?')">
Delete
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<!-- Items by Storage Location -->
<div class="mt-4">
<h3>Stored Items by Location</h3>
<?php if (empty($itemsByLocation)): ?>
<p>No items currently stored in any location.</p>
<?php else: ?>
<?php foreach ($itemsByLocation as $locationID => $data): ?>
<div class="card mt-3">
<h4><?php echo $data['location']; ?></h4>
<div class="row">
<?php foreach ($data['items'] as $item): ?>
<div class="col-4">
<div class="card">
<?php if ($item['imagePath']): ?>
<img src="<?php echo $item['imagePath']; ?>" alt="<?php echo $item['description']; ?>"
style="width: 100%; height: 150px; object-fit: cover; border-radius: 5px;">
<?php else: ?>
<div style="width: 100%; height: 150px; background: #e9ecef; display: flex; align-items: center; justify-content: center; border-radius: 5px;">
<span>No Image</span>
</div>
<?php endif; ?>
<h5 class="mt-2"><?php echo $item['category']; ?></h5>
<p><?php echo substr($item['description'], 0, 100); ?>...</p>
<p><small>Found by: <?php echo $item['finderName']; ?></small></p>
<p><small>Location: <?php echo $item['locationFound']; ?></small></p>
<p>
<span class="badge badge-<?php
switch($item['status']) {
case 'available': echo 'success'; break;
case 'claimed': echo 'warning'; break;
case 'returned': echo 'info'; break;
default: echo 'secondary';
}
?>">
<?php echo ucfirst($item['status']); ?>
</span>
</p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</main>
<footer>
<div class="container">
<div class="footer-content">
<div class="logo">Lost & Found Pro</div>
<p>&copy; 2024 Lost & Found Management System. All rights reserved.</p>
</div>
</div>
</footer>
</body>
</html>
```