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.
136 lines
4.7 KiB
PHP
136 lines
4.7 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace MauticPlugin\MT3SMarketingProgramBundle\Service;
|
||
|
||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterCatalog;
|
||
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterMode;
|
||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||
|
||
/**
|
||
* Read-only access to the Smart Links UTM configuration. Other services (e.g. the link builder)
|
||
* must use this class instead of reading `mt3s_smart_links_utm_config` from local.php.
|
||
*
|
||
* Every returned parameter has this shape:
|
||
* key, label, mode, enabled, group (standard|platform|custom), custom, auto_generated
|
||
* + values (list of {value, label}) and default (?string) for `dropdown` parameters only.
|
||
*/
|
||
final class UtmConfigurationProvider
|
||
{
|
||
public const CONFIG_PARAMETER = 'mt3s_smart_links_utm_config';
|
||
|
||
/** Key of the standalone SmartlinksBundle era (0.1.0–0.18.0); still honored until the settings are saved again. */
|
||
public const LEGACY_SMARTLINKS_PARAMETER = 'smartlinks_utm_config';
|
||
|
||
/** Key of the UtmManagerBundle era (< 0.5.0); still honored until the settings are saved again. */
|
||
public const LEGACY_CONFIG_PARAMETER = 'utm_manager_config';
|
||
|
||
public function __construct(
|
||
private readonly CoreParametersHelper $coreParametersHelper,
|
||
private readonly UtmConfigurationNormalizer $normalizer,
|
||
private readonly UtmParameterCatalog $catalog,
|
||
private readonly TranslatorInterface $translator,
|
||
) {
|
||
}
|
||
|
||
/**
|
||
* Parameters that are enabled and not in `disabled` mode, in display order, keyed by parameter key.
|
||
*
|
||
* @return array<string, array<string, mixed>>
|
||
*/
|
||
public function getEnabledParameters(): array
|
||
{
|
||
$enabled = [];
|
||
foreach ($this->getConfig()['parameters'] as $key => $parameter) {
|
||
if ($parameter['enabled'] && UtmParameterMode::DISABLED !== $parameter['mode']) {
|
||
$enabled[$key] = $this->decorate($key, $parameter);
|
||
}
|
||
}
|
||
|
||
return $enabled;
|
||
}
|
||
|
||
/**
|
||
* Any catalog or custom parameter, whatever its state. Null when the key is unknown.
|
||
*
|
||
* @return array<string, mixed>|null
|
||
*/
|
||
public function getParameter(string $key): ?array
|
||
{
|
||
$parameter = $this->getConfig()['parameters'][$key] ?? null;
|
||
|
||
return null === $parameter ? null : $this->decorate($key, $parameter);
|
||
}
|
||
|
||
/**
|
||
* Allowed values of a `dropdown` parameter. Always empty for `free` and `disabled` parameters.
|
||
*
|
||
* @return list<array{value: string, label: string}>
|
||
*/
|
||
public function getDropdownValues(string $key): array
|
||
{
|
||
$parameter = $this->getConfig()['parameters'][$key] ?? null;
|
||
|
||
if (null === $parameter || UtmParameterMode::DROPDOWN !== $parameter['mode']) {
|
||
return [];
|
||
}
|
||
|
||
return $parameter['values'] ?? [];
|
||
}
|
||
|
||
public function isLowercaseEnforced(): bool
|
||
{
|
||
return $this->getConfig()['enforce_lowercase'];
|
||
}
|
||
|
||
public function getSeparatorReplacement(): string
|
||
{
|
||
return $this->getConfig()['separator_replacement'];
|
||
}
|
||
|
||
/**
|
||
* @return array{enforce_lowercase: bool, separator_replacement: string, parameters: array<string, array<string, mixed>>}
|
||
*/
|
||
private function getConfig(): array
|
||
{
|
||
$raw = $this->coreParametersHelper->get(self::CONFIG_PARAMETER, []);
|
||
|
||
// Upgrades keep their settings in local.php under a legacy key (the standalone
|
||
// SmartlinksBundle wrote `smartlinks_utm_config`, the UtmManagerBundle before it
|
||
// `utm_manager_config`); serve them until the Configuration page is saved again
|
||
// (which writes the new key).
|
||
foreach ([self::LEGACY_SMARTLINKS_PARAMETER, self::LEGACY_CONFIG_PARAMETER] as $legacyKey) {
|
||
if (null === $raw || [] === $raw) {
|
||
$legacy = $this->coreParametersHelper->get($legacyKey, []);
|
||
if (null !== $legacy && [] !== $legacy) {
|
||
$raw = $legacy;
|
||
}
|
||
}
|
||
}
|
||
|
||
return $this->normalizer->normalize($raw);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $parameter
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function decorate(string $key, array $parameter): array
|
||
{
|
||
$definition = $this->catalog->get($key);
|
||
|
||
if ('' === $parameter['label']) {
|
||
$parameter['label'] = null !== $definition ? $this->translator->trans($definition->getLabelKey()) : $key;
|
||
}
|
||
|
||
return ['key' => $key] + $parameter + [
|
||
'group' => $definition?->group ?? UtmParameterCatalog::GROUP_CUSTOM,
|
||
'custom' => null === $definition,
|
||
'auto_generated' => $definition?->autoGenerated ?? false,
|
||
];
|
||
}
|
||
}
|