Files
Alexandre Gain b138056e43 style: fix indentation, alignment, and docblock annotations
- 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
2026-11-21 11:23:33 +01:00

101 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Mautic\IntegrationsBundle\Migration\AbstractMigration;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\SmartLink;
use MauticPlugin\MT3SMarketingProgramBundle\Entity\UtmTaxonomy;
/**
* The 0.6.0 UTM Taxonomies feature: the `utm_taxonomies` table, and the taxonomy of each
* Smart Link (`utm_taxonomy_id`, ON DELETE SET NULL: deleting a taxonomy keeps its links,
* they redirect with the values they already carry).
*
* Idempotent: runs on plugin update (core's migration engine) and right after a fresh install
* (PluginInstallSubscriber), and stops when everything is already in place.
*/
class Version_0_6_0_SmartLinks extends AbstractMigration
{
protected function isApplicable(Schema $schema): bool
{
return !$schema->hasTable($this->concatPrefix(UtmTaxonomy::TABLE_NAME))
|| !$this->hasTaxonomyColumn($schema)
|| [] !== $this->getMissingForeignKeys($schema);
}
protected function up(): void
{
$schema = $this->entityManager->getConnection()->createSchemaManager()->introspectSchema();
if (!$schema->hasTable($this->concatPrefix(UtmTaxonomy::TABLE_NAME))) {
$table = $this->concatPrefix(UtmTaxonomy::TABLE_NAME);
$this->addSql(<<<SQL
CREATE TABLE {$table} (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
is_published TINYINT(1) NOT NULL,
date_added DATETIME DEFAULT NULL,
created_by INT DEFAULT NULL,
created_by_user VARCHAR(191) DEFAULT NULL,
date_modified DATETIME DEFAULT NULL,
modified_by INT DEFAULT NULL,
modified_by_user VARCHAR(191) DEFAULT NULL,
checked_out DATETIME DEFAULT NULL,
checked_out_by INT DEFAULT NULL,
checked_out_by_user VARCHAR(191) DEFAULT NULL,
name VARCHAR(50) NOT NULL,
description LONGTEXT DEFAULT NULL,
parameters JSON NOT NULL,
UNIQUE INDEX UNIQ_TAXONOMY_NAME (name),
INDEX utm_taxonomy_name (name),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB
SQL);
}
if (!$this->hasTaxonomyColumn($schema)) {
$this->addSql(sprintf(
'ALTER TABLE %s ADD utm_taxonomy_id INT UNSIGNED DEFAULT NULL',
$this->concatPrefix(SmartLink::TABLE_NAME)
));
}
foreach ($this->getMissingForeignKeys($schema) as $foreignKey) {
$this->addSql($this->generateAlterTableForeignKeyStatement(
SmartLink::TABLE_NAME,
['utm_taxonomy_id'],
UtmTaxonomy::TABLE_NAME,
['id'],
'ON DELETE SET NULL'
));
}
}
private function hasTaxonomyColumn(Schema $schema): bool
{
$links = $this->concatPrefix(SmartLink::TABLE_NAME);
return $schema->hasTable($links) && $schema->getTable($links)->hasColumn('utm_taxonomy_id');
}
/**
* @return list<string>
*/
private function getMissingForeignKeys(Schema $schema): array
{
$links = $this->concatPrefix(SmartLink::TABLE_NAME);
$missing = ['utm_taxonomy_id'];
if ($schema->hasTable($links)) {
foreach ($schema->getTable($links)->getForeignKeys() as $foreignKey) {
$missing = array_diff($missing, $foreignKey->getLocalColumns());
}
}
return array_values($missing);
}
}