WizdomWeb/app/Utilities/SubmissionCheck.php

54 lines
1.8 KiB
PHP

<?php
/**
* ============================================
* File: SubmissionCheck.php
* Path: /app/Utilities/
* Purpose: Utility to check for recent form submissions by email
* Namespace: WizdomNetworks\WizeWeb\Utilities
* Version: 1.0
* Author: Wizdom Networks
* ============================================
*/
namespace WizdomNetworks\WizeWeb\Utilities;
use WizdomNetworks\WizeWeb\Utilities\Database;
use PDO;
use Exception;
class SubmissionCheck
{
/**
* Check if the given email has a recent submission in the specified table.
*
* @param string $email - The email address to check.
* @param string $table - The name of the table (e.g., contact_messages, subscribers).
* @param string $emailField - The name of the email field in the table.
* @param string $timestampField - The name of the datetime/timestamp field.
* @param int $days - The number of days to consider as "recent" (default: 7).
* @return array|null - Returns array with date of last submission if found, or null if none.
* @throws Exception - If query fails.
*/
public static function hasRecentSubmission(string $email, string $table, string $emailField, string $timestampField, int $days = 7): ?array
{
$pdo = Database::getConnection();
$sql = "SELECT $timestampField FROM $table WHERE $emailField = :email ORDER BY $timestampField DESC LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute(['email' => $email]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row && isset($row[$timestampField])) {
$last = new \DateTime($row[$timestampField]);
$cutoff = (new \DateTime())->modify("-{$days} days");
if ($last >= $cutoff) {
return ['submitted_at' => $last->format('Y-m-d H:i:s')];
}
}
return null;
}
}