2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EditableDropdown
|
|
|
|
* Represents a set of selectable radio buttons
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fieldeditor
|
|
|
|
*/
|
|
|
|
class EditableCheckboxGroupField extends EditableFormField {
|
|
|
|
|
|
|
|
protected $readonly;
|
|
|
|
|
|
|
|
function ReadonlyOption() {
|
|
|
|
$this->readonly = true;
|
|
|
|
return $this->Option();
|
|
|
|
}
|
|
|
|
|
|
|
|
function isReadonly() {
|
|
|
|
return $this->readonly;
|
|
|
|
}
|
|
|
|
|
|
|
|
static $has_many = array(
|
|
|
|
"Options" => "EditableCheckboxOption"
|
|
|
|
);
|
|
|
|
|
|
|
|
static $singular_name = "Checkbox group";
|
|
|
|
static $plural_name = "Checkbox groups";
|
|
|
|
|
|
|
|
function duplicate() {
|
|
|
|
$clonedNode = parent::duplicate();
|
|
|
|
|
|
|
|
foreach( $this->Options() as $field ) {
|
|
|
|
$newField = $field->duplicate();
|
|
|
|
$newField->ParentID = $clonedNode->ID;
|
|
|
|
$newField->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $clonedNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
function delete() {
|
|
|
|
$options = $this->Options();
|
|
|
|
|
|
|
|
foreach( $options as $option )
|
|
|
|
$option->delete();
|
|
|
|
|
|
|
|
parent::delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
function EditSegment() {
|
|
|
|
return $this->renderWith( $this->class );
|
|
|
|
}
|
|
|
|
|
|
|
|
function populateFromPostData( $data ) {
|
|
|
|
parent::populateFromPostData( $data );
|
|
|
|
|
|
|
|
$fieldSet = $this->Options();
|
|
|
|
|
|
|
|
$deletedOptions = explode( ',', $data['Deleted'] );
|
2009-01-28 06:59:23 +01:00
|
|
|
$optionNumber = 0;
|
2008-09-29 05:18:23 +02:00
|
|
|
|
|
|
|
// store default, etc
|
2009-01-28 06:59:23 +01:00
|
|
|
foreach($fieldSet as $option) {
|
|
|
|
if($deletedOptions && array_search($option->ID, $deletedOptions) !== false) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$option->delete();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-28 06:59:23 +01:00
|
|
|
if($data[$option->ID]) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$option->populateFromPostData( $data[$option->ID] );
|
2009-01-28 06:59:23 +01:00
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
|
|
|
|
unset( $data[$option->ID] );
|
|
|
|
}
|
|
|
|
|
2009-01-28 06:59:23 +01:00
|
|
|
foreach($data as $tempID => $optionData) {
|
|
|
|
if(!$tempID || !is_array($optionData) || empty($optionData) || !preg_match('/^_?\d+$/', $tempID)) {
|
2008-09-29 05:18:23 +02:00
|
|
|
continue;
|
2009-01-28 06:59:23 +01:00
|
|
|
}
|
2008-09-29 05:18:23 +02:00
|
|
|
|
|
|
|
// what will we name the new option?
|
|
|
|
$newOption = new EditableCheckboxOption();
|
2009-01-28 06:59:23 +01:00
|
|
|
$newOption->Name = 'option' . (string)$optionNumber++;
|
2008-09-29 05:18:23 +02:00
|
|
|
$newOption->ParentID = $this->ID;
|
2009-01-28 06:59:23 +01:00
|
|
|
$newOption->populateFromPostData($optionData);
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function DefaultOption() {
|
|
|
|
$defaultOption = 0;
|
|
|
|
|
|
|
|
foreach( $this->Options() as $option ) {
|
|
|
|
if( $option->getField('Default') )
|
|
|
|
return $defaultOption;
|
|
|
|
else
|
|
|
|
$defaultOption++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFormField() {
|
|
|
|
return $this->createField();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilterField() {
|
|
|
|
return $this->createField( true );
|
|
|
|
}
|
|
|
|
|
2009-01-28 06:59:23 +01:00
|
|
|
function createField($asFilter = false) {
|
2008-09-29 05:18:23 +02:00
|
|
|
$optionSet = $this->Options();
|
|
|
|
$options = array();
|
|
|
|
|
2009-01-28 06:59:23 +01:00
|
|
|
$optionMap = ($optionSet) ? $optionSet->map('ID', 'Title') : array();
|
2008-09-29 05:18:23 +02:00
|
|
|
|
2009-01-28 06:59:23 +01:00
|
|
|
return new CheckboxSetField($this->Name, $this->Title, $optionMap);
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getValueFromData($data) {
|
|
|
|
if(empty($data[$this->Name])) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2009-01-28 06:59:23 +01:00
|
|
|
$result = '';
|
2008-09-29 05:18:23 +02:00
|
|
|
$entries = $data[$this->Name];
|
|
|
|
|
|
|
|
if(!is_array($data[$this->Name])) {
|
|
|
|
$entries = array($data[$this->Name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$selectedOptions = DataObject::get('EditableCheckboxOption', "ParentID={$this->ID} AND ID IN (" . implode(',', $entries) . ")");
|
|
|
|
foreach($selectedOptions as $selected) {
|
|
|
|
if(!$result) {
|
|
|
|
$result = $selected->Title;
|
|
|
|
} else {
|
|
|
|
$result .= "," . $selected->Title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function TemplateOption() {
|
|
|
|
$option = new EditableCheckboxOption();
|
|
|
|
return $option->EditSegment();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|