260 lines
13 KiB
PHP
260 lines
13 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$notification = null;
|
|
|
|
try {
|
|
$pdo = db();
|
|
// Idempotent table creation
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS contacts (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
name VARCHAR(255) NOT NULL,
|
|
email VARCHAR(255) NOT NULL,
|
|
phone VARCHAR(50),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
// Handle POST requests
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// Add a new contact
|
|
if (isset($_POST['add_contact'])) {
|
|
$name = trim($_POST['name']);
|
|
$email = trim($_POST['email']);
|
|
$phone = trim($_POST['phone']);
|
|
|
|
if (!empty($name) && !empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$stmt = $pdo->prepare("INSERT INTO contacts (name, email, phone) VALUES (?, ?, ?)");
|
|
$stmt->execute([$name, $email, $phone]);
|
|
$_SESSION['notification'] = ['type' => 'success', 'message' => 'Contact added successfully!'];
|
|
} else {
|
|
$_SESSION['notification'] = ['type' => 'danger', 'message' => 'Invalid data. Please check your input.'];
|
|
}
|
|
}
|
|
// Update an existing contact
|
|
elseif (isset($_POST['update_contact'])) {
|
|
$id = $_POST['contact_id'];
|
|
$name = trim($_POST['name']);
|
|
$email = trim($_POST['email']);
|
|
$phone = trim($_POST['phone']);
|
|
|
|
if (!empty($id) && !empty($name) && !empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$stmt = $pdo->prepare("UPDATE contacts SET name = ?, email = ?, phone = ? WHERE id = ?");
|
|
$stmt->execute([$name, $email, $phone, $id]);
|
|
$_SESSION['notification'] = ['type' => 'success', 'message' => 'Contact updated successfully!'];
|
|
} else {
|
|
$_SESSION['notification'] = ['type' => 'danger', 'message' => 'Invalid data. Please check your input.'];
|
|
}
|
|
}
|
|
// Delete a contact
|
|
elseif (isset($_POST['delete_contact'])) {
|
|
$id = $_POST['contact_id'];
|
|
if (!empty($id)) {
|
|
$stmt = $pdo->prepare("DELETE FROM contacts WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
$_SESSION['notification'] = ['type' => 'info', 'message' => 'Contact deleted.'];
|
|
}
|
|
}
|
|
|
|
header("Location: index.php");
|
|
exit;
|
|
}
|
|
|
|
// Check for notification from session
|
|
if (isset($_SESSION['notification'])) {
|
|
$notification = $_SESSION['notification'];
|
|
unset($_SESSION['notification']);
|
|
}
|
|
|
|
// Fetch all contacts
|
|
$stmt = $pdo->query("SELECT id, name, email, phone, created_at FROM contacts ORDER BY created_at DESC");
|
|
$contacts = $stmt->fetchAll();
|
|
|
|
} catch (PDOException $e) {
|
|
// For a real app, you'd log this error and show a user-friendly message.
|
|
$notification = ['type' => 'danger', 'message' => 'Database error: ' . $e->getMessage()];
|
|
$contacts = [];
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>CRM - Contact Management</title>
|
|
<meta name="description" content="A simple and effective CRM for managing your contacts.">
|
|
|
|
<!-- Open Graph -->
|
|
<meta property="og:title" content="CRM - Contact Management">
|
|
<meta property="og:description" content="A simple and effective CRM for managing your contacts.">
|
|
<meta property="og:type" content="website">
|
|
<meta property="og:url" content="https://your-app-url.com/">
|
|
|
|
<!-- Stylesheets -->
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link href="assets/css/custom.css?v=<?php echo time(); ?>" rel="stylesheet">
|
|
|
|
<!-- Icons -->
|
|
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<!-- Toast Notification -->
|
|
<?php if ($notification): ?>
|
|
<div class="toast-container position-fixed top-0 end-0 p-3">
|
|
<div id="notificationToast" class="toast align-items-center text-white bg-<?php echo htmlspecialchars($notification['type']); ?> border-0" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<div class="d-flex">
|
|
<div class="toast-body">
|
|
<?php echo htmlspecialchars($notification['message']); ?>
|
|
</div>
|
|
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<nav class="navbar navbar-expand-lg navbar-light">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="#">
|
|
<i data-feather="box" class="me-2"></i>CRM
|
|
</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<main class="container my-5">
|
|
<div class="row g-5">
|
|
<!-- Add Contact Form -->
|
|
<div class="col-lg-4">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h4 class="mb-0">Add New Contact</h4>
|
|
</div>
|
|
<div class="card-body">
|
|
<form id="addContactForm" method="POST" action="index.php" novalidate>
|
|
<input type="hidden" name="add_contact" value="1">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
<div class="invalid-feedback">Please enter a name.</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email</label>
|
|
<input type="email" class="form-control" id="email" name="email" required>
|
|
<div class="invalid-feedback">Please enter a valid email address.</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="phone" class="form-label">Phone (Optional)</label>
|
|
<input type="text" class="form-control" id="phone" name="phone">
|
|
</div>
|
|
<div class="d-grid">
|
|
<button type="submit" class="btn btn-primary">
|
|
<i data-feather="plus-circle" class="me-2"></i>Add Contact
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Contact List -->
|
|
<div class="col-lg-8">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h4 class="mb-0">Contact List</h4>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Name</th>
|
|
<th>Email</th>
|
|
<th>Phone</th>
|
|
<th>Date Added</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($contacts)): ?>
|
|
<tr>
|
|
<td colspan="5" class="text-center text-muted">No contacts yet. Add one to get started!</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($contacts as $contact): ?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($contact['name']); ?></td>
|
|
<td><?php echo htmlspecialchars($contact['email']); ?></td>
|
|
<td><?php echo htmlspecialchars($contact['phone'] ?? ''); ?></td>
|
|
<td><?php echo date("M d, Y", strtotime($contact['created_at'])); ?></td>
|
|
<td class="text-end">
|
|
<button type="button" class="btn btn-sm btn-outline-primary me-2 edit-btn"
|
|
data-id="<?php echo $contact['id']; ?>"
|
|
data-name="<?php echo htmlspecialchars($contact['name']); ?>"
|
|
data-email="<?php echo htmlspecialchars($contact['email']); ?>"
|
|
data-phone="<?php echo htmlspecialchars($contact['phone'] ?? ''); ?>"
|
|
data-bs-toggle="modal" data-bs-target="#editContactModal">
|
|
<i data-feather="edit-2" class="feather-sm"></i> Edit
|
|
</button>
|
|
<form method="POST" action="index.php" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this contact?');">
|
|
<input type="hidden" name="delete_contact" value="1">
|
|
<input type="hidden" name="contact_id" value="<?php echo $contact['id']; ?>">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger">
|
|
<i data-feather="trash-2" class="feather-sm"></i> Delete
|
|
</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Edit Contact Modal -->
|
|
<div class="modal fade" id="editContactModal" tabindex="-1" aria-labelledby="editContactModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content">
|
|
<form id="editContactForm" method="POST" action="index.php">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="editContactModalLabel">Edit Contact</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="update_contact" value="1">
|
|
<input type="hidden" id="edit_contact_id" name="contact_id">
|
|
<div class="mb-3">
|
|
<label for="edit_name" class="form-label">Name</label>
|
|
<input type="text" class="form-control" id="edit_name" name="name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="edit_email" class="form-label">Email</label>
|
|
<input type="email" class="form-control" id="edit_email" name="email" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="edit_phone" class="form-label">Phone (Optional)</label>
|
|
<input type="text" class="form-control" id="edit_phone" name="phone">
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
|
<button type="submit" class="btn btn-primary">Save Changes</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<footer class="text-center text-muted py-4">
|
|
<small>Powered by Flatlogic</small>
|
|
</footer>
|
|
|
|
<!-- Scripts -->
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|