From 1609d489f14b8f3d63685a86ccc3e907bc5ab14d Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Wed, 8 Oct 2025 05:46:19 +0000 Subject: [PATCH] Auto commit: 2025-10-08T05:46:19.932Z --- claim.php | 41 +++++++ dashboard.php | 100 +++++++++++++++- db/migrate.php | 34 ++++-- .../003_create_food_listings_table.sql | 14 +++ .../004_add_claimed_by_to_listings.sql | 8 ++ listings.php | 108 +++++++++++++++++- 6 files changed, 287 insertions(+), 18 deletions(-) create mode 100644 claim.php create mode 100644 db/migrations/003_create_food_listings_table.sql create mode 100644 db/migrations/004_add_claimed_by_to_listings.sql diff --git a/claim.php b/claim.php new file mode 100644 index 0000000..5d399d5 --- /dev/null +++ b/claim.php @@ -0,0 +1,41 @@ +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; diff --git a/dashboard.php b/dashboard.php index 7b72507..df8b8cd 100644 --- a/dashboard.php +++ b/dashboard.php @@ -1,20 +1,108 @@ 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); + ?>
+
+

NGO Dashboard

+ Logout +
+ +

Welcome, ! Here are the current listings.

+ + +
Donation claimed successfully! You can see it in your claimed donations list below.
+ +
There was an error. The donation might have already been claimed.
+ + + +
+

My Claimed Donations

-
-

Dashboard

-

Welcome, !

-

This is your dashboard. More features will be added soon.

-
+ +
+
You have not claimed any donations yet.
+
+ + +
+
+
Claimed
+
+
+
From:
+

+
    +
  • Pickup By:
  • +
+
+
+
+ + +
+ + +
+

Available Food Donations

+
+ +
+
There are no available food donations at the moment. Please check back later.
+
+ + +
+
+
+
+
From:
+

+
    +
  • Quantity:
  • +
  • Pickup By:
  • +
+ Claim Donation +
+
+
+ +
diff --git a/db/migrate.php b/db/migrate.php index 074e173..154e17a 100644 --- a/db/migrate.php +++ b/db/migrate.php @@ -3,16 +3,34 @@ require_once __DIR__ . '/config.php'; try { $pdo = db(); - $migrationsDir = __DIR__ . '/migrations'; - $files = glob($migrationsDir . '/*.sql'); - foreach ($files as $file) { - $sql = file_get_contents($file); - $pdo->exec($sql); - echo "Migration from $file executed successfully.\n"; + // 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); + $pdo->exec($sql); + + // 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) { die("Database migration failed: " . $e->getMessage()); -} - +} \ No newline at end of file diff --git a/db/migrations/003_create_food_listings_table.sql b/db/migrations/003_create_food_listings_table.sql new file mode 100644 index 0000000..8a6ff2c --- /dev/null +++ b/db/migrations/003_create_food_listings_table.sql @@ -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 +); diff --git a/db/migrations/004_add_claimed_by_to_listings.sql b/db/migrations/004_add_claimed_by_to_listings.sql new file mode 100644 index 0000000..b22d7b8 --- /dev/null +++ b/db/migrations/004_add_claimed_by_to_listings.sql @@ -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; \ No newline at end of file diff --git a/listings.php b/listings.php index 98372c8..03567dd 100644 --- a/listings.php +++ b/listings.php @@ -1,5 +1,6 @@ Please fill in all required fields.'; + } 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 = '
Food listing created successfully!
'; + } catch (PDOException $e) { + $message = '
Error: ' . $e->getMessage() . '
'; + } + } +} + +// 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 +} + ?>
-

Restaurant Dashboard

-

Welcome, !

-

This is where you will manage your food listings.

+
+

Restaurant Dashboard

+ Logout +
+

Welcome, !

+ +
+ +
+
+

Your Food Listings

+ +

You haven't posted any food listings yet.

+ +
+ +
+
+
+ Status: +
+

+ Quantity: +
+ +
+ +
+
+

Add New Listing

+ +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+
+
- \ No newline at end of file +