Completed
Pull Request — master (#5653)
by Damian
12:15
created

FormAction::Title()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A FormAction::FieldHolder() 0 3 1
A FormAction::Type() 0 3 1
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
	 * Enables the use of <button> instead of <input>
39
	 * in {@link Field()} - for more customizeable styling.
40
	 *
41
	 * @var boolean
42
	 */
43
	public $useButtonTag = false;
44
45
	/**
46
	 * Literal button content, used when useButtonTag is true.
47
	 *
48
	 * @var string
49
	 */
50
	protected $buttonContent = null;
51
52
	/**
53
	 * Should validation be skipped when performing this action?
54
	 *
55
	 * @var bool
56
	 */
57
	protected $validationExempt = false;
58
59
	/**
60
	 * Create a new action button.
61
	 *
62
	 * @param string $action The method to call when the button is clicked
63
	 * @param string $title The label on the button. This should be plain text, not escaped as HTML.
64
	 * @param Form form The parent form, auto-set when the field is placed inside a form
65
	 */
66
	public function __construct($action, $title = "", $form = null) {
67
		$this->action = "action_$action";
68
		$this->setForm($form);
0 ignored issues
show
Bug introduced by
It seems like $form defined by parameter $form on line 66 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...
69
70
		parent::__construct($this->action, $title);
71
	}
72
73
	/**
74
	 * Get the action name
75
	 *
76
	 * @return string
77
	 */
78
	public function actionName() {
79
		return substr($this->name, 7);
80
	}
81
82
	/**
83
	 * Set the full action name, including action_
84
	 * This provides an opportunity to replace it with something else
85
	 *
86
	 * @param string $fullAction
87
	 * @return $this
88
	 */
89
	public function setFullAction($fullAction) {
90
		$this->action = $fullAction;
91
		return $this;
92
	}
93
94
	/**
95
	 * @param array $properties
96
	 * @return string
97
	 */
98
	public function Field($properties = array()) {
99
		$properties = array_merge(
100
			$properties,
101
			array(
102
				'Name' => $this->action,
103
				'Title' => ($this->description && !$this->useButtonTag) ? $this->description : $this->Title(),
104
				'UseButtonTag' => $this->useButtonTag
105
			)
106
		);
107
108
		return parent::Field($properties);
109
	}
110
111
	/**
112
	 * @param array $properties
113
	 * @return DBHTMLText
114
	 */
115
	public function FieldHolder($properties = array()) {
116
		return $this->Field($properties);
117
	}
118
119
	public function Type() {
120
		return 'action';
121
	}
122
123
	public function getAttributes() {
124
		$type = (isset($this->attributes['src'])) ? 'image' : 'submit';
125
126
		return array_merge(
127
			parent::getAttributes(),
128
			array(
129
				'disabled' => ($this->isReadonly() || $this->isDisabled()),
130
				'value' => $this->Title(),
131
				'type' => $type,
132
				'title' => ($this->useButtonTag) ? $this->description : null,
133
			)
134
		);
135
	}
136
137
	/**
138
	 * Add content inside a button field. This should be pre-escaped raw HTML and should be used sparingly.
139
	 *
140
	 * @param string $content
141
	 * @return $this
142
	 */
143
	public function setButtonContent($content) {
144
		$this->buttonContent = (string) $content;
145
		return $this;
146
	}
147
148
	/**
149
	 * Gets the content inside the button field. This is raw HTML, and should be used sparingly.
150
	 *
151
	 * @return string
152
	 */
153
	public function getButtonContent() {
154
		return $this->buttonContent;
155
	}
156
157
	/**
158
	 * Enable or disable the rendering of this action as a <button />
159
	 *
160
	 * @param boolean
161
	 * @return $this
162
	 */
163
	public function setUseButtonTag($bool) {
164
		$this->useButtonTag = $bool;
165
		return $this;
166
	}
167
168
	/**
169
	 * Determine if this action is rendered as a <button />
170
	 *
171
	 * @return boolean
172
	 */
173
	public function getUseButtonTag() {
174
		return $this->useButtonTag;
175
	}
176
177
	/**
178
	 * Set whether this action can be performed without validating the data
179
	 *
180
	 * @param bool $exempt
181
	 * @return $this
182
	 */
183
	public function setValidationExempt($exempt = true) {
184
		$this->validationExempt = $exempt;
185
		return $this;
186
	}
187
188
	/**
189
	 * Get whether this action can be performed without vaidating the data
190
	 *
191
	 * @return bool
192
	 */
193
	public function getValidationExempt() {
194
		return $this->validationExempt;
195
	}
196
197
	/**
198
	 * Does not transform to readonly by purpose.
199
	 * Globally disabled buttons would break the CMS.
200
	 */
201
	public function performReadonlyTransformation() {
202
		$clone = clone $this;
203
		$clone->setReadonly(true);
204
		return $clone;
205
	}
206
207
}
208