Completed
Push — master ( 5776a0...605463 )
by Daniel
23s
created

ReadonlyField::castingHelper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 1
b 1
f 0
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
	 */
33
	public function setIncludeHiddenField($includeHiddenField) {
34
		$this->includeHiddenField = $includeHiddenField;
35
	}
36
37
	public function performReadonlyTransformation() {
38
		return clone $this;
39
	}
40
41
	/**
42
	 * @param array $properties
43
	 * @return string
44
	 */
45
	public function Field($properties = array()) {
46
		// Include a hidden field in the HTML
47
		if($this->includeHiddenField && $this->readonly) {
48
			$hidden = clone $this;
49
			$hidden->setReadonly(false);
50
			return parent::Field($properties) . $hidden->Field($properties);
51
52
		} else {
53
			return parent::Field($properties);
54
		}
55
	}
56
57
	public function getAttributes() {
58
		return array_merge(
59
			parent::getAttributes(),
60
			array(
61
				'type' => 'hidden',
62
				'value' => $this->readonly ? null : $this->value,
63
			)
64
		);
65
	}
66
67
	public function Type() {
68
		return 'readonly';
69
	}
70
71
	public function castingHelper($field) {
72
		// Get dynamic cast for 'Value' field
73
		if(strcasecmp($field, 'Value') === 0) {
74
			return $this->getValueCast();
75
		}
76
77
		// Fall back to default casting
78
		return parent::castingHelper($field);
79
	}
80
81
82
	/**
83
	 * If $dontEscape is true the returned value will be plain text
84
	 * and should be escaped in templates via .XML
85
	 *
86
	 * If $dontEscape is false the returned value will be safely encoded,
87
	 * but should not be escaped by the frontend.
88
	 *
89
	 * @return mixed|string
90
	 */
91
	public function Value() {
92
		// Get raw value
93
		$value = $this->dataValue();
94
		if($value) {
95
			return $value;
96
		}
97
98
		// "none" text
99
		$label = _t('FormField.NONE', 'none');
100
		return "<i>('{$label}')</i>";
101
	}
102
103
	/**
104
	 * Get custom cating helper for Value() field
105
	 *
106
	 * @return string
107
	 */
108
	public function getValueCast() {
109
		// Casting class for 'none' text
110
		$value = $this->dataValue();
111
		if(empty($value)) {
112
			return 'HTMLFragment';
113
		}
114
115
		// Use default casting
116
		return $this->config()->casting['Value'];
117
	}
118
119
}
120