115 lines
4.2 KiB
PHP
115 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* File: ContactController.php
|
|
* Version: 2.0
|
|
* Path: /app/Controllers/ContactController.php
|
|
* Purpose: Handles contact form submission with error alerts to admins and success to sales.
|
|
* Project: Wizdom Networks Website
|
|
*/
|
|
|
|
namespace WizdomNetworks\WizeWeb\Controllers;
|
|
|
|
use WizdomNetworks\WizeWeb\Core\View;
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\Validator;
|
|
use WizdomNetworks\WizeWeb\Utilities\Sanitizer;
|
|
use WizdomNetworks\WizeWeb\Utilities\Database;
|
|
use WizdomNetworks\WizeWeb\Utilities\EmailHelper;
|
|
use WizdomNetworks\WizeWeb\Models\ContactModel;
|
|
use Exception;
|
|
|
|
class ContactController
|
|
{
|
|
public function index(): void
|
|
{
|
|
View::render('pages/landing');
|
|
}
|
|
|
|
public function submit(): void
|
|
{
|
|
Logger::info("Executing controller: ContactController::submit");
|
|
Logger::info("📦 PHP Session ID: " . session_id());
|
|
|
|
|
|
try {
|
|
$formData = [
|
|
'first_name' => Sanitizer::sanitizeString($_POST['first_name'] ?? ''),
|
|
'last_name' => Sanitizer::sanitizeString($_POST['last_name'] ?? ''),
|
|
'email' => Sanitizer::sanitizeString($_POST['email'] ?? ''),
|
|
'phone' => Sanitizer::sanitizeString($_POST['phone'] ?? ''),
|
|
'subject' => Sanitizer::sanitizeString($_POST['subject'] ?? ''),
|
|
'message' => Sanitizer::sanitizeString($_POST['message'] ?? ''),
|
|
'ip_address' => $_SERVER['REMOTE_ADDR'] ?? 'unknown',
|
|
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'unknown',
|
|
];
|
|
|
|
foreach ($formData as $key => $value) {
|
|
Logger::info("Sanitized input: {$key} = {$value}");
|
|
}
|
|
|
|
// Validate required fields
|
|
if (
|
|
empty($formData['first_name']) ||
|
|
empty($formData['last_name']) ||
|
|
empty($formData['email']) ||
|
|
empty($formData['phone']) ||
|
|
empty($formData['subject']) ||
|
|
empty($formData['message']) ||
|
|
!Validator::isEmail($formData['email'])
|
|
) {
|
|
Logger::info("Validation failed for contact form submission");
|
|
$_SESSION['contact_error'] = 'An internal error occurred. Please try again later.';
|
|
|
|
header("Location: /?contact_error=1#contact");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
// Save to DB
|
|
$db = Database::getConnection();
|
|
$contactModel = new ContactModel($db);
|
|
$saveSuccess = $contactModel->saveContactForm($formData);
|
|
|
|
// Send to sales team
|
|
$emailSuccess = EmailHelper::sendContactNotification($formData);
|
|
|
|
// Send confirmation to user
|
|
$confirmationSuccess = EmailHelper::sendConfirmationToUser($formData);
|
|
|
|
if ($saveSuccess && $emailSuccess) {
|
|
$_SESSION['contact_success'] = true;
|
|
|
|
} else {
|
|
Logger::error("Form processed but saveSuccess={$saveSuccess}, emailSuccess={$emailSuccess}");
|
|
$_SESSION['contact_error'] = 'Your message was received but an internal error occurred. A confirmation may not have been sent.';
|
|
|
|
EmailHelper::alertAdmins('ContactController::submit - DB or email failure', 'Partial failure', $formData);
|
|
}
|
|
|
|
if (!$confirmationSuccess) {
|
|
Logger::error("Confirmation email failed to send to user: {$formData['email']}");
|
|
// Don't show user error — it's non-critical
|
|
}
|
|
Logger::info("✅ Writing session flag: contact_success = true");
|
|
Logger::info("✅ Session content before redirect: " . json_encode($_SESSION));
|
|
|
|
header("Location: /?contact_submitted=1#contact");
|
|
|
|
exit;
|
|
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Fatal error in ContactController::submit: " . $e->getMessage());
|
|
EmailHelper::alertAdmins('ContactController::submit - Uncaught Exception', $e->getMessage(), $_POST ?? []);
|
|
$_SESSION['contact_error'] = 'An internal error occurred. Please try again later.';
|
|
|
|
Logger::info("✅ Writing session flag: catch contact_error = " . $_SESSION['contact_error']);
|
|
Logger::info("✅ Session content before redirect: " . json_encode($_SESSION));
|
|
header("Location: /?contact_error=2#contact");
|
|
|
|
exit;
|
|
}
|
|
}
|
|
|
|
}
|