<?php
namespace App\Extension\defaultExt\modules\Contacts\Service;
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
use App\Engine\LegacyHandler\LegacyHandler;
use App\Engine\LegacyHandler\LegacyScopeState;
use App\Process\Entity\Process;
use App\Process\Service\ProcessHandlerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class DuplicateEmailValidationHandler extends LegacyHandler implements ProcessHandlerInterface, LoggerAwareInterface
{
protected const MSG_OPTIONS_NOT_FOUND = 'Process options are not defined';
protected const PROCESS_TYPE = 'validate-contact-duplicate-email';
protected LoggerInterface $logger;
public function __construct(
string $projectDir,
string $legacyDir,
string $legacySessionName,
string $defaultSessionName,
LegacyScopeState $legacyScopeState,
RequestStack $requestStack,
protected DuplicateEmailPresetHandler $presetHandler
) {
parent::__construct(
$projectDir,
$legacyDir,
$legacySessionName,
$defaultSessionName,
$legacyScopeState,
$requestStack
);
}
public function getHandlerKey(): string
{
return self::PROCESS_TYPE;
}
public function getProcessType(): string
{
return self::PROCESS_TYPE;
}
public function requiredAuthRole(): string
{
return 'ROLE_USER';
}
public function getRequiredACLs(Process $process): array
{
return [
'Contacts' => [
[
'action' => 'edit',
]
],
];
}
public function configure(Process $process): void
{
$process->setId(self::PROCESS_TYPE);
$process->setAsync(false);
}
public function validate(Process $process): void
{
if (empty($process->getOptions())) {
throw new InvalidArgumentException(self::MSG_OPTIONS_NOT_FOUND);
}
}
public function run(Process $process): void
{
$this->init();
$this->startLegacyApp();
try {
$options = $process->getOptions();
$attributes = $options['attributes'] ?? [];
$email = $this->extractEmail($attributes);
// No email — nothing to check
if (empty($email)) {
$process->setStatus('success');
$process->setMessages([]);
return;
}
$currentId = $attributes['id'] ?? '';
$presetParams = [
'email' => $email,
'excludeId' => $currentId,
];
$criteria = [
'preset' => [
'type' => 'duplicate-email-contacts',
'params' => $presetParams,
],
];
// Quick check: fetch just 1 record to see if duplicates exist
$listData = $this->presetHandler->fetch('Contacts', $criteria, 0, 1, []);
if (empty($listData->getRecords())) {
// No duplicates — pass validation
$process->setStatus('success');
$process->setMessages([]);
return;
}
// Duplicates found — show the list confirmation modal
$process->setStatus('error');
$process->setMessages(['LBL_DUPLICATE_EMAIL_FOUND']);
$process->setData([
'displayListConfirmation' => true,
'confirmationTitle' => 'LBL_DUPLICATE_EMAIL_TITLE',
'confirmationLabel' => 'LBL_DUPLICATE_EMAIL_MESSAGE',
'module' => 'Contacts',
'showFilter' => true,
'fields' => [
['name' => 'name'],
['name' => 'email1', 'link' => false],
['name' => 'account_name', 'link' => false],
],
'preset' => [
'type' => 'duplicate-email-contacts',
'params' => $presetParams,
],
'actions' => [],
]);
} finally {
$this->close();
}
}
protected function extractEmail(array $attributes): string
{
// Try email1 first (simple email field)
$email1 = $attributes['email1'] ?? '';
if (!empty($email1)) {
return trim($email1);
}
// Fall back to email_addresses array (line-items format)
$emailAddresses = $attributes['email_addresses'] ?? [];
if (empty($emailAddresses)) {
return '';
}
foreach ($emailAddresses as $entry) {
$addr = $entry['email_address'] ?? '';
if (!empty($addr)) {
return trim($addr);
}
}
return '';
}
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
}