refactor(plugin): rename Plan to Program and add audience/UTM routes
Rename the Plan bundle to Program across config, routes, menus, and controllers (Plan -> Initiative), bump version to 0.16.0, and introduce new audience and UTM taxonomy routes and permissions to support the expanded Programs feature set.
This commit is contained in:
1 parent
8268fad886
commit
71ff11390b
121 files changed
+5011
-2874
No files matched your search
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MauticPlugin\ProgramBundle\Migrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
|
||||
use MauticPlugin\ProgramBundle\Entity\Audience;
|
||||
use MauticPlugin\ProgramBundle\Entity\Initiative;
|
||||
|
||||
/**
|
||||
* Historical migration (from 0.15.0, when the bundle was still PlanBundle): it renamed "targets"
|
||||
* to "audiences" — plan_targets became plan_audiences (its target_type column audience_type),
|
||||
* plan_targets_plan became plan_audiences_plan (target_id became audience_id), plan_target_stages
|
||||
* became plan_audiences_stages — and created the segments join table plan_audiences_segments.
|
||||
*
|
||||
* Since 0.16.0 the tables are named initiative_* (the 0.16.0 migration renames them), so this
|
||||
* migration only still applies to an install upgrading from 0.14.x or older, which still carries
|
||||
* the plan_targets-era names. The names of that era are kept literal here on purpose.
|
||||
*/
|
||||
class Version_0_15_0 extends AbstractMigration
|
||||
{
|
||||
protected function isApplicable(Schema $schema): bool
|
||||
{
|
||||
if (!$schema->hasTable($this->concatPrefix(Initiative::TABLE_NAME))
|
||||
&& !$schema->hasTable($this->concatPrefix('plans'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The pre-0.15.0 names, or a missing piece of the 0.15.0 schema.
|
||||
return $schema->hasTable($this->concatPrefix('plan_targets'))
|
||||
|| $schema->hasTable($this->concatPrefix('plan_targets_plan'))
|
||||
|| $schema->hasTable($this->concatPrefix('plan_target_stages'))
|
||||
|| $this->needsColumnNameRename($schema, ['plan_targets', 'plan_audiences'], 'target_type')
|
||||
|| $this->needsColumnNameRename($schema, ['plan_targets_plan', 'plan_audiences_plan'], 'target_id')
|
||||
|| $this->needsColumnNameRename($schema, ['plan_target_stages', 'plan_audiences_stages'], 'target_id')
|
||||
|| !$schema->hasTable($this->concatPrefix('plan_audiences_segments'))
|
||||
|| $this->needsForeignKey($schema, 'plan_audiences_plan', 'plan_id')
|
||||
|| $this->needsForeignKey($schema, 'plan_audiences_segments', 'audience_id');
|
||||
}
|
||||
|
||||
protected function up(): void
|
||||
{
|
||||
// The directory table: plan_targets becomes plan_audiences.
|
||||
$this->renameTableIfExists('plan_targets', 'plan_audiences');
|
||||
|
||||
// Its type column: target_type becomes audience_type (on whichever name the table has now).
|
||||
$this->renameColumnIfExists(
|
||||
['plan_targets', 'plan_audiences'],
|
||||
'target_type',
|
||||
'audience_type',
|
||||
"VARCHAR(30) NOT NULL DEFAULT 'persona'"
|
||||
);
|
||||
|
||||
// The link table: plan_targets_plan becomes plan_audiences_plan.
|
||||
$this->renameTableIfExists('plan_targets_plan', 'plan_audiences_plan');
|
||||
|
||||
// Its audience reference: target_id becomes audience_id.
|
||||
$this->renameColumnIfExists(
|
||||
['plan_targets_plan', 'plan_audiences_plan'],
|
||||
'target_id',
|
||||
'audience_id',
|
||||
'INT UNSIGNED NOT NULL'
|
||||
);
|
||||
|
||||
// The stages join table: plan_target_stages becomes plan_audiences_stages.
|
||||
$this->renameTableIfExists('plan_target_stages', 'plan_audiences_stages');
|
||||
|
||||
// Same column rename, on whichever name the table has now.
|
||||
$this->renameColumnIfExists(
|
||||
['plan_target_stages', 'plan_audiences_stages'],
|
||||
'target_id',
|
||||
'audience_id',
|
||||
'INT UNSIGNED NOT NULL'
|
||||
);
|
||||
|
||||
// The segments join table: purely informational, created empty when missing.
|
||||
$this->createSegmentsTableIfMissing();
|
||||
|
||||
// The link table's plan_id foreign key, on the installs where it's missing.
|
||||
$this->repairLinkForeignKeyIfMissing();
|
||||
|
||||
// The segments table's audience_id foreign key: deleting an audience deletes its links.
|
||||
$this->repairSegmentsForeignKeyIfMissing();
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames a table when the old name still exists and the new one doesn't.
|
||||
*/
|
||||
private function renameTableIfExists(string $from, string $to): void
|
||||
{
|
||||
$connection = $this->entityManager->getConnection();
|
||||
|
||||
if ($this->getTableName($connection->createSchemaManager()->introspectSchema(), [$from, $to]) === $this->concatPrefix($from)) {
|
||||
$connection->executeStatement(sprintf(
|
||||
'RENAME TABLE %1$s TO %2$s',
|
||||
$this->concatPrefix($from),
|
||||
$this->concatPrefix($to)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $tables the table's possible names, oldest first: the first that exists is used
|
||||
*/
|
||||
private function renameColumnIfExists(array $tables, string $fromColumn, string $toColumn, string $definition): void
|
||||
{
|
||||
$connection = $this->entityManager->getConnection();
|
||||
$table = $this->getTableName($connection->createSchemaManager()->introspectSchema(), $tables);
|
||||
|
||||
if (null === $table) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($connection->createSchemaManager()->introspectTable($table)->getColumns() as $name => $column) {
|
||||
if (strtolower($name) === strtolower($fromColumn)) {
|
||||
$connection->executeStatement(sprintf(
|
||||
'ALTER TABLE %s CHANGE %s %s %s',
|
||||
$table,
|
||||
$name,
|
||||
$toColumn,
|
||||
$definition
|
||||
));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the first of the tables that exists still carries the old column.
|
||||
*
|
||||
* @param list<string> $tables
|
||||
*/
|
||||
private function needsColumnNameRename(Schema $schema, array $tables, string $column): bool
|
||||
{
|
||||
$table = $this->getTableName($schema, $tables);
|
||||
|
||||
if (null === $table) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($schema->getTable($table)->getColumns() as $name => $columnObject) {
|
||||
if (strtolower($name) === strtolower($column)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the table exists and lacks a foreign key on the given column.
|
||||
*/
|
||||
private function needsForeignKey(Schema $schema, string $table, string $column): bool
|
||||
{
|
||||
$table = $this->concatPrefix($table);
|
||||
|
||||
if (!$schema->hasTable($table)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($schema->getTable($table)->getForeignKeys() as $foreignKey) {
|
||||
if (in_array($column, $foreignKey->getLocalColumns(), true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* The first of the given table names that exists, prefixed.
|
||||
*
|
||||
* @param list<string> $tables
|
||||
*/
|
||||
private function getTableName(Schema $schema, array $tables): ?string
|
||||
{
|
||||
foreach ($tables as $table) {
|
||||
if ($schema->hasTable($this->concatPrefix($table))) {
|
||||
return $this->concatPrefix($table);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function createSegmentsTableIfMissing(): void
|
||||
{
|
||||
$connection = $this->entityManager->getConnection();
|
||||
|
||||
if ($connection->createSchemaManager()->introspectSchema()->hasTable($this->concatPrefix('initiative_audiences_segments'))
|
||||
|| $connection->createSchemaManager()->introspectSchema()->hasTable($this->concatPrefix('plan_audiences_segments'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$connection->executeStatement(sprintf(
|
||||
'CREATE TABLE IF NOT EXISTS %s (audience_id INT UNSIGNED NOT NULL, leadlist_id INT UNSIGNED NOT NULL, '.
|
||||
'INDEX IDX_%1$s_AUDIENCE (audience_id), INDEX IDX_%1$s_LEADLIST (leadlist_id), '.
|
||||
'PRIMARY KEY (audience_id, leadlist_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB',
|
||||
$this->concatPrefix('plan_audiences_segments')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the link table's plan_id foreign key when it's missing (the plugin installer drops
|
||||
* the foreign keys that point to core tables, such as plans — initiatives since 0.16.0).
|
||||
*/
|
||||
private function repairLinkForeignKeyIfMissing(): void
|
||||
{
|
||||
$connection = $this->entityManager->getConnection();
|
||||
$schema = $connection->createSchemaManager()->introspectSchema();
|
||||
$links = $this->getTableName($schema, ['plan_targets_plan', 'plan_audiences_plan']);
|
||||
|
||||
if (null === $links) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($schema->getTable($links)->getForeignKeys() as $foreignKey) {
|
||||
if (in_array('plan_id', $foreignKey->getLocalColumns(), true)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$connection->executeStatement(sprintf(
|
||||
'ALTER TABLE %1$s ADD CONSTRAINT FK_%1$s_plan_id FOREIGN KEY (plan_id) REFERENCES %2$s (id) ON DELETE CASCADE',
|
||||
$links,
|
||||
$this->concatPrefix('plans')
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the segments table's audience_id foreign key when it's missing.
|
||||
*/
|
||||
private function repairSegmentsForeignKeyIfMissing(): void
|
||||
{
|
||||
$connection = $this->entityManager->getConnection();
|
||||
$schema = $connection->createSchemaManager()->introspectSchema();
|
||||
$segments = $this->getTableName($schema, ['initiative_audiences_segments', 'plan_audiences_segments']);
|
||||
|
||||
if (null === $segments) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($schema->getTable($segments)->getForeignKeys() as $foreignKey) {
|
||||
if (in_array('audience_id', $foreignKey->getLocalColumns(), true)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$audiences = $this->getTableName($schema, ['initiative_audiences', 'plan_audiences']);
|
||||
if (null === $audiences) {
|
||||
return;
|
||||
}
|
||||
|
||||
$connection->executeStatement(sprintf(
|
||||
'ALTER TABLE %1$s ADD CONSTRAINT FK_%1$s_audience_id FOREIGN KEY (audience_id) REFERENCES %2$s (id) ON DELETE CASCADE',
|
||||
$segments,
|
||||
$audiences
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user