|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Read-only field to display a non-editable value with a label. |
|
4
|
|
|
* Consider using an {@link LabelField} if you just need a label-less |
|
5
|
|
|
* value display. |
|
6
|
|
|
* |
|
7
|
|
|
* @package forms |
|
8
|
|
|
* @subpackage fields-basic |
|
9
|
|
|
*/ |
|
10
|
|
|
class ReadonlyField extends FormField { |
|
11
|
|
|
|
|
12
|
|
|
protected $readonly = true; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Include a hidden field in the HTML for the readonly field |
|
16
|
|
|
* @var boolean |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $includeHiddenField = false; |
|
19
|
|
|
|
|
20
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* If true, a hidden field will be included in the HTML for the readonly field. |
|
24
|
|
|
* |
|
25
|
|
|
* This can be useful if you need to pass the data through on the form submission, as |
|
26
|
|
|
* long as it's okay than an attacker could change the data before it's submitted. |
|
27
|
|
|
* |
|
28
|
|
|
* This is disabled by default as it can introduce security holes if the data is not |
|
29
|
|
|
* allowed to be modified by the user. |
|
30
|
|
|
* |
|
31
|
|
|
* @param boolean $includeHiddenField |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setIncludeHiddenField($includeHiddenField) { |
|
35
|
|
|
$this->includeHiddenField = $includeHiddenField; |
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getIncludeHiddenField() { |
|
43
|
|
|
return $this->includeHiddenField; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function performReadonlyTransformation() { |
|
47
|
|
|
return clone $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $properties |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function Field($properties = array()) { |
|
55
|
|
|
// Include a hidden field in the HTML |
|
56
|
|
|
if($this->includeHiddenField && $this->readonly) { |
|
57
|
|
|
$hidden = clone $this; |
|
58
|
|
|
$hidden->setReadonly(false); |
|
59
|
|
|
return parent::Field($properties) . $hidden->Field($properties); |
|
60
|
|
|
|
|
61
|
|
|
} else { |
|
62
|
|
|
return parent::Field($properties); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getAttributes() { |
|
67
|
|
|
return array_merge( |
|
68
|
|
|
parent::getAttributes(), |
|
69
|
|
|
array( |
|
70
|
|
|
'type' => 'hidden', |
|
71
|
|
|
'value' => $this->readonly ? null : $this->value, |
|
72
|
|
|
) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function Type() { |
|
77
|
|
|
return 'readonly'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function castingHelper($field) { |
|
81
|
|
|
// Get dynamic cast for 'Value' field |
|
82
|
|
|
if(strcasecmp($field, 'Value') === 0) { |
|
83
|
|
|
return $this->getValueCast(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Fall back to default casting |
|
87
|
|
|
return parent::castingHelper($field); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* If $dontEscape is true the returned value will be plain text |
|
93
|
|
|
* and should be escaped in templates via .XML |
|
94
|
|
|
* |
|
95
|
|
|
* If $dontEscape is false the returned value will be safely encoded, |
|
96
|
|
|
* but should not be escaped by the frontend. |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed|string |
|
99
|
|
|
*/ |
|
100
|
|
|
public function Value() { |
|
101
|
|
|
// Get raw value |
|
102
|
|
|
$value = $this->dataValue(); |
|
103
|
|
|
if($value) { |
|
104
|
|
|
return $value; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// "none" text |
|
108
|
|
|
$label = _t('FormField.NONE', 'none'); |
|
109
|
|
|
return "<i>('{$label}')</i>"; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get custom cating helper for Value() field |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getValueCast() { |
|
118
|
|
|
// Casting class for 'none' text |
|
119
|
|
|
$value = $this->dataValue(); |
|
120
|
|
|
if(empty($value)) { |
|
121
|
|
|
return 'HTMLFragment'; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Use default casting |
|
125
|
|
|
return $this->config()->casting['Value']; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|