37325-vm/admin/companies.php
Flatlogic Bot c7e40bdd09 1.1
2026-01-08 17:14:09 +00:00

89 lines
3.0 KiB
PHP

<?php
require_once __DIR__ . '/../includes/session.php';
require_once __DIR__ . '/../../db/config.php';
checkAuth();
$pdo = db();
// Handle delete action
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id'])) {
$id = $_GET['id'];
try {
$stmt = $pdo->prepare("DELETE FROM companies WHERE id = :id");
$stmt->execute(['id' => $id]);
$_SESSION['success_message'] = 'Company deleted successfully!';
} catch (PDOException $e) {
$_SESSION['error_message'] = 'Error deleting company: ' . $e->getMessage();
}
header('Location: companies.php');
exit();
}
// Fetch all companies
$companies = [];
try {
$stmt = $pdo->query("SELECT * FROM companies ORDER BY name");
$companies = $stmt->fetchAll();
} catch (PDOException $e) {
$_SESSION['error_message'] = 'Error fetching companies: ' . $e->getMessage();
}
$title = 'Companies';
include __DIR__ . '/../templates/_header.php';
?>
<h1 class="mb-4">Companies</h1>
<?php if (isset($_SESSION['success_message'])): ?>
<div class="alert alert-success" role="alert">
<?php echo htmlspecialchars($_SESSION['success_message']); unset($_SESSION['success_message']); ?>
</div>
<?php endif; ?>
<?php if (isset($_SESSION['error_message'])): ?>
<div class="alert alert-danger" role="alert">
<?php echo htmlspecialchars($_SESSION['error_message']); unset($_SESSION['error_message']); ?>
</div>
<?php endif; ?>
<div class="d-flex justify-content-end mb-3">
<a href="company_edit.php" class="btn btn-primary">Add New Company</a>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (count($companies) > 0): ?>
<?php foreach ($companies as $company): ?>
<tr>
<td><?php echo htmlspecialchars($company['id']); ?></td>
<td><?php echo htmlspecialchars($company['name']); ?></td>
<td><?php echo htmlspecialchars($company['email']); ?></td>
<td><?php echo htmlspecialchars($company['phone']); ?></td>
<td><?php echo htmlspecialchars($company['address']); ?></td>
<td>
<a href="company_edit.php?id=<?php echo htmlspecialchars($company['id']); ?>" class="btn btn-sm btn-info">Edit</a>
<a href="companies.php?action=delete&id=<?php echo htmlspecialchars($company['id']); ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this company?');">Delete</a>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="6">No companies found.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<?php include __DIR__ . '/../templates/_footer.php'; ?>