1 | <?php |
||
2 | |||
3 | namespace LeKoala\CmsActions; |
||
4 | |||
5 | use ReflectionClass; |
||
6 | use SilverStripe\Forms\GridField\GridField; |
||
7 | use SilverStripe\Forms\GridField\GridField_HTMLProvider; |
||
8 | |||
9 | /** |
||
10 | * Provide a simple way to declare links for GridField tables |
||
11 | */ |
||
12 | class GridFieldTableLink implements GridField_HTMLProvider |
||
13 | { |
||
14 | use DefaultLink; |
||
15 | |||
16 | /** |
||
17 | * Fragment to write the button to |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $targetFragment; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $actionName; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $buttonLabel; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $fontIcon; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $parentID; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $confirm; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $prompt; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $promptDefault; |
||
56 | |||
57 | /** |
||
58 | * @var array<string,mixed> |
||
59 | */ |
||
60 | protected $attributes = []; |
||
61 | |||
62 | /** |
||
63 | * @var boolean |
||
64 | */ |
||
65 | protected $noAjax = false; |
||
66 | |||
67 | /** |
||
68 | * @param string $targetFragment The HTML fragment to write the button into |
||
69 | * @param string $buttonLabel |
||
70 | * @param string $actionName |
||
71 | */ |
||
72 | public function __construct($targetFragment = "buttons-before-right", $buttonLabel = null, $actionName = null) |
||
73 | { |
||
74 | $this->targetFragment = $targetFragment; |
||
75 | if ($buttonLabel) { |
||
76 | $this->buttonLabel = $buttonLabel; |
||
77 | } |
||
78 | if ($actionName) { |
||
79 | $this->actionName = $actionName; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return mixed|string |
||
85 | */ |
||
86 | public function getActionName() |
||
87 | { |
||
88 | if ($this->actionName) { |
||
89 | return $this->actionName; |
||
90 | } |
||
91 | $class = (new ReflectionClass(get_called_class()))->getShortName(); |
||
92 | |||
93 | // ! without lowercase, in does not work |
||
94 | return strtolower(str_replace('Button', '', $class)); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getButtonLabel() |
||
101 | { |
||
102 | return $this->buttonLabel; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Place the export button in a <p> tag below the field |
||
107 | * @param GridField $gridField |
||
108 | * @return array<string,mixed> |
||
109 | */ |
||
110 | public function getHTMLFragments($gridField) |
||
111 | { |
||
112 | $action = $this->getActionName(); |
||
113 | |||
114 | $button = new CustomLink( |
||
115 | $action, |
||
116 | $this->getButtonLabel() |
||
117 | ); |
||
118 | $button->addExtraClass('btn btn-secondary action_' . $action); |
||
119 | if ($this->fontIcon) { |
||
120 | $button->addExtraClass('font-icon-' . $this->fontIcon); |
||
121 | } |
||
122 | if ($this->noAjax) { |
||
123 | $button->setNoAjax($this->noAjax); |
||
124 | } |
||
125 | //TODO: replace prompt and confirm with inline js |
||
126 | if ($this->prompt) { |
||
127 | $button->setAttribute('data-prompt', $this->prompt); |
||
128 | $promptDefault = $this->getPromptDefault(); |
||
129 | if ($promptDefault) { |
||
130 | $button->setAttribute('data-prompt-default', $promptDefault); |
||
131 | } |
||
132 | } |
||
133 | if ($this->confirm) { |
||
134 | $button->setAttribute('data-confirm', $this->confirm); |
||
135 | } |
||
136 | if ($this->newWindow) { |
||
137 | $button->setNewWindow($this->newWindow); |
||
138 | } |
||
139 | if ($this->link) { |
||
140 | $button->setLink($this->link); |
||
141 | } |
||
142 | foreach ($this->attributes as $attributeName => $attributeValue) { |
||
143 | $button->setAttribute($attributeName, $attributeValue); |
||
144 | } |
||
145 | $button->setForm($gridField->getForm()); |
||
146 | |||
147 | return [$this->targetFragment => $button->Field()]; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $name |
||
152 | * @param string $value |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setAttribute($name, $value) |
||
156 | { |
||
157 | $this->attributes[$name] = $value; |
||
158 | |||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param string $name |
||
164 | * @return string|null |
||
165 | */ |
||
166 | public function getAttribute($name) |
||
167 | { |
||
168 | if (isset($this->attributes[$name])) { |
||
169 | return $this->attributes[$name]; |
||
170 | } |
||
171 | |||
172 | return null; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param GridField $gridField |
||
177 | * @return array<string> |
||
178 | */ |
||
179 | public function getActions($gridField) |
||
0 ignored issues
–
show
|
|||
180 | { |
||
181 | return [$this->getActionName()]; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get the value of fontIcon |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getFontIcon() |
||
190 | { |
||
191 | return $this->fontIcon; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Set the value of fontIcon |
||
196 | * |
||
197 | * @param string $fontIcon |
||
198 | * |
||
199 | * @return $this |
||
200 | */ |
||
201 | public function setFontIcon($fontIcon) |
||
202 | { |
||
203 | $this->fontIcon = $fontIcon; |
||
204 | |||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | |||
209 | /** |
||
210 | * Get the parent record id |
||
211 | * |
||
212 | * @return int |
||
213 | */ |
||
214 | public function getParentID() |
||
215 | { |
||
216 | return $this->parentID; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Set the parent record id |
||
221 | * |
||
222 | * @param int $id |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function setParentID($id) |
||
226 | { |
||
227 | $this->parentID = $id; |
||
228 | |||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Get the value of confirm |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getConfirm() |
||
238 | { |
||
239 | return $this->confirm; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Set the value of confirm |
||
244 | * |
||
245 | * @param string $confirm |
||
246 | * @return $this |
||
247 | */ |
||
248 | public function setConfirm($confirm) |
||
249 | { |
||
250 | $this->confirm = $confirm; |
||
251 | |||
252 | return $this; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get the value of prompt |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public function getPrompt() |
||
261 | { |
||
262 | return $this->prompt; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Set the value of prompt |
||
267 | * |
||
268 | * @param string $prompt |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function setPrompt($prompt) |
||
272 | { |
||
273 | $this->prompt = $prompt; |
||
274 | |||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Get the value of promptDefault |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function getPromptDefault() |
||
284 | { |
||
285 | return $this->promptDefault; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Set the value of promptDefault |
||
290 | * |
||
291 | * @param string $promptDefault |
||
292 | * @return $this |
||
293 | */ |
||
294 | public function setPromptDefault($promptDefault) |
||
295 | { |
||
296 | $this->promptDefault = $promptDefault; |
||
297 | |||
298 | return $this; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get the value of noAjax |
||
303 | * @return boolean |
||
304 | */ |
||
305 | public function getNoAjax() |
||
306 | { |
||
307 | return $this->noAjax; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Set the value of noAjax |
||
312 | * |
||
313 | * @param boolean $noAjax |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function setNoAjax($noAjax) |
||
317 | { |
||
318 | $this->noAjax = $noAjax; |
||
319 | |||
320 | return $this; |
||
321 | } |
||
322 | } |
||
323 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.