36874-vm/my_listings.php
Flatlogic Bot b450e7e6df 1.6
2025-12-11 20:06:52 +00:00

309 lines
16 KiB
PHP

<?php
session_start();
require_once 'db/config.php'; // Include the database configuration
// Handle CSV download
if (isset($_GET['download_csv'])) {
$user_id = $_SESSION['user_id'];
$pdo = db();
$query = "SELECT name, description, price, location, status, creation_date FROM items WHERE vendor_id = ?";
$query_params = [$user_id];
if (isset($_GET['search']) && !empty($_GET['search'])) {
$searchTerm = '%' . $_GET['search'] . '%';
$query .= " AND (name LIKE ? OR description LIKE ?)";
$query_params[] = $searchTerm;
$query_params[] = $searchTerm;
}
if (isset($_GET['status']) && !empty($_GET['status'])) {
$statusFilter = $_GET['status'];
$query .= " AND status = ?";
$query_params[] = $statusFilter;
}
$orderBy = " ORDER BY creation_date DESC"; // Default sort
if (isset($_GET['sort']) && !empty($_GET['sort'])) {
switch ($_GET['sort']) {
case 'name_asc':
$orderBy = " ORDER BY name ASC";
break;
case 'name_desc':
$orderBy = " ORDER BY name DESC";
break;
case 'price_asc':
$orderBy = " ORDER BY price ASC";
break;
case 'price_desc':
$orderBy = " ORDER BY price DESC";
break;
case 'date_asc':
$orderBy = " ORDER BY creation_date ASC";
break;
case 'date_desc':
$orderBy = " ORDER BY creation_date DESC";
break;
}
}
$query .= $orderBy;
$stmt = $pdo->prepare($query);
$stmt->execute($query_params);
$items_to_export = $stmt->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="my_listings.csv"');
$output = fopen('php://output', 'w');
if (!empty($items_to_export)) {
fputcsv($output, array_keys($items_to_export[0])); // CSV Header
foreach ($items_to_export as $row) {
fputcsv($output, $row);
}
}
fclose($output);
exit();
}
// Pagination constants
$items_per_page = isset($_GET['items_per_page']) ? (int)$_GET['items_per_page'] : 6;
// Validate $items_per_page to be a positive integer, e.g., 6, 12, 24, 48
if (!in_array($items_per_page, [6, 12, 24, 48])) {
$items_per_page = 6; // Default to 6 if invalid value is provided
}
// Pagination constants
// const ITEMS_PER_PAGE = 6;
// If user is not logged in or not a vendor, redirect
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'vendor') {
header("Location: login.php"); // Redirect to login or a suitable page
exit;
}
$user_id = $_SESSION['user_id'];
$user_name = $_SESSION['user_name'];
$pdo = db(); // Get the PDO connection
$items = []; // Initialize an empty array to hold items
// Fetch items listed by the current vendor
$total_items_query = "SELECT COUNT(*) FROM items WHERE vendor_id = ?";
$total_items_params = [$user_id];
if (isset($_GET['search']) && !empty($_GET['search'])) {
$searchTerm = '%' . $_GET['search'] . '%';
$total_items_query .= " AND (name LIKE ? OR description LIKE ?)";
$total_items_params[] = $searchTerm;
$total_items_params[] = $searchTerm;
}
if (isset($_GET['status']) && !empty($_GET['status'])) {
$statusFilter = $_GET['status'];
$total_items_query .= " AND status = ?";
$total_items_params[] = $statusFilter;
}
$total_stmt = $pdo->prepare($total_items_query);
$total_stmt->execute($total_items_params);
$total_items = $total_stmt->fetchColumn();
$total_pages = ceil($total_items / $items_per_page);
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$current_page = max(1, min($current_page, $total_pages == 0 ? 1 : $total_pages)); // Ensure current page is within valid range
$offset = ($current_page - 1) * $items_per_page;
// Fetch items listed by the current vendor
$query = "SELECT * FROM items WHERE vendor_id = ?";
$query_params = [$user_id];
if (isset($_GET['search']) && !empty($_GET['search'])) {
$searchTerm = '%' . $_GET['search'] . '%';
$query .= " AND (name LIKE ? OR description LIKE ?)";
$query_params[] = $searchTerm;
$query_params[] = $searchTerm;
}
if (isset($_GET['status']) && !empty($_GET['status'])) {
$statusFilter = $_GET['status'];
$query .= " AND status = ?";
$query_params[] = $statusFilter;
}
$orderBy = " ORDER BY creation_date DESC"; // Default sort
if (isset($_GET['sort']) && !empty($_GET['sort'])) {
switch ($_GET['sort']) {
case 'name_asc':
$orderBy = " ORDER BY name ASC";
break;
case 'name_desc':
$orderBy = " ORDER BY name DESC";
break;
case 'price_asc':
$orderBy = " ORDER BY price ASC";
break;
case 'price_desc':
$orderBy = " ORDER BY price DESC";
break;
case 'date_asc':
$orderBy = " ORDER BY creation_date ASC";
break;
case 'date_desc':
$orderBy = " ORDER BY creation_date DESC";
break;
}
}
$query .= $orderBy;
$query .= " LIMIT ? OFFSET ?";
$query_params[] = $items_per_page;
$query_params[] = $offset;
$stmt = $pdo->prepare($query);
$stmt->execute($query_params);
$items = $stmt->fetchAll();
?>
<?php include 'header.php'; ?>
<div class="container mt-5">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4><?php echo htmlspecialchars($user_name); ?>'s Listings</h4>
</div>
<div class="card-body">
<?php if (isset($_SESSION['success_message'])): ?>
<div class="alert alert-success">
<?php echo htmlspecialchars($_SESSION['success_message']); unset($_SESSION['success_message']); ?>
</div>
<?php endif; ?>
<?php if (isset($_SESSION['error_message'])): ?>
<div class="alert alert-danger">
<?php echo htmlspecialchars($_SESSION['error_message']); unset($_SESSION['error_message']); ?>
</div>
<?php endif; ?>
<p>Manage your listed items here.</p>
<form action="my_listings.php" method="get" class="mb-4">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search your listings..." name="search" value="<?php echo htmlspecialchars($_GET['search'] ?? ''); ?>">
<select class="form-select" name="status">
<option value="">All Statuses</option>
<option value="available" <?php echo (isset($_GET['status']) && $_GET['status'] === 'available') ? 'selected' : ''; ?>>Available</option>
<option value="rented" <?php echo (isset($_GET['status']) && $_GET['status'] === 'rented') ? 'selected' : ''; ?>>Rented</option>
<option value="unavailable" <?php echo (isset($_GET['status']) && $_GET['status'] === 'unavailable') ? 'selected' : ''; ?>>Unavailable</option>
</select>
<select class="form-select" name="sort">
<option value="" <?php echo (!isset($_GET['sort']) || $_GET['sort'] === '') ? 'selected' : ''; ?>>Sort By</option>
<option value="name_asc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'name_asc') ? 'selected' : ''; ?>>Name (A-Z)</option>
<option value="name_desc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'name_desc') ? 'selected' : ''; ?>>Name (Z-A)</option>
<option value="price_asc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'price_asc') ? 'selected' : ''; ?>>Price (Low to High)</option>
<option value="price_desc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'price_desc') ? 'selected' : ''; ?>>Price (High to Low)</option>
<option value="date_desc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'date_desc') ? 'selected' : ''; ?>>Date Added (Newest)</option>
<option value="date_asc" <?php echo (isset($_GET['sort']) && $_GET['sort'] === 'date_asc') ? 'selected' : ''; ?>>Date Added (Oldest)</option>
</select>
<select class="form-select" name="items_per_page">
<option value="6" <?php echo ((isset($_GET['items_per_page']) && $_GET['items_per_page'] == 6) || !isset($_GET['items_per_page'])) ? 'selected' : ''; ?>>6 per page</option>
<option value="12" <?php echo (isset($_GET['items_per_page']) && $_GET['items_per_page'] == 12) ? 'selected' : ''; ?>>12 per page</option>
<option value="24" <?php echo (isset($_GET['items_per_page']) && $_GET['items_per_page'] == 24) ? 'selected' : ''; ?>>24 per page</option>
<option value="48" <?php echo (isset($_GET['items_per_page']) && $_GET['items_per_page'] == 48) ? 'selected' : ''; ?>>48 per page</option>
</select>
<button class="btn btn-outline-secondary" type="submit">Filter</button>
<a href="my_listings.php" class="btn btn-outline-danger">Clear Filters</a>
<button class="btn btn-outline-success" type="submit" name="download_csv">Download CSV</button>
</div>
</form>
<?php
$activeFilters = [];
if (!empty($_GET['search'])) {
$activeFilters[] = 'Search: <strong>' . htmlspecialchars($_GET['search']) . '</strong>';
}
if (!empty($_GET['status'])) {
$statusText = [
'available' => 'Available',
'rented' => 'Rented',
'unavailable' => 'Unavailable'
][$_GET['status']] ?? ucfirst($_GET['status']);
$activeFilters[] = 'Status: <strong>' . htmlspecialchars($statusText) . '</strong>';
}
if (!empty($_GET['sort'])) {
$sortText = [
'name_asc' => 'Name (A-Z)',
'name_desc' => 'Name (Z-A)',
'price_asc' => 'Price (Low to High)',
'price_desc' => 'Price (High to Low)',
'date_desc' => 'Date Added (Newest)',
'date_asc' => 'Date Added (Oldest)'
][$_GET['sort']] ?? '';
if (!empty($sortText)) {
$activeFilters[] = 'Sort By: <strong>' . htmlspecialchars($sortText) . '</strong>';
}
}
if (!empty($_GET['items_per_page']) && (int)$_GET['items_per_page'] !== 6) { // Assume 6 is default
$activeFilters[] = 'Items per page: <strong>' . htmlspecialchars($_GET['items_per_page']) . '</strong>';
}
if (!empty($activeFilters)):
?>
<div class="alert alert-info mb-4" role="alert">
<strong>Active Filters:</strong> <?php echo implode(' | ', $activeFilters); ?>
</div>
<?php endif; ?>
<?php if (empty($items)): ?>
<p>You have not listed any items yet. <a href="add_item.php">Add a new item</a>.</p>
<?php else: ?>
<div class="row">
<?php foreach ($items as $item): ?>
<div class="col-md-4 mb-4">
<div class="card h-100">
<img src="<?php echo htmlspecialchars($item['image_url'] ?? 'https://via.placeholder.com/150'); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($item['name']); ?>">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?php echo htmlspecialchars($item['name']); ?></h5>
<p class="card-text flex-grow-1"><?php echo htmlspecialchars($item['description']); ?></p>
<p class="card-text"><strong>Price:</strong> $<?php echo htmlspecialchars(number_format($item['price'], 2)); ?> per day</p>
<p class="card-text"><strong>Location:</strong> <?php echo htmlspecialchars($item['location']); ?></p>
<div class="mt-auto">
<a href="item_details.php?id=<?php echo htmlspecialchars($item['id']); ?>" class="btn btn-info btn-sm">View</a>
<a href="edit_item.php?id=<?php echo htmlspecialchars($item['id']); ?>" class="btn btn-warning btn-sm">Edit</a>
<a href="delete_item.php?id=<?php echo htmlspecialchars($item['id']); ?>" class="btn btn-danger btn-sm">Delete</a>
<p class="text-info mt-2">Status: <?php echo htmlspecialchars(ucfirst($item['status'])); ?></p>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php if ($total_pages > 1): ?>
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<li class="page-item <?php echo ($current_page <= 1) ? 'disabled' : ''; ?>">
<a class="page-link" href="?<?php echo http_build_query(array_merge($_GET, ['page' => $current_page - 1])); ?>" tabindex="-1" aria-disabled="true">Previous</a>
</li>
<?php for ($i = 1; $i <= $total_pages; $i++): ?>
<li class="page-item <?php echo ($current_page == $i) ? 'active' : ''; ?>"><a class="page-link" href="?<?php echo http_build_query(array_merge($_GET, ['page' => $i])); ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
<li class="page-item <?php echo ($current_page >= $total_pages) ? 'disabled' : ''; ?>">
<a class="page-link" href="?<?php echo http_build_query(array_merge($_GET, ['page' => $current_page + 1])); ?>">Next</a>
</li>
</ul>
</nav>
<?php endif; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<?php include 'footer.php'; ?>