Skip to content

Commit 8e2638b

Browse files
committed
Add migration
1 parent 7811196 commit 8e2638b

4 files changed

Lines changed: 73 additions & 1 deletion

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@
4040
"source": "https://github.com/hofff/contao-formtools"
4141
},
4242
"require": {
43-
"php": "^8.2",
43+
"php": "^8.1",
4444
"ext-json": "*",
4545
"contao/core-bundle": "^5.3",
46+
"doctrine/dbal": "^3.9",
4647
"symfony/config": "^6.4 || ^7.0",
4748
"symfony/dependency-injection": "^6.4 || ^7.0",
4849
"symfony/http-foundation": "^6.4 || ^7.0",

src/DependencyInjection/HofffContaoFormToolsExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ public function load(array $configs, ContainerBuilder $container): void
2222
);
2323

2424
$loader->load('listener.xml');
25+
$loader->load('services.xml');
2526
}
2627
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hofff\Contao\FormTools\Migration;
6+
7+
use Contao\CoreBundle\Migration\AbstractMigration;
8+
use Contao\CoreBundle\Migration\MigrationResult;
9+
use Doctrine\DBAL\Connection;
10+
use Override;
11+
12+
final class SuccessMessageMigration extends AbstractMigration
13+
{
14+
public function __construct(private readonly Connection $connection)
15+
{
16+
}
17+
18+
#[Override]
19+
public function shouldRun(): bool
20+
{
21+
$schemaManager = $this->connection->createSchemaManager();
22+
if (! $schemaManager->tablesExist(['tl_form'])) {
23+
return false;
24+
}
25+
26+
$columns = $schemaManager->listTableColumns('tl_form');
27+
$exists = isset(
28+
$columns['confirmation'],
29+
$columns['hofff_formtools_addsuccess'],
30+
$columns['hofff_formtools_success'],
31+
);
32+
33+
if (! $exists) {
34+
return false;
35+
}
36+
37+
$result = $this->connection->executeQuery(
38+
'SELECT count(*) AS count FROM tl_form WHERE confirmation IS NULL AND hofff_formtools_addSuccess=1',
39+
);
40+
41+
return (int) $result->fetchOne() > 0;
42+
}
43+
44+
#[Override]
45+
public function run(): MigrationResult
46+
{
47+
$sql = <<<'SQL'
48+
UPDATE tl_form
49+
SET confirmation = hofff_formtools_success
50+
WHERE confirmation IS NULL
51+
AND hofff_formtools_addSuccess=1
52+
SQL;
53+
54+
$this->connection->executeQuery($sql);
55+
56+
return $this->createResult(true);
57+
}
58+
}

src/Resources/config/services.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<services>
7+
<service id="Hofff\Contao\FormTools\Migration\SuccessMessageMigration" autoconfigure="true">
8+
<argument type="service" id="database_connection"/>
9+
<tag name="contao.migration"/>
10+
</service>
11+
</services>
12+
</container>

0 commit comments

Comments
 (0)