migrations/Version20251022183255.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20251022183255 extends AbstractMigration
  10. {
  11.     // shared-sales-stats (old, to be replaced)
  12.     public const OLD_WIDGET_ID 36;
  13.     // New widgets
  14.     public const WIDGET_ID_1 38// shared-sales-ranking
  15.     public const WIDGET_ID_2 39// shared-sales-customers
  16.     public const WIDGET_ID_3 40// shared-sales-fees
  17.     public const WIDGET_ID_4 41// shared-sales-cash
  18.     public const WIDGET_ID_5 42// shared-sales-new-leads
  19.     public const WIDGET_ID_6 43// shared-sales-new-opportunities
  20.     public const WIDGET_ID_7 44// shared-unread-messages
  21.     public const WIDGET_ID_8 45// shared-unread-feeds
  22.     public const WIDGET_ID_9 46// shared-unread-streams
  23.     public const WIDGET_ID_10 47// shared-unread-projects
  24.     public const WIDGET_ID_11 48// shared-open-issues
  25.     public function getDescription(): string
  26.     {
  27.         return '';
  28.     }
  29.     public function up(Schema $schema): void
  30.     {
  31.         // this up() migration is auto-generated, please modify it to your needs
  32.         foreach (self::newWidgets() as [$id$token$name]) {
  33.             $this->addSql(
  34.                 'INSERT INTO widget (id, token, name, category, enabled) VALUES (?, ?, ?, ?, 1)',
  35.                 [$id$token$name'Shared']
  36.             );
  37.         }
  38.         /** @#var string $idList */
  39.         $idList $this->idListSql(self::newWidgetIds());
  40.         $this->addSql(sprintf(
  41.             '
  42.                 INSERT INTO user_widget (object_id, widget_id)
  43.                 SELECT uw.object_id, w.id
  44.                 FROM user_widget uw
  45.                 JOIN widget w ON w.id IN (%s)
  46.                 WHERE uw.widget_id = %d
  47.             ',
  48.             $idList,
  49.             self::OLD_WIDGET_ID
  50.         ));
  51.         $this->addSql(sprintf('DELETE FROM widget WHERE id = %d'self::OLD_WIDGET_ID));
  52.         $this->addSql(sprintf('DELETE FROM user_widget WHERE widget_id = %d'self::OLD_WIDGET_ID));
  53.     }
  54.     public function down(Schema $schema): void
  55.     {
  56.         // this down() migration is auto-generated, please modify it to your needs
  57.         $this->addSql(sprintf('INSERT INTO widget (id, token, name, category, enabled) VALUES (%d, \'shared-sales-stats\', \'Sales Stats\', \'Shared\', 1)'self::OLD_WIDGET_ID));
  58.         /** @#var string $idList */
  59.         $idList $this->idListSql(self::newWidgetIds());
  60.         $this->addSql(sprintf(
  61.             '
  62.                 INSERT INTO user_widget (object_id, widget_id)
  63.                 SELECT DISTINCT object_id, %d
  64.                 FROM user_widget
  65.                 WHERE widget_id IN (%s)
  66.             ',
  67.             self::OLD_WIDGET_ID,
  68.             $idList
  69.         ));
  70.         $this->addSql(sprintf('DELETE FROM widget WHERE id in (%s)'$idList));
  71.         $this->addSql(sprintf('DELETE FROM user_widget WHERE widget_id in (%s)'$idList));
  72.     }
  73.     /**
  74.      * @return array
  75.      */
  76.     private static function newWidgetIds(): array
  77.     {
  78.         return [
  79.             self::WIDGET_ID_1,
  80.             self::WIDGET_ID_2,
  81.             self::WIDGET_ID_3,
  82.             self::WIDGET_ID_4,
  83.             self::WIDGET_ID_5,
  84.             self::WIDGET_ID_6,
  85.             self::WIDGET_ID_7,
  86.             self::WIDGET_ID_8,
  87.             self::WIDGET_ID_9,
  88.             self::WIDGET_ID_10,
  89.             self::WIDGET_ID_11,
  90.         ];
  91.     }
  92.     /**
  93.      * @return array
  94.      */
  95.     private static function newWidgets(): array
  96.     {
  97.         return [
  98.             [self::WIDGET_ID_1,  'shared-sales-ranking',           'Sales Ranking'],
  99.             [self::WIDGET_ID_2,  'shared-sales-customers',         'Sales Customers'],
  100.             [self::WIDGET_ID_3,  'shared-sales-fees',              'Sales Fees'],
  101.             [self::WIDGET_ID_4,  'shared-sales-cash',              'Sales Cash'],
  102.             [self::WIDGET_ID_5,  'shared-sales-new-leads',         'Sales New Leads'],
  103.             [self::WIDGET_ID_6,  'shared-sales-new-opportunities''Sales New Opportunities'],
  104.             [self::WIDGET_ID_7,  'shared-unread-messages',         'Sales Unread Messages'],
  105.             [self::WIDGET_ID_8,  'shared-unread-feeds',            'Sales Unread Feeds'],
  106.             [self::WIDGET_ID_9,  'shared-unread-streams',          'Sales Unread Streams'],
  107.             [self::WIDGET_ID_10'shared-unread-projects',         'Sales Unread Projects'],
  108.             [self::WIDGET_ID_11'shared-open-issues',             'Sales Open Tickets'],
  109.         ];
  110.     }
  111.     /**
  112.      * @param array $ids
  113.      *
  114.      * @return string
  115.      */
  116.     private function idListSql(array $ids): string
  117.     {
  118.         return implode(','array_map('intval'$ids));
  119.     }
  120. }