Files
mautic-marketing-program-bu…/Model/UtmTaxonomyModel.php
T
Alexandre Gain 9d81d6c815 refactor(naming): normalise the Smart Links domain to the bundle's canonical names (0.18.1)
One bundle, two domains (Marketing Programs and Smart Links), one naming scheme.\nNothing in the database changed (no tables, columns, URLs or stored data);\nthe runtime was rebuilt and verified live.\n\n- Route names mautic_smartlinks_* -> mt3s_smartlinks_* (paths unchanged);\n  the menu ids follow\n- Model service aliases collapsed to one canonical alias per model:\n  mautic.mt3smarketingprogram.model.<entity> (core's mautic.<x>.model.<x>\n  pattern), so getModel('mt3smarketingprogram.smart_link'); the dead\n  mt3s.* and mautic.mt3smp.*-era aliases are gone\n- Translation keys mautic.smartlinks.* -> mt3s.smart_links.*\n  (mautic.config.tab.smartlinksconfig stays: core derives it)\n- Settings parameters smartlinks_utm_config/smartlinks_config ->\n  mt3s_smart_links_utm_config/mt3s_smart_links_config, with both legacy\n  keys (smartlinks_* of the standalone bundle, utm_manager_* of the\n  UtmManagerBundle era) still readable until the settings are saved again\n- JS loader prefixes smartlinksSmartLink/smartlinksUtmTaxonomy ->\n  mt3sMpSmartLink/mt3sMpUtmTaxonomy (the mt3sMp* family of the program\n  domain)\n- Integration/Support/ConfigSupport -> SmartlinksConfigSupport; integration\n  display names aligned ('MT3S Marketing Program' / 'Smart Links'); the\n  dead mautic.plugin.smartlinks.name catalog keys removed\n- phpunit suite renamed UtmManagerBundle -> MT3SMarketingProgramBundle;\n  version 0.18.1\n\nVerified: full php -l, 90/90 unit tests (phpunit 11.5 phar), cache clear +\nwarm + assets regenerate + plugins reload (as www-data), live: both lists\nand forms (mauticContent mt3sMpSmartLink/mt3sMpUtmTaxonomy), both config\ntabs, the menu labels, the program pages (regression), the public /l/y32b\nredirect through the legacy utm_manager_* settings (302 with the taxonomy's\nUTM values, wrong host and unknown slug 404), all ini catalogs parse, zero\nnew log errors.
2026-11-16 15:47:30 +01:00

149 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Model;
use Doctrine\ORM\EntityManagerInterface;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Model\FormModel;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\UtmTaxonomy;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\UtmTaxonomyRepository;
use MauticPlugin\MT3SMarketingProgramBundle\Form\Type\UtmTaxonomyType;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Available as mautic.mt3smarketingprogram.model.utm_taxonomy, or getModel('mt3smarketingprogram.utm_taxonomy').
*
* @extends FormModel<UtmTaxonomy>
*/
class UtmTaxonomyModel extends FormModel
{
public const MODEL_NAME = 'mt3smarketingprogram.utm_taxonomy';
public function __construct(
EntityManagerInterface $em,
CorePermissions $security,
EventDispatcherInterface $dispatcher,
UrlGeneratorInterface $router,
TranslatorInterface $translator,
UserHelper $userHelper,
LoggerInterface $logger,
CoreParametersHelper $coreParametersHelper,
) {
parent::__construct($em, $security, $dispatcher, $router, $translator, $userHelper, $logger, $coreParametersHelper);
}
public function getRepository(): UtmTaxonomyRepository
{
$repository = $this->em->getRepository(UtmTaxonomy::class);
\assert($repository instanceof UtmTaxonomyRepository);
return $repository;
}
public function getPermissionBase(): string
{
return 'mt3smarketingprogram:taxonomies';
}
public function getEntity($id = null): ?UtmTaxonomy
{
if (null === $id) {
return new UtmTaxonomy();
}
$entity = parent::getEntity($id);
return $entity instanceof UtmTaxonomy ? $entity : null;
}
/**
* @param array<string, mixed> $options
*/
public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface
{
if (!$entity instanceof UtmTaxonomy) {
throw new MethodNotAllowedHttpException(['UtmTaxonomy']);
}
if (!empty($action)) {
$options['action'] = $action;
}
return $formFactory->create(UtmTaxonomyType::class, $entity, $options);
}
/**
* Applies the canonical shape to the value lists (trimmed, de-duplicated, sorted), then saves.
*/
public function saveEntity($entity, $unlock = true): void
{
if ($entity instanceof UtmTaxonomy) {
$entity->setParameters($this->normalizeParameters($entity->getParameters()));
}
parent::saveEntity($entity, $unlock);
}
/**
* The canonical shape of the `parameters` array: parameter key => list of {value, label} rows.
* Empty keys and values are dropped, duplicates keep their first label, labels fall back to
* the value, keys and rows keep a stable order. The UtmTaxonomyValidator guarantees the
* technical pattern of the values; this is the safety net behind it.
*
* @param array<string, mixed> $parameters
*
* @return array<string, list<array{value: string, label: string}>>
*/
public function normalizeParameters(array $parameters): array
{
$clean = [];
foreach ($parameters as $key => $rows) {
$key = trim((string) $key);
if ('' === $key || !is_array($rows)) {
continue;
}
$values = [];
foreach ($rows as $row) {
if (!is_array($row)) {
continue;
}
$value = trim((string) ($row['value'] ?? ''));
if ('' === $value || isset($values[$value])) {
continue;
}
$label = trim((string) ($row['label'] ?? ''));
$values[$value] = ['value' => $value, 'label' => '' !== $label ? $label : $value];
}
if ([] !== $values) {
$clean[$key] = array_values($values);
}
}
ksort($clean);
return $clean;
}
/**
* True when another saved taxonomy already uses this name. $ignoreId keeps a taxonomy being
* edited from colliding with itself.
*/
public function isNameTaken(string $name, ?int $ignoreId = null): bool
{
return $this->getRepository()->nameExists($name, $ignoreId);
}
}