Merge the Smartlinks plugin (SmartlinksBundle 0.6.0) into the bundle as the Smart Links domain (0.18.0)

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.
This commit is contained in:
Alexandre Gain committed 2026-11-16 13:48:13 +01:00
1 parent debce4ba4a
commit dce849610c
69 files changed
+8225 -43

No files matched your search

+304
View File
@@ -0,0 +1,304 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\FormEntity;
use Mautic\FormBundle\Entity\Form;
use Mautic\PageBundle\Entity\Page;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\SmartLinkRedirectCode;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\SmartLinkRedirectType;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* A Smart Link: a short link ({shortLinkDomain}/l/{slug}) that carries the UTM parameters.
*
* The slug is applied when the link is created (spaces replaced, lowercase enforced) and is
* immutable afterwards. Destination URLs only exist for external redirects; Mautic redirects
* are not processed yet.
*/
class SmartLink extends FormEntity
{
public const TABLE_NAME = 'smart_links';
public const SLUG_MAX_LENGTH = 9; // "text length < 10"
private ?int $id = null;
private ?string $slug = null;
private SmartLinkRedirectType $redirectType = SmartLinkRedirectType::External;
private SmartLinkRedirectCode $redirectCode = SmartLinkRedirectCode::Found;
private ?string $destinationUrl = null;
/**
* Values of the UTM parameters of the link: parameter key => value.
*
* @var array<string, string>
*/
private array $utmParameters = [];
/**
* The landing page of a Mautic redirect (null for external ones).
*/
private ?Page $page = null;
/**
* The standalone form of a Mautic redirect (null for external ones).
*/
private ?Form $form = null;
/**
* The UTM Taxonomy of the link: every UTM value is chosen among its allowed values.
* Null only on links that predate the feature; the form then demands one.
*/
private ?UtmTaxonomy $taxonomy = null;
public static function loadMetadata(OrmClassMetadata $metadata): void
{
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(self::TABLE_NAME)
->setCustomRepositoryClass(SmartLinkRepository::class)
->addIndex(['slug'], 'smart_link_slug');
$builder->addId();
$builder->createField('slug', Types::STRING)
->length(self::SLUG_MAX_LENGTH)
->unique()
->build();
// Mapped on the raw metadata: ClassMetadataBuilder::addField() forces VARCHAR(191) on strings
// and doesn't accept enum types.
$metadata->mapField([
'fieldName' => 'redirectType',
'columnName'=> 'redirect_type',
'type' => Types::STRING,
'length' => 20,
'enumType' => SmartLinkRedirectType::class,
'options' => ['default' => SmartLinkRedirectType::External->value],
]);
$metadata->mapField([
'fieldName' => 'redirectCode',
'columnName'=> 'redirect_code',
'type' => Types::INTEGER,
'enumType' => SmartLinkRedirectCode::class,
'options' => ['default' => SmartLinkRedirectCode::Found->value],
]);
$metadata->mapField([
'fieldName' => 'destinationUrl',
'columnName'=> 'destination_url',
'type' => Types::TEXT,
'nullable' => true,
]);
$metadata->mapField([
'fieldName' => 'utmParameters',
'columnName'=> 'utm_parameters',
'type' => Types::JSON,
]);
// Mautic destinations: a landing page or a standalone form. Deleting the page or the
// form keeps the link (the column becomes null, the link then 404s until reconfigured).
$builder->createManyToOne('page', Page::class)
->addJoinColumn('page_id', 'id', true, false, 'SET NULL')
->build();
$builder->createManyToOne('form', Form::class)
->addJoinColumn('form_id', 'id', true, false, 'SET NULL')
->build();
// The UTM Taxonomy of the link. Deleting the taxonomy keeps the link (the column becomes
// null): it keeps redirecting with the values it already carries, and its next edit
// demands a new taxonomy.
$builder->createManyToOne('taxonomy', UtmTaxonomy::class)
->addJoinColumn('utm_taxonomy_id', 'id', true, false, 'SET NULL')
->build();
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('slug', new NotBlank(message: 'mautic.smartlinks.smart_links.error.slug_required'));
$metadata->addPropertyConstraint('slug', new Length(
max: self::SLUG_MAX_LENGTH,
maxMessage: 'mautic.smartlinks.smart_links.error.slug_length',
));
}
public function getId(): ?int
{
return $this->id;
}
/**
* Core's standard list actions and flash messages call getName(); the slug is the
* link's name.
*/
public function getName(): string
{
return (string) $this->slug;
}
/**
* The short link, for example https://lnk.example.com/l/ab3x. Empty when no domain is set.
*/
public function getShortLink(string $domain): string
{
$domain = trim((string) $domain);
return '' === $domain || '' === (string) $this->slug ? '' : 'https://'.rtrim($domain, '/').'/l/'.$this->slug;
}
public function getSlug(): ?string
{
return $this->slug;
}
/**
* The slug of a saved link is immutable: once set at creation time, it can't change.
*/
public function setSlug(?string $slug): static
{
$immutable = null !== $this->id && null !== $this->slug && '' !== $this->slug;
if ($immutable && null !== $slug && $slug !== $this->slug) {
throw new \LogicException('The slug of a saved smart link cannot be changed.');
}
$this->isChanged('slug', $slug);
$this->slug = $slug;
return $this;
}
public function getRedirectType(): SmartLinkRedirectType
{
return $this->redirectType;
}
public function setRedirectType(SmartLinkRedirectType $redirectType): static
{
if ($redirectType !== $this->redirectType) {
// isChanged() can't log enums (they aren't stringable), so log the backing values.
$this->addChange('redirectType', [$this->redirectType->value, $redirectType->value]);
$this->redirectType = $redirectType;
}
// The destination only exists for external redirects.
if ($redirectType->isMautic()) {
$this->setDestinationUrl(null);
}
return $this;
}
public function getRedirectCode(): SmartLinkRedirectCode
{
return $this->redirectCode;
}
public function setRedirectCode(SmartLinkRedirectCode $redirectCode): static
{
if ($redirectCode !== $this->redirectCode) {
$this->addChange('redirectCode', [$this->redirectCode->value, $redirectCode->value]);
$this->redirectCode = $redirectCode;
}
return $this;
}
public function getDestinationUrl(): ?string
{
return $this->destinationUrl;
}
public function setDestinationUrl(?string $destinationUrl): static
{
if (null !== $destinationUrl && '' === trim($destinationUrl)) {
$destinationUrl = null;
}
$this->isChanged('destinationUrl', $destinationUrl);
$this->destinationUrl = $destinationUrl;
return $this;
}
/**
* @return array<string, string>
*/
public function getUtmParameters(): array
{
return $this->utmParameters;
}
/**
* @param array<string, string>|null $utmParameters
*/
public function setUtmParameters(?array $utmParameters): static
{
$this->isChanged('utmParameters', $utmParameters);
$this->utmParameters = $utmParameters ?? [];
return $this;
}
public function getPage(): ?Page
{
return $this->page;
}
public function setPage(?Page $page): static
{
$this->isChanged('page', $page);
$this->page = $page;
return $this;
}
public function getForm(): ?Form
{
return $this->form;
}
public function setForm(?Form $form): static
{
$this->isChanged('form', $form);
$this->form = $form;
return $this;
}
public function getTaxonomy(): ?UtmTaxonomy
{
return $this->taxonomy;
}
public function setTaxonomy(?UtmTaxonomy $taxonomy): static
{
$this->isChanged('taxonomy', $taxonomy);
$this->taxonomy = $taxonomy;
return $this;
}
/**
* The title of the destination, whatever its type: the slug for external links, the page
* title or the form name for Mautic ones. Used by the list and the flash messages.
*/
public function getDestinationLabel(): string
{
if ($this->redirectType->isMautic()) {
return $this->page?->getTitle() ?? $this->form?->getName() ?? '';
}
return (string) ($this->destinationUrl ?? '');
}
}
+118
View File
@@ -0,0 +1,118 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\FormEntity;
/**
* One call of a Smart Link: the short link was requested by a visitor and redirected them.
* One row is written per hit; the Smart Links list shows the derived counts.
*
* date_added and the other audit columns are inherited from FormEntity (mapped there).
*/
class SmartLinkHit extends FormEntity
{
public const TABLE_NAME = 'smart_link_hits';
private ?int $id = null;
private ?SmartLink $smartLink = null;
private ?string $ipAddress = null;
private ?string $referer = null;
private ?string $userAgent = null;
public static function loadMetadata(OrmClassMetadata $metadata): void
{
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(self::TABLE_NAME)
->setCustomRepositoryClass(SmartLinkHitRepository::class)
->addIndex(['smart_link_id'], 'smart_link_hit_link');
$builder->addId();
// Deleting the link deletes its hits.
$builder->createManyToOne('smartLink', SmartLink::class)
->addJoinColumn('smart_link_id', 'id', false, false, 'CASCADE')
->build();
$builder->createField('ipAddress', 'string')
->columnName('ip_address')
->length(45) // IPv6
->nullable()
->build();
$builder->createField('referer', 'text')
->nullable()
->build();
$builder->createField('userAgent', 'text')
->columnName('user_agent')
->nullable()
->build();
}
public function getId(): ?int
{
return $this->id;
}
public function getSmartLink(): ?SmartLink
{
return $this->smartLink;
}
public function setSmartLink(?SmartLink $smartLink): static
{
$this->isChanged('smartLink', $smartLink);
$this->smartLink = $smartLink;
return $this;
}
public function getIpAddress(): ?string
{
return $this->ipAddress;
}
public function setIpAddress(?string $ipAddress): static
{
$this->isChanged('ipAddress', $ipAddress);
$this->ipAddress = $ipAddress;
return $this;
}
public function getReferer(): ?string
{
return $this->referer;
}
public function setReferer(?string $referer): static
{
$this->isChanged('referer', $referer);
$this->referer = $referer;
return $this;
}
public function getUserAgent(): ?string
{
return $this->userAgent;
}
public function setUserAgent(?string $userAgent): static
{
$this->isChanged('userAgent', $userAgent);
$this->userAgent = $userAgent;
return $this;
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
use Mautic\CoreBundle\Entity\CommonRepository;
/**
* @extends CommonRepository<SmartLinkHit>
*/
class SmartLinkHitRepository extends CommonRepository
{
public function getTableAlias(): string
{
return 'slh';
}
/**
* Hit counts of every Smart Link, in one query (list view): link id => number of hits.
*
* @return array<int, int>
*/
public function getHitCounts(): array
{
$alias = $this->getTableAlias();
$rows = $this->createQueryBuilder($alias)
->select('IDENTITY('.$alias.'.smartLink) AS link_id, COUNT('.$alias.'.id) AS hit_count')
->groupBy($alias.'.smartLink')
->getQuery()
->getArrayResult();
$counts = [];
foreach ($rows as $row) {
$counts[(int) $row['link_id']] = (int) $row['hit_count'];
}
return $counts;
}
/**
* The last hits of a link, most recent first.
*
* @return SmartLinkHit[]
*/
public function findRecentBySmartLink(SmartLink $smartLink, int $limit = 10): array
{
return $this->createQueryBuilder($this->getTableAlias())
->where($this->getTableAlias().'.smartLink = :link')
->setParameter('link', $smartLink)
->orderBy($this->getTableAlias().'.dateAdded', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult();
}
}
+104
View File
@@ -0,0 +1,104 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
use Mautic\CoreBundle\Entity\CommonRepository;
/**
* @extends CommonRepository<SmartLink>
*/
class SmartLinkRepository extends CommonRepository
{
public function getTableAlias(): string
{
return 'sl';
}
/**
* Loads the page and the form with the links, so that the list doesn't query them one by one.
*
* @param array<string, mixed> $args
*/
public function getEntities(array $args = [])
{
$alias = $this->getTableAlias();
// The hit count of each link as a HIDDEN scalar: not hydrated into the entities, but
// available as an order by column ("sl.hitCount") for the Hits column of the list.
$hitCount = '(SELECT COUNT(h.id) FROM '.SmartLinkHit::class.' h WHERE h.smartLink = '.$alias.'.id) AS HIDDEN hitCount';
$args['qb'] = $this->createQueryBuilder($alias)
->select($alias, 'pg', 'fm', $hitCount)
->leftJoin($alias.'.page', 'pg')
->leftJoin($alias.'.form', 'fm');
return parent::getEntities($args);
}
/**
* True when another link already uses this slug. A link being edited never collides with itself.
*/
public function slugExists(string $slug, ?int $ignoreId = null): bool
{
if ('' === $slug) {
return false;
}
$qb = $this->createQueryBuilder($this->getTableAlias())
->select('COUNT('.$this->getTableAlias().'.id)')
->where($this->getTableAlias().'.slug = :slug')
->setParameter('slug', $slug);
if (null !== $ignoreId) {
$qb->andWhere($this->getTableAlias().'.id != :id')
->setParameter('id', $ignoreId);
}
return (int) $qb->getQuery()->getSingleScalarResult() > 0;
}
/**
* The list's search box matches the slug and the destination URL.
*
* @return array<int, mixed>
*/
protected function addCatchAllWhereClause($q, $filter): array
{
return $this->addStandardCatchAllWhereClause($q, $filter, [
$this->getTableAlias().'.slug',
$this->getTableAlias().'.destinationUrl',
]);
}
/**
* The Hits column of the list sorts by the number of hits of each link: the virtual
* "hitCount" order by maps to the HIDDEN hitCount scalar of the list query. Everything
* else falls back to core's column handling.
*/
protected function buildOrderByClause($q, array $args): void
{
$alias = $this->getTableAlias();
$orderBy = (string) ($args['orderBy'] ?? '');
$virtual = $alias.'.hitCount' === $orderBy || 'hitCount' === $orderBy;
if ($virtual && $q instanceof \Doctrine\ORM\QueryBuilder) {
$orderByDir = $this->sanitize((string) ($args['orderByDir'] ?? 'ASC'));
$q->addOrderBy('hitCount', $orderByDir);
return;
}
parent::buildOrderByClause($q, $args);
}
/**
* @return array<array{0: string, 1: string}>
*/
protected function getDefaultOrder(): array
{
return [
[$this->getTableAlias().'.slug', 'ASC'],
];
}
}
+143
View File
@@ -0,0 +1,143 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;
use Mautic\CoreBundle\Entity\FormEntity;
/**
* A UTM Taxonomy: a named set of allowed values for the UTM parameters.
*
* A Smart Link carries one taxonomy; every UTM value of the link is chosen
* among the values the taxonomy defines for its parameter.
*
* The allowed values live in the `parameters` array: parameter key => list of
* {value, label} rows. Only the parameters enabled in Settings > Configuration
* > Smart Links Settings can be given values (the form only offers those).
*/
class UtmTaxonomy extends FormEntity
{
public const TABLE_NAME = 'utm_taxonomies';
public const NAME_MAX_LENGTH = 50;
private ?int $id = null;
private ?string $name = null;
private ?string $description = null;
/**
* Allowed values per parameter: parameter key => list of {value, label}.
*
* @var array<string, list<array{value: string, label: string}>>
*/
private array $parameters = [];
public static function loadMetadata(OrmClassMetadata $metadata): void
{
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(self::TABLE_NAME)
->setCustomRepositoryClass(UtmTaxonomyRepository::class)
->addIndex(['name'], 'utm_taxonomy_name');
$builder->addId();
$builder->createField('name', Types::STRING)
->length(self::NAME_MAX_LENGTH)
->unique()
->build();
$builder->createField('description', Types::TEXT)
->nullable()
->build();
// Mapped on the raw metadata: ClassMetadataBuilder::addField() forces VARCHAR(191) on strings.
$metadata->mapField([
'fieldName' => 'parameters',
'columnName' => 'parameters',
'type' => Types::JSON,
]);
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->isChanged('name', $name);
$this->name = '' !== trim((string) $name) ? $name : null;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
if (null !== $description && '' === trim($description)) {
$description = null;
}
$this->isChanged('description', $description);
$this->description = $description;
return $this;
}
/**
* @return array<string, list<array{value: string, label: string}>>
*/
public function getParameters(): array
{
return $this->parameters;
}
/**
* @param array<string, list<array{value: string, label: string}>>|null $parameters
*/
public function setParameters(?array $parameters): static
{
$this->isChanged('parameters', $parameters);
$this->parameters = $parameters ?? [];
ksort($this->parameters);
return $this;
}
/**
* The allowed values of one parameter, keyed by value (labels are unique per value).
*
* @return list<array{value: string, label: string}>
*/
public function getParameterValues(string $key): array
{
return $this->parameters[$key] ?? [];
}
/**
* The parameter keys the taxonomy defines, in display order.
*
* @return list<string>
*/
public function getParameterKeys(): array
{
return array_keys($this->parameters);
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Entity;
use Mautic\CoreBundle\Entity\CommonRepository;
/**
* @extends CommonRepository<UtmTaxonomy>
*/
class UtmTaxonomyRepository extends CommonRepository
{
public function getTableAlias(): string
{
return 'ut';
}
/**
* True when another taxonomy already uses this name. A taxonomy being edited never collides with itself.
*/
public function nameExists(string $name, ?int $ignoreId = null): bool
{
if ('' === $name) {
return false;
}
$qb = $this->createQueryBuilder($this->getTableAlias())
->select('COUNT('.$this->getTableAlias().'.id)')
->where($this->getTableAlias().'.name = :name')
->setParameter('name', $name);
if (null !== $ignoreId) {
$qb->andWhere($this->getTableAlias().'.id != :id')
->setParameter('id', $ignoreId);
}
return (int) $qb->getQuery()->getSingleScalarResult() > 0;
}
/**
* The list's search box matches the name and the description.
*
* @return array<int, mixed>
*/
protected function addCatchAllWhereClause($q, $filter): array
{
return $this->addStandardCatchAllWhereClause($q, $filter, [
$this->getTableAlias().'.name',
$this->getTableAlias().'.description',
]);
}
/**
* @return array<array{0: string, 1: string}>
*/
protected function getDefaultOrder(): array
{
return [
[$this->getTableAlias().'.name', 'ASC'],
];
}
}