2009-04-17 04:26:40 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\Forms\FieldList;
|
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\GridFieldDeleteAction;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\Tab;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\ORM\HasManyList;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\ORM\Map;
|
2021-02-26 04:13:23 +01:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2009-04-17 04:26:40 +02:00
|
|
|
/**
|
2015-09-11 00:20:06 +02:00
|
|
|
* Base class for multiple option fields such as {@link EditableDropdownField}
|
|
|
|
* and radio sets.
|
|
|
|
*
|
|
|
|
* Implemented as a class but should be viewed as abstract, you should
|
2012-04-14 08:36:50 +02:00
|
|
|
* instantiate a subclass such as {@link EditableDropdownField}
|
|
|
|
*
|
|
|
|
* @see EditableCheckboxGroupField
|
|
|
|
* @see EditableDropdownField
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* @package userforms
|
2023-12-15 05:04:35 +01:00
|
|
|
* @method HasManyList<EditableOption> Options()
|
2009-04-17 04:26:40 +02:00
|
|
|
*/
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableMultipleOptionField extends EditableFormField
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Define this field as abstract (not inherited)
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $abstract = true;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_many = [
|
2017-08-15 05:30:59 +02:00
|
|
|
'Options' => EditableOption::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $owns = [
|
|
|
|
'Options',
|
|
|
|
];
|
|
|
|
|
|
|
|
private static $cascade_deletes = [
|
|
|
|
'Options',
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'EditableMultipleOptionField';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
/**
|
2021-02-26 04:13:23 +01:00
|
|
|
* @return FieldList
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-08-11 02:37:03 +02:00
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
2016-11-28 04:26:27 +01:00
|
|
|
$editableColumns = new GridFieldEditableColumns();
|
2017-08-11 01:33:06 +02:00
|
|
|
$editableColumns->setDisplayFields([
|
|
|
|
'Title' => [
|
|
|
|
'title' => _t(__CLASS__.'.TITLE', 'Title'),
|
2016-11-28 04:26:27 +01:00
|
|
|
'callback' => function ($record, $column, $grid) {
|
|
|
|
return TextField::create($column);
|
|
|
|
}
|
2017-08-11 01:33:06 +02:00
|
|
|
],
|
|
|
|
'Value' => [
|
|
|
|
'title' => _t(__CLASS__.'.VALUE', 'Value'),
|
2016-11-28 04:26:27 +01:00
|
|
|
'callback' => function ($record, $column, $grid) {
|
|
|
|
return TextField::create($column);
|
|
|
|
}
|
2017-08-11 01:33:06 +02:00
|
|
|
],
|
|
|
|
'Default' => [
|
|
|
|
'title' => _t(__CLASS__.'.DEFAULT', 'Selected by default?'),
|
2016-11-28 04:26:27 +01:00
|
|
|
'callback' => function ($record, $column, $grid) {
|
|
|
|
return CheckboxField::create($column);
|
|
|
|
}
|
2017-08-11 01:33:06 +02:00
|
|
|
]
|
|
|
|
]);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2016-11-28 04:26:27 +01:00
|
|
|
$optionsConfig = GridFieldConfig::create()
|
|
|
|
->addComponents(
|
|
|
|
new GridFieldToolbarHeader(),
|
|
|
|
new GridFieldTitleHeader(),
|
|
|
|
new GridFieldOrderableRows('Sort'),
|
|
|
|
$editableColumns,
|
|
|
|
new GridFieldButtonRow(),
|
|
|
|
new GridFieldAddNewInlineButton(),
|
|
|
|
new GridFieldDeleteAction()
|
|
|
|
);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2016-11-28 04:26:27 +01:00
|
|
|
$optionsGrid = GridField::create(
|
|
|
|
'Options',
|
2017-08-11 01:39:58 +02:00
|
|
|
_t('SilverStripe\\UserForms\\Model\\EditableFormField.CUSTOMOPTIONS', 'Options'),
|
2016-11-28 04:26:27 +01:00
|
|
|
$this->Options(),
|
|
|
|
$optionsConfig
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
|
|
|
|
2023-01-19 09:16:49 +01:00
|
|
|
$fields->insertAfter('Main', Tab::create('Options', _t(__CLASS__.'.OPTIONSTAB', 'Options')));
|
2016-11-28 04:26:27 +01:00
|
|
|
$fields->addFieldToTab('Root.Options', $optionsGrid);
|
|
|
|
});
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2016-11-28 04:26:27 +01:00
|
|
|
$fields = parent::getCMSFields();
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Duplicate a pages content. We need to make sure all the fields attached
|
|
|
|
* to that page go with it
|
2017-08-15 05:30:59 +02:00
|
|
|
* {@inheritDoc}
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
2023-01-25 01:55:24 +01:00
|
|
|
public function duplicate(bool $doWrite = true, array|null $relations = null): static
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
2022-11-28 21:36:08 +01:00
|
|
|
$clonedNode = parent::duplicate(true);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
foreach ($this->Options() as $field) {
|
|
|
|
$newField = $field->duplicate(false);
|
|
|
|
$newField->ParentID = $clonedNode->ID;
|
|
|
|
$newField->Version = 0;
|
|
|
|
$newField->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $clonedNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether or not this field has addable options such as a
|
2017-09-04 23:45:16 +02:00
|
|
|
* {@link EditableDropdown} or {@link EditableRadioField}
|
2016-07-21 07:53:59 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getHasAddableOptions()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets map of field options suitable for use in a form
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getOptionsMap()
|
|
|
|
{
|
|
|
|
$optionSet = $this->Options();
|
|
|
|
$optionMap = $optionSet->map('Value', 'Title');
|
2017-08-09 01:55:09 +02:00
|
|
|
if ($optionMap instanceof Map) {
|
2016-07-21 07:53:59 +02:00
|
|
|
return $optionMap->toArray();
|
|
|
|
}
|
|
|
|
return $optionMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all default options
|
|
|
|
*
|
2021-02-26 04:13:23 +01:00
|
|
|
* @return SS_List
|
2016-07-21 07:53:59 +02:00
|
|
|
*/
|
|
|
|
protected function getDefaultOptions()
|
|
|
|
{
|
|
|
|
return $this->Options()->filter('Default', 1);
|
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|