silverstripe-userforms/code/model/editableformfields/EditableMultipleOptionField.php

190 lines
4.3 KiB
PHP
Raw Normal View History

<?php
/**
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
*/
class EditableMultipleOptionField extends EditableFormField {
/**
* Define this field as abstract (not inherited)
*
* @config
* @var bool
*/
private static $abstract = true;
2015-09-11 00:20:06 +02:00
private static $has_many = array(
"Options" => "EditableOption"
);
2015-07-24 04:37:48 +02:00
/**
* @return FieldList
*/
public function getCMSFields() {
$fields = parent::getCMSFields();
$editableColumns = new GridFieldEditableColumns();
$editableColumns->setDisplayFields(array(
'Title' => array(
2015-11-17 17:35:35 +01:00
'title' => _t('EditableMultipleOptionField.TITLE', 'Title'),
2015-07-24 04:37:48 +02:00
'callback' => function($record, $column, $grid) {
return TextField::create($column);
}
),
'Default' => array(
'title' => _t('EditableMultipleOptionField.DEFAULT', 'Selected by default?'),
'callback' => function($record, $column, $grid) {
return CheckboxField::create($column);
}
)
));
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
$optionsConfig = GridFieldConfig::create()
->addComponents(
new GridFieldToolbarHeader(),
new GridFieldTitleHeader(),
new GridFieldOrderableRows('Sort'),
2015-07-24 04:37:48 +02:00
$editableColumns,
new GridFieldButtonRow(),
new GridFieldAddNewInlineButton(),
2015-08-14 04:51:42 +02:00
new GridFieldDeleteAction()
2015-07-24 04:37:48 +02:00
);
$optionsGrid = GridField::create(
'Options',
_t('EditableFormField.CUSTOMOPTIONS', 'Options'),
$this->Options(),
$optionsConfig
);
2015-11-17 17:35:35 +01:00
$fields->insertAfter(new Tab('Options', _t('EditableMultipleOptionField.OPTIONSTAB','Options')), 'Main');
2015-07-24 04:37:48 +02:00
$fields->addFieldToTab('Root.Options', $optionsGrid);
return $fields;
}
2015-09-11 00:20:06 +02:00
/**
* Publishing Versioning support.
*
* When publishing it needs to handle copying across / publishing
* each of the individual field options
2015-09-11 00:20:06 +02:00
*
* @return void
*/
public function doPublish($fromStage, $toStage, $createNewVersion = false) {
$live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
if($live) {
foreach($live as $option) {
$option->delete();
}
}
2015-09-11 00:20:06 +02:00
if($this->Options()) {
foreach($this->Options() as $option) {
$option->publish($fromStage, $toStage, $createNewVersion);
}
}
2015-09-11 00:20:06 +02:00
2015-09-18 01:07:58 +02:00
parent::doPublish($fromStage, $toStage, $createNewVersion);
}
2015-09-11 00:20:06 +02:00
/**
* Unpublishing Versioning support
2015-09-11 00:20:06 +02:00
*
* When unpublishing the field it has to remove all options attached
*
* @return void
*/
public function doDeleteFromStage($stage) {
2015-09-18 01:07:58 +02:00
// Remove options
$options = Versioned::get_by_stage('EditableOption', $stage)
->filter('ParentID', $this->ID);
foreach($options as $option) {
$option->deleteFromStage($stage);
}
2015-09-11 00:20:06 +02:00
2015-09-18 01:07:58 +02:00
parent::doDeleteFromStage($stage);
}
2015-09-11 00:20:06 +02:00
/**
2015-09-11 00:20:06 +02:00
* Deletes all the options attached to this field before deleting the
* field. Keeps stray options from floating around
*
* @return void
*/
public function delete() {
$options = $this->Options();
if($options) {
foreach($options as $option) {
$option->delete();
}
}
2015-09-11 00:20:06 +02:00
parent::delete();
}
2015-09-11 00:20:06 +02:00
/**
2015-09-11 00:20:06 +02:00
* Duplicate a pages content. We need to make sure all the fields attached
* to that page go with it
2015-09-11 00:20:06 +02:00
*
* @return DataObject
*/
public function duplicate($doWrite = true) {
$clonedNode = parent::duplicate();
2015-09-11 00:20:06 +02:00
2015-07-24 04:37:48 +02:00
foreach($this->Options() as $field) {
$newField = $field->duplicate(false);
$newField->ParentID = $clonedNode->ID;
$newField->Version = 0;
$newField->write();
}
2015-09-11 00:20:06 +02:00
return $clonedNode;
}
2015-09-11 00:20:06 +02:00
/**
2015-09-11 00:20:06 +02:00
* Return whether or not this field has addable options such as a
* {@link EditableDropdownField} or {@link EditableRadioField}
*
* @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('EscapedTitle', 'Title');
if($optionMap instanceof SS_Map) {
return $optionMap->toArray();
}
return $optionMap;
}
/**
* Returns all default options
*
* @return SS_List
*/
protected function getDefaultOptions() {
return $this->Options()->filter('Default', 1);
}
}