Completed
Push — master ( d19955...af891e )
by Sam
22s
created

FormAction::getSchemaDataDefaults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
use SilverStripe\ORM\FieldType\DBHTMLText;
3
4
/**
5
 * The action buttons are <input type="submit"> as well as <button> tags.
6
 *
7
 * Upon clicking the button below will redirect the user to doAction under the current controller.
8
 *
9
 * <code>
10
 * new FormAction (
11
 *    // doAction has to be a defined controller member
12
 *    $action = "doAction",
13
 *    $title = "Submit button"
14
 * )
15
 * </code>
16
 *
17
 * @package forms
18
 * @subpackage actions
19
 */
20
class FormAction extends FormField {
21
22
	/**
23
	 * @config
24
	 * @var array
25
	 */
26
	private static $casting = [
27
		'ButtonContent' => 'HTMLFragment',
28
	];
29
30
	/**
31
	 * Action name, normally prefixed with 'action_'
32
	 *
33
	 * @var string
34
	 */
35
	protected $action;
36
37
	/**
38
	 * Identifier of icon, if supported on the frontend
39
	 *
40
	 * @var string
41
	 */
42
	protected $icon = null;
43
44
	protected $schemaComponent = 'FormAction';
45
46
	/**
47
	 * Enables the use of <button> instead of <input>
48
	 * in {@link Field()} - for more customizeable styling.
49
	 *
50
	 * @var boolean
51
	 */
52
	public $useButtonTag = false;
53
54
	/**
55
	 * Literal button content, used when useButtonTag is true.
56
	 *
57
	 * @var string
58
	 */
59
	protected $buttonContent = null;
60
61
	/**
62
	 * Should validation be skipped when performing this action?
63
	 *
64
	 * @var bool
65
	 */
66
	protected $validationExempt = false;
67
68
	/**
69
	 * Create a new action button.
70
	 *
71
	 * @param string $action The method to call when the button is clicked
72
	 * @param string $title The label on the button. This should be plain text, not escaped as HTML.
73
	 * @param Form form The parent form, auto-set when the field is placed inside a form
74
	 */
75
	public function __construct($action, $title = "", $form = null) {
76
		$this->action = "action_$action";
77
		$this->setForm($form);
0 ignored issues
show
Bug introduced by
It seems like $form defined by parameter $form on line 75 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...
78
79
		parent::__construct($this->action, $title);
80
	}
81
82
	/**
83
	 * Add extra options to data
84
	 */
85
	public function getSchemaDataDefaults() {
86
		$defaults = parent::getSchemaDataDefaults();
87
		$defaults['attributes']['type'] = $this->getUseButtonTag() ? 'button' : 'submit';
88
		$defaults['data']['icon'] = $this->getIcon();
89
		return $defaults;
90
	}
91
92
	/**
93
	 * Get button icon, if supported
94
	 *
95
	 * @return string
96
	 */
97
	public function getIcon() {
98
		return $this->icon;
99
	}
100
101
	/**
102
	 * Sets button icon
103
	 *
104
	 * @param string $icon Icon identifier (not path)
105
	 * @return $this
106
	 */
107
	public function setIcon($icon) {
108
		$this->icon = $icon;
109
		return $this;
110
	}
111
112
	/**
113
	 * Get the action name
114
	 *
115
	 * @return string
116
	 */
117
	public function actionName() {
118
		return substr($this->name, 7);
119
	}
120
121
	/**
122
	 * Set the full action name, including action_
123
	 * This provides an opportunity to replace it with something else
124
	 *
125
	 * @param string $fullAction
126
	 * @return $this
127
	 */
128
	public function setFullAction($fullAction) {
129
		$this->action = $fullAction;
130
		return $this;
131
	}
132
133
	/**
134
	 * @param array $properties
135
	 * @return string
136
	 */
137
	public function Field($properties = array()) {
138
		$properties = array_merge(
139
			$properties,
140
			array(
141
				'Name' => $this->action,
142
				'Title' => ($this->description && !$this->useButtonTag) ? $this->description : $this->Title(),
143
				'UseButtonTag' => $this->useButtonTag
144
			)
145
		);
146
147
		return parent::Field($properties);
148
	}
149
150
	/**
151
	 * @param array $properties
152
	 * @return DBHTMLText
153
	 */
154
	public function FieldHolder($properties = array()) {
155
		return $this->Field($properties);
156
	}
157
158
	public function Type() {
159
		return 'action';
160
	}
161
162
	public function getAttributes() {
163
		$type = (isset($this->attributes['src'])) ? 'image' : 'submit';
164
165
		return array_merge(
166
			parent::getAttributes(),
167
			array(
168
				'disabled' => ($this->isReadonly() || $this->isDisabled()),
169
				'value' => $this->Title(),
170
				'type' => $type,
171
				'title' => ($this->useButtonTag) ? $this->description : null,
172
			)
173
		);
174
	}
175
176
	/**
177
	 * Add content inside a button field. This should be pre-escaped raw HTML and should be used sparingly.
178
	 *
179
	 * @param string $content
180
	 * @return $this
181
	 */
182
	public function setButtonContent($content) {
183
		$this->buttonContent = (string) $content;
184
		return $this;
185
	}
186
187
	/**
188
	 * Gets the content inside the button field. This is raw HTML, and should be used sparingly.
189
	 *
190
	 * @return string
191
	 */
192
	public function getButtonContent() {
193
		return $this->buttonContent;
194
	}
195
196
	/**
197
	 * Enable or disable the rendering of this action as a <button />
198
	 *
199
	 * @param boolean
200
	 * @return $this
201
	 */
202
	public function setUseButtonTag($bool) {
203
		$this->useButtonTag = $bool;
204
		return $this;
205
	}
206
207
	/**
208
	 * Determine if this action is rendered as a <button />
209
	 *
210
	 * @return boolean
211
	 */
212
	public function getUseButtonTag() {
213
		return $this->useButtonTag;
214
	}
215
216
	/**
217
	 * Set whether this action can be performed without validating the data
218
	 *
219
	 * @param bool $exempt
220
	 * @return $this
221
	 */
222
	public function setValidationExempt($exempt = true) {
223
		$this->validationExempt = $exempt;
224
		return $this;
225
	}
226
227
	/**
228
	 * Get whether this action can be performed without vaidating the data
229
	 *
230
	 * @return bool
231
	 */
232
	public function getValidationExempt() {
233
		return $this->validationExempt;
234
	}
235
236
	/**
237
	 * Does not transform to readonly by purpose.
238
	 * Globally disabled buttons would break the CMS.
239
	 */
240
	public function performReadonlyTransformation() {
241
		$clone = clone $this;
242
		$clone->setReadonly(true);
243
		return $clone;
244
	}
245
246
}
247