Auto commit: 2025-10-08T05:46:19.932Z

This commit is contained in:
Flatlogic Bot 2025-10-08 05:46:19 +00:00
parent 238c66845b
commit 1609d489f1
6 changed files with 287 additions and 18 deletions

41
claim.php Normal file
View File

@ -0,0 +1,41 @@
<?php
session_start();
require_once 'db/config.php';
// Ensure user is logged in and is an NGO
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'ngo') {
header("Location: login.php");
exit;
}
// Check if listing_id is provided
if (!isset($_GET['listing_id']) || !is_numeric($_GET['listing_id'])) {
header("Location: dashboard.php?error=invalid_listing");
exit;
}
$listing_id = $_GET['listing_id'];
$ngo_id = $_SESSION['user_id'];
$pdo = db();
// Check if the listing exists and is available
$stmt = $pdo->prepare("SELECT * FROM food_listings WHERE id = ? AND status = 'listed'");
$stmt->execute([$listing_id]);
$listing = $stmt->fetch();
if (!$listing) {
header("Location: dashboard.php?error=listing_not_available");
exit;
}
// Update the listing to mark it as claimed
$stmt = $pdo->prepare("UPDATE food_listings SET status = 'claimed', claimed_by_id = ? WHERE id = ?");
$success = $stmt->execute([$ngo_id, $listing_id]);
if ($success) {
header("Location: dashboard.php?success=claimed");
} else {
header("Location: dashboard.php?error=claim_failed");
}
exit;

View File

@ -1,20 +1,108 @@
<?php <?php
session_start(); session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) { // Ensure user is logged in and is an NGO
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'ngo') {
header("Location: login.php"); header("Location: login.php");
exit; exit;
} }
$pdo = db();
$ngo_id = $_SESSION['user_id'];
// Fetch available food listings
$stmt_available = $pdo->prepare("
SELECT fl.*, u.name AS restaurant_name
FROM food_listings fl
JOIN users u ON fl.user_id = u.id
WHERE fl.status = 'listed'
ORDER BY fl.pickup_deadline ASC
");
$stmt_available->execute();
$available_listings = $stmt_available->fetchAll(PDO::FETCH_ASSOC);
// Fetch listings claimed by the current NGO
$stmt_claimed = $pdo->prepare("
SELECT fl.*, u.name AS restaurant_name
FROM food_listings fl
JOIN users u ON fl.user_id = u.id
WHERE fl.status = 'claimed' AND fl.claimed_by_id = ?
ORDER BY fl.pickup_deadline ASC
");
$stmt_claimed->execute([$ngo_id]);
$claimed_listings = $stmt_claimed->fetchAll(PDO::FETCH_ASSOC);
?> ?>
<?php include 'partials/header.php'; ?> <?php include 'partials/header.php'; ?>
<div class="container py-5"> <div class="container py-5">
<div class="row"> <div class="d-flex justify-content-between align-items-center mb-4">
<div class="col-md-12"> <h2 class="mb-0">NGO Dashboard</h2>
<h2 class="text-center mb-4">Dashboard</h2> <a href="logout.php" class="btn btn-danger">Logout</a>
<p class="text-center">Welcome, <?php echo htmlspecialchars($_SESSION['user_email']); ?>!</p>
<p class="text-center">This is your dashboard. More features will be added soon.</p>
</div> </div>
<p>Welcome, <strong><?php echo htmlspecialchars($_SESSION['user_name']); ?></strong>! Here are the current listings.</p>
<?php if (isset($_GET['success']) && $_GET['success'] == 'claimed'): ?>
<div class="alert alert-success">Donation claimed successfully! You can see it in your claimed donations list below.</div>
<?php elseif (isset($_GET['error'])): ?>
<div class="alert alert-danger">There was an error. The donation might have already been claimed.</div>
<?php endif; ?>
<!-- Claimed Listings -->
<hr class="my-5">
<h3 class="mb-4">My Claimed Donations</h3>
<div class="row">
<?php if (empty($claimed_listings)): ?>
<div class="col-12">
<div class="alert alert-secondary">You have not claimed any donations yet.</div>
</div>
<?php else: ?>
<?php foreach ($claimed_listings as $listing): ?>
<div class="col-md-6 col-lg-4 mb-4">
<div class="card h-100 border-primary">
<div class="card-header bg-primary text-white">Claimed</div>
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?php echo htmlspecialchars($listing['title']); ?></h5>
<h6 class="card-subtitle mb-2 text-muted">From: <?php echo htmlspecialchars($listing['restaurant_name']); ?></h6>
<p class="card-text"><?php echo htmlspecialchars($listing['description']); ?></p>
<ul class="list-group list-group-flush mt-auto">
<li class="list-group-item"><strong>Pickup By:</strong> <span class="fw-bold"><?php echo date('g:i A, M j', strtotime($listing['pickup_deadline'])); ?></span></li>
</ul>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Available Listings -->
<hr class="my-5">
<h3 class="mb-4">Available Food Donations</h3>
<div class="row">
<?php if (empty($available_listings)): ?>
<div class="col-12">
<div class="alert alert-info">There are no available food donations at the moment. Please check back later.</div>
</div>
<?php else: ?>
<?php foreach ($available_listings as $listing): ?>
<div class="col-md-6 col-lg-4 mb-4">
<div class="card h-100">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?php echo htmlspecialchars($listing['title']); ?></h5>
<h6 class="card-subtitle mb-2 text-muted">From: <?php echo htmlspecialchars($listing['restaurant_name']); ?></h6>
<p class="card-text"><?php echo htmlspecialchars($listing['description']); ?></p>
<ul class="list-group list-group-flush mt-auto">
<li class="list-group-item"><strong>Quantity:</strong> <?php echo htmlspecialchars($listing['quantity']); ?></li>
<li class="list-group-item"><strong>Pickup By:</strong> <span class="text-danger fw-bold"><?php echo date('g:i A, M j', strtotime($listing['pickup_deadline'])); ?></span></li>
</ul>
<a href="claim.php?listing_id=<?php echo $listing['id']; ?>" class="btn btn-success mt-3" onclick="return confirm('Are you sure you want to claim this donation?');">Claim Donation</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div> </div>
</div> </div>

View File

@ -3,16 +3,34 @@ require_once __DIR__ . '/config.php';
try { try {
$pdo = db(); $pdo = db();
$migrationsDir = __DIR__ . '/migrations';
$files = glob($migrationsDir . '/*.sql');
foreach ($files as $file) { // 1. Create migrations table if it doesn't exist
$pdo->exec('CREATE TABLE IF NOT EXISTS migrations (migration VARCHAR(255) PRIMARY KEY)');
// 2. Get all executed migrations
$executedMigrations = $pdo->query('SELECT migration FROM migrations')->fetchAll(PDO::FETCH_COLUMN);
// 3. Find all migration files
$migrationFiles = glob(__DIR__ . '/migrations/*.sql');
// 4. Determine which migrations to run
foreach ($migrationFiles as $file) {
$migrationName = basename($file);
if (!in_array($migrationName, $executedMigrations)) {
// 5. Execute the migration
$sql = file_get_contents($file); $sql = file_get_contents($file);
$pdo->exec($sql); $pdo->exec($sql);
echo "Migration from $file executed successfully.\n";
// 6. Record the migration
$stmt = $pdo->prepare('INSERT INTO migrations (migration) VALUES (?)');
$stmt->execute([$migrationName]);
echo "Migration from $migrationName executed successfully.\n";
} }
}
echo "All new migrations have been executed.";
} catch (PDOException $e) { } catch (PDOException $e) {
die("Database migration failed: " . $e->getMessage()); die("Database migration failed: " . $e->getMessage());
} }

View File

@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS `food_listings` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`user_id` INT NOT NULL,
`title` VARCHAR(255) NOT NULL,
`description` TEXT,
`quantity` VARCHAR(255),
`food_type` VARCHAR(50),
`prepared_time` DATETIME,
`pickup_deadline` DATETIME,
`photo_url` VARCHAR(255),
`status` VARCHAR(50) DEFAULT 'listed',
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
);

View File

@ -0,0 +1,8 @@
ALTER TABLE `food_listings` ADD `claimed_by_id` INT(11) NULL DEFAULT NULL AFTER `user_id`,
ADD INDEX `fk_claimed_by_user_idx` (`claimed_by_id` ASC);
ALTER TABLE `food_listings` ADD CONSTRAINT `fk_claimed_by_user`
FOREIGN KEY (`claimed_by_id`)
REFERENCES `users` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE;

View File

@ -1,5 +1,6 @@
<?php <?php
session_start(); session_start();
require_once 'db/config.php';
// Redirect to login if not logged in or not a restaurant // Redirect to login if not logged in or not a restaurant
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'restaurant') { if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'restaurant') {
@ -7,13 +8,112 @@ if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'restaurant') {
exit; exit;
} }
$message = '';
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_listing'])) {
$user_id = $_SESSION['user_id'];
$title = $_POST['title'];
$description = $_POST['description'];
$quantity = $_POST['quantity'];
$food_type = $_POST['food_type'];
$prepared_time = $_POST['prepared_time'];
$pickup_deadline = $_POST['pickup_deadline'];
// Photo upload will be handled later
if (empty($title) || empty($quantity) || empty($prepared_time) || empty($pickup_deadline)) {
$message = '<div class="alert alert-danger">Please fill in all required fields.</div>';
} else {
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO food_listings (user_id, title, description, quantity, food_type, prepared_time, pickup_deadline) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->execute([$user_id, $title, $description, $quantity, $food_type, $prepared_time, $pickup_deadline]);
$message = '<div class="alert alert-success">Food listing created successfully!</div>';
} catch (PDOException $e) {
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
}
}
// Fetch existing listings for this restaurant
$listings = [];
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM food_listings WHERE user_id = ? ORDER BY created_at DESC");
$stmt->execute([$_SESSION['user_id']]);
$listings = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// Handle error
}
?> ?>
<?php include 'partials/header.php'; ?> <?php include 'partials/header.php'; ?>
<div class="container py-5"> <div class="container py-5">
<h1 class="text-center">Restaurant Dashboard</h1> <div class="d-flex justify-content-between align-items-center mb-4">
<p class="text-center">Welcome, <?php echo htmlspecialchars($_SESSION['user_email']); ?>!</p> <h1>Restaurant Dashboard</h1>
<p class="text-center">This is where you will manage your food listings.</p> <a href="logout.php" class="btn btn-danger">Logout</a>
</div>
<p class="lead">Welcome, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!</p>
<hr>
<div class="row">
<div class="col-md-8">
<h2>Your Food Listings</h2>
<?php if (empty($listings)): ?>
<p>You haven't posted any food listings yet.</p>
<?php else: ?>
<div class="list-group">
<?php foreach ($listings as $listing): ?>
<div class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1"><?php echo htmlspecialchars($listing['title']); ?></h5>
<small>Status: <?php echo htmlspecialchars($listing['status']); ?></small>
</div>
<p class="mb-1"><?php echo htmlspecialchars($listing['description']); ?></p>
<small>Quantity: <?php echo htmlspecialchars($listing['quantity']); ?></small>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="col-md-4">
<h2>Add New Listing</h2>
<?php echo $message; ?>
<form action="listings.php" method="POST">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="description" class="form-label">Description</label>
<textarea class="form-control" id="description" name="description" rows="3"></textarea>
</div>
<div class="mb-3">
<label for="quantity" class="form-label">Quantity (e.g., "Serves 10-15 people")</label>
<input type="text" class="form-control" id="quantity" name="quantity" required>
</div>
<div class="mb-3">
<label for="food_type" class="form-label">Food Type</label>
<select class="form-select" id="food_type" name="food_type">
<option value="veg">Vegetarian</option>
<option value="non-veg">Non-Vegetarian</option>
<option value="halal">Halal</option>
<option value="other">Other</option>
</select>
</div>
<div class="mb-3">
<label for="prepared_time" class="form-label">Time of Preparation</label>
<input type="datetime-local" class="form-control" id="prepared_time" name="prepared_time" required>
</div>
<div class="mb-3">
<label for="pickup_deadline" class="form-label">Pickup Deadline</label>
<input type="datetime-local" class="form-control" id="pickup_deadline" name="pickup_deadline" required>
</div>
<button type="submit" name="add_listing" class="btn btn-primary">Add Listing</button>
</form>
</div>
</div>
</div> </div>
<?php include 'partials/footer.php'; ?> <?php include 'partials/footer.php'; ?>