|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Service to support upgrade of userforms module |
|
5
|
|
|
*/ |
|
6
|
|
|
class UserFormsUpgradeService |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var bool |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $quiet; |
|
13
|
|
|
|
|
14
|
2 |
|
public function run() |
|
15
|
|
|
{ |
|
16
|
2 |
|
$this->log("Upgrading formfield rules and custom settings"); |
|
17
|
|
|
|
|
18
|
|
|
// List of rules that have been created in all stages |
|
19
|
2 |
|
$fields = Versioned::get_including_deleted('EditableFormField'); |
|
20
|
2 |
|
foreach ($fields as $field) { |
|
21
|
2 |
|
$this->upgradeField($field); |
|
22
|
2 |
|
} |
|
23
|
2 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Migrate a versioned field in all stages |
|
27
|
|
|
* |
|
28
|
|
|
* @param EditableFormField $field |
|
29
|
|
|
*/ |
|
30
|
2 |
|
protected function upgradeField(EditableFormField $field) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->log("Upgrading formfield ID = ".$field->ID); |
|
33
|
|
|
|
|
34
|
|
|
// Check versions this field exists on |
|
35
|
2 |
|
$filter = sprintf('"EditableFormField"."ID" = \'%d\' AND "Migrated" = 0', $field->ID); |
|
36
|
2 |
|
$stageField = Versioned::get_one_by_stage('EditableFormField', 'Stage', $filter); |
|
37
|
2 |
|
$liveField = Versioned::get_one_by_stage('EditableFormField', 'Live', $filter); |
|
38
|
|
|
|
|
39
|
2 |
|
if ($stageField) { |
|
40
|
2 |
|
$this->upgradeFieldInStage($stageField, 'Stage'); |
|
|
|
|
|
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
if ($liveField) { |
|
44
|
|
|
$this->upgradeFieldInStage($liveField, 'Live'); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Migrate a versioned field in a single stage |
|
50
|
|
|
* |
|
51
|
|
|
* @param EditableFormField $field |
|
52
|
|
|
* @param stage $stage |
|
53
|
|
|
*/ |
|
54
|
2 |
|
protected function upgradeFieldInStage(EditableFormField $field, $stage) |
|
55
|
|
|
{ |
|
56
|
2 |
|
Versioned::reading_stage($stage); |
|
57
|
|
|
|
|
58
|
|
|
// Migrate field rules |
|
59
|
2 |
|
$this->migrateRules($field, $stage); |
|
60
|
|
|
|
|
61
|
|
|
// Migrate custom settings |
|
62
|
2 |
|
$this->migrateCustomSettings($field, $stage); |
|
63
|
|
|
|
|
64
|
|
|
// Flag as migrated |
|
65
|
2 |
|
$field->Migrated = true; |
|
|
|
|
|
|
66
|
2 |
|
$field->write(); |
|
67
|
2 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Migrate custom rules for the given field |
|
71
|
|
|
* |
|
72
|
|
|
* @param EditableFormField $field |
|
73
|
|
|
* @param string $stage |
|
74
|
|
|
*/ |
|
75
|
2 |
|
protected function migrateRules(EditableFormField $field, $stage) |
|
76
|
|
|
{ |
|
77
|
2 |
|
$rulesData = $field->CustomRules |
|
|
|
|
|
|
78
|
2 |
|
? unserialize($field->CustomRules) |
|
|
|
|
|
|
79
|
2 |
|
: array(); |
|
80
|
|
|
|
|
81
|
|
|
// Skip blank rules or fields with custom rules already |
|
82
|
2 |
|
if (empty($rulesData) || $field->DisplayRules()->count()) { |
|
83
|
2 |
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Check value of this condition |
|
87
|
2 |
|
foreach ($rulesData as $ruleDataItem) { |
|
88
|
2 |
|
if (empty($ruleDataItem['ConditionOption']) || empty($ruleDataItem['Display'])) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Get data for this rule |
|
93
|
2 |
|
$conditionOption = $ruleDataItem['ConditionOption']; |
|
94
|
2 |
|
$display = $ruleDataItem['Display']; |
|
95
|
2 |
|
$conditionFieldName = empty($ruleDataItem['ConditionField']) ? null : $ruleDataItem['ConditionField']; |
|
96
|
2 |
|
$value = isset($ruleDataItem['Value']) |
|
97
|
2 |
|
? $ruleDataItem['Value'] |
|
98
|
2 |
|
: null; |
|
99
|
|
|
|
|
100
|
|
|
// Create rule |
|
101
|
2 |
|
$rule = $this->findOrCreateRule($field, $stage, $conditionOption, $display, $conditionFieldName, $value); |
|
102
|
2 |
|
$this->log("Upgrading rule ID = " . $rule->ID); |
|
103
|
2 |
|
} |
|
104
|
2 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Migrate custom settings for the given field |
|
110
|
|
|
* |
|
111
|
|
|
* @param EditableFormField $field |
|
112
|
|
|
* @param string $stage |
|
113
|
|
|
*/ |
|
114
|
2 |
|
protected function migrateCustomSettings(EditableFormField $field, $stage) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
// Custom settings include: |
|
117
|
|
|
// - ExtraClass |
|
118
|
|
|
// - RightTitle |
|
119
|
|
|
// - ShowOnLoad (show or '' are treated as true) |
|
120
|
|
|
// |
|
121
|
|
|
// - CheckedDefault (new field on EditableCheckbox - should be read from old "default" value) |
|
122
|
|
|
// - Default (EditableCheckbox) |
|
|
|
|
|
|
123
|
|
|
// - DefaultToToday (EditableDateField) |
|
124
|
|
|
// - Folder (EditableFileField) |
|
125
|
|
|
// - Level (EditableFormHeading) |
|
126
|
|
|
// - HideFromReports (EditableFormHeading / EditableLiteralField) |
|
127
|
|
|
// - Content (EditableLiteralField) |
|
128
|
|
|
// - GroupID (EditableMemberListField) |
|
129
|
|
|
// - MinValue (EditableNumericField) |
|
130
|
|
|
// - MaxValue (EditableNumericField) |
|
131
|
|
|
// - MinLength (EditableTextField) |
|
132
|
|
|
// - MaxLength (EditableTextField) |
|
133
|
|
|
// - Rows (EditableTextField) |
|
134
|
|
|
// - Placeholder (EditableTextField / EditableEmailField / EditableNumericField) |
|
135
|
2 |
|
|
|
136
|
2 |
|
$customSettings = $field->CustomSettings |
|
|
|
|
|
|
137
|
2 |
|
? unserialize($field->CustomSettings) |
|
|
|
|
|
|
138
|
|
|
: array(); |
|
139
|
|
|
|
|
140
|
2 |
|
// Skip blank rules or fields with custom rules already |
|
141
|
2 |
|
if (empty($customSettings)) { |
|
142
|
|
|
return; |
|
143
|
|
|
} |
|
144
|
2 |
|
|
|
145
|
2 |
|
$field->migrateSettings($customSettings); |
|
146
|
2 |
|
|
|
147
|
|
|
if ($field->config()->has_placeholder) { |
|
148
|
|
|
$this->migratePlaceholder($field, $field->ClassName); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$field->write(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Create or find an existing field with the matched specification |
|
156
|
|
|
* |
|
157
|
|
|
* @param EditableFormField $field |
|
158
|
|
|
* @param string $stage |
|
159
|
2 |
|
* @param string $conditionOption |
|
160
|
|
|
* @param string $display |
|
161
|
|
|
* @param string $conditionFieldName |
|
162
|
|
|
* @param string $value |
|
163
|
2 |
|
* @return EditableCustomRule |
|
164
|
2 |
|
*/ |
|
165
|
|
|
protected function findOrCreateRule(EditableFormField $field, $stage, $conditionOption, $display, $conditionFieldName, $value) |
|
166
|
|
|
{ |
|
167
|
2 |
|
// Get id of field |
|
168
|
|
|
$conditionField = $conditionFieldName |
|
169
|
|
|
? EditableFormField::get()->filter('Name', $conditionFieldName)->first() |
|
170
|
|
|
: null; |
|
171
|
|
|
|
|
172
|
|
|
// If live, search stage record for matching one |
|
173
|
|
|
if ($stage === 'Live') { |
|
174
|
|
|
$list = Versioned::get_by_stage('EditableCustomRule', 'Stage') |
|
175
|
|
|
->filter(array( |
|
176
|
|
|
'ParentID' => $field->ID, |
|
177
|
|
|
'ConditionFieldID' => $conditionField ? $conditionField->ID : 0, |
|
178
|
|
|
'Display' => $display, |
|
179
|
|
|
'ConditionOption' => $conditionOption |
|
180
|
|
|
)); |
|
181
|
|
|
if ($value) { |
|
182
|
|
|
$list = $list->filter('FieldValue', $value); |
|
183
|
|
|
} else { |
|
184
|
|
|
$list = $list->where('"FieldValue" IS NULL OR "FieldValue" = \'\''); |
|
185
|
|
|
} |
|
186
|
|
|
$rule = $list->first(); |
|
187
|
|
|
if ($rule) { |
|
188
|
|
|
$rule->write(); |
|
189
|
2 |
|
$rule->publish("Stage", "Live"); |
|
190
|
2 |
|
return $rule; |
|
191
|
2 |
|
} |
|
192
|
2 |
|
} |
|
193
|
2 |
|
|
|
194
|
2 |
|
// If none found, or in stage, create new record |
|
195
|
2 |
|
$rule = new EditableCustomRule(); |
|
196
|
2 |
|
$rule->ParentID = $field->ID; |
|
|
|
|
|
|
197
|
|
|
$rule->ConditionFieldID = $conditionField ? $conditionField->ID : 0; |
|
|
|
|
|
|
198
|
|
|
$rule->Display = $display; |
|
199
|
2 |
|
$rule->ConditionOption = $conditionOption; |
|
200
|
|
|
$rule->FieldValue = $value; |
|
201
|
2 |
|
$rule->write(); |
|
202
|
2 |
|
return $rule; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function log($message) |
|
206
|
|
|
{ |
|
207
|
|
|
if ($this->getQuiet()) { |
|
208
|
|
|
return; |
|
209
|
|
|
} |
|
210
|
|
|
if (Director::is_cli()) { |
|
211
|
|
|
echo "{$message}\n"; |
|
212
|
|
|
} else { |
|
213
|
|
|
echo "{$message}<br />"; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
2 |
|
/** |
|
218
|
|
|
* Set if this service should be quiet |
|
219
|
2 |
|
* |
|
220
|
2 |
|
* @param bool $quiet |
|
221
|
|
|
* @return $ths |
|
|
|
|
|
|
222
|
|
|
*/ |
|
223
|
2 |
|
public function setQuiet($quiet) |
|
224
|
|
|
{ |
|
225
|
2 |
|
$this->quiet = $quiet; |
|
226
|
|
|
return $this; |
|
227
|
|
|
} |
|
228
|
1 |
|
|
|
229
|
|
|
public function getQuiet() |
|
230
|
|
|
{ |
|
231
|
|
|
return $this->quiet; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Migrate Placeholder data from field specific table to the EditableFormField table |
|
237
|
|
|
* |
|
238
|
|
|
* @param EditableFormField $field |
|
239
|
|
|
* @param string $tableName |
|
240
|
|
|
*/ |
|
241
|
|
|
private function migratePlaceholder($field, $tableName) |
|
242
|
|
|
{ |
|
243
|
|
|
// Migrate Placeholder setting from $tableName table to EditableFormField table |
|
244
|
|
|
if ($field->Placeholder) { |
|
|
|
|
|
|
245
|
|
|
return; |
|
246
|
|
|
} |
|
247
|
|
|
// Check if draft table exists |
|
248
|
|
|
if (!DB::get_schema()->hasTable($tableName)) { |
|
249
|
|
|
// Check if _obsolete_ draft table exists |
|
250
|
|
|
$tableName = '_obsolete_' . $tableName; |
|
251
|
|
|
if (!DB::get_schema()->hasTable($tableName)) { |
|
252
|
|
|
return; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
// Check if old Placeholder column exists |
|
256
|
|
|
if (!DB::get_schema()->hasField($tableName, 'Placeholder')) { |
|
257
|
|
|
return; |
|
258
|
|
|
} |
|
259
|
|
|
// Fetch existing draft Placeholder value |
|
260
|
|
|
$query = "SELECT \"Placeholder\" FROM \"$tableName\" WHERE \"ID\" = '$field->ID'"; |
|
261
|
|
|
$draftPlaceholder = DB::query($query)->value(); |
|
262
|
|
|
|
|
263
|
|
|
if (!$draftPlaceholder) { |
|
264
|
|
|
return; |
|
265
|
|
|
} |
|
266
|
|
|
// Update draft Placeholder value |
|
267
|
|
|
DB::prepared_query( |
|
268
|
|
|
"UPDATE \"EditableFormField\" SET \"Placeholder\" = ? WHERE \"ID\" = ?", |
|
269
|
|
|
array($draftPlaceholder, $field->ID) |
|
270
|
|
|
); |
|
271
|
|
|
|
|
272
|
|
|
$livePlaceholder = $draftPlaceholder; |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
// Check if live table exists |
|
275
|
|
|
$tableName = $tableName . '_Live'; |
|
276
|
|
|
if (DB::get_schema()->hasTable($tableName)) { |
|
277
|
|
|
// Fetch existing live Placeholder value |
|
278
|
|
|
$query = "SELECT \"Placeholder\" FROM \"$tableName\" WHERE \"ID\" = '" . $field->ID . "'"; |
|
279
|
|
|
$livePlaceholder = DB::query($query)->value(); |
|
280
|
|
|
if (!$livePlaceholder) { |
|
281
|
|
|
$livePlaceholder = $draftPlaceholder; |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
// Update live Placeholder value |
|
286
|
|
|
DB::prepared_query( |
|
287
|
|
|
"UPDATE \"EditableFormField_Live\" SET \"Placeholder\" = ? WHERE \"ID\" = ?", |
|
288
|
|
|
array($draftPlaceholder, $field->ID) |
|
289
|
|
|
); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.