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
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
class LookupField extends MultiSelectField {
|
2016-10-05 03:30:19 +02:00
|
|
|
|
|
|
|
protected $schemaComponent = 'LookupField';
|
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()) {
|
2015-02-13 05:35:39 +01:00
|
|
|
$source = ArrayLib::flatten($this->getSource());
|
|
|
|
$values = $this->getValueArray();
|
2007-09-15 23:39:23 +02:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Get selected values
|
2011-09-14 10:58:17 +02:00
|
|
|
$mapped = array();
|
2015-02-13 05:35:39 +01:00
|
|
|
foreach($values as $value) {
|
|
|
|
if(isset($source[$value])) {
|
|
|
|
$mapped[] = $source[$value];
|
2011-10-29 06:08:47 +02:00
|
|
|
}
|
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
|
2015-02-13 05:35:39 +01:00
|
|
|
if($this->value && is_string($this->value) && empty($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
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
$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(
|
2016-06-03 10:51:02 +02:00
|
|
|
'AttrValue' => DBField::create_field('HTMLFragment', $attrValue),
|
2013-10-17 23:28:17 +02:00
|
|
|
'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;
|
|
|
|
return $clone;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-12 04:58:48 +02:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
public function getHasEmptyDefault() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|