FormAction.php
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* The action buttons are <input type="submit"> as well as <button> tags.
*
* Upon clicking the button below will redirect the user to doAction under the current controller.
*
* <code>
* new FormAction (
* // doAction has to be a defined controller member
* $action = "doAction",
* $title = "Submit button"
* )
* </code>
*
* @package forms
* @subpackage actions
*/
class FormAction extends FormField {
/**
* Action name, normally prefixed with 'action_'
*
* @var string
*/
protected $action;
/**
* Enables the use of <button> instead of <input>
* in {@link Field()} - for more customizeable styling.
*
* @var boolean
*/
public $useButtonTag = false;
/**
* Literal button content, used when useButtonTag is true.
*
* @var string
*/
protected $buttonContent = null;
/**
* Create a new action button.
*
* @param string $action The method to call when the button is clicked
* @param string $title The label on the button. This should be plain text, not escaped as HTML.
* @param Form form The parent form, auto-set when the field is placed inside a form
*/
public function __construct($action, $title = "", $form = null) {
$this->action = "action_$action";
$this->setForm($form);
parent::__construct($this->action, $title);
}
/**
* Get the action name
*
* @return string
*/
public function actionName() {
return substr($this->name, 7);
}
/**
* Set the full action name, including action_
* This provides an opportunity to replace it with something else
*
* @param string $fullAction
* @return $this
*/
public function setFullAction($fullAction) {
$this->action = $fullAction;
return $this;
}
public function Field($properties = array()) {
$properties = array_merge(
$properties,
array(
'Name' => $this->action,
'Title' => ($this->description && !$this->useButtonTag) ? $this->description : $this->Title(),
'UseButtonTag' => $this->useButtonTag
)
);
return parent::Field($properties);
}
public function FieldHolder($properties = array()) {
return $this->Field($properties);
}
public function Type() {
return 'action';
}
public function Title() {
$title = parent::Title();
// Remove this method override in 4.0
$decoded = Convert::xml2raw($title);
if($decoded !== $title) {
Deprecation::notice(
'4.0',
'The FormAction title field should not be html encoded. Use buttonContent to set custom html instead'
);
return $decoded;
}
return $title;
}
public function getAttributes() {
$type = (isset($this->attributes['src'])) ? 'image' : 'submit';
return array_merge(
parent::getAttributes(),
array(
'disabled' => ($this->isReadonly() || $this->isDisabled()),
'value' => $this->Title(),
'type' => $type,
'title' => ($this->useButtonTag) ? $this->description : null,
)
);
}
/**
* Add content inside a button field.
*
* @param string $content
* @return $this
*/
public function setButtonContent($content) {
$this->buttonContent = (string) $content;
return $this;
}
/**
* Gets the content inside the button field
*
* @return string
*/
public function getButtonContent() {
return $this->buttonContent;
}
/**
* Enable or disable the rendering of this action as a <button />
*
* @param boolean
* @return $this
*/
public function setUseButtonTag($bool) {
$this->useButtonTag = $bool;
return $this;
}
/**
* Determine if this action is rendered as a <button />
*
* @return boolean
*/
public function getUseButtonTag() {
return $this->useButtonTag;
}
/**
* Does not transform to readonly by purpose.
* Globally disabled buttons would break the CMS.
*/
public function performReadonlyTransformation() {
$clone = clone $this;
$clone->setReadonly(true);
return $clone;
}
}