FIX: Add support for multi dimensional source arrays in LookupField (open/6132)

This commit is contained in:
Will Rossiter 2013-05-11 00:01:39 +12:00
parent 718108969b
commit 42cf2a95bd
2 changed files with 74 additions and 9 deletions

View File

@ -1,30 +1,51 @@
<?php
/**
* Read-only complement of {@link DropdownField}.
* Shows the "human value" of the dropdown field for the currently selected value.
*
* Shows the "human value" of the dropdown field for the currently selected
* value.
*
* @package forms
* @subpackage fields-basic
*/
class LookupField extends DropdownField {
/**
* @var boolean $readonly
*/
protected $readonly = true;
/**
* Returns a readonly span containing the correct value.
*
* @param array $properties
*
* @return string
*/
public function Field($properties = array()) {
$source = $this->getSource();
// Normalize value to array to simplify further processing
$values = (is_array($this->value) || is_object($this->value)) ? $this->value : array(trim($this->value));
if(is_array($this->value) || is_object($this->value)) {
$values = $this->value;
} else {
$values = array(trim($this->value));
}
$mapped = array();
if($source instanceof SQLMap) {
foreach($values as $value) $mapped[] = $source->getItem($value);
} else if($source instanceof ArrayAccess || is_array($source)) {
foreach($values as $value) {
if(isset($source[$value])) $mapped[] = $source[$value];
$mapped[] = $source->getItem($value);
}
} else if($source instanceof ArrayAccess || is_array($source)) {
$source = ArrayLib::flatten($source);
foreach($values as $value) {
if(isset($source[$value])) {
$mapped[] = $source[$value];
}
}
} else {
$mapped = array();
@ -39,7 +60,11 @@ class LookupField extends DropdownField {
if($mapped) {
$attrValue = implode(', ', array_values($mapped));
if(!$this->dontEscape) $attrValue = Convert::raw2xml($attrValue);
if(!$this->dontEscape) {
$attrValue = Convert::raw2xml($attrValue);
}
$inputValue = implode(', ', array_values($values));
} else {
$attrValue = "<i>(none)</i>";
@ -51,17 +76,26 @@ class LookupField extends DropdownField {
"\" value=\"" . $inputValue . "\" />";
}
/**
* @return LookupField
*/
public function performReadonlyTransformation() {
$clone = clone $this;
return $clone;
}
/**
* @return string
*/
public function Type() {
return "lookup readonly";
}
/**
* Override parent behaviour by not merging arrays.
* Override parent behavior by not merging arrays.
*
* @return array
*/
public function getSource() {
return $this->source;

View File

@ -1,4 +1,5 @@
<?php
/**
* @package framework
* @subpackage tests
@ -79,5 +80,35 @@ class LookupFieldTest extends SapphireTest {
$f->Field()
);
}
public function testWithMultiDimensionalSource() {
$choices = array(
"Non-vegetarian" => array(
0 => 'Carnivore',
),
"Vegetarian" => array(
3 => 'Carrots',
),
"Other" => array(
9 => 'Vegan'
)
);
$f = new LookupField('test', 'test', $choices);
$f->setValue(3);
$this->assertEquals(
'<span class="readonly" id="test">Carrots</span><input type="hidden" name="test" value="3" />',
$f->Field()
);
$f->setValue(array(
3, 9
));
$this->assertEquals(
'<span class="readonly" id="test">Carrots, Vegan</span><input type="hidden" name="test" value="3, 9" />',
$f->Field()
);
}
}