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

ReadonlyField   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 93
rs 10
wmc 10
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setIncludeHiddenField() 0 4 1
A getIncludeHiddenField() 0 3 1
A performReadonlyTransformation() 0 3 1
A Type() 0 3 1
A castingHelper() 0 9 2
A Value() 0 11 2
A getValueCast() 0 10 2
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