Two domains, one MT3S bundle: Marketing Programs (unchanged) and the
imported Smart Links (smart links + UTM taxonomies), each keeping its
own menu, tables, routes and translations.
- Code moved from MauticPlugin\SmartlinksBundle\ to
MauticPlugin\MT3SMarketingProgramBundle\; template namespace
@Smartlinks/ -> @MT3SMarketingProgram/; smartlinks.js ->
Assets/js/mt3s-mp-smartlinks.js; icon -> Assets/img/smartlinks-icon.svg.
- Incoming migrations renamed Version_0_X_0_SmartLinks.php to avoid
the class-name collision; schema untouched, data intact.
- Permissions folded into MT3SMarketingProgramPermissions:
smart_links + taxonomies under the mt3smarketingprogram: base
(roles that had the old smartlinks: grants must be re-granted).
- config.php: Smart Links routes + menu group, public /l/{slug},
SmartlinksIntegration services, smartlinks_* parameters (legacy
utm_manager_* keys still readable), version 0.18.0. The 0.17.0
placeholder /s/marketing-program-utm-taxonomies is replaced by the
real /s/utm-taxonomies.
- Translations merged (en_US) and added (fr_FR); Tests/ + phpunit moved
in; readme documents both domains and the 0.18.0 merge history.
Verified live: menus, both config tabs, both lists + forms, program
pages, public redirect with taxonomy UTM values, host + unknown-slug
404s, 90/90 unit tests, full php -l, all ini catalogs parse.
178 lines
7.1 KiB
PHP
178 lines
7.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Form\Validator;
|
|
|
|
use Mautic\FormBundle\Entity\Form;
|
|
use Mautic\PageBundle\Entity\Page;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\SmartLinkRedirectType;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterMode;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\UtmTaxonomy;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Service\UtmConfigurationProvider;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Model\SmartLinkModel;
|
|
|
|
/**
|
|
* Business rules of the Smart Link form. The form type calls these methods from Callback
|
|
* constraints; each method returns translation keys (validators domain) and parameters.
|
|
*/
|
|
final class SmartLinkValidator
|
|
{
|
|
public const ERROR_SLUG_TAKEN = 'mautic.smartlinks.smart_links.error.slug_taken';
|
|
public const ERROR_SLUG_REQUIRED = 'mautic.smartlinks.smart_links.error.slug_required';
|
|
public const ERROR_SLUG_LENGTH = 'mautic.smartlinks.smart_links.error.slug_length';
|
|
public const ERROR_DESTINATION_REQUIRED = 'mautic.smartlinks.smart_links.error.destination_required';
|
|
public const ERROR_DESTINATION_REQUIRED_MAUTIC = 'mautic.smartlinks.smart_links.error.destination_required_mautic';
|
|
public const ERROR_DESTINATION_INVALID = 'mautic.smartlinks.smart_links.error.destination_invalid';
|
|
public const ERROR_UTM_VALUE_INVALID = 'mautic.smartlinks.smart_links.error.utm_value_invalid';
|
|
public const ERROR_UTM_VALUE_NOT_ALLOWED = 'mautic.smartlinks.smart_links.error.utm_value_not_allowed';
|
|
public const ERROR_TAXONOMY_PARAMETER_UNKNOWN = 'mautic.smartlinks.smart_links.error.taxonomy_parameter_unknown';
|
|
|
|
public function __construct(
|
|
private readonly SmartLinkModel $smartLinkModel,
|
|
private readonly UtmConfigurationProvider $utmConfiguration,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Unique slug. The rules (lowercase, separator replacement) are applied by the model
|
|
* before this check, so the comparison uses the final slug.
|
|
*
|
|
* @return list<array{0: string, 1: array<string, string>}>
|
|
*/
|
|
public function validateSlug(string $slug, ?int $ignoreId = null): array
|
|
{
|
|
if ('' === $slug) {
|
|
return [[self::ERROR_SLUG_REQUIRED, []]];
|
|
}
|
|
|
|
if ($this->smartLinkModel->isSlugTaken($slug, $ignoreId)) {
|
|
return [[self::ERROR_SLUG_TAKEN, ['%slug%' => $slug]]];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* A destination is required for every redirect type: an URL for external ones, a landing
|
|
* page or a standalone form for Mautic ones. Only http(s) URLs are accepted.
|
|
*
|
|
* @return list<array{0: string, 1: array<string, string>}>
|
|
*/
|
|
public function validateDestination(SmartLinkRedirectType $redirectType, ?string $url, ?Page $page = null, ?Form $form = null): array
|
|
{
|
|
if ($redirectType->isMautic()) {
|
|
if (null === $page && null === $form) {
|
|
return [[self::ERROR_DESTINATION_REQUIRED_MAUTIC, []]];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
$url = trim((string) $url);
|
|
|
|
if ('' === $url) {
|
|
return [[self::ERROR_DESTINATION_REQUIRED, []]];
|
|
}
|
|
|
|
if (!filter_var($url, FILTER_VALIDATE_URL)
|
|
|| !in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'], true)) {
|
|
return [[self::ERROR_DESTINATION_INVALID, ['%url%' => $url]]];
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* UTM values follow the Smart Links Settings the admin defines. With a taxonomy chosen:
|
|
* every parameter the taxonomy defines limits the value -- the checkbox subset of the
|
|
* admin's values for a dropdown, the taxonomy's own list for a free parameter. The
|
|
* values the link carried before the edit stay acceptable (they were chosen when the
|
|
* taxonomy — or, before the feature, the global configuration — allowed them), so a plain
|
|
* re-save never errors. Without a taxonomy (links that predate the feature, before their
|
|
* next edit), the values follow the rules of Smart Links Settings.
|
|
*
|
|
* @param array<string, string> $utmParameters
|
|
* @param array<string, string> $stored the values the link carried before the edit
|
|
*
|
|
* @return list<array{0: string, 1: array<string, string>}>
|
|
*/
|
|
public function validateUtmParameters(array $utmParameters, ?UtmTaxonomy $taxonomy = null, array $stored = []): array
|
|
{
|
|
if (null === $taxonomy) {
|
|
return $this->validateAgainstGlobalConfiguration($utmParameters, $stored);
|
|
}
|
|
|
|
$errors = [];
|
|
|
|
foreach ($utmParameters as $key => $value) {
|
|
$parameter = $this->utmConfiguration->getParameter($key);
|
|
|
|
if (null === $parameter || (!$parameter['enabled']) || UtmParameterMode::DISABLED === $parameter['mode']) {
|
|
$errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]];
|
|
|
|
continue;
|
|
}
|
|
|
|
// Every parameter of the taxonomy limits the value: a dropdown subset selected
|
|
// among the admin's values, or the taxonomy's own list for a free parameter.
|
|
$allowed = $taxonomy->getParameterValues($key);
|
|
|
|
if ([] === $allowed) {
|
|
$errors[] = [self::ERROR_TAXONOMY_PARAMETER_UNKNOWN, ['%key%' => $key, '%name%' => (string) $taxonomy->getName()]];
|
|
|
|
continue;
|
|
}
|
|
|
|
if (in_array($value, array_column($allowed, 'value'), true) || ($stored[$key] ?? null) === $value) {
|
|
continue;
|
|
}
|
|
|
|
$errors[] = [self::ERROR_UTM_VALUE_NOT_ALLOWED, ['%key%' => $key, '%value%' => $value]];
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
/**
|
|
* The rules of Smart Links Settings: the values belong to enabled parameters, respect the
|
|
* technical pattern, and dropdown values stay within the configured list — unless the link
|
|
* already carried them.
|
|
*
|
|
* @param array<string, string> $utmParameters
|
|
* @param array<string, string> $stored
|
|
*
|
|
* @return list<array{0: string, 1: array<string, string>}>
|
|
*/
|
|
private function validateAgainstGlobalConfiguration(array $utmParameters, array $stored): array
|
|
{
|
|
$valuePattern = UtmSettingsValidator::VALUE_PATTERN;
|
|
$errors = [];
|
|
|
|
foreach ($utmParameters as $key => $value) {
|
|
$parameter = $this->utmConfiguration->getParameter($key);
|
|
|
|
if (null === $parameter || (!$parameter['enabled']) || UtmParameterMode::DISABLED === $parameter['mode']) {
|
|
$errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]];
|
|
|
|
continue;
|
|
}
|
|
|
|
if (1 !== preg_match($valuePattern, $value)) {
|
|
$errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]];
|
|
|
|
continue;
|
|
}
|
|
|
|
$inList = UtmParameterMode::DROPDOWN !== $parameter['mode']
|
|
|| in_array($value, array_column($this->utmConfiguration->getDropdownValues($key), 'value'), true);
|
|
|
|
if (!$inList && ($stored[$key] ?? null) !== $value) {
|
|
$errors[] = [self::ERROR_UTM_VALUE_NOT_ALLOWED, ['%key%' => $key, '%value%' => $value]];
|
|
}
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
}
|