Files
mautic-marketing-program-bu…/Entity/SmartLink.php
T
Alexandre Gain dce849610c 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.
2026-11-16 13:48:13 +01:00

305 lines
9.2 KiB
PHP

<?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 ?? '');
}
}