104 lines
2.8 KiB
PHP
104 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace WizdomNetworks\WizeWeb\Models;
|
|
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
|
|
|
|
/**
|
|
* Contact Model
|
|
*
|
|
* Handles database operations related to contacts.
|
|
*/
|
|
class ContactModel
|
|
{
|
|
private $db;
|
|
|
|
public function __construct($db)
|
|
{
|
|
$this->db = $db;
|
|
}
|
|
|
|
/**
|
|
* Retrieve a contact by ID.
|
|
*
|
|
* @param int $id
|
|
* @return array|null
|
|
*/
|
|
public function getContactById(int $id): ?array
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Fetching contact with ID: $id");
|
|
|
|
$stmt = $this->db->prepare("SELECT * FROM contacts WHERE id = :id");
|
|
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
|
|
$contact = $stmt->fetch(\PDO::FETCH_ASSOC);
|
|
|
|
Logger::info("[DEBUG] Contact data retrieved: " . json_encode($contact));
|
|
|
|
return $contact ?: null;
|
|
} catch (\Exception $e) {
|
|
Logger::error("[ERROR] Failed to fetch contact with ID $id: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add a new contact to the database.
|
|
*
|
|
* @param array $contactData
|
|
* @return bool
|
|
*/
|
|
public function addContact(array $contactData): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Adding new contact: " . json_encode($contactData));
|
|
|
|
$stmt = $this->db->prepare(
|
|
"INSERT INTO contacts (name, email, message) VALUES (:name, :email, :message)"
|
|
);
|
|
$stmt->bindParam(':name', $contactData['name']);
|
|
$stmt->bindParam(':email', $contactData['email']);
|
|
$stmt->bindParam(':message', $contactData['message']);
|
|
|
|
$stmt->execute();
|
|
|
|
Logger::info("[DEBUG] Contact successfully added.");
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Logger::error("[ERROR] Failed to add contact: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a contact by ID.
|
|
*
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function deleteContactById(int $id): bool
|
|
{
|
|
try {
|
|
Logger::info("[DEBUG] Deleting contact with ID: $id");
|
|
|
|
$stmt = $this->db->prepare("DELETE FROM contacts WHERE id = :id");
|
|
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
|
|
Logger::info("[DEBUG] Contact with ID $id successfully deleted.");
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
Logger::error("[ERROR] Failed to delete contact with ID $id: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|