Files
mautic-marketing-program-bu…/Model/StakeholderModel.php
T
Alexandre Gain ea88b7a772 refactor(plugin): rebrand as MT3SMarketingProgram, release 0.17.0
Rename the plugin's vendor/namespace from experiential/Program to mt3s/MT3SMarketingProgram (composer: mt3s/mautic-marketing-program-bundle):

- ProgramBundle -> MT3SMarketingProgramBundle; extension, integration,
  config support, permissions, events and twig extension renamed likewise
- Initiative* -> Program*: controller (InitiativeController ->
  ProgramController), model, entities, repositories, form types and views
  (new _tabs/_task_table/_tab_table_filter partials, details page)
- Settings config form theme -> _config_mt3smarketingprogramconfig_widget;
  routes and translations re-prefixed (mt3s_marketing_program_*)
- Assets: icon.svg -> mt3s-mp-icon.svg; program.js -> mt3s-mp-program.js
  (OnLoad hooks renamed mt3sMpProgram*)
- Add Command/RefreshStatsCommand; Migrations/Version_0_17_0.php renames
  the tables to mt3s_mp_program_*
2026-11-15 17:48:01 +01:00

112 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Model;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Mautic\CoreBundle\Model\FormModel;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Stakeholder;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\StakeholderRepository;
use MauticPlugin\MT3SMarketingProgramBundle\Form\Type\StakeholderType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
/**
* Available as mt3s.marketing_program.model.stakeholder, or getModel('mt3smp.stakeholder').
*
* On save, the phone number is normalized to E.164 using the default region from Settings >
* Configuration > Program Settings (parameter mt3s_marketing_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 = 'mt3s_marketing_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 'mt3smarketingprogram: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;
}
}