|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\Forms\Tab; |
|
8
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow; |
|
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
|
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton; |
|
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction; |
|
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
|
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
|
15
|
|
|
use SilverStripe\ORM\DataExtension; |
|
16
|
|
|
use SilverStripe\UserForms\Form\GridFieldAddClassesButton; |
|
17
|
|
|
use SilverStripe\UserForms\Model\EditableFormField; |
|
18
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup; |
|
19
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd; |
|
20
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep; |
|
21
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField; |
|
22
|
|
|
use SilverStripe\Versioned\Versioned; |
|
23
|
|
|
use SilverStripe\View\Requirements; |
|
24
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
|
25
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @package userforms |
|
29
|
|
|
*/ |
|
30
|
|
|
class UserFormFieldEditorExtension extends DataExtension |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
36
|
|
|
'Fields' => EditableFormField::class |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
|
|
private static $owns = [ |
|
|
|
|
|
|
40
|
|
|
'Fields' |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
private static $cascade_deletes = [ |
|
|
|
|
|
|
44
|
|
|
'Fields' |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Adds the field editor to the page. |
|
49
|
|
|
* |
|
50
|
|
|
* @return FieldList |
|
51
|
|
|
*/ |
|
52
|
|
|
public function updateCMSFields(FieldList $fields) |
|
53
|
|
|
{ |
|
54
|
|
|
$fieldEditor = $this->getFieldEditorGrid(); |
|
55
|
|
|
|
|
56
|
|
|
$fields->insertAfter(new Tab('FormFields', _t(__CLASS__.'.FORMFIELDS', 'Form Fields')), 'Main'); |
|
|
|
|
|
|
57
|
|
|
$fields->addFieldToTab('Root.FormFields', $fieldEditor); |
|
58
|
|
|
|
|
59
|
|
|
return $fields; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Gets the field editor, for adding and removing EditableFormFields. |
|
64
|
|
|
* |
|
65
|
|
|
* @return GridField |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getFieldEditorGrid() |
|
68
|
|
|
{ |
|
69
|
|
|
$module = ModuleLoader::getModule('silverstripe/userforms'); |
|
70
|
|
|
Requirements::javascript($module->getRelativeResourcePath('javascript/FieldEditor.js')); |
|
71
|
|
|
|
|
72
|
|
|
$fields = $this->owner->Fields(); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$this->createInitialFormStep(true); |
|
75
|
|
|
|
|
76
|
|
|
$editableColumns = new GridFieldEditableColumns(); |
|
77
|
|
|
$fieldClasses = singleton(EditableFormField::class)->getEditableFieldClasses(); |
|
78
|
|
|
$editableColumns->setDisplayFields([ |
|
79
|
|
|
'ClassName' => function ($record, $column, $grid) use ($fieldClasses) { |
|
|
|
|
|
|
80
|
|
|
if ($record instanceof EditableFormField) { |
|
81
|
|
|
return $record->getInlineClassnameField($column, $fieldClasses); |
|
82
|
|
|
} |
|
83
|
|
|
}, |
|
84
|
|
|
'Title' => function ($record, $column, $grid) { |
|
|
|
|
|
|
85
|
|
|
if ($record instanceof EditableFormField) { |
|
86
|
|
|
return $record->getInlineTitleField($column); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
]); |
|
90
|
|
|
|
|
91
|
|
|
$config = GridFieldConfig::create() |
|
92
|
|
|
->addComponents( |
|
93
|
|
|
$editableColumns, |
|
94
|
|
|
new GridFieldButtonRow(), |
|
95
|
|
|
(new GridFieldAddClassesButton(EditableTextField::class)) |
|
96
|
|
|
->setButtonName(_t(__CLASS__.'.ADD_FIELD', 'Add Field')) |
|
97
|
|
|
->setButtonClass('ss-ui-action-constructive'), |
|
98
|
|
|
(new GridFieldAddClassesButton(EditableFormStep::class)) |
|
99
|
|
|
->setButtonName(_t(__CLASS__.'.ADD_PAGE_BREAK', 'Add Page Break')), |
|
100
|
|
|
(new GridFieldAddClassesButton([EditableFieldGroup::class, EditableFieldGroupEnd::class])) |
|
101
|
|
|
->setButtonName(_t(__CLASS__.'.ADD_FIELD_GROUP', 'Add Field Group')), |
|
102
|
|
|
new GridFieldEditButton(), |
|
103
|
|
|
new GridFieldDeleteAction(), |
|
104
|
|
|
new GridFieldToolbarHeader(), |
|
105
|
|
|
new GridFieldOrderableRows('Sort'), |
|
106
|
|
|
new GridFieldDetailForm() |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$fieldEditor = GridField::create( |
|
110
|
|
|
'Fields', |
|
111
|
|
|
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.FIELDS', 'Fields'), |
|
112
|
|
|
$fields, |
|
113
|
|
|
$config |
|
114
|
|
|
)->addExtraClass('uf-field-editor'); |
|
115
|
|
|
|
|
116
|
|
|
return $fieldEditor; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* A UserForm must have at least one step. |
|
121
|
|
|
* If no steps exist, create an initial step, and put all fields inside it. |
|
122
|
|
|
* |
|
123
|
|
|
* @param bool $force |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
public function createInitialFormStep($force = false) |
|
127
|
|
|
{ |
|
128
|
|
|
// Only invoke once saved |
|
129
|
|
|
if (!$this->owner->exists()) { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Check if first field is a step |
|
134
|
|
|
$fields = $this->owner->Fields(); |
|
|
|
|
|
|
135
|
|
|
$firstField = $fields->first(); |
|
136
|
|
|
if ($firstField instanceof EditableFormStep) { |
|
137
|
|
|
return; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Don't create steps on write if there are no formfields, as this |
|
141
|
|
|
// can create duplicate first steps during publish of new records |
|
142
|
|
|
if (!$force && !$firstField) { |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// Re-apply sort to each field starting at 2 |
|
147
|
|
|
$next = 2; |
|
148
|
|
|
foreach ($fields as $field) { |
|
149
|
|
|
$field->Sort = $next++; |
|
150
|
|
|
$field->write(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Add step |
|
154
|
|
|
$step = EditableFormStep::create(); |
|
155
|
|
|
$step->Title = _t('SilverStripe\\UserForms\\Model\\EditableFormField\\EditableFormStep.TITLE_FIRST', 'First Page'); |
|
156
|
|
|
$step->Sort = 1; |
|
157
|
|
|
$step->write(); |
|
158
|
|
|
$fields->add($step); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Ensure that at least one page exists at the start |
|
163
|
|
|
*/ |
|
164
|
|
|
public function onAfterWrite() |
|
165
|
|
|
{ |
|
166
|
|
|
$this->createInitialFormStep(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Remove any orphaned child records on publish |
|
171
|
|
|
*/ |
|
172
|
|
|
public function onAfterPublish() |
|
173
|
|
|
{ |
|
174
|
|
|
// store IDs of fields we've published |
|
175
|
|
|
$seenIDs = []; |
|
176
|
|
|
foreach ($this->owner->Fields() as $field) { |
|
|
|
|
|
|
177
|
|
|
// store any IDs of fields we publish so we don't unpublish them |
|
178
|
|
|
$seenIDs[] = $field->ID; |
|
179
|
|
|
$field->doPublish(Versioned::DRAFT, Versioned::LIVE); |
|
180
|
|
|
$field->destroy(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// fetch any orphaned live records |
|
184
|
|
|
$live = Versioned::get_by_stage(EditableFormField::class, Versioned::LIVE) |
|
185
|
|
|
->filter([ |
|
186
|
|
|
'ParentID' => $this->owner->ID, |
|
187
|
|
|
]); |
|
188
|
|
|
|
|
189
|
|
|
if (!empty($seenIDs)) { |
|
190
|
|
|
$live = $live->exclude([ |
|
191
|
|
|
'ID' => $seenIDs, |
|
192
|
|
|
]); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// delete orphaned records |
|
196
|
|
|
foreach ($live as $field) { |
|
197
|
|
|
$field->deleteFromStage(Versioned::LIVE); |
|
198
|
|
|
$field->destroy(); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Remove all fields from the live stage when unpublishing the page |
|
204
|
|
|
*/ |
|
205
|
|
|
public function onAfterUnpublish() |
|
206
|
|
|
{ |
|
207
|
|
|
foreach ($this->owner->Fields() as $field) { |
|
|
|
|
|
|
208
|
|
|
$field->deleteFromStage(Versioned::LIVE); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* When duplicating a UserDefinedForm, duplicate all of its fields and display rules |
|
214
|
|
|
* |
|
215
|
|
|
* @see DataObject::duplicate |
|
216
|
|
|
* @param DataObject $oldPage |
|
217
|
|
|
* @param bool $doWrite |
|
218
|
|
|
* @param string $manyMany |
|
219
|
|
|
* @return DataObject |
|
220
|
|
|
*/ |
|
221
|
|
|
public function onAfterDuplicate($oldPage, $doWrite, $manyMany) |
|
|
|
|
|
|
222
|
|
|
{ |
|
223
|
|
|
// List of EditableFieldGroups, where the key of the array is the ID of the old end group |
|
224
|
|
|
$fieldGroups = []; |
|
225
|
|
|
foreach ($oldPage->Fields() as $field) { |
|
226
|
|
|
$newField = $field->duplicate(false); |
|
227
|
|
|
$newField->ParentID = $this->owner->ID; |
|
228
|
|
|
$newField->ParentClass = $this->owner->ClassName; |
|
229
|
|
|
$newField->Version = 0; |
|
230
|
|
|
$newField->write(); |
|
231
|
|
|
|
|
232
|
|
|
// If we encounter a group start, record it for later use |
|
233
|
|
|
if ($field instanceof EditableFieldGroup) { |
|
234
|
|
|
$fieldGroups[$field->EndID] = $newField; |
|
|
|
|
|
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
// If we encounter an end group, link it back to the group start |
|
238
|
|
|
if ($field instanceof EditableFieldGroupEnd && isset($fieldGroups[$field->ID])) { |
|
239
|
|
|
$groupStart = $fieldGroups[$field->ID]; |
|
240
|
|
|
$groupStart->EndID = $newField->ID; |
|
241
|
|
|
$groupStart->write(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
View Code Duplication |
foreach ($field->DisplayRules() as $customRule) { |
|
|
|
|
|
|
245
|
|
|
$newRule = $customRule->duplicate(false); |
|
246
|
|
|
$newRule->ParentID = $newField->ID; |
|
247
|
|
|
$newRule->Version = 0; |
|
248
|
|
|
$newRule->write(); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Checks child fields to see if any are modified in draft as well. The owner of this extension will still |
|
255
|
|
|
* use the Versioned method to determine its own status. |
|
256
|
|
|
* |
|
257
|
|
|
* @see Versioned::isModifiedOnDraft |
|
258
|
|
|
* |
|
259
|
|
|
* @return boolean|null |
|
260
|
|
|
*/ |
|
261
|
|
|
public function isModifiedOnDraft() |
|
262
|
|
|
{ |
|
263
|
|
|
foreach ($this->owner->Fields() as $field) { |
|
|
|
|
|
|
264
|
|
|
if ($field->isModifiedOnDraft()) { |
|
265
|
|
|
return true; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @see Versioned::doRevertToLive |
|
272
|
|
|
*/ |
|
273
|
|
|
public function onAfterRevertToLive() |
|
274
|
|
|
{ |
|
275
|
|
|
foreach ($this->owner->Fields() as $field) { |
|
|
|
|
|
|
276
|
|
|
$field->copyVersionToStage(Versioned::LIVE, Versioned::DRAFT, false); |
|
277
|
|
|
$field->writeWithoutVersion(); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.