<?php
declare(strict_types=1);
namespace PlanningBundleMigrations;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\Uid\UuidV6;
use Doctrine\Migrations\AbstractMigration;
final class Version20230830081226 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE planning_bundle__slot_type ADD respected TINYINT(1) DEFAULT 0 NOT NULL');
$this->addSql('CREATE INDEX respected ON planning_bundle__slot_type (respected)');
}
public function down(Schema $schema): void
{
$this->addSql('DROP INDEX respected ON planning_bundle__slot_type');
$this->addSql('ALTER TABLE planning_bundle__slot_type DROP respected');
}
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';
$slotTypes = [
[
'names' => ['fr' => 'Respecté', 'en' => 'Respected'],
'color' => '#22d325',
'text_color' => '#ffffff',
'active' => 1,
'default' => 0,
'respected' => 1
]
];
foreach ($slotTypes as $slotType) {
$slotTypeId = (new UuidV6())->toBinary();
$this->connection->insert('planning_bundle__slot_type', [
'id' => $slotTypeId,
'name' => !empty($slotType['names'][$defaultLocale]) ?
$slotType['names'][$defaultLocale] : $slotType['names'][$rootLocale],
'color' => $slotType['color'],
'text_color' => $slotType['text_color'],
'is_active' => $slotType['active'],
'is_default' => $slotType['default'],
'respected' => $slotType['respected'],
'created_at' => $now,
'updated_at' => $now
]);
foreach ($languages as $language) {
$localeTo = $rootLocale;
if (!empty($slotType['names'][$language['locale']])) {
$localeTo = $language['locale'];
} elseif (!empty($slotType['names'][$defaultLocale])) {
$localeTo = $defaultLocale;
}
$this->connection->insert('planning_bundle__slot_type_translations', [
'object_id' => $slotTypeId,
'locale' => $language['locale'],
'field' => 'name',
'content' => $slotType['names'][$localeTo]
]);
}
}
}
}