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 = CustomLink::create($action, $this->getButtonLabel()); |
||
115 | $button->addExtraClass('btn btn-secondary action_' . $action); |
||
116 | if ($this->fontIcon) { |
||
117 | $button->addExtraClass('font-icon-' . $this->fontIcon); |
||
118 | } |
||
119 | if ($this->noAjax) { |
||
120 | $button->setNoAjax($this->noAjax); |
||
121 | } |
||
122 | //TODO: replace prompt and confirm with inline js |
||
123 | if ($this->prompt) { |
||
124 | $button->setAttribute('data-prompt', $this->prompt); |
||
125 | $promptDefault = $this->getPromptDefault(); |
||
126 | if ($promptDefault) { |
||
127 | $button->setAttribute('data-prompt-default', $promptDefault); |
||
128 | } |
||
129 | } |
||
130 | if ($this->confirm) { |
||
131 | $button->setAttribute('data-confirm', $this->confirm); |
||
132 | } |
||
133 | if ($this->newWindow) { |
||
134 | $button->setNewWindow($this->newWindow); |
||
135 | } |
||
136 | if ($this->link) { |
||
137 | $button->setLink($this->link); |
||
138 | } |
||
139 | foreach ($this->attributes as $attributeName => $attributeValue) { |
||
140 | $button->setAttribute($attributeName, $attributeValue); |
||
141 | } |
||
142 | $button->setForm($gridField->getForm()); |
||
143 | |||
144 | return [$this->targetFragment => $button->Field()]; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * @param string $value |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function setAttribute($name, $value) |
||
153 | { |
||
154 | $this->attributes[$name] = $value; |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string $name |
||
161 | * @return string|null |
||
162 | */ |
||
163 | public function getAttribute($name) |
||
164 | { |
||
165 | if (isset($this->attributes[$name])) { |
||
166 | return $this->attributes[$name]; |
||
167 | } |
||
168 | |||
169 | return null; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param GridField $gridField |
||
174 | * @return array<string> |
||
175 | */ |
||
176 | public function getActions($gridField) |
||
0 ignored issues
–
show
|
|||
177 | { |
||
178 | return [$this->getActionName()]; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Get the value of fontIcon |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getFontIcon() |
||
187 | { |
||
188 | return $this->fontIcon; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Set the value of fontIcon |
||
193 | * |
||
194 | * @param string $fontIcon |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setFontIcon($fontIcon) |
||
199 | { |
||
200 | $this->fontIcon = $fontIcon; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Get the parent record id |
||
208 | * |
||
209 | * @return int |
||
210 | */ |
||
211 | public function getParentID() |
||
212 | { |
||
213 | return $this->parentID; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Set the parent record id |
||
218 | * |
||
219 | * @param int $id |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function setParentID($id) |
||
223 | { |
||
224 | $this->parentID = $id; |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get the value of confirm |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | public function getConfirm() |
||
235 | { |
||
236 | return $this->confirm; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Set the value of confirm |
||
241 | * |
||
242 | * @param string $confirm |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setConfirm($confirm) |
||
246 | { |
||
247 | $this->confirm = $confirm; |
||
248 | |||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get the value of prompt |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getPrompt() |
||
258 | { |
||
259 | return $this->prompt; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Set the value of prompt |
||
264 | * |
||
265 | * @param string $prompt |
||
266 | * @return $this |
||
267 | */ |
||
268 | public function setPrompt($prompt) |
||
269 | { |
||
270 | $this->prompt = $prompt; |
||
271 | |||
272 | return $this; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get the value of promptDefault |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getPromptDefault() |
||
281 | { |
||
282 | return $this->promptDefault; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Set the value of promptDefault |
||
287 | * |
||
288 | * @param string $promptDefault |
||
289 | * @return $this |
||
290 | */ |
||
291 | public function setPromptDefault($promptDefault) |
||
292 | { |
||
293 | $this->promptDefault = $promptDefault; |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get the value of noAjax |
||
300 | * @return boolean |
||
301 | */ |
||
302 | public function getNoAjax() |
||
303 | { |
||
304 | return $this->noAjax; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Set the value of noAjax |
||
309 | * |
||
310 | * @param boolean $noAjax |
||
311 | * @return $this |
||
312 | */ |
||
313 | public function setNoAjax($noAjax) |
||
314 | { |
||
315 | $this->noAjax = $noAjax; |
||
316 | |||
317 | return $this; |
||
318 | } |
||
319 | } |
||
320 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.