2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-03-10 16:03:34 +01:00
|
|
|
* Read-only field to display a non-editable value with a label.
|
|
|
|
* Consider using an {@link LabelField} if you just need a label-less
|
|
|
|
* value display.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ReadonlyField extends FormField {
|
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
protected $readonly = true;
|
|
|
|
|
2013-01-07 06:44:01 +01:00
|
|
|
/**
|
|
|
|
* Include a hidden field in the HTML for the readonly field
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $includeHiddenField = false;
|
|
|
|
|
2016-04-21 04:38:02 +02:00
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
|
|
|
|
|
2013-01-07 06:44:01 +01:00
|
|
|
/**
|
|
|
|
* If true, a hidden field will be included in the HTML for the readonly field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-01-07 06:44:01 +01:00
|
|
|
* This can be useful if you need to pass the data through on the form submission, as
|
|
|
|
* long as it's okay than an attacker could change the data before it's submitted.
|
|
|
|
*
|
|
|
|
* This is disabled by default as it can introduce security holes if the data is not
|
|
|
|
* allowed to be modified by the user.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-01-07 06:44:01 +01:00
|
|
|
* @param boolean $includeHiddenField
|
2016-07-14 04:43:53 +02:00
|
|
|
* @return $this
|
2013-01-07 06:44:01 +01:00
|
|
|
*/
|
|
|
|
public function setIncludeHiddenField($includeHiddenField) {
|
|
|
|
$this->includeHiddenField = $includeHiddenField;
|
2016-07-14 04:43:53 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIncludeHiddenField() {
|
|
|
|
return $this->includeHiddenField;
|
2013-01-07 06:44:01 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
return clone $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
public function Type() {
|
|
|
|
return 'readonly';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function castingHelper($field) {
|
|
|
|
// Get dynamic cast for 'Value' field
|
|
|
|
if(strcasecmp($field, 'Value') === 0) {
|
|
|
|
return $this->getValueCast();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to default casting
|
|
|
|
return parent::castingHelper($field);
|
|
|
|
}
|
|
|
|
|
2016-10-12 01:19:07 +02:00
|
|
|
public function getSchemaStateDefaults() {
|
2016-10-20 10:04:41 +02:00
|
|
|
$state = parent::getSchemaStateDefaults();
|
|
|
|
$state['value'] = $this->dataValue();
|
|
|
|
|
|
|
|
return $state;
|
2016-10-12 01:19:07 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
|
2016-04-21 04:38:02 +02:00
|
|
|
/**
|
|
|
|
* If $dontEscape is true the returned value will be plain text
|
|
|
|
* and should be escaped in templates via .XML
|
|
|
|
*
|
|
|
|
* If $dontEscape is false the returned value will be safely encoded,
|
|
|
|
* but should not be escaped by the frontend.
|
|
|
|
*
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Value() {
|
2016-06-03 10:51:02 +02:00
|
|
|
// Get raw value
|
|
|
|
$value = $this->dataValue();
|
|
|
|
if($value) {
|
|
|
|
return $value;
|
2016-04-21 04:38:02 +02:00
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
// "none" text
|
|
|
|
$label = _t('FormField.NONE', 'none');
|
|
|
|
return "<i>('{$label}')</i>";
|
2011-12-22 13:10:57 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 10:51:02 +02:00
|
|
|
/**
|
|
|
|
* Get custom cating helper for Value() field
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getValueCast() {
|
|
|
|
// Casting class for 'none' text
|
|
|
|
$value = $this->dataValue();
|
|
|
|
if(empty($value)) {
|
|
|
|
return 'HTMLFragment';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use default casting
|
|
|
|
return $this->config()->casting['Value'];
|
2011-12-22 13:10:57 +01:00
|
|
|
}
|
2014-11-12 03:19:12 +01:00
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|