Files
mautic-marketing-program-bu…/Model/StakeholderModel.php
T
Alexandre Gain 8696acd01c feat: Add TargetType form and migration for plan targets
- Implemented TargetType form for managing target details including name, type, priority, stages, estimated size, profile criteria, pain points, value proposition, and preferred channels.
- Created migration scripts for adding type and priority columns to plan objectives and tasks.
- Added migration for creating plan targets and plan target stages tables, including foreign key constraints.
- Introduced stakeholder management with forms and views for creating, editing, and listing stakeholders.
- Enhanced UI with read more functionality for long text descriptions and filtering options in plan tab tables.
2026-11-08 11:54:25 +01:00

112 lines
3.6 KiB
PHP

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