63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !isset($_POST['id'])) {
|
|
header('Location: ngo_dashboard.php');
|
|
exit();
|
|
}
|
|
|
|
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
|
|
|
|
if ($id === false) {
|
|
header('Location: ngo_dashboard.php?status=error');
|
|
exit();
|
|
}
|
|
|
|
// Simulate a logged-in NGO by fetching the first one
|
|
try {
|
|
$pdo = db();
|
|
$sql_ngo = "SELECT id FROM ngos ORDER BY id ASC LIMIT 1";
|
|
$stmt_ngo = $pdo->query($sql_ngo);
|
|
$ngo = $stmt_ngo->fetch(PDO::FETCH_ASSOC);
|
|
if (!$ngo) {
|
|
throw new Exception('No NGOs found in the database. Please register an NGO first.');
|
|
}
|
|
$ngo_id = $ngo['id'];
|
|
} catch (Exception $e) {
|
|
// error_log($e->getMessage());
|
|
header('Location: ngo_dashboard.php?status=no_ngo');
|
|
exit();
|
|
}
|
|
|
|
try {
|
|
$pdo = db();
|
|
// Check if the listing is still available before claiming
|
|
$checkSql = "SELECT status FROM food_listings WHERE id = :id";
|
|
$checkStmt = $pdo->prepare($checkSql);
|
|
$checkStmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
$checkStmt->execute();
|
|
$currentStatus = $checkStmt->fetchColumn();
|
|
|
|
if ($currentStatus === 'available') {
|
|
$updateSql = "UPDATE food_listings SET status = 'claimed', ngo_id = :ngo_id WHERE id = :id";
|
|
$updateStmt = $pdo->prepare($updateSql);
|
|
$updateStmt->bindParam(':ngo_id', $ngo_id, PDO::PARAM_INT);
|
|
$updateStmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
|
|
if ($updateStmt->execute()) {
|
|
header('Location: ngo_dashboard.php?status=claimed');
|
|
} else {
|
|
header('Location: ngo_dashboard.php?status=error');
|
|
}
|
|
} else {
|
|
// The listing was already claimed by someone else
|
|
header('Location: ngo_dashboard.php?status=already_claimed');
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
// error_log($e->getMessage());
|
|
header('Location: ngo_dashboard.php?status=error');
|
|
}
|
|
|
|
exit();
|