Completed
Push — correct-classname-values ( f9b487 )
by Sam
08:29
created

ReadonlyField::Field()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
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
	 * @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
	public function Type() {
51
		return 'readonly';
52
	}
53
54
	public function castingHelper($field) {
55
		// Get dynamic cast for 'Value' field
56
		if(strcasecmp($field, 'Value') === 0) {
57
			return $this->getValueCast();
58
		}
59
60
		// Fall back to default casting
61
		return parent::castingHelper($field);
62
	}
63
64
65
	/**
66
	 * If $dontEscape is true the returned value will be plain text
67
	 * and should be escaped in templates via .XML
68
	 *
69
	 * If $dontEscape is false the returned value will be safely encoded,
70
	 * but should not be escaped by the frontend.
71
	 *
72
	 * @return mixed|string
73
	 */
74
	public function Value() {
75
		// Get raw value
76
		$value = $this->dataValue();
77
		if($value) {
78
			return $value;
79
		}
80
81
		// "none" text
82
		$label = _t('FormField.NONE', 'none');
83
		return "<i>('{$label}')</i>";
84
	}
85
86
	/**
87
	 * Get custom cating helper for Value() field
88
	 *
89
	 * @return string
90
	 */
91
	public function getValueCast() {
92
		// Casting class for 'none' text
93
		$value = $this->dataValue();
94
		if(empty($value)) {
95
			return 'HTMLFragment';
96
		}
97
98
		// Use default casting
99
		return $this->config()->casting['Value'];
100
	}
101
102
}
103