135 lines
4.2 KiB
PHP
135 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* File: ContactModel.php
|
|
* Version: 2.1
|
|
* Path: /app/Models/ContactModel.php
|
|
* Purpose: Manages saving and retrieving contact records from both legacy and full form submissions.
|
|
* Project: Wizdom Networks Website
|
|
*/
|
|
|
|
namespace WizdomNetworks\WizeWeb\Models;
|
|
|
|
use PDO;
|
|
use Exception;
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
|
|
|
|
class ContactModel
|
|
{
|
|
private PDO $db;
|
|
|
|
/**
|
|
* ContactModel constructor.
|
|
*
|
|
* @param PDO $db Database connection
|
|
*/
|
|
public function __construct(PDO $db)
|
|
{
|
|
$this->db = $db;
|
|
}
|
|
|
|
/**
|
|
* Legacy method to insert simplified contact into `contacts` table.
|
|
*
|
|
* @param array $contactData ['name' => string, 'email' => string, 'message' => string]
|
|
* @return bool
|
|
*/
|
|
public function addContact(array $contactData): bool
|
|
{
|
|
try {
|
|
$stmt = $this->db->prepare("
|
|
INSERT INTO contacts (name, email, message)
|
|
VALUES (:name, :email, :message)
|
|
");
|
|
|
|
$name = trim(($contactData['name'] ?? '') ?: (($contactData['first_name'] ?? '') . ' ' . ($contactData['last_name'] ?? '')));
|
|
$stmt->bindParam(':name', $name);
|
|
$stmt->bindParam(':email', $contactData['email']);
|
|
$stmt->bindParam(':message', $contactData['message']);
|
|
|
|
return $stmt->execute();
|
|
} catch (Exception $e) {
|
|
Logger::error("ContactModel::addContact failed: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Saves full contact form submission to the `contact_messages` table.
|
|
*
|
|
* @param array $formData Associative array of form input
|
|
* @return bool True on success, false on failure
|
|
*/
|
|
public function saveContactForm(array $formData): bool
|
|
{
|
|
try {
|
|
$stmt = $this->db->prepare("
|
|
INSERT INTO contact_messages (
|
|
first_name, last_name, email, phone, subject, message,
|
|
ip_address, user_agent
|
|
) VALUES (
|
|
:first_name, :last_name, :email, :phone, :subject, :message,
|
|
:ip_address, :user_agent
|
|
)
|
|
");
|
|
|
|
$stmt->bindParam(':first_name', $formData['first_name']);
|
|
$stmt->bindParam(':last_name', $formData['last_name']);
|
|
$stmt->bindParam(':email', $formData['email']);
|
|
$stmt->bindParam(':phone', $formData['phone']);
|
|
$stmt->bindParam(':subject', $formData['subject']);
|
|
$stmt->bindParam(':message', $formData['message']);
|
|
$stmt->bindParam(':ip_address', $formData['ip_address']);
|
|
$stmt->bindParam(':user_agent', $formData['user_agent']);
|
|
|
|
return $stmt->execute();
|
|
} catch (Exception $e) {
|
|
Logger::error("ContactModel::saveContactForm failed: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves a contact record by ID from `contact_messages`.
|
|
*
|
|
* @param int $id
|
|
* @return array|null
|
|
*/
|
|
public function getContactById(int $id): ?array
|
|
{
|
|
try {
|
|
$stmt = $this->db->prepare("SELECT * FROM contact_messages WHERE id = :id");
|
|
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$result = $stmt->fetch();
|
|
|
|
return $result ?: null;
|
|
} catch (Exception $e) {
|
|
Logger::error("ContactModel::getContactById failed: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deletes a contact record by ID from `contact_messages`.
|
|
*
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function deleteContactById(int $id): bool
|
|
{
|
|
try {
|
|
$stmt = $this->db->prepare("DELETE FROM contact_messages WHERE id = :id");
|
|
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
|
|
return $stmt->execute();
|
|
} catch (Exception $e) {
|
|
Logger::error("ContactModel::deleteContactById failed: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
return false;
|
|
}
|
|
}
|
|
}
|