Completed
Push — 3.1 ( 76ce9f...612a91 )
by Daniel
08:13
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
	public function Field($properties = array()) {
40
		// Include a hidden field in the HTML
41
		if($this->includeHiddenField && $this->readonly) {
42
			$hidden = clone $this;
43
			$hidden->setReadonly(false);
44
			return parent::Field($properties) . $hidden->Field($properties);
45
46
		} else {
47
			return parent::Field($properties);
48
		}
49
	}
50
51
	public function Value() {
52
		if($this->value) return $this->value;
53
		else return '<i>(' . _t('FormField.NONE', 'none') . ')</i>';
54
	}
55
56
	/**
57
	 * This is a legacy fix to ensure that the `dontEscape` flag has an impact on readonly fields
58
	 * now that we've moved to casting template values more rigidly
59
	 *
60
	 * @param string $field
61
	 * @return string
62
	 */
63
	public function castingHelper($field) {
64
		if (
65
			(strcasecmp($field, 'Value') === 0)
66
			&& ($this->dontEscape || empty($this->value))
67
		) {
68
			// Value is either empty, or unescaped
69
			return 'HTMLText';
70
		}
71
		return parent::castingHelper($field);
72
	}
73
74
	public function getAttributes() {
75
		return array_merge(
76
			parent::getAttributes(),
77
			array(
78
				'type' => 'hidden',
79
				'value' => $this->readonly ? null : $this->value,
80
			)
81
		);
82
	}
83
84
	public function Type() {
85
		return 'readonly';
86
	}
87
}
88