Spaces:
Configuration error

brucelloyd1's picture
You are a professional full-stack web developer assistant.
8df07b6 verified
Raw History Blame Contribute Delete
14.9 kB
```php
<?php
include 'includes/auth.php';
include 'config/database.php';
$db = new Database();
$connection = $db->getConnection();
// Handle search and filters
$search = $_GET['search'] ?? '';
$category = $_GET['category'] ?? '';
$status = $_GET['status'] ?? 'available';
// Build query with filters
$query = "SELECT i.*, r.locationFound, r.timestamp as foundDate, sl.name as storageName
FROM items i
LEFT JOIN reports r ON i.reportID = r.reportID
LEFT JOIN storage_locations sl ON i.storageLocationID = sl.locationID
WHERE 1=1";
$params = [];
if ($search) {
$query .= " AND (i.description LIKE :search OR i.category LIKE :search)";
$params[':search'] = "%$search%";
}
if ($category) {
$query .= " AND i.category = :category";
$params[':category'] = $category;
}
if ($status) {
$query .= " AND i.status = :status";
$params[':status'] = $status;
}
$query .= " ORDER BY i.createdAt DESC";
$stmt = $connection->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->execute();
$items = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get categories for filter
$categoriesQuery = "SELECT DISTINCT category FROM items WHERE category IS NOT NULL";
$categoriesStmt = $connection->prepare($categoriesQuery);
$categoriesStmt->execute();
$categories = $categoriesStmt->fetchAll(PDO::FETCH_COLUMN);
// Handle claim submission
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['claim_item'])) {
if (!isLoggedIn() || !isOwner()) {
header("Location: login.php");
exit();
}
$itemID = $_POST['itemID'];
$userId = $_SESSION['user_id'];
// Check if item exists and is available
$checkQuery = "SELECT * FROM items WHERE itemID = :itemID AND status = 'available'";
$checkStmt = $connection->prepare($checkQuery);
$checkStmt->bindParam(':itemID', $itemID);
$checkStmt->execute();
if ($checkStmt->rowCount() > 0) {
// Handle proof upload
$proofPath = null;
if (isset($_FILES['proof']) && $_FILES['proof']['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/proofs/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$fileExtension = pathinfo($_FILES['proof']['name'], PATHINFO_EXTENSION);
$fileName = uniqid() . '.' . $fileExtension;
$targetPath = $uploadDir . $fileName;
if (move_uploaded_file($_FILES['proof']['tmp_name'], $targetPath)) {
$proofPath = $targetPath;
}
}
// Create claim
$claimQuery = "INSERT INTO claims (itemID, ownerUserId, proofPath) VALUES (:itemID, :userId, :proofPath)";
$claimStmt = $connection->prepare($claimQuery);
$claimStmt->bindParam(':itemID', $itemID);
$claimStmt->bindParam(':userId', $userId);
$claimStmt->bindParam(':proofPath', $proofPath);
if ($claimStmt->execute()) {
// Update item status
$updateQuery = "UPDATE items SET status = 'claimed' WHERE itemID = :itemID";
$updateStmt = $connection->prepare($updateQuery);
$updateStmt->bindParam(':itemID', $itemID);
$updateStmt->execute();
$success = "Claim submitted successfully! We'll review your claim and contact you soon.";
}
} else {
$error = "Item not available for claiming.";
}
}
// Get specific item details if requested
$itemDetail = null;
if (isset($_GET['item'])) {
$itemQuery = "SELECT i.*, r.*, sl.name as storageName, sl.address as storageAddress
FROM items i
LEFT JOIN reports r ON i.reportID = r.reportID
LEFT JOIN storage_locations sl ON i.storageLocationID = sl.locationID
WHERE i.itemID = :itemID";
$itemStmt = $connection->prepare($itemQuery);
$itemStmt->bindParam(':itemID', $_GET['item']);
$itemStmt->execute();
$itemDetail = $itemStmt->fetch(PDO::FETCH_ASSOC);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browse Items - 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="items.php">Browse Items</a></li>
<?php if (isLoggedIn()): ?>
<li><a href="dashboard.php">Dashboard</a></li>
<?php if (isFinder()): ?>
<li><a href="report_item.php">Report Item</a></li>
<?php endif; ?>
<li><a href="notifications.php">Notifications</a></li>
<li><a href="profile.php">Profile</a></li>
<li><a href="logout.php">Logout</a></li>
<?php else: ?>
<li><a href="login.php">Login</a></li>
<li><a href="register.php">Register</a></li>
<?php endif; ?>
</ul>
</nav>
</div>
</header>
<main class="container">
<?php if ($itemDetail): ?>
<!-- Item Detail View -->
<div class="card">
<a href="items.php" class="btn btn-secondary mb-3">← Back to Browse</a>
<div class="row">
<div class="col-6">
<?php if ($itemDetail['imagePath']): ?>
<img src="<?php echo $itemDetail['imagePath']; ?>" alt="<?php echo $itemDetail['description']; ?>"
style="width: 100%; max-height: 400px; object-fit: cover; border-radius: 10px;">
<?php else: ?>
<div style="width: 100%; height: 400px; background: #e9ecef; display: flex; align-items: center; justify-content: center; border-radius: 10px;">
<span>No Image Available</span>
</div>
<?php endif; ?>
</div>
<div class="col-6">
<h2><?php echo $itemDetail['category']; ?></h2>
<p><strong>Description:</strong> <?php echo $itemDetail['description']; ?></p>
<p><strong>Found Location:</strong> <?php echo $itemDetail['locationFound']; ?></p>
<p><strong>Found Date:</strong> <?php echo date('M j, Y', strtotime($itemDetail['foundDate'])); ?></p>
<p><strong>Status:</strong>
<span class="badge badge-<?php
switch($itemDetail['status']) {
case 'available': echo 'success'; break;
case 'claimed': echo 'warning'; break;
case 'returned': echo 'info'; break;
default: echo 'secondary';
}
?>">
<?php echo ucfirst($itemDetail['status']); ?>
</span>
</p>
<?php if ($itemDetail['storageName']): ?>
<p><strong>Storage Location:</strong> <?php echo $itemDetail['storageName']; ?></p>
<p><strong>Storage Address:</strong> <?php echo $itemDetail['storageAddress']; ?></p>
<?php endif; ?>
<?php if (isOwner() && $itemDetail['status'] == 'available'): ?>
<div class="mt-4">
<h4>Claim This Item</h4>
<form method="POST" enctype="multipart/form-data">
<input type="hidden" name="itemID" value="<?php echo $itemDetail['itemID']; ?>">
<div class="form-group">
<label for="proof">Upload Proof of Ownership (Optional)</label>
<input type="file" id="proof" name="proof" class="form-control" accept="image/*,.pdf">
<small>Photos, receipts, or documents that prove this item belongs to you</small>
</div>
<button type="submit" name="claim_item" class="btn btn-primary">Submit Claim</button>
</form>
</div>
<?php elseif (!isLoggedIn() && $itemDetail['status'] == 'available'): ?>
<div class="mt-4">
<p>You need to <a href="login.php">login</a> as an owner to claim this item.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php else: ?>
<!-- Browse Items View -->
<div class="card">
<div class="card-header">
<h2>Browse Found Items</h2>
</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; ?>
<!-- Search and Filters -->
<form method="GET" class="row">
<div class="col-4">
<div class="form-group">
<input type="text" name="search" class="form-control" placeholder="Search items..." value="<?php echo htmlspecialchars($search); ?>">
</div>
</div>
<div class="col-3">
<div class="form-group">
<select name="category" class="form-control">
<option value="">All Categories</option>
<?php foreach ($categories as $cat): ?>
<option value="<?php echo $cat; ?>" <?php echo $category == $cat ? 'selected' : ''; ?>>
<?php echo $cat; ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="col-3">
<div class="form-group">
<select name="status" class="form-control">
<option value="available" <?php echo $status == 'available' ? 'selected' : ''; ?>>Available</option>
<option value="claimed" <?php echo $status == 'claimed' ? 'selected' : ''; ?>>Claimed</option>
<option value="returned" <?php echo $status == 'returned' ? 'selected' : ''; ?>>Returned</option>
</select>
</div>
</div>
<div class="col-2">
<button type="submit" class="btn btn-primary w-full">Filter</button>
</div>
</form>
<!-- Items Grid -->
<div class="row mt-3">
<?php foreach ($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: 200px; object-fit: cover; border-radius: 5px;">
<?php else: ?>
<div style="width: 100%; height: 200px; background: #e9ecef; display: flex; align-items: center; justify-content: center; border-radius: 5px;">
<span>No Image</span>
</div>
<?php endif; ?>
<h4 class="mt-2"><?php echo $item['category']; ?></h4>
<p><?php echo substr($item['description'], 0, 100); ?>...</p>
<p><small>Found: <?php echo date('M j, Y', strtotime($item['foundDate'])); ?></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>
<a href="items.php?item=<?php echo $item['itemID']; ?>" class="btn btn-primary btn-sm w-full">View Details</a>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if (empty($items)): ?>
<div class="text-center p-4">
<p>No items found matching your criteria.</p>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</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>
```