Completed
Push — master ( 098f19...84193b )
by Hamish
11:36
created

FormAction::Field()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 1
nop 1
1
<?php
2
/**
3
 * The action buttons are <input type="submit"> as well as <button> tags.
4
 *
5
 * Upon clicking the button below will redirect the user to doAction under the current controller.
6
 *
7
 * <code>
8
 * new FormAction (
9
 *    // doAction has to be a defined controller member
10
 *    $action = "doAction",
11
 *    $title = "Submit button"
12
 * )
13
 * </code>
14
 *
15
 * @package forms
16
 * @subpackage actions
17
 */
18
class FormAction extends FormField {
19
20
	/**
21
	 * Action name, normally prefixed with 'action_'
22
	 *
23
	 * @var string
24
	 */
25
	protected $action;
26
27
	/**
28
	 * Enables the use of <button> instead of <input>
29
	 * in {@link Field()} - for more customizeable styling.
30
	 *
31
	 * @var boolean
32
	 */
33
	public $useButtonTag = false;
34
35
	/**
36
	 * Literal button content, used when useButtonTag is true.
37
	 *
38
	 * @var string
39
	 */
40
	protected $buttonContent = null;
41
42
	/**
43
	 * Should validation be skipped when performing this action?
44
	 *
45
	 * @var bool
46
	 */
47
	protected $validationExempt = false;
48
49
	/**
50
	 * Create a new action button.
51
	 *
52
	 * @param string $action The method to call when the button is clicked
53
	 * @param string $title The label on the button. This should be plain text, not escaped as HTML.
54
	 * @param Form form The parent form, auto-set when the field is placed inside a form
55
	 */
56
	public function __construct($action, $title = "", $form = null) {
57
		$this->action = "action_$action";
58
		$this->setForm($form);
0 ignored issues
show
Bug introduced by
It seems like $form defined by parameter $form on line 56 can be null; however, FormField::setForm() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
59
60
		parent::__construct($this->action, $title);
61
	}
62
63
	/**
64
	 * Get the action name
65
	 *
66
	 * @return string
67
	 */
68
	public function actionName() {
69
		return substr($this->name, 7);
70
	}
71
72
	/**
73
	 * Set the full action name, including action_
74
	 * This provides an opportunity to replace it with something else
75
	 *
76
	 * @param string $fullAction
77
	 * @return $this
78
	 */
79
	public function setFullAction($fullAction) {
80
		$this->action = $fullAction;
81
		return $this;
82
	}
83
84
	/**
85
	 * @param array $properties
86
	 * @return HTMLText
87
	 */
88
	public function Field($properties = array()) {
89
		$properties = array_merge(
90
			$properties,
91
			array(
92
				'Name' => $this->action,
93
				'Title' => ($this->description && !$this->useButtonTag) ? $this->description : $this->Title(),
94
				'UseButtonTag' => $this->useButtonTag
95
			)
96
		);
97
98
		return parent::Field($properties);
99
	}
100
101
	/**
102
	 * @param array $properties
103
	 * @return HTMLText
104
	 */
105
	public function FieldHolder($properties = array()) {
106
		return $this->Field($properties);
107
	}
108
109
	public function Type() {
110
		return 'action';
111
	}
112
113
	public function Title() {
114
		$title = parent::Title();
115
116
		// Remove this method override in 4.0
117
		$decoded = Convert::xml2raw($title);
118
		if($title && $decoded !== $title) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
119
			Deprecation::notice(
120
				'4.0',
121
				'The FormAction title field should not be html encoded. Use buttonContent to set custom html instead'
122
			);
123
			return $decoded;
124
		}
125
126
		return $title;
127
	}
128
129
	public function getAttributes() {
130
		$type = (isset($this->attributes['src'])) ? 'image' : 'submit';
131
132
		return array_merge(
133
			parent::getAttributes(),
134
			array(
135
				'disabled' => ($this->isReadonly() || $this->isDisabled()),
136
				'value' => $this->Title(),
137
				'type' => $type,
138
				'title' => ($this->useButtonTag) ? $this->description : null,
139
			)
140
		);
141
	}
142
143
	/**
144
	 * Add content inside a button field.
145
	 *
146
	 * @param string $content
147
	 * @return $this
148
	 */
149
	public function setButtonContent($content) {
150
		$this->buttonContent = (string) $content;
151
		return $this;
152
	}
153
154
	/**
155
	 * Gets the content inside the button field
156
	 *
157
	 * @return string
158
	 */
159
	public function getButtonContent() {
160
		return $this->buttonContent;
161
	}
162
163
	/**
164
	 * Enable or disable the rendering of this action as a <button />
165
	 *
166
	 * @param boolean
167
	 * @return $this
168
	 */
169
	public function setUseButtonTag($bool) {
170
		$this->useButtonTag = $bool;
171
		return $this;
172
	}
173
174
	/**
175
	 * Determine if this action is rendered as a <button />
176
	 *
177
	 * @return boolean
178
	 */
179
	public function getUseButtonTag() {
180
		return $this->useButtonTag;
181
	}
182
183
	/**
184
	 * Set whether this action can be performed without validating the data
185
	 *
186
	 * @param bool $exempt
187
	 * @return $this
188
	 */
189
	public function setValidationExempt($exempt = true) {
190
		$this->validationExempt = $exempt;
191
		return $this;
192
	}
193
194
	/**
195
	 * Get whether this action can be performed without vaidating the data
196
	 *
197
	 * @return bool
198
	 */
199
	public function getValidationExempt() {
200
		return $this->validationExempt;
201
	}
202
203
	/**
204
	 * Does not transform to readonly by purpose.
205
	 * Globally disabled buttons would break the CMS.
206
	 */
207
	public function performReadonlyTransformation() {
208
		$clone = clone $this;
209
		$clone->setReadonly(true);
210
		return $clone;
211
	}
212
213
}
214