silverstripe-userforms/code/Model/EditableFormField/EditableMultipleOptionField.php

174 lines
5.1 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\Map;
use SilverStripe\ORM\SS_List;
use SilverStripe\UserForms\Model\EditableFormField;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
/**
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
* instantiate a subclass such as {@link EditableDropdownField}
*
* @see EditableCheckboxGroupField
* @see EditableDropdownField
*
* @package userforms
* @method HasManyList|EditableOption[] Options()
*/
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;
private static $has_many = [
'Options' => EditableOption::class,
];
private static $owns = [
'Options',
];
private static $cascade_deletes = [
'Options',
];
private static $table_name = 'EditableMultipleOptionField';
2016-07-21 07:53:59 +02: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) {
$editableColumns = new GridFieldEditableColumns();
$editableColumns->setDisplayFields([
'Title' => [
'title' => _t(__CLASS__.'.TITLE', 'Title'),
'callback' => function ($record, $column, $grid) {
return TextField::create($column);
}
],
'Value' => [
'title' => _t(__CLASS__.'.VALUE', 'Value'),
'callback' => function ($record, $column, $grid) {
return TextField::create($column);
}
],
'Default' => [
'title' => _t(__CLASS__.'.DEFAULT', 'Selected by default?'),
'callback' => function ($record, $column, $grid) {
return CheckboxField::create($column);
}
]
]);
2016-07-21 07:53:59 +02: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
$optionsGrid = GridField::create(
'Options',
_t('SilverStripe\\UserForms\\Model\\EditableFormField.CUSTOMOPTIONS', 'Options'),
$this->Options(),
$optionsConfig
2016-07-21 07:53:59 +02:00
);
$fields->insertAfter(Tab::create('Options', _t(__CLASS__.'.OPTIONSTAB', 'Options')), 'Main');
$fields->addFieldToTab('Root.Options', $optionsGrid);
});
2016-07-21 07:53:59 +02: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
*
2022-11-28 21:36:08 +01:00
* @param bool $doWrite @deprecated
* @param string $manyMany @deprecated
* {@inheritDoc}
2016-07-21 07:53:59 +02:00
*/
2022-12-13 05:42:43 +01:00
public function duplicate(bool $doWrite = true, ?array $manyMany = 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) {
/** @var EditableOption $newField */
2016-07-21 07:53:59 +02:00
$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
* {@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');
if ($optionMap instanceof Map) {
2016-07-21 07:53:59 +02:00
return $optionMap->toArray();
}
return $optionMap;
}
/**
* Returns all default options
*
* @return SS_List
2016-07-21 07:53:59 +02:00
*/
protected function getDefaultOptions()
{
return $this->Options()->filter('Default', 1);
}
}