97 lines
3.4 KiB
PHP
97 lines
3.4 KiB
PHP
<?php
|
|
/**
|
|
* File: UnsubscribeController.php
|
|
* Version: 1.0
|
|
* Path: app/Controllers/
|
|
* Purpose: Handles newsletter unsubscribe confirmation and processing.
|
|
* Project: Wizdom Networks Website
|
|
*/
|
|
|
|
namespace WizdomNetworks\WizeWeb\Controllers;
|
|
|
|
use WizdomNetworks\WizeWeb\Core\View;
|
|
use WizdomNetworks\WizeWeb\Utils\Database;
|
|
use WizdomNetworks\WizeWeb\Utils\Logger;
|
|
use WizdomNetworks\WizeWeb\Utils\ErrorHandler;
|
|
|
|
class UnsubscribeController
|
|
{
|
|
/**
|
|
* GET /unsubscribe
|
|
* Show confirmation form for unsubscribing.
|
|
*/
|
|
public function confirm(): void
|
|
{
|
|
try {
|
|
$email = trim($_GET['email'] ?? '');
|
|
|
|
if (empty($email)) {
|
|
Logger::error("Unsubscribe access without email.");
|
|
View::render('pages/unsubscribe_failed', ['reason' => 'No email provided.']);
|
|
return;
|
|
}
|
|
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("SELECT is_verified, unsubscribed_at FROM subscribers WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$subscriber = $stmt->fetch();
|
|
|
|
if (!$subscriber) {
|
|
Logger::error("Unsubscribe: Subscriber not found [$email]");
|
|
View::render('pages/unsubscribe_failed', ['reason' => 'Subscriber not found.']);
|
|
return;
|
|
}
|
|
|
|
if ($subscriber['unsubscribed_at']) {
|
|
View::render('pages/unsubscribe_success', ['email' => $email, 'alreadyUnsubscribed' => true]);
|
|
return;
|
|
}
|
|
|
|
View::render('pages/unsubscribe_confirm', ['email' => $email]);
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Unsubscribe view error: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
View::render('pages/unsubscribe_failed', ['reason' => 'An unexpected error occurred.']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST /unsubscribe
|
|
* Perform the actual unsubscribe action.
|
|
*/
|
|
public function process(): void
|
|
{
|
|
try {
|
|
$email = trim($_POST['email'] ?? '');
|
|
$reason = trim($_POST['unsubscribe_reason'] ?? '');
|
|
|
|
if (empty($email)) {
|
|
Logger::error("Unsubscribe form submitted without email.");
|
|
View::render('pages/unsubscribe_failed', ['reason' => 'No email address was provided.']);
|
|
return;
|
|
}
|
|
|
|
$db = Database::getConnection();
|
|
$stmt = $db->prepare("SELECT id FROM subscribers WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$subscriber = $stmt->fetch();
|
|
|
|
if (!$subscriber) {
|
|
Logger::error("Unsubscribe: Subscriber not found during processing [$email]");
|
|
View::render('pages/unsubscribe_failed', ['reason' => 'Subscriber not found.']);
|
|
return;
|
|
}
|
|
|
|
$stmt = $db->prepare("UPDATE subscribers SET unsubscribed_at = NOW(), unsubscribe_reason = ? WHERE id = ?");
|
|
$stmt->execute([$reason, $subscriber['id']]);
|
|
|
|
Logger::info("Subscriber unsubscribed: $email");
|
|
View::render('pages/unsubscribe_success', ['email' => $email]);
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Unsubscribe processing error for $email: " . $e->getMessage());
|
|
ErrorHandler::exception($e);
|
|
View::render('pages/unsubscribe_failed', ['reason' => 'An error occurred while processing your unsubscribe.']);
|
|
}
|
|
}
|
|
}
|