- Correct getChoices() docblock to reflect translated label => enum backing value order instead of value => label - Re-indent Smart Links menu config to match actual array nesting - Align assignment operators in RefreshStatsCommand - Add missing newline at end of RefreshStatsCommand.php
144 lines
3.7 KiB
PHP
144 lines
3.7 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;
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|