2015-07-24 04:37:48 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Extension;
|
|
|
|
|
2022-03-24 08:29:29 +01:00
|
|
|
use SilverStripe\Admin\AdminRootController;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2019-07-08 14:26:58 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\Tab;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
2024-08-20 06:25:59 +02:00
|
|
|
use SilverStripe\Core\Extension;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\ORM\DataList;
|
2024-01-18 22:29:08 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2024-02-02 02:08:57 +01:00
|
|
|
use SilverStripe\ORM\HasManyList;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Form\GridFieldAddClassesButton;
|
2017-08-15 05:30:59 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroup;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFieldGroupEnd;
|
2020-06-10 06:59:01 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFormStep;
|
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
/**
|
2024-02-02 02:08:57 +01:00
|
|
|
* @method HasManyList<EditableFormField> Fields()
|
2015-07-24 04:37:48 +02:00
|
|
|
*/
|
2024-08-20 06:25:59 +02:00
|
|
|
class UserFormFieldEditorExtension extends Extension
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $has_many = array(
|
2017-08-09 01:55:09 +02:00
|
|
|
'Fields' => EditableFormField::class
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
|
|
|
|
2017-08-15 05:30:59 +02:00
|
|
|
private static $owns = [
|
|
|
|
'Fields'
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $cascade_deletes = [
|
|
|
|
'Fields'
|
|
|
|
];
|
|
|
|
|
2024-08-15 04:01:27 +02:00
|
|
|
private static array $scaffold_cms_fields_settings = [
|
|
|
|
'ignoreRelations' => [
|
|
|
|
'Fields',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Adds the field editor to the page.
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
2024-05-20 07:06:29 +02:00
|
|
|
protected function updateCMSFields(FieldList $fields)
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
$fieldEditor = $this->getFieldEditorGrid();
|
|
|
|
|
2022-11-28 21:36:08 +01:00
|
|
|
$fields->insertAfter('Main', new Tab('FormFields', _t(__CLASS__.'.FORMFIELDS', 'Form Fields')));
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields->addFieldToTab('Root.FormFields', $fieldEditor);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the field editor, for adding and removing EditableFormFields.
|
|
|
|
*
|
|
|
|
* @return GridField
|
|
|
|
*/
|
|
|
|
public function getFieldEditorGrid()
|
|
|
|
{
|
2020-06-10 06:59:01 +02:00
|
|
|
Requirements::javascript('silverstripe/admin:client/dist/js/vendor.js');
|
|
|
|
Requirements::javascript('silverstripe/admin:client/dist/js/bundle.js');
|
2017-10-18 11:02:49 +02:00
|
|
|
Requirements::javascript('silverstripe/userforms:client/dist/js/userforms-cms.js');
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$fields = $this->owner->Fields();
|
|
|
|
|
|
|
|
$this->createInitialFormStep(true);
|
|
|
|
|
|
|
|
$editableColumns = new GridFieldEditableColumns();
|
2017-08-09 01:55:09 +02:00
|
|
|
$fieldClasses = singleton(EditableFormField::class)->getEditableFieldClasses();
|
2017-08-13 23:26:53 +02:00
|
|
|
$editableColumns->setDisplayFields([
|
2017-08-24 05:15:36 +02:00
|
|
|
'ClassName' => function ($record, $column, $grid) use ($fieldClasses) {
|
|
|
|
if ($record instanceof EditableFormField) {
|
2020-06-10 06:59:01 +02:00
|
|
|
$field = $record->getInlineClassnameField($column, $fieldClasses);
|
|
|
|
if ($record instanceof EditableFileField) {
|
|
|
|
$field->setAttribute('data-folderconfirmed', $record->FolderConfirmed ? 1 : 0);
|
|
|
|
}
|
|
|
|
return $field;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2017-08-24 05:15:36 +02:00
|
|
|
},
|
|
|
|
'Title' => function ($record, $column, $grid) {
|
|
|
|
if ($record instanceof EditableFormField) {
|
|
|
|
return $record->getInlineTitleField($column);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2017-08-24 05:15:36 +02:00
|
|
|
}
|
2017-08-13 23:26:53 +02:00
|
|
|
]);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponents(
|
|
|
|
$editableColumns,
|
|
|
|
new GridFieldButtonRow(),
|
2017-08-13 23:26:53 +02:00
|
|
|
(new GridFieldAddClassesButton(EditableTextField::class))
|
2017-08-11 01:33:06 +02:00
|
|
|
->setButtonName(_t(__CLASS__.'.ADD_FIELD', 'Add Field'))
|
2017-09-05 03:41:43 +02:00
|
|
|
->setButtonClass('btn-primary'),
|
2017-08-13 23:26:53 +02:00
|
|
|
(new GridFieldAddClassesButton(EditableFormStep::class))
|
2017-09-05 03:41:43 +02:00
|
|
|
->setButtonName(_t(__CLASS__.'.ADD_PAGE_BREAK', 'Add Page Break'))
|
|
|
|
->setButtonClass('btn-secondary'),
|
2017-08-13 23:26:53 +02:00
|
|
|
(new GridFieldAddClassesButton([EditableFieldGroup::class, EditableFieldGroupEnd::class]))
|
2017-09-05 03:41:43 +02:00
|
|
|
->setButtonName(_t(__CLASS__.'.ADD_FIELD_GROUP', 'Add Field Group'))
|
|
|
|
->setButtonClass('btn-secondary'),
|
|
|
|
$editButton = new GridFieldEditButton(),
|
2016-07-21 07:53:59 +02:00
|
|
|
new GridFieldDeleteAction(),
|
|
|
|
new GridFieldToolbarHeader(),
|
|
|
|
new GridFieldOrderableRows('Sort'),
|
2019-07-08 14:26:58 +02:00
|
|
|
new GridFieldDetailForm(),
|
2019-07-12 10:48:59 +02:00
|
|
|
// Betterbuttons prev and next is enabled by adding a GridFieldPaginator component
|
2019-07-08 14:26:58 +02:00
|
|
|
new GridFieldPaginator(999)
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
|
|
|
|
2017-09-05 03:41:43 +02:00
|
|
|
$editButton->removeExtraClass('grid-field__icon-action--hidden-on-hover');
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$fieldEditor = GridField::create(
|
|
|
|
'Fields',
|
2017-09-05 03:41:43 +02:00
|
|
|
'',
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields,
|
|
|
|
$config
|
2022-03-24 08:29:29 +01:00
|
|
|
)
|
2023-06-07 05:27:20 +02:00
|
|
|
->addExtraClass('uf-field-editor');
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
return $fieldEditor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A UserForm must have at least one step.
|
|
|
|
* If no steps exist, create an initial step, and put all fields inside it.
|
|
|
|
*
|
|
|
|
* @param bool $force
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function createInitialFormStep($force = false)
|
|
|
|
{
|
|
|
|
// Only invoke once saved
|
|
|
|
if (!$this->owner->exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if first field is a step
|
|
|
|
$fields = $this->owner->Fields();
|
|
|
|
$firstField = $fields->first();
|
|
|
|
if ($firstField instanceof EditableFormStep) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't create steps on write if there are no formfields, as this
|
|
|
|
// can create duplicate first steps during publish of new records
|
|
|
|
if (!$force && !$firstField) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-apply sort to each field starting at 2
|
|
|
|
$next = 2;
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
$field->Sort = $next++;
|
|
|
|
$field->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add step
|
|
|
|
$step = EditableFormStep::create();
|
2017-08-11 01:33:06 +02:00
|
|
|
$step->Title = _t('SilverStripe\\UserForms\\Model\\EditableFormField\\EditableFormStep.TITLE_FIRST', 'First Page');
|
2016-07-21 07:53:59 +02:00
|
|
|
$step->Sort = 1;
|
|
|
|
$step->write();
|
|
|
|
$fields->add($step);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that at least one page exists at the start
|
|
|
|
*/
|
2024-05-20 07:06:29 +02:00
|
|
|
protected function onAfterWrite()
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
$this->createInitialFormStep();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-15 05:30:59 +02:00
|
|
|
* Remove any orphaned child records on publish
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
2024-05-20 07:06:29 +02:00
|
|
|
protected function onAfterPublish()
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2017-01-24 21:35:50 +01:00
|
|
|
// store IDs of fields we've published
|
2017-08-15 05:30:59 +02:00
|
|
|
$seenIDs = [];
|
2016-07-21 07:53:59 +02:00
|
|
|
foreach ($this->owner->Fields() as $field) {
|
2017-01-24 21:35:50 +01:00
|
|
|
// store any IDs of fields we publish so we don't unpublish them
|
|
|
|
$seenIDs[] = $field->ID;
|
2022-11-28 21:36:08 +01:00
|
|
|
$field->publishRecursive();
|
2017-01-24 21:35:50 +01:00
|
|
|
$field->destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch any orphaned live records
|
2017-08-15 05:30:59 +02:00
|
|
|
$live = Versioned::get_by_stage(EditableFormField::class, Versioned::LIVE)
|
2017-08-13 23:26:53 +02:00
|
|
|
->filter([
|
2017-08-15 05:30:59 +02:00
|
|
|
'ParentID' => $this->owner->ID,
|
2021-01-17 03:29:33 +01:00
|
|
|
'ParentClass' => get_class($this->owner),
|
2017-08-13 23:26:53 +02:00
|
|
|
]);
|
2017-01-24 21:35:50 +01:00
|
|
|
|
|
|
|
if (!empty($seenIDs)) {
|
2017-08-13 23:26:53 +02:00
|
|
|
$live = $live->exclude([
|
2017-01-24 21:35:50 +01:00
|
|
|
'ID' => $seenIDs,
|
2017-08-13 23:26:53 +02:00
|
|
|
]);
|
2017-01-24 21:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete orphaned records
|
|
|
|
foreach ($live as $field) {
|
2017-08-15 05:30:59 +02:00
|
|
|
$field->deleteFromStage(Versioned::LIVE);
|
2017-01-24 21:35:50 +01:00
|
|
|
$field->destroy();
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-15 05:30:59 +02:00
|
|
|
* Remove all fields from the live stage when unpublishing the page
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
2024-05-20 07:06:29 +02:00
|
|
|
protected function onAfterUnpublish()
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2017-08-15 05:30:59 +02:00
|
|
|
foreach ($this->owner->Fields() as $field) {
|
|
|
|
$field->deleteFromStage(Versioned::LIVE);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-15 05:30:59 +02:00
|
|
|
* When duplicating a UserDefinedForm, duplicate all of its fields and display rules
|
2016-07-21 07:53:59 +02:00
|
|
|
*
|
2024-01-18 22:29:08 +01:00
|
|
|
* @see DataObject::duplicate
|
|
|
|
* @param DataObject $oldPage
|
2017-08-15 05:30:59 +02:00
|
|
|
* @param bool $doWrite
|
|
|
|
* @param string $manyMany
|
2024-01-18 22:29:08 +01:00
|
|
|
* @return DataObject
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
2024-05-20 07:06:29 +02:00
|
|
|
protected function onAfterDuplicate($oldPage, $doWrite, $manyMany)
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2017-08-15 05:30:59 +02:00
|
|
|
// List of EditableFieldGroups, where the key of the array is the ID of the old end group
|
|
|
|
$fieldGroups = [];
|
2017-08-16 02:14:46 +02:00
|
|
|
foreach ($oldPage->Fields() as $field) {
|
2018-03-22 23:53:43 +01:00
|
|
|
/** @var EditableFormField $newField */
|
2016-07-21 07:53:59 +02:00
|
|
|
$newField = $field->duplicate(false);
|
2017-08-16 02:14:46 +02:00
|
|
|
$newField->ParentID = $this->owner->ID;
|
|
|
|
$newField->ParentClass = $this->owner->ClassName;
|
2016-07-21 07:53:59 +02:00
|
|
|
$newField->Version = 0;
|
|
|
|
$newField->write();
|
|
|
|
|
|
|
|
// If we encounter a group start, record it for later use
|
|
|
|
if ($field instanceof EditableFieldGroup) {
|
|
|
|
$fieldGroups[$field->EndID] = $newField;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we encounter an end group, link it back to the group start
|
|
|
|
if ($field instanceof EditableFieldGroupEnd && isset($fieldGroups[$field->ID])) {
|
|
|
|
$groupStart = $fieldGroups[$field->ID];
|
|
|
|
$groupStart->EndID = $newField->ID;
|
|
|
|
$groupStart->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($field->DisplayRules() as $customRule) {
|
|
|
|
$newRule = $customRule->duplicate(false);
|
|
|
|
$newRule->ParentID = $newField->ID;
|
|
|
|
$newRule->Version = 0;
|
|
|
|
$newRule->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-15 05:30:59 +02:00
|
|
|
* Checks child fields to see if any are modified in draft as well. The owner of this extension will still
|
|
|
|
* use the Versioned method to determine its own status.
|
|
|
|
*
|
|
|
|
* @see Versioned::isModifiedOnDraft
|
2016-07-21 07:53:59 +02:00
|
|
|
*
|
2017-08-15 05:30:59 +02:00
|
|
|
* @return boolean|null
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
2017-08-15 05:30:59 +02:00
|
|
|
public function isModifiedOnDraft()
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2017-08-15 05:30:59 +02:00
|
|
|
foreach ($this->owner->Fields() as $field) {
|
|
|
|
if ($field->isModifiedOnDraft()) {
|
|
|
|
return true;
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-15 05:30:59 +02:00
|
|
|
* @see Versioned::doRevertToLive
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
2024-05-20 07:06:29 +02:00
|
|
|
protected function onAfterRevertToLive()
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2017-08-15 05:30:59 +02:00
|
|
|
foreach ($this->owner->Fields() as $field) {
|
2022-11-28 21:36:08 +01:00
|
|
|
$field->copyVersionToStage(Versioned::LIVE, Versioned::DRAFT);
|
2016-07-21 07:53:59 +02:00
|
|
|
$field->writeWithoutVersion();
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
}
|