-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathUpgradePolymorphicExtension.php
More file actions
108 lines (96 loc) · 4.15 KB
/
Copy pathUpgradePolymorphicExtension.php
File metadata and controls
108 lines (96 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace SilverStripe\UserForms\Extension;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Extension;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Core\Validation\ValidationException;
use SilverStripe\Dev\Deprecation;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
use SilverStripe\UserForms\Model\UserDefinedForm;
use SilverStripe\UserForms\UserForm;
/**
* This extension provides a hook that runs when building the db which will check for existing data in various
* polymorphic relationship fields for userforms models, and ensure that the data is correct.
*
* Various `Parent` relationships in silverstripe/userforms for SilverStripe 3 were mapped directly to UserDefinedForm
* instances, and were made polymorphic in SilverStripe 4 (which also requires a class name). This means that a
* certain amount of manual checking is required to ensure that upgrades are performed smoothly.
*
* @extends Extension<UserDefinedForm>
* @deprecated 6.1.0 Will be removed without equivalent functionality to replace it in a future major release.
*/
class UpgradePolymorphicExtension extends Extension
{
/**
* A list of userforms classes that have had polymorphic relationships added in SilverStripe 4, and the fields
* on them that are polymorphic
*
* @var array
*/
protected $targets = [
EditableFormField::class => ['ParentClass'],
EmailRecipient::class => ['FormClass'],
SubmittedForm::class => ['ParentClass'],
];
/**
* The default class name that will be used to replace values with
*
* @var string
*/
protected $defaultReplacement = UserDefinedForm::class;
public function __construct()
{
Deprecation::noticeWithNoReplacment('6.1.0');
}
protected function onRequireDefaultRecords()
{
if (!Deprecation::withSuppressedNotice(fn () => UserDefinedForm::config()->get('upgrade_on_build'))) {
return;
}
$updated = 0;
foreach ($this->targets as $className => $fieldNames) {
foreach ($fieldNames as $fieldName) {
/** @var DataList $list */
$list = $className::get();
foreach ($list as $entry) {
/** @var DataObject $relationshipObject */
$relationshipObject = Injector::inst()->get($entry->$fieldName);
if (!$relationshipObject) {
continue;
}
// If the defined data class doesn't have the UserForm trait applied, it's probably wrong. Re-map
// it to a default value that does
$classTraits = class_uses($relationshipObject);
if (in_array(UserForm::class, $classTraits ?? [])) {
continue;
}
// Don't rewrite class values when an existing value is set and is an instance of UserDefinedForm
if ($relationshipObject instanceof UserDefinedForm) {
continue;
}
$entry->$fieldName = $this->defaultReplacement;
try {
$entry->write();
$updated++;
} catch (ValidationException $ex) {
// no-op, allow the rest of the db build to continue. There may be an error indicating that the
// object's class doesn't exist, which can be fixed by {@link DatabaseAdmin::doBuild} and this
// logic will work the next time the db is built.
}
}
}
}
if ($updated) {
$message = "Corrected {$updated} default polymorphic class names to {$this->defaultReplacement}";
if (Environment::isCli()) {
echo sprintf(" * %s\n", $message);
} else {
echo sprintf("<li>%s</li>\n", $message);
}
}
}
}