ENHANCEMENT Added ListboxField->setDisabledItems() and setDefaultItems() (similar to CheckboxSetField API)

This commit is contained in:
Ingo Schommer 2012-03-05 12:27:55 +01:00
parent ea31e1798a
commit 5d565dcfa3
3 changed files with 75 additions and 19 deletions

View File

@ -40,6 +40,16 @@ class ListboxField extends DropdownField {
* @var boolean
*/
protected $multiple = false;
/**
* @var Array
*/
protected $disabledItems = array();
/**
* @var Array
*/
protected $defaultItems = array();
/**
* Creates a new dropdown field.
@ -61,43 +71,31 @@ class ListboxField extends DropdownField {
* Returns a <select> tag containing all the appropriate <option> tags
*/
function Field($properties = array()) {
if($this->multiple) {
$this->name .= '[]';
}
if($this->multiple) $this->name .= '[]';
$options = array();
// We have an array of values
if(is_array($this->value)){
// Loop through and figure out which values were selected.
foreach($this->getSource() as $value => $title) {
// Loop through the array of values to find out if this value is selected.
$selected = "";
foreach($this->value as $v){
if($value == $v) {
$selected = " selected=\"selected\"";
break;
}
}
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
'Selected' => $selected,
'Selected' => (in_array($value, $this->value) || in_array($value, $this->defaultItems)),
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
));
}
} else {
// Listbox was based a singlular value, so treat it like a dropdown.
foreach($this->getSource() as $value => $title) {
$selected = $value == $this->value ? " selected=\"selected\"" : "";
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
'Selected' => $selected,
'Selected' => ($value == $this->value || in_array($value, $this->defaultItems)),
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
));
}
}
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
return $this->customise($properties)->renderWith($this->getTemplate());
}
@ -214,7 +212,6 @@ class ListboxField extends DropdownField {
// Doesn't check against unknown values in order to allow for less rigid data handling.
// They're silently ignored and overwritten the next time the field is saved.
parent::setValue($parts);
} else {
if(!in_array($val, array_keys($this->source))) {
@ -232,5 +229,42 @@ class ListboxField extends DropdownField {
return $this;
}
/**
* Mark certain elements as disabled,
* regardless of the {@link setDisabled()} settings.
*
* @param array $items Collection of array keys, as defined in the $source array
*/
function setDisabledItems($items) {
$this->disabledItems = $items;
return $this;
}
/**
* @return Array
*/
function getDisabledItems() {
return $this->disabledItems;
}
/**
* Default selections, regardless of the {@link setValue()} settings.
* Note: Items marked as disabled through {@link setDisabledItems()} can still be
* selected by default through this method.
*
* @param Array $items Collection of array keys, as defined in the $source array
*/
function setDefaultItems($items) {
$this->defaultItems = $items;
return $this;
}
/**
* @return Array
*/
function getDefaultItems() {
return $this->defaultItems;
}
}

View File

@ -1,5 +1,5 @@
<select $AttributesHTML>
<% control Options %>
<option value="$Value"<% if Selected %> selected<% end_if %>>$Title</option>
<option value="$Value"<% if Selected %> selected="selected"<% end_if %><% if Disabled %> disabled="disabled"<% end_if %>>$Title</option>
<% end_control %>
</select>

View File

@ -27,6 +27,28 @@ class ListboxFieldTest extends SapphireTest {
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
$this->assertNull($tag3xml[0]['selected']);
}
function testFieldWithDisabledItems() {
$articleWithTags = $this->objFromFixture('ListboxFieldTest_Article', 'articlewithtags');
$tag1 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag1');
$tag2 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag2');
$tag3 = $this->objFromFixture('ListboxFieldTest_Tag', 'tag3');
$field = new ListboxField("Tags", "Test field", DataObject::get("ListboxFieldTest_Tag")->map()->toArray());
$field->setMultiple(true);
$field->setValue(null, $articleWithTags);
$field->setDisabledItems(array($tag1->ID, $tag3->ID));
$p = new CSSContentParser($field->Field());
$tag1xml = $p->getByXpath('//option[@value=' . $tag1->ID . ']');
$tag2xml = $p->getByXpath('//option[@value=' . $tag2->ID . ']');
$tag3xml = $p->getByXpath('//option[@value=' . $tag3->ID . ']');
$this->assertEquals('selected', (string)$tag1xml[0]['selected']);
$this->assertEquals('disabled', (string)$tag1xml[0]['disabled']);
$this->assertEquals('selected', (string)$tag2xml[0]['selected']);
$this->assertNull($tag2xml[0]['disabled']);
$this->assertNull($tag3xml[0]['selected']);
$this->assertEquals('disabled', (string)$tag3xml[0]['disabled']);
}
function testSaveIntoNullValueWithMultipleOff() {
$choices = array('a' => 'a value', 'b' => 'b value','c' => 'c value');