Files
alexandre_gain_mt3s 4fb36eebfc feat(call-script): add the Call Script component (0.19.0)
A new Mautic component, listed under Components > Call Scripts (/s/call-scripts),
built like the other domains on AbstractStandardFormController/FormModel.

- CallScript (mt3s_call_scripts): name, description, category, owner, status,
the publish window shown as start/end date and time, the HTML script with
dynamic tokens kept as typed (html cleaning mask), the calling window
(weekdays, hours, sentinel in the contact's or a fixed timezone) and the retry
policy (max attempts, delay, backoff: none/linear/exponential)
- CallScriptOutcome (mt3s_call_script_outcomes, ON DELETE CASCADE): label,
data type (checkbox/radio/free text/dropdown), options JSON ({value,label})
and position; nested collections reuse the program-collection JS
- validators: ValidCallingWindow, ValidCallScriptOutcomeOptions, and an atPath
option on EndDateAfterStartDate so its violation lands on publishDown
- config: routes, the Components menu item (parent mautic.core.components),
call_script category type, model alias, call_scripts permissions, version 0.19.0
- migration Version_0_19_0 creates both tables and their FKs immediately
(users/categories SET NULL, outcomes CASCADE), idempotent
- translations en_US/fr_FR, unit tests, readme/AGENTS documentation
2026-11-21 20:58:25 +01:00

201 lines
5.5 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 MauticPlugin\MT3SMarketingProgramBundle\Validator\Constraints\ValidCallScriptOutcomeOptions;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* One outcome of a call script: what the agent records after the call.
*
* CallScript::$outcomes owns the lifecycle: outcomes are saved and deleted with their script, and are
* ordered by position. The options are stored as a list of {value, label} rows; they are required for
* the checkbox, radio and dropdown types, and forbidden for free text.
*/
class CallScriptOutcome
{
public const TABLE_NAME = 'mt3s_call_script_outcomes';
public const LABEL_MAX_LENGTH = 191;
public const VALUE_MAX_LENGTH = 191;
public const OPTIONS_MAX = 100;
private ?int $id = null;
private ?CallScript $callScript = null;
private ?string $label = null;
/**
* Defaults to free text: a new outcome is valid without options.
*/
private CallScriptOutcomeInputType $inputType = CallScriptOutcomeInputType::FreeText;
/**
* The choices of a checkbox, radio or dropdown outcome, as a list of {value, label} rows.
*
* @var list<array{value: string, label: string}>
*/
private array $options = [];
/**
* Display order within the script, from 0. Set by drag and drop in the form.
*/
private int $position = 0;
public static function loadMetadata(OrmClassMetadata $metadata): void
{
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable(self::TABLE_NAME);
$builder->addId();
$builder->createManyToOne('callScript', CallScript::class)
->inversedBy('outcomes')
->addJoinColumn('call_script_id', 'id', false, false, 'CASCADE')
->build();
$builder->createField('label', Types::STRING)
->length(self::LABEL_MAX_LENGTH)
->build();
// Mapped on the raw metadata: ClassMetadataBuilder forces VARCHAR(191) on strings and
// doesn't accept enum or JSON types.
$metadata->mapField([
'fieldName' => 'inputType',
'columnName' => 'input_type',
'type' => Types::STRING,
'length' => 20,
'enumType' => CallScriptOutcomeInputType::class,
'options' => ['default' => CallScriptOutcomeInputType::FreeText->value],
]);
$metadata->mapField([
'fieldName' => 'options',
'columnName' => 'options',
'type' => Types::JSON,
]);
$builder->createField('position', Types::INTEGER)
->option('default', 0)
->build();
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('label', new NotBlank(message: 'mt3s.call_script.outcome.label.required'));
$metadata->addPropertyConstraint('label', new Length(max: self::LABEL_MAX_LENGTH));
$metadata->addConstraint(new ValidCallScriptOutcomeOptions());
}
public function getId(): ?int
{
return $this->id;
}
public function getCallScript(): ?CallScript
{
return $this->callScript;
}
public function setCallScript(?CallScript $callScript): static
{
$this->callScript = $callScript;
return $this;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(?string $label): static
{
$this->label = $label;
return $this;
}
public function getInputType(): CallScriptOutcomeInputType
{
return $this->inputType;
}
/**
* Null (a type field missing from the submitted form) keeps the current type.
*/
public function setInputType(?CallScriptOutcomeInputType $inputType): static
{
if (null !== $inputType) {
$this->inputType = $inputType;
}
return $this;
}
public function hasOptions(): bool
{
return $this->inputType->hasOptions();
}
/**
* @return list<array{value: string, label: string}>
*/
public function getOptions(): array
{
return $this->options;
}
/**
* Keeps the {value, label} rows of the submitted options, dropping the empty ones and
* defaulting an empty label to the value.
*
* @param list<array{value?: string, label?: string}>|array<int, mixed>|null $options
*/
public function setOptions(?array $options): static
{
$clean = [];
foreach (array_slice($options ?? [], 0, self::OPTIONS_MAX) as $option) {
if (!is_array($option)) {
continue;
}
$value = trim((string) ($option['value'] ?? ''));
$label = trim((string) ($option['label'] ?? ''));
if ('' === $value && '' === $label) {
continue;
}
$clean[] = ['value' => $value, 'label' => '' !== $label ? $label : $value];
}
$this->options = $clean;
return $this;
}
public function getPosition(): int
{
return $this->position;
}
public function setPosition(?int $position): static
{
$this->position = $position ?? 0;
return $this;
}
}