2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* Read-only complement of {@link DropdownField}.
|
|
|
|
* Shows the "human value" of the dropdown field for the currently selected value.
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class LookupField extends DropdownField {
|
2007-09-15 23:39:23 +02:00
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
protected $readonly = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns a readonly span containing the correct value.
|
|
|
|
*/
|
2012-04-11 07:33:36 +02:00
|
|
|
function Field($properties = array()) {
|
2011-09-14 10:58:17 +02:00
|
|
|
$source = $this->getSource();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
// Normalize value to array to simplify further processing
|
2011-10-29 06:08:47 +02:00
|
|
|
$values = (is_array($this->value) || is_object($this->value)) ? $this->value : array(trim($this->value));
|
2007-09-15 23:39:23 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
$mapped = array();
|
|
|
|
if($source instanceof SQLMap) {
|
|
|
|
foreach($values as $value) $mapped[] = $source->getItem($value);
|
2011-10-29 06:08:47 +02:00
|
|
|
} else if($source instanceof ArrayAccess || is_array($source)) {
|
|
|
|
foreach($values as $value) {
|
|
|
|
if(isset($source[$value])) $mapped[] = $source[$value];
|
|
|
|
}
|
2007-09-15 23:39:23 +02:00
|
|
|
} else {
|
2011-09-14 10:58:17 +02:00
|
|
|
$mapped = array();
|
2007-09-15 23:39:23 +02:00
|
|
|
}
|
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
// Don't check if string arguments are matching against the source,
|
|
|
|
// as they might be generated HTML diff views instead of the actual values
|
|
|
|
if($this->value && !$mapped) {
|
|
|
|
$mapped = array(trim($this->value));
|
|
|
|
$values = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($mapped) {
|
|
|
|
$attrValue = implode(', ', array_values($mapped));
|
|
|
|
if(!$this->dontEscape) $attrValue = Convert::raw2xml($attrValue);
|
|
|
|
$inputValue = implode(', ', array_values($values));
|
|
|
|
} else {
|
|
|
|
$attrValue = "<i>(none)</i>";
|
|
|
|
$inputValue = '';
|
|
|
|
}
|
2007-09-15 23:39:23 +02:00
|
|
|
|
|
|
|
return "<span class=\"readonly\" id=\"" . $this->id() .
|
2011-09-14 10:58:17 +02:00
|
|
|
"\">$attrValue</span><input type=\"hidden\" name=\"" . $this->name .
|
|
|
|
"\" value=\"" . $inputValue . "\" />";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-12 04:58:48 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-12 04:58:48 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function Type() {
|
|
|
|
return "lookup readonly";
|
|
|
|
}
|
2008-10-15 14:39:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Override parent behaviour by not merging arrays.
|
|
|
|
*/
|
|
|
|
function getSource() {
|
|
|
|
return $this->source;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|