<?php
declare(strict_types=1);
namespace CrmBundleMigrations;
use Bluue\CrmBundle\Entity\Opportunity;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20231121155915 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE crm_bundle__opportunity_priority (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', name VARCHAR(128) NOT NULL, color VARCHAR(7) NOT NULL, text_color VARCHAR(7) NOT NULL, position INT NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, is_active TINYINT(1) DEFAULT 1 NOT NULL, is_default TINYINT(1) NOT NULL, INDEX IDX_F3D2BD24B03A8386 (created_by_id), INDEX IDX_F3D2BD24896DBBDE (updated_by_id), INDEX IDX_F3D2BD24C76F1F52 (deleted_by_id), INDEX color (color), INDEX text_color (text_color), INDEX name (name), INDEX position (position), INDEX is_active (is_active), INDEX is_default (is_default), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('CREATE TABLE crm_bundle__opportunity_priority_translations (id INT AUTO_INCREMENT NOT NULL, object_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', locale VARCHAR(8) NOT NULL, field VARCHAR(32) NOT NULL, content LONGTEXT DEFAULT NULL, INDEX IDX_A922A7A7232D562B (object_id), FULLTEXT INDEX content (content), UNIQUE INDEX lookup_unique_idx (locale, object_id, field), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority ADD CONSTRAINT FK_F3D2BD24B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority ADD CONSTRAINT FK_F3D2BD24896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority ADD CONSTRAINT FK_F3D2BD24C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority_translations ADD CONSTRAINT FK_A922A7A7232D562B FOREIGN KEY (object_id) REFERENCES crm_bundle__opportunity_priority (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE crm_bundle__opportunity ADD opportunity_priority_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE crm_bundle__opportunity ADD CONSTRAINT FK_CEF0F8C742612354 FOREIGN KEY (opportunity_priority_id) REFERENCES crm_bundle__opportunity_priority (id)');
$this->addSql('CREATE INDEX IDX_CEF0F8C742612354 ON crm_bundle__opportunity (opportunity_priority_id)');
}
public function postUp(Schema $schema): void
{
parent::postUp($schema);
$now = (new \DateTime())->format('Y-m-d H:i:s');
$languages = $this->connection->executeQuery('SELECT * FROM language WHERE deleted_at IS NULL')
->fetchAllAssociative();
$rootLocale = 'en';
$defaultLocale = $this->connection
->executeQuery('SELECT locale FROM language WHERE deleted_at IS NULL AND is_default = 1')
->fetchOne() ?: 'en';
$opportunityPriorities = [
[
'names' => ['fr' => 'Faible', 'en' => 'Low'],
'color' => '#79affd',
'text_color' => '#000000',
'active' => 1,
'default' => 0,
'position' => 1
],
[
'names' => ['fr' => 'Normal', 'en' => 'Normal'],
'color' => '#ffa500',
'text_color' => '#ffffff',
'active' => 1,
'default' => 1,
'position' => 2
],
[
'names' => ['fr' => 'Important', 'en' => 'Important'],
'color' => '#febd63',
'text_color' => '#ffffff',
'active' => 1,
'default' => 0,
'position' => 3
],
[
'names' => ['fr' => 'Critique', 'en' => 'Critical'],
'color' => '#c85c75',
'text_color' => '#ffffff',
'active' => 1,
'default' => 0,
'position' => 4
]
];
foreach ($opportunityPriorities as $opportunityPriority) {
$opportunityPriorityId = (new UuidV6())->toBinary();
$this->connection->insert('crm_bundle__opportunity_priority', [
'id' => $opportunityPriorityId,
'name' => !empty($opportunityPriority['names'][$defaultLocale]) ?
$opportunityPriority['names'][$defaultLocale] : $opportunityPriority['names'][$rootLocale],
'color' => $opportunityPriority['color'],
'text_color' => $opportunityPriority['text_color'],
'is_active' => $opportunityPriority['active'],
'is_default' => $opportunityPriority['default'],
'position' => $opportunityPriority['position'],
'created_at' => $now,
'updated_at' => $now
]);
foreach ($languages as $language) {
$localeTo = $rootLocale;
if (!empty($opportunityPriority['names'][$language['locale']])) {
$localeTo = $language['locale'];
} elseif (!empty($opportunityPriority['names'][$defaultLocale])) {
$localeTo = $defaultLocale;
}
$this->connection->insert('crm_bundle__opportunity_priority_translations', [
'object_id' => $opportunityPriorityId,
'locale' => $language['locale'],
'field' => 'name',
'content' => $opportunityPriority['names'][$localeTo]
]);
}
}
$opportunities = $this->connection->executeQuery('SELECT o.id FROM crm_bundle__opportunity o')->fetchAllAssociative();
$priorityDefaultId = $this->connection->executeQuery('SELECT id FROM crm_bundle__opportunity_priority op WHERE op.is_default = 1')->fetchOne();
$priorityDefaultId = UuidV6::fromBinary($priorityDefaultId)->toRfc4122();
$priorityDefaultId = '0x' . str_replace('-', '', $priorityDefaultId);
foreach ($opportunities as $opportunity) {
$id = UuidV6::fromBinary($opportunity['id'])->toRfc4122();
$this->connection->executeStatement("UPDATE crm_bundle__opportunity SET opportunity_priority_id = " . $priorityDefaultId . " WHERE id = UNHEX(CONCAT('', REPLACE('" . $id . "', '-', '')))");
}
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority DROP FOREIGN KEY FK_F3D2BD24B03A8386');
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority DROP FOREIGN KEY FK_F3D2BD24896DBBDE');
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority DROP FOREIGN KEY FK_F3D2BD24C76F1F52');
$this->addSql('ALTER TABLE crm_bundle__opportunity_priority_translations DROP FOREIGN KEY FK_A922A7A7232D562B');
$this->addSql('DROP TABLE crm_bundle__opportunity_priority');
$this->addSql('DROP TABLE crm_bundle__opportunity_priority_translations');
$this->addSql('ALTER TABLE crm_bundle__opportunity DROP FOREIGN KEY FK_CEF0F8C742612354');
$this->addSql('DROP INDEX IDX_CEF0F8C742612354 ON crm_bundle__opportunity');
$this->addSql('ALTER TABLE crm_bundle__opportunity DROP opportunity_priority_id');
}
}