Completed
Push — 3.3 ( b7bb7a...6552dc )
by Daniel
07:43
created

ReadonlyField::castingHelper()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.2
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
	/**
21
	 * If true, a hidden field will be included in the HTML for the readonly field.
22
	 *
23
	 * This can be useful if you need to pass the data through on the form submission, as
24
	 * long as it's okay than an attacker could change the data before it's submitted.
25
	 *
26
	 * This is disabled by default as it can introduce security holes if the data is not
27
	 * allowed to be modified by the user.
28
	 *
29
	 * @param boolean $includeHiddenField
30
	 */
31
	public function setIncludeHiddenField($includeHiddenField) {
32
		$this->includeHiddenField = $includeHiddenField;
33
	}
34
35
	public function performReadonlyTransformation() {
36
		return clone $this;
37
	}
38
39
	/**
40
	 * @param array $properties
41
	 * @return HTMLText
42
	 */
43
	public function Field($properties = array()) {
44
		// Include a hidden field in the HTML
45
		if($this->includeHiddenField && $this->readonly) {
46
			$hidden = clone $this;
47
			$hidden->setReadonly(false);
48
			return parent::Field($properties) . $hidden->Field($properties);
49
50
		} else {
51
			return parent::Field($properties);
52
		}
53
	}
54
55
	public function Value() {
56
		if($this->value) return $this->value;
57
		else return '<i>(' . _t('FormField.NONE', 'none') . ')</i>';
58
	}
59
60
	/**
61
	 * This is a legacy fix to ensure that the `dontEscape` flag has an impact on readonly fields
62
	 * now that we've moved to casting template values more rigidly
63
	 *
64
	 * @param string $field
65
	 * @return string
66
	 */
67
	public function castingHelper($field) {
68
		if (
69
			(strcasecmp($field, 'Value') === 0)
70
			&& ($this->dontEscape || empty($this->value))
71
		) {
72
			// Value is either empty, or unescaped
73
			return 'HTMLText';
74
		}
75
		return parent::castingHelper($field);
76
	}
77
78
	public function getAttributes() {
79
		return array_merge(
80
			parent::getAttributes(),
81
			array(
82
				'type' => 'hidden',
83
				'value' => $this->readonly ? null : $this->value,
84
			)
85
		);
86
	}
87
88
	public function Type() {
89
		return 'readonly';
90
	}
91
92
}
93