Files
mautic-marketing-program-bu…/Migrations/Version_0_19_0.php
T
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

137 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\Tools\SchemaTool;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScript;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\CallScriptOutcome;
/**
* Creates the Call Script component's tables (mt3s_call_scripts and mt3s_call_script_outcomes) for
* installs created before 0.19.0.
*
* As in Version_0_2_0, the plugin installer drops the foreign keys that point to core tables
* (users, categories), so they are re-added here with ON DELETE SET NULL. The outcomes table gets
* its ON DELETE CASCADE key to the scripts. Every step runs only if needed.
*
* The statements run immediately through the connection instead of addSql(): the engine executes
* addSql() statements only after up() returns, and the schema must be re-read between steps.
*/
class Version_0_19_0 extends AbstractMigration
{
/**
* Local table (unprefixed) => local column, referenced table, ON DELETE clause.
*/
private const FOREIGN_KEYS = [
['mt3s_call_scripts', 'owner_id', 'users', 'ON DELETE SET NULL'],
['mt3s_call_scripts', 'category_id', 'categories', 'ON DELETE SET NULL'],
['mt3s_call_script_outcomes', 'call_script_id', 'mt3s_call_scripts', 'ON DELETE CASCADE'],
];
protected function isApplicable(Schema $schema): bool
{
return null !== $this->getCreateTableSql($schema)
|| [] !== $this->getMissingForeignKeys($schema);
}
protected function up(): void
{
$connection = $this->entityManager->getConnection();
$schema = $this->introspectSchema();
if (!$schema->hasTable($this->concatPrefix(CallScript::TABLE_NAME))) {
$connection->executeStatement($this->buildCreateTableSql(CallScript::class, CallScript::TABLE_NAME));
$schema = $this->introspectSchema();
}
if (!$schema->hasTable($this->concatPrefix(CallScriptOutcome::TABLE_NAME))) {
$connection->executeStatement($this->buildCreateTableSql(CallScriptOutcome::class, CallScriptOutcome::TABLE_NAME));
$schema = $this->introspectSchema();
}
foreach ($this->getMissingForeignKeys($schema) as [$table, $column, $referenceTable, $onDelete]) {
$connection->executeStatement($this->generateAlterTableForeignKeyStatement(
$table,
[$column],
$referenceTable,
['id'],
$onDelete
));
}
}
/**
* The missing CREATE TABLE statement, or null when both tables already exist.
*/
private function getCreateTableSql(Schema $schema): ?string
{
if (!$schema->hasTable($this->concatPrefix(CallScript::TABLE_NAME))) {
return 'mt3s_call_scripts';
}
if (!$schema->hasTable($this->concatPrefix(CallScriptOutcome::TABLE_NAME))) {
return 'mt3s_call_script_outcomes';
}
return null;
}
/**
* @return list<array{0: string, 1: string, 2: string, 3: string}>
*/
private function getMissingForeignKeys(Schema $schema): array
{
$missing = [];
foreach (self::FOREIGN_KEYS as $foreignKey) {
[$table, $column] = $foreignKey;
$prefixed = $this->concatPrefix($table);
// The tables may not exist yet; up() adds their keys after creating them.
if (!$schema->hasTable($prefixed)) {
continue;
}
$found = false;
foreach ($schema->getTable($prefixed)->getForeignKeys() as $existing) {
if (in_array($column, $existing->getLocalColumns(), true)) {
$found = true;
break;
}
}
if (!$found) {
$missing[] = $foreignKey;
}
}
return $missing;
}
/**
* @param class-string $entityClass
*/
private function buildCreateTableSql(string $entityClass, string $table): string
{
$schemaTool = new SchemaTool($this->entityManager);
$prefix = 'CREATE TABLE '.$this->concatPrefix($table).' ';
foreach ($schemaTool->getCreateSchemaSql([$this->entityManager->getClassMetadata($entityClass)]) as $sql) {
if (str_starts_with($sql, $prefix)) {
return $sql;
}
}
throw new \RuntimeException('SchemaTool returned no CREATE TABLE statement for '.$table.'.');
}
private function introspectSchema(): Schema
{
return $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
}
}