|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Oveleon\ContaoCookiebar\Migration; |
| 6 | + |
| 7 | +use Contao\CoreBundle\Migration\AbstractMigration; |
| 8 | +use Contao\CoreBundle\Migration\MigrationResult; |
| 9 | +use Doctrine\DBAL\Connection; |
| 10 | +use Doctrine\DBAL\Exception; |
| 11 | + |
| 12 | +class ContaoCookiebarOpenerMigration extends AbstractMigration |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var Connection |
| 16 | + */ |
| 17 | + private $connection; |
| 18 | + |
| 19 | + public function __construct(Connection $connection) |
| 20 | + { |
| 21 | + $this->connection = $connection; |
| 22 | + } |
| 23 | + |
| 24 | + public function getName(): string |
| 25 | + { |
| 26 | + return 'Contao Cookiebar: Cookiebar opener migration'; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @throws Exception |
| 31 | + */ |
| 32 | + public function shouldRun(): bool |
| 33 | + { |
| 34 | + $schemaManager = $this->connection->createSchemaManager(); |
| 35 | + |
| 36 | + if (!$schemaManager->tablesExist(['tl_content', 'tl_module'])) |
| 37 | + { |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + $columns = $schemaManager->listTableColumns('tl_content'); |
| 42 | + |
| 43 | + $total = $this->connection->fetchOne('SELECT COUNT(*) FROM tl_content WHERE type=?', ['cookiebarOpener']); |
| 44 | + $total += $this->connection->fetchOne('SELECT COUNT(*) FROM tl_module WHERE type=?', ['cookiebarOpener']); |
| 45 | + |
| 46 | + return $total > 0; |
| 47 | + } |
| 48 | + |
| 49 | + public function run(): MigrationResult |
| 50 | + { |
| 51 | + $elements = $this->connection->fetchAllAssociative('SELECT id FROM tl_content WHERE type=?', ['cookiebarOpener']); |
| 52 | + $modules = $this->connection->fetchAllAssociative('SELECT id FROM tl_module WHERE type=?', ['cookiebarOpener']); |
| 53 | + |
| 54 | + foreach ($elements as $element) |
| 55 | + { |
| 56 | + $this->connection->update('tl_content', ['type' => 'cookiebar_opener'], ['id' => $element['id']]); |
| 57 | + } |
| 58 | + |
| 59 | + foreach ($modules as $module) |
| 60 | + { |
| 61 | + $this->connection->update('tl_module', ['type' => 'cookiebar_opener'], ['id' => $module['id']]); |
| 62 | + } |
| 63 | + |
| 64 | + return $this->createResult(true); |
| 65 | + } |
| 66 | +} |
0 commit comments