2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2013-05-10 14:01:39 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\ORM\ArrayLib;
|
2016-07-12 08:51:08 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2016-06-15 06:03:16 +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
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class LookupField extends MultiSelectField
|
|
|
|
{
|
2017-07-03 02:21:27 +02:00
|
|
|
/** @skipUpgrade */
|
2016-10-05 03:30:19 +02:00
|
|
|
protected $schemaComponent = 'LookupField';
|
2007-09-15 23:39:23 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @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 = ArrayLib::flatten($this->getSource());
|
|
|
|
$values = $this->getValueArray();
|
|
|
|
|
|
|
|
// Get selected values
|
|
|
|
$mapped = array();
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (isset($source[$value])) {
|
|
|
|
$mapped[] = $source[$value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 && is_string($this->value) && empty($mapped)) {
|
|
|
|
$mapped = array(trim($this->value));
|
|
|
|
$values = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($mapped) {
|
|
|
|
$attrValue = implode(', ', array_values($mapped));
|
|
|
|
|
|
|
|
$attrValue = Convert::raw2xml($attrValue);
|
|
|
|
$inputValue = implode(', ', array_values($values));
|
|
|
|
} else {
|
2017-04-20 03:15:24 +02:00
|
|
|
$attrValue = '<i>('._t('SilverStripe\\Forms\\FormField.NONE', 'none').')</i>';
|
2016-11-29 00:31:16 +01:00
|
|
|
$inputValue = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'AttrValue' => DBField::create_field('HTMLFragment', $attrValue),
|
|
|
|
'InputValue' => $inputValue
|
|
|
|
));
|
|
|
|
|
|
|
|
return parent::Field($properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ignore validation as the field is readonly
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate($validator)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stubbed so invalid data doesn't save into the DB
|
|
|
|
*
|
|
|
|
* @param DataObjectInterface $record DataObject to save data into
|
|
|
|
*/
|
|
|
|
public function saveInto(DataObjectInterface $record)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return LookupField
|
|
|
|
*/
|
|
|
|
public function performReadonlyTransformation()
|
|
|
|
{
|
|
|
|
$clone = clone $this;
|
|
|
|
return $clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHasEmptyDefault()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Type()
|
|
|
|
{
|
|
|
|
return "lookup readonly";
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|