2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-10 14:01:39 +02:00
|
|
|
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
* Read-only complement of {@link DropdownField}.
|
2013-05-10 14:01:39 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Shows the "human value" of the dropdown field for the currently selected
|
2013-05-10 14:01:39 +02:00
|
|
|
* value.
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @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
|
|
|
|
2013-05-10 14:01:39 +02:00
|
|
|
/**
|
|
|
|
* @var boolean $readonly
|
|
|
|
*/
|
2008-08-12 04:58:48 +02:00
|
|
|
protected $readonly = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns a readonly span containing the correct value.
|
2013-05-10 14:01:39 +02:00
|
|
|
*
|
|
|
|
* @param array $properties
|
|
|
|
*
|
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2011-09-14 10:58:17 +02:00
|
|
|
$source = $this->getSource();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
// Normalize value to array to simplify further processing
|
2013-05-10 14:01:39 +02:00
|
|
|
if(is_array($this->value) || is_object($this->value)) {
|
|
|
|
$values = $this->value;
|
|
|
|
} else {
|
|
|
|
$values = array(trim($this->value));
|
|
|
|
}
|
2007-09-15 23:39:23 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
$mapped = array();
|
2013-05-10 14:01:39 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
if($source instanceof SQLMap) {
|
2013-05-10 14:01:39 +02:00
|
|
|
foreach($values as $value) {
|
|
|
|
$mapped[] = $source->getItem($value);
|
|
|
|
}
|
2011-10-29 06:08:47 +02:00
|
|
|
} else if($source instanceof ArrayAccess || is_array($source)) {
|
2013-05-10 14:01:39 +02:00
|
|
|
$source = ArrayLib::flatten($source);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-10-29 06:08:47 +02:00
|
|
|
foreach($values as $value) {
|
2013-05-10 14:01:39 +02:00
|
|
|
if(isset($source[$value])) {
|
|
|
|
$mapped[] = $source[$value];
|
|
|
|
}
|
2011-10-29 06:08:47 +02:00
|
|
|
}
|
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
|
2012-12-13 13:51:28 +01:00
|
|
|
if($this->value && !is_array($this->value) && !$mapped) {
|
2011-09-14 10:58:17 +02:00
|
|
|
$mapped = array(trim($this->value));
|
|
|
|
$values = array();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-09-14 10:58:17 +02:00
|
|
|
if($mapped) {
|
|
|
|
$attrValue = implode(', ', array_values($mapped));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-10 14:01:39 +02:00
|
|
|
if(!$this->dontEscape) {
|
|
|
|
$attrValue = Convert::raw2xml($attrValue);
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
$inputValue = implode(', ', array_values($values));
|
2011-09-14 10:58:17 +02:00
|
|
|
} else {
|
2014-10-09 17:43:29 +02:00
|
|
|
$attrValue = '<i>('._t('FormField.NONE', 'none').')</i>';
|
2011-09-14 10:58:17 +02:00
|
|
|
$inputValue = '';
|
|
|
|
}
|
2007-09-15 23:39:23 +02:00
|
|
|
|
2013-10-17 23:28:17 +02:00
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'AttrValue' => $attrValue,
|
|
|
|
'InputValue' => $inputValue
|
|
|
|
));
|
|
|
|
|
|
|
|
return parent::Field($properties);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-12-15 08:00:57 +01:00
|
|
|
/**
|
2016-01-06 00:34:58 +01:00
|
|
|
* Ignore validation as the field is readonly
|
2015-12-15 08:00:57 +01:00
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate($validator) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-15 23:42:14 +01:00
|
|
|
/**
|
|
|
|
* Stubbed so invalid data doesn't save into the DB
|
|
|
|
*
|
|
|
|
* @param DataObjectInterface $record DataObject to save data into
|
|
|
|
*/
|
|
|
|
public function saveInto(DataObjectInterface $record) {
|
|
|
|
}
|
|
|
|
|
2013-05-10 14:01:39 +02:00
|
|
|
/**
|
|
|
|
* @return LookupField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
2013-05-10 14:01:39 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-12 04:58:48 +02:00
|
|
|
|
2013-05-10 14:01:39 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return "lookup readonly";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|