Rename the Plan bundle to Program across config, routes, menus, and controllers (Plan -> Initiative), bump version to 0.16.0, and introduce new audience and UTM taxonomy routes and permissions to support the expanded Programs feature set.
112 lines
3.6 KiB
PHP
112 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\ProgramBundle\Model;
|
|
|
|
use libphonenumber\NumberParseException;
|
|
use libphonenumber\PhoneNumberFormat;
|
|
use libphonenumber\PhoneNumberUtil;
|
|
use Mautic\CoreBundle\Model\FormModel;
|
|
use MauticPlugin\ProgramBundle\Entity\Stakeholder;
|
|
use MauticPlugin\ProgramBundle\Entity\StakeholderRepository;
|
|
use MauticPlugin\ProgramBundle\Form\Type\StakeholderType;
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
|
|
|
/**
|
|
* Available as mautic.program.model.stakeholder, or getModel('program.stakeholder').
|
|
*
|
|
* On save, the phone number is normalized to E.164 using the default region from Settings >
|
|
* Configuration > Initiative Settings (parameter program_stakeholder_phone_region, FR by default). A number that
|
|
* doesn't parse (free input, extensions…) is kept exactly as typed: the save is never blocked.
|
|
*/
|
|
class StakeholderModel extends FormModel
|
|
{
|
|
/**
|
|
* local.php parameter: default region for phone numbers typed without a country prefix.
|
|
*/
|
|
public const PHONE_REGION_PARAMETER = 'program_stakeholder_phone_region';
|
|
|
|
public const DEFAULT_PHONE_REGION = 'FR';
|
|
|
|
public function getRepository(): StakeholderRepository
|
|
{
|
|
$repository = $this->em->getRepository(Stakeholder::class);
|
|
\assert($repository instanceof StakeholderRepository);
|
|
|
|
return $repository;
|
|
}
|
|
|
|
public function getPermissionBase(): string
|
|
{
|
|
return 'program:stakeholders';
|
|
}
|
|
|
|
public function getEntity($id = null): ?Stakeholder
|
|
{
|
|
if (null === $id) {
|
|
return new Stakeholder();
|
|
}
|
|
|
|
$stakeholder = parent::getEntity($id);
|
|
|
|
return $stakeholder instanceof Stakeholder ? $stakeholder : null;
|
|
}
|
|
|
|
/**
|
|
* Normalizes the phone number to E.164 before saving; keeps the raw input when it doesn't parse.
|
|
*/
|
|
public function saveEntity($entity, $unlock = true): void
|
|
{
|
|
if ($entity instanceof Stakeholder && null !== $entity->getPhone() && '' !== trim($entity->getPhone())) {
|
|
$entity->setPhone($this->normalizePhone($entity->getPhone()));
|
|
}
|
|
|
|
parent::saveEntity($entity, $unlock);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $options
|
|
*/
|
|
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface
|
|
{
|
|
if (!$entity instanceof Stakeholder) {
|
|
throw new MethodNotAllowedHttpException(['Stakeholder']);
|
|
}
|
|
|
|
if (!empty($action)) {
|
|
$options['action'] = $action;
|
|
}
|
|
|
|
return $formFactory->create(StakeholderType::class, $entity, $options);
|
|
}
|
|
|
|
/**
|
|
* The number as E.164 when it parses with the default region, or the raw input as typed when it
|
|
* doesn't ("+33 6 … ext. 12" stays as typed).
|
|
*/
|
|
public function normalizePhone(string $phone): string
|
|
{
|
|
try {
|
|
$number = PhoneNumberUtil::getInstance()->parse(trim($phone), $this->getDefaultPhoneRegion());
|
|
|
|
if (PhoneNumberUtil::getInstance()->isValidNumber($number)) {
|
|
return PhoneNumberUtil::getInstance()->format($number, PhoneNumberFormat::E164);
|
|
}
|
|
} catch (NumberParseException) {
|
|
// Not a parseable number: keep the raw input.
|
|
}
|
|
|
|
return $phone;
|
|
}
|
|
|
|
public function getDefaultPhoneRegion(): string
|
|
{
|
|
$region = $this->coreParametersHelper->get(self::PHONE_REGION_PARAMETER);
|
|
|
|
return is_string($region) && '' !== trim($region) ? strtoupper(trim($region)) : self::DEFAULT_PHONE_REGION;
|
|
}
|
|
}
|