vendor/bluue/planning-bundle/migrations/Version20230830081226.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PlanningBundleMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Symfony\Component\Uid\UuidV6;
  6. use Doctrine\Migrations\AbstractMigration;
  7. final class Version20230830081226 extends AbstractMigration
  8. {
  9.     public function up(Schema $schema): void
  10.     {
  11.         $this->addSql('ALTER TABLE planning_bundle__slot_type ADD respected TINYINT(1) DEFAULT 0 NOT NULL');
  12.         $this->addSql('CREATE INDEX respected ON planning_bundle__slot_type (respected)');
  13.     }
  14.     public function down(Schema $schema): void
  15.     {
  16.         $this->addSql('DROP INDEX respected ON planning_bundle__slot_type');
  17.         $this->addSql('ALTER TABLE planning_bundle__slot_type DROP respected');
  18.     }
  19.     public function postUp(Schema $schema): void
  20.     {
  21.         parent::postUp($schema);
  22.         $now = (new \DateTime())->format('Y-m-d H:i:s');
  23.         $languages $this->connection->executeQuery('SELECT * FROM language WHERE deleted_at IS NULL')
  24.             ->fetchAllAssociative();
  25.         $rootLocale 'en';
  26.         $defaultLocale $this->connection
  27.             ->executeQuery('SELECT locale FROM language WHERE deleted_at IS NULL AND is_default = 1')
  28.             ->fetchOne() ?: 'en';
  29.         $slotTypes = [
  30.             [
  31.                 'names' => ['fr' => 'Respecté''en' => 'Respected'],
  32.                 'color' => '#22d325',
  33.                 'text_color' => '#ffffff',
  34.                 'active' => 1,
  35.                 'default' => 0,
  36.                 'respected' => 1
  37.             ]
  38.         ];
  39.         foreach ($slotTypes as $slotType) {
  40.             $slotTypeId = (new UuidV6())->toBinary();
  41.             $this->connection->insert('planning_bundle__slot_type', [
  42.                 'id' => $slotTypeId,
  43.                 'name' => !empty($slotType['names'][$defaultLocale]) ?
  44.                     $slotType['names'][$defaultLocale] : $slotType['names'][$rootLocale],
  45.                 'color' => $slotType['color'],
  46.                 'text_color' => $slotType['text_color'],
  47.                 'is_active' => $slotType['active'],
  48.                 'is_default' => $slotType['default'],
  49.                 'respected' => $slotType['respected'],
  50.                 'created_at' => $now,
  51.                 'updated_at' => $now
  52.             ]);
  53.             foreach ($languages as $language) {
  54.                 $localeTo $rootLocale;
  55.                 if (!empty($slotType['names'][$language['locale']])) {
  56.                     $localeTo $language['locale'];
  57.                 } elseif (!empty($slotType['names'][$defaultLocale])) {
  58.                     $localeTo $defaultLocale;
  59.                 }
  60.                 $this->connection->insert('planning_bundle__slot_type_translations', [
  61.                     'object_id' => $slotTypeId,
  62.                     'locale' => $language['locale'],
  63.                     'field' => 'name',
  64.                     'content' => $slotType['names'][$localeTo]
  65.                 ]);
  66.             }
  67.         }
  68.     }
  69. }