1 | <?php |
||
2 | |||
3 | namespace LeKoala\Tabulator; |
||
4 | |||
5 | use Exception; |
||
6 | use SilverStripe\ORM\DataList; |
||
7 | use SilverStripe\Control\Controller; |
||
8 | use SilverStripe\Control\HTTPRequest; |
||
9 | use SilverStripe\Control\HTTPResponse; |
||
10 | use SilverStripe\Control\RequestHandler; |
||
11 | |||
12 | /** |
||
13 | * Base class to extend for all custom bulk action handlers |
||
14 | * Simply implement "process" method in your subclass |
||
15 | */ |
||
16 | class AbstractBulkAction extends AbstractTabulatorTool |
||
17 | { |
||
18 | /** |
||
19 | * Internal name for this action |
||
20 | */ |
||
21 | protected string $name = ''; |
||
22 | |||
23 | /** |
||
24 | * Front-end label for this handler's action |
||
25 | */ |
||
26 | protected string $label = 'Action'; |
||
27 | |||
28 | /** |
||
29 | * Whether this handler should be called via an XHR from the front-end |
||
30 | */ |
||
31 | protected bool $xhr = true; |
||
32 | |||
33 | /** |
||
34 | * Set to true is this handler will destroy any data. |
||
35 | * A warning and confirmation will be shown on the front-end. |
||
36 | */ |
||
37 | protected bool $destructive = false; |
||
38 | |||
39 | /** |
||
40 | * Reload after action |
||
41 | */ |
||
42 | protected bool $reload = true; |
||
43 | |||
44 | /** |
||
45 | * refresh page after action |
||
46 | */ |
||
47 | protected bool $refresh = false; |
||
48 | |||
49 | /** |
||
50 | * Return front-end configuration |
||
51 | */ |
||
52 | public function getConfig(): array |
||
53 | { |
||
54 | $config = array( |
||
55 | 'name' => $this->getName(), |
||
56 | 'label' => $this->getI18nLabel(), |
||
57 | 'xhr' => $this->getXhr(), |
||
58 | 'destructive' => $this->getDestructive() |
||
59 | ); |
||
60 | return $config; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Set if handler performs destructive actions |
||
65 | */ |
||
66 | public function setDestructive(bool $destructive): self |
||
67 | { |
||
68 | $this->destructive = $destructive; |
||
69 | return $this; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * True if the handler performs destructive actions |
||
74 | */ |
||
75 | public function getDestructive(): bool |
||
76 | { |
||
77 | return $this->destructive; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Set if handler is called via XHR |
||
82 | */ |
||
83 | |||
84 | public function setXhr(bool $xhr): self |
||
85 | { |
||
86 | $this->xhr = $xhr; |
||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * True if handler is called via XHR |
||
92 | */ |
||
93 | public function getXhr(): bool |
||
94 | { |
||
95 | return $this->xhr; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * True if reload after action |
||
100 | */ |
||
101 | public function setReload(bool $reload): self |
||
102 | { |
||
103 | $this->reload = $reload; |
||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Return reload |
||
109 | */ |
||
110 | public function getReload(): bool |
||
111 | { |
||
112 | return $this->reload; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * True if refresh after action |
||
117 | */ |
||
118 | public function setRefresh(bool $refresh): self |
||
119 | { |
||
120 | $this->refresh = $refresh; |
||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Return refresh |
||
126 | */ |
||
127 | public function getRefresh(): bool |
||
128 | { |
||
129 | return $this->refresh; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Set name |
||
134 | */ |
||
135 | public function setName($name): self |
||
136 | { |
||
137 | $this->name = $name; |
||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Return name |
||
143 | */ |
||
144 | public function getName(): string |
||
145 | { |
||
146 | return $this->name; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Set front-end label |
||
151 | */ |
||
152 | public function setLabel(string $label): self |
||
153 | { |
||
154 | $this->label = $label; |
||
155 | return $this; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Return front-end label |
||
160 | */ |
||
161 | public function getLabel(): string |
||
162 | { |
||
163 | return $this->label; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Return i18n localized front-end label |
||
168 | */ |
||
169 | public function getI18nLabel(): string |
||
170 | { |
||
171 | return _t(__CLASS__ . '.HANDLER_LABEL', $this->getLabel()); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns the URL for this RequestHandler. |
||
176 | * |
||
177 | * @param string $action |
||
178 | * @return string |
||
179 | */ |
||
180 | public function Link($action = null) |
||
181 | { |
||
182 | return Controller::join_links($this->tabulatorGrid->Link(), 'bulkAction', $action); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns the list of record IDs selected in the front-end. |
||
187 | */ |
||
188 | public function getRecordIDList(): array |
||
189 | { |
||
190 | $vars = $this->tabulatorGrid->getRequest()->requestVars(); |
||
191 | return $vars['records'] ?? []; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Returns a DataList of the records selected in the front-end. |
||
196 | */ |
||
197 | public function getRecords(): ?DataList |
||
198 | { |
||
199 | $ids = $this->getRecordIDList(); |
||
200 | |||
201 | if ($ids) { |
||
0 ignored issues
–
show
|
|||
202 | $class = $this->tabulatorGrid->getModelClass(); |
||
203 | return DataList::create($class)->byIDs($ids); |
||
204 | } |
||
205 | return null; |
||
206 | } |
||
207 | |||
208 | public function process(HTTPRequest $request): string |
||
209 | { |
||
210 | throw new Exception("Not implemented"); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Wrap the process call for this action in a generic way |
||
215 | */ |
||
216 | public function index(HTTPRequest $request): HTTPResponse |
||
217 | { |
||
218 | $response = new HTTPResponse(); |
||
219 | |||
220 | try { |
||
221 | $message = $this->process($request); |
||
222 | |||
223 | $body = json_encode([ |
||
224 | 'success' => true, |
||
225 | 'message' => $message, |
||
226 | 'reload' => $this->reload, |
||
227 | 'refresh' => $this->refresh, |
||
228 | ]); |
||
229 | $response->setBody($body); |
||
230 | $response->addHeader('Content-Type', 'application/json'); |
||
231 | } catch (Exception $ex) { |
||
232 | $response->setStatusCode(500); |
||
233 | $response->setBody($ex->getMessage()); |
||
234 | } |
||
235 | |||
236 | return $response; |
||
237 | } |
||
238 | } |
||
239 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.