33951-vm/vendors.php
Flatlogic Bot 0a0873a371 v2
2025-09-09 15:57:31 +00:00

192 lines
9.1 KiB
PHP

<?php
require_once 'db/config.php';
$errorMessage = null;
$successMessage = null;
// Handle form submission for deleting a vendor
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_vendor'])) {
$vendor_id = filter_input(INPUT_POST, 'vendor_id', FILTER_VALIDATE_INT);
if ($vendor_id) {
try {
$pdo = db();
$stmt = $pdo->prepare('DELETE FROM vendors WHERE id = ?');
$stmt->execute([$vendor_id]);
header("Location: vendors.php?success=2");
exit;
} catch (PDOException $e) {
$errorMessage = "Error: Could not delete the vendor. " . $e->getMessage();
}
}
}
// Handle form submission for adding a new vendor
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_vendor'])) {
$name = trim($_POST['name'] ?? '');
$service_type = trim($_POST['service_type'] ?? '');
$contact_email = filter_input(INPUT_POST, 'contact_email', FILTER_VALIDATE_EMAIL);
$contact_phone = trim($_POST['contact_phone'] ?? '');
$rating = filter_input(INPUT_POST, 'rating', FILTER_VALIDATE_FLOAT);
if (empty($name) || empty($service_type) || !$contact_email) {
$errorMessage = "Error: Vendor name, service type, and a valid email are required.";
} else {
try {
$pdo = db();
$stmt = $pdo->prepare('INSERT INTO vendors (name, service_type, contact_email, contact_phone, rating) VALUES (?, ?, ?, ?, ?)');
$stmt->execute([$name, $service_type, $contact_email, $contact_phone, $rating]);
header("Location: vendors.php?success=1");
exit;
} catch (PDOException $e) {
$errorMessage = "Error: Could not add the vendor. " . $e->getMessage();
}
}
}
// Handle success messages
if (isset($_GET['success'])) {
if ($_GET['success'] == 1) $successMessage = "Vendor added successfully!";
if ($_GET['success'] == 2) $successMessage = "Vendor deleted successfully!";
if ($_GET['success'] == 3) $successMessage = "Vendor updated successfully!";
}
// Fetch vendors from the database
$vendors = [];
try {
$pdo = db();
$stmt = $pdo->query('SELECT * FROM vendors ORDER BY name');
$vendors = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$errorMessage = "Error: Could not connect to the database.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vendors - The Best Events</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">The Best Events</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="venues.php">Venues</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="vendors.php">Vendors</a>
</li>
</ul>
</div>
</div>
</nav>
<main class="container mt-4">
<h1 class="mb-4">Manage Vendors</h1>
<?php if ($successMessage): ?>
<div class="alert alert-success"><?= htmlspecialchars($successMessage) ?></div>
<?php endif; ?>
<?php if ($errorMessage): ?>
<div class="alert alert-danger"><?= htmlspecialchars($errorMessage) ?></div>
<?php endif; ?>
<p>
<button class="btn btn-primary" type="button" data-bs-toggle="collapse" data-bs-target="#add-vendor-form" aria-expanded="false" aria-controls="add-vendor-form">
Add New Vendor
</button>
</p>
<div class="collapse mb-4" id="add-vendor-form">
<div class="card card-body">
<form action="vendors.php" method="POST">
<input type="hidden" name="add_vendor" value="1">
<div class="mb-3">
<label for="name" class="form-label">Vendor Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="service_type" class="form-label">Service Type</label>
<input type="text" class="form-control" id="service_type" name="service_type" placeholder="e.g., Catering, Photography" required>
</div>
<div class="mb-3">
<label for="contact_email" class="form-label">Contact Email</label>
<input type="email" class="form-control" id="contact_email" name="contact_email" required>
</div>
<div class="mb-3">
<label for="contact_phone" class="form-label">Contact Phone</label>
<input type="tel" class="form-control" id="contact_phone" name="contact_phone">
</div>
<div class="mb-3">
<label for="rating" class="form-label">Rating (1-5)</label>
<input type="number" step="0.1" min="1" max="5" class="form-control" id="rating" name="rating">
</div>
<button type="submit" class="btn btn-success">Save Vendor</button>
</form>
</div>
</div>
<?php if (empty($vendors) && !$errorMessage): ?>
<div class="alert alert-info">
No vendors found. Get started by adding one!
</div>
<?php elseif (!$errorMessage): ?>
<div class="list-group">
<?php foreach ($vendors as $vendor): ?>
<div class="list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1"><?= htmlspecialchars($vendor['name']) ?></h5>
<small>Service: <?= htmlspecialchars($vendor['service_type']) ?></small>
</div>
<p class="mb-1">Contact: <?= htmlspecialchars($vendor['contact_email']) ?> | <?= htmlspecialchars($vendor['contact_phone']) ?></p>
<small class="d-block mb-2">Rating: <?= htmlspecialchars($vendor['rating']) ?> / 5</small>
<div>
<a href="edit-vendor.php?id=<?= $vendor['id'] ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
<button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#delete-modal-<?= $vendor['id'] ?>">
Delete
</button>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div class="modal fade" id="delete-modal-<?= $vendor['id'] ?>" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Confirm Deletion</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure you want to delete the vendor "<?= htmlspecialchars($vendor['name']) ?>"?
</div>
<div class="modal-footer">
<form action="vendors.php" method="POST" class="d-inline">
<input type="hidden" name="delete_vendor" value="1">
<input type="hidden" name="vendor_id" value="<?= $vendor['id'] ?>">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>