2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2007-07-19 12:40:28 +02:00
|
|
|
* EditableDropdown
|
|
|
|
* Represents a set of selectable radio buttons
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fieldeditor
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
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'] );
|
|
|
|
|
|
|
|
|
|
|
|
// store default, etc
|
|
|
|
foreach( $fieldSet as $option ) {
|
|
|
|
if( $deletedOptions && array_search( $option->ID, $deletedOptions ) !== false ) {
|
|
|
|
$option->delete();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( $data[$option->ID] )
|
|
|
|
$option->populateFromPostData( $data[$option->ID] );
|
|
|
|
|
|
|
|
unset( $data[$option->ID] );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach( $data as $tempID => $optionData ) {
|
|
|
|
|
|
|
|
if( !$tempID || !is_array( $optionData ) || empty( $optionData ) || !preg_match('/^_?\d+$/', $tempID ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// what will we name the new option?
|
|
|
|
$newOption = new EditableCheckboxOption();
|
|
|
|
$newOption->Name = 'option' . (string)$optionNumber++;
|
|
|
|
$newOption->ParentID = $this->ID;
|
|
|
|
$newOption->populateFromPostData( $optionData );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
function createField( $asFilter = false ) {
|
|
|
|
$optionSet = $this->Options();
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
if( $asFilter )
|
|
|
|
$options['-1'] = '(Any)';
|
|
|
|
|
|
|
|
$defaultOption = '-1';
|
|
|
|
|
|
|
|
/*foreach( $optionSet as $option ) {
|
|
|
|
$options[$option->Title] = $option->Title;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
// return radiofields
|
|
|
|
$checkboxSet = new CheckboxSetField( $this->Name, $this->Title, $optionSet, $optionSet );
|
|
|
|
|
|
|
|
return $checkboxSet;
|
|
|
|
}
|
|
|
|
|
2008-06-25 07:35:17 +02:00
|
|
|
function getValueFromData($data) {
|
|
|
|
if(empty($data[$this->Name])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$entries = $data[$this->Name];
|
|
|
|
|
2008-06-25 07:35:17 +02:00
|
|
|
if(!is_array($data[$this->Name])) {
|
|
|
|
$entries = array($data[$this->Name]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-06-25 07:35:17 +02:00
|
|
|
$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;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-06-25 07:35:17 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2008-06-25 07:35:17 +02:00
|
|
|
function TemplateOption() {
|
|
|
|
$option = new EditableCheckboxOption();
|
|
|
|
return $option->EditSegment();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-06-25 07:35:17 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
?>
|