Skip to content

Commit 7621048

Browse files
committed
Add script for the migration of existing contact options
1 parent 58f9721 commit 7621048

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

com.woltlab.wcf/package.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@
4949

5050
<instruction type="script">acp/install_com.woltlab.wcf_step2.php</instruction>
5151
</instructions>
52+
53+
<!--
54+
Required order of the following steps for the update to 6.2:
55+
<instruction type="database" run="standalone">acp/database/update_com.woltlab.wcf_62_step1.php</instruction>
56+
<instruction type="script">acp/update_com.woltlab.wcf_6.2_contactOptions.php</instruction>
57+
<instruction type="database" run="standalone">acp/database/update_com.woltlab.wcf_62_step2.php</instruction>
58+
-->
5259
</package>

wcfsetup/install/files/acp/database/update_com.woltlab.wcf_6.2.php renamed to wcfsetup/install/files/acp/database/update_com.woltlab.wcf_6.2_step1.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@
5252
]),
5353
PartialDatabaseTable::create('wcf1_contact_option')
5454
->columns([
55-
MediumtextDatabaseTableColumn::create('defaultValue')
56-
->drop(),
57-
TextDatabaseTableColumn::create('validationPattern')
58-
->drop(),
59-
MediumtextDatabaseTableColumn::create('selectOptions')
60-
->drop(),
61-
TinyintDatabaseTableColumn::create('required')
62-
->drop(),
6355
MediumtextDatabaseTableColumn::create('configurationData'),
6456
]),
6557
PartialDatabaseTable::create('wcf1_file')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Updates the database layout during the update from 6.1 to 6.2.
5+
*
6+
* @author Olaf Braun
7+
* @copyright 2001-2024 WoltLab GmbH
8+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9+
*/
10+
11+
use wcf\system\database\table\column\MediumtextDatabaseTableColumn;
12+
use wcf\system\database\table\column\TextDatabaseTableColumn;
13+
use wcf\system\database\table\column\TinyintDatabaseTableColumn;
14+
use wcf\system\database\table\PartialDatabaseTable;
15+
16+
return [
17+
PartialDatabaseTable::create('wcf1_contact_option')
18+
->columns([
19+
MediumtextDatabaseTableColumn::create('defaultValue')
20+
->drop(),
21+
TextDatabaseTableColumn::create('validationPattern')
22+
->drop(),
23+
MediumtextDatabaseTableColumn::create('selectOptions')
24+
->drop(),
25+
TinyintDatabaseTableColumn::create('required')
26+
->drop(),
27+
]),
28+
];
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* This script converts existing contact options to the new form option system.
5+
*/
6+
7+
use wcf\data\contact\option\ContactOptionEditor;
8+
use wcf\data\contact\option\ContactOptionList;
9+
use wcf\util\JSON;
10+
use wcf\util\OptionUtil;
11+
12+
$contactOptionList = new ContactOptionList();
13+
$contactOptionList->readObjects();
14+
15+
foreach ($contactOptionList as $contactOption) {
16+
$configurationData = [];
17+
$optionType = '';
18+
$optionType = match ($contactOption->optionType) {
19+
'multiSelect' => 'checkboxes',
20+
'message' => 'wysiwyg',
21+
'URL' => 'url',
22+
default => $contactOption->optionType,
23+
};
24+
25+
if ($contactOption->required) {
26+
$configurationData['required'] = 1;
27+
}
28+
if ($contactOption->defaultValue && $contactOption->optionType == 'text') {
29+
$configurationData['defaultValue'] = $contactOption->defaultValue;
30+
}
31+
if ($contactOption->selectOptions) {
32+
$configurationData['required'] = convertSelectOptions($contactOption->selectOptions);
33+
}
34+
35+
$editor = new ContactOptionEditor($contactOption);
36+
$editor->update([
37+
'optionType' => $optionType,
38+
'configurationData' => JSON::encode($configurationData),
39+
]);
40+
}
41+
42+
function convertSelectOptions(string $selectOptions): string
43+
{
44+
$options = [];
45+
46+
$parsedSelectOptions = OptionUtil::parseSelectOptions($selectOptions);
47+
foreach ($parsedSelectOptions as $key => $value) {
48+
$options[] = [
49+
'key' => $key,
50+
'value' => [
51+
0 => $value
52+
]
53+
];
54+
}
55+
56+
return JSON::encode($options);
57+
}

0 commit comments

Comments
 (0)