98 lines
4.8 KiB
PHP
98 lines
4.8 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'header.php';
|
|
|
|
$search = $_GET['search'] ?? '';
|
|
$sort = $_GET['sort'] ?? 'created_at_desc';
|
|
|
|
$sort_options = [
|
|
'name_asc' => '`name` ASC',
|
|
'name_desc' => '`name` DESC',
|
|
'company_asc' => '`company` ASC',
|
|
'company_desc' => '`company` DESC',
|
|
'created_at_asc' => '`created_at` ASC',
|
|
'created_at_desc' => '`created_at` DESC',
|
|
];
|
|
$order_by = $sort_options[$sort] ?? '`created_at` DESC';
|
|
|
|
try {
|
|
$pdo = db();
|
|
$search_term = "%$search%";
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM contacts WHERE name LIKE :search OR company LIKE :search OR email LIKE :search ORDER BY $order_by");
|
|
$stmt->bindParam(':search', $search_term);
|
|
$stmt->execute();
|
|
$contacts = $stmt->fetchAll();
|
|
} catch (PDOException $e) {
|
|
echo "Error: " . $e->getMessage();
|
|
$contacts = [];
|
|
}
|
|
?>
|
|
|
|
<div class="container mt-4">
|
|
<?php
|
|
if (isset($_SESSION['success_message'])) {
|
|
echo '<div class="alert alert-success alert-dismissible fade show" role="alert">' . $_SESSION['success_message'] . '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
|
|
unset($_SESSION['success_message']);
|
|
}
|
|
if (isset($_SESSION['error_message'])) {
|
|
echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">' . $_SESSION['error_message'] . '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
|
|
unset($_SESSION['error_message']);
|
|
}
|
|
?>
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h2><i class="fas fa-address-book me-2"></i>Contacts</h2>
|
|
<a href="add_contact.php" class="btn btn-primary"><i class="fas fa-plus me-2"></i>Add Contact</a>
|
|
</div>
|
|
|
|
<form method="get" class="row g-3 mb-3">
|
|
<div class="col-md-6">
|
|
<input type="text" name="search" class="form-control" placeholder="Search contacts..." value="<?php echo htmlspecialchars($search); ?>">
|
|
</div>
|
|
<div class="col-md-4">
|
|
<select name="sort" class="form-select">
|
|
<option value="created_at_desc" <?php if ($sort === 'created_at_desc') echo 'selected'; ?>>Newest First</option>
|
|
<option value="created_at_asc" <?php if ($sort === 'created_at_asc') echo 'selected'; ?>>Oldest First</option>
|
|
<option value="name_asc" <?php if ($sort === 'name_asc') echo 'selected'; ?>>Name (A-Z)</option>
|
|
<option value="name_desc" <?php if ($sort === 'name_desc') echo 'selected'; ?>>Name (Z-A)</option>
|
|
<option value="company_asc" <?php if ($sort === 'company_asc') echo 'selected'; ?>>Company (A-Z)</option>
|
|
<option value="company_desc" <?php if ($sort === 'company_desc') echo 'selected'; ?>>Company (Z-A)</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<button type="submit" class="btn btn-secondary w-100">Filter</button>
|
|
</div>
|
|
</form>
|
|
|
|
<div class="list-group">
|
|
<?php if (empty($contacts)): ?>
|
|
<div class="list-group-item text-center text-muted">
|
|
No contacts found. <a href="add_contact.php">Add one now</a>.
|
|
</div>
|
|
<?php else: ?>
|
|
<?php foreach ($contacts as $contact): ?>
|
|
<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($contact['name']); ?></h5>
|
|
<small class="text-muted"><?php echo date('M j, Y', strtotime($contact['created_at'])); ?></small>
|
|
</div>
|
|
<p class="mb-1"><?php echo htmlspecialchars($contact['company']); ?></p>
|
|
<small class="text-muted d-block"><?php echo htmlspecialchars($contact['email']); ?> | <?php echo htmlspecialchars($contact['phone']); ?></small>
|
|
<div class="mt-2">
|
|
<a href="edit_contact.php?id=<?php echo $contact['id']; ?>" class="btn btn-sm btn-outline-secondary me-2">
|
|
<i class="fas fa-pencil-alt me-1"></i>Edit
|
|
</a>
|
|
<a href="delete_contact.php?id=<?php echo $contact['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Are you sure you want to delete this contact? This action cannot be undone.');">
|
|
<i class="fas fa-trash-alt me-1"></i>Delete
|
|
</a>
|
|
<span class="badge rounded-pill bg-light text-dark ms-2">Source: <?php echo htmlspecialchars($contact['source']); ?></span>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'footer.php'; ?>
|