Total Complexity | 42 |
Total Lines | 585 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TriggersController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TriggersController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TriggersController extends BaseController |
||
17 | { |
||
18 | public $controller_title = 'strtables'; |
||
19 | |||
20 | /** |
||
21 | * Default method to render the controller according to the action parameter. |
||
22 | */ |
||
23 | public function render() |
||
24 | { |
||
25 | if ('tree' === $this->action) { |
||
26 | return $this->doTree(); |
||
27 | } |
||
28 | |||
29 | $this->printHeader($this->headerTitle('', '', $_REQUEST['table'] . ' - ' . $this->lang['strtriggers'])); |
||
30 | $this->printBody(); |
||
31 | |||
32 | switch ($this->action) { |
||
33 | case 'alter': |
||
34 | if (null !== $this->getPostParam('alter')) { |
||
35 | $this->doSaveAlter(); |
||
36 | } else { |
||
37 | $this->doDefault(); |
||
38 | } |
||
39 | |||
40 | break; |
||
41 | case 'confirm_alter': |
||
42 | $this->doAlter(); |
||
43 | |||
44 | break; |
||
45 | case 'confirm_enable': |
||
46 | $this->doEnable(true); |
||
47 | |||
48 | break; |
||
49 | case 'confirm_disable': |
||
50 | $this->doDisable(true); |
||
51 | |||
52 | break; |
||
53 | case 'save_create': |
||
54 | if (null !== $this->getPostParam('cancel')) { |
||
55 | $this->doDefault(); |
||
56 | } else { |
||
57 | $this->doSaveCreate(); |
||
58 | } |
||
59 | |||
60 | break; |
||
61 | case 'create': |
||
62 | $this->doCreate(); |
||
63 | |||
64 | break; |
||
65 | case 'drop': |
||
66 | if (isset($_POST['yes'])) { |
||
67 | $this->doDrop(false); |
||
68 | } else { |
||
69 | $this->doDefault(); |
||
70 | } |
||
71 | |||
72 | break; |
||
73 | case 'confirm_drop': |
||
74 | $this->doDrop(true); |
||
75 | |||
76 | break; |
||
77 | case 'enable': |
||
78 | if (isset($_POST['yes'])) { |
||
79 | $this->doEnable(false); |
||
80 | } else { |
||
81 | $this->doDefault(); |
||
82 | } |
||
83 | |||
84 | break; |
||
85 | case 'disable': |
||
86 | if (isset($_POST['yes'])) { |
||
87 | $this->doDisable(false); |
||
88 | } else { |
||
89 | $this->doDefault(); |
||
90 | } |
||
91 | |||
92 | break; |
||
93 | |||
94 | default: |
||
95 | $this->doDefault(); |
||
96 | |||
97 | break; |
||
98 | } |
||
99 | |||
100 | return $this->printFooter(); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * List all the triggers on the table. |
||
105 | * |
||
106 | * @param mixed $msg |
||
107 | */ |
||
108 | public function doDefault($msg = ''): void |
||
109 | { |
||
110 | $data = $this->misc->getDatabaseAccessor(); |
||
111 | |||
112 | $tgPre = static function (&$rowdata, $actions) use ($data) { |
||
113 | // toggle enable/disable trigger per trigger |
||
114 | if (!$data->phpBool($rowdata->fields['tgenabled'])) { |
||
115 | unset($actions['disable']); |
||
116 | } else { |
||
117 | unset($actions['enable']); |
||
118 | } |
||
119 | |||
120 | return $actions; |
||
121 | }; |
||
122 | |||
123 | $this->printTrail('table'); |
||
124 | $this->printTabs('table', 'triggers'); |
||
125 | $this->printMsg($msg); |
||
126 | |||
127 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
128 | |||
129 | $columns = [ |
||
130 | 'trigger' => [ |
||
131 | 'title' => $this->lang['strname'], |
||
132 | 'field' => Decorator::field('tgname'), |
||
133 | ], |
||
134 | 'definition' => [ |
||
135 | 'title' => $this->lang['strdefinition'], |
||
136 | 'field' => Decorator::field('tgdef'), |
||
137 | ], |
||
138 | 'function' => [ |
||
139 | 'title' => $this->lang['strfunction'], |
||
140 | 'field' => Decorator::field('proproto'), |
||
141 | 'url' => \sprintf( |
||
142 | 'functions?action=properties&server=%s&database=%s&', |
||
143 | $_REQUEST['server'], |
||
144 | $_REQUEST['database'] |
||
145 | ), |
||
146 | 'vars' => [ |
||
147 | 'schema' => 'pronamespace', |
||
148 | 'function' => 'proproto', |
||
149 | 'function_oid' => 'prooid', |
||
150 | ], |
||
151 | ], |
||
152 | 'actions' => [ |
||
153 | 'title' => $this->lang['stractions'], |
||
154 | ], |
||
155 | ]; |
||
156 | |||
157 | $actions = [ |
||
158 | 'alter' => [ |
||
159 | 'content' => $this->lang['stralter'], |
||
160 | 'attr' => [ |
||
161 | 'href' => [ |
||
162 | 'url' => 'triggers', |
||
163 | 'urlvars' => [ |
||
164 | 'action' => 'confirm_alter', |
||
165 | 'table' => $_REQUEST['table'], |
||
166 | 'trigger' => Decorator::field('tgname'), |
||
167 | ], |
||
168 | ], |
||
169 | ], |
||
170 | ], |
||
171 | 'drop' => [ |
||
172 | 'content' => $this->lang['strdrop'], |
||
173 | 'attr' => [ |
||
174 | 'href' => [ |
||
175 | 'url' => 'triggers', |
||
176 | 'urlvars' => [ |
||
177 | 'action' => 'confirm_drop', |
||
178 | 'table' => $_REQUEST['table'], |
||
179 | 'trigger' => Decorator::field('tgname'), |
||
180 | ], |
||
181 | ], |
||
182 | ], |
||
183 | ], |
||
184 | ]; |
||
185 | |||
186 | if ($data->hasDisableTriggers()) { |
||
187 | $actions['enable'] = [ |
||
188 | 'content' => $this->lang['strenable'], |
||
189 | 'attr' => [ |
||
190 | 'href' => [ |
||
191 | 'url' => 'triggers', |
||
192 | 'urlvars' => [ |
||
193 | 'action' => 'confirm_enable', |
||
194 | 'table' => $_REQUEST['table'], |
||
195 | 'trigger' => Decorator::field('tgname'), |
||
196 | ], |
||
197 | ], |
||
198 | ], |
||
199 | ]; |
||
200 | $actions['disable'] = [ |
||
201 | 'content' => $this->lang['strdisable'], |
||
202 | 'attr' => [ |
||
203 | 'href' => [ |
||
204 | 'url' => 'triggers', |
||
205 | 'urlvars' => [ |
||
206 | 'action' => 'confirm_disable', |
||
207 | 'table' => $_REQUEST['table'], |
||
208 | 'trigger' => Decorator::field('tgname'), |
||
209 | ], |
||
210 | ], |
||
211 | ], |
||
212 | ]; |
||
213 | } |
||
214 | |||
215 | echo $this->printTable($triggers, $columns, $actions, 'triggers-triggers', $this->lang['strnotriggers'], $tgPre); |
||
|
|||
216 | |||
217 | $this->printNavLinks(['create' => [ |
||
218 | 'attr' => [ |
||
219 | 'href' => [ |
||
220 | 'url' => 'triggers', |
||
221 | 'urlvars' => [ |
||
222 | 'action' => 'create', |
||
223 | 'server' => $_REQUEST['server'], |
||
224 | 'database' => $_REQUEST['database'], |
||
225 | 'schema' => $_REQUEST['schema'], |
||
226 | 'table' => $_REQUEST['table'], |
||
227 | ], |
||
228 | ], |
||
229 | ], |
||
230 | 'content' => $this->lang['strcreatetrigger'], |
||
231 | ]], 'triggers-triggers', \get_defined_vars()); |
||
232 | } |
||
233 | |||
234 | public function doTree() |
||
235 | { |
||
236 | $data = $this->misc->getDatabaseAccessor(); |
||
237 | |||
238 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
239 | |||
240 | $attrs = [ |
||
241 | 'text' => Decorator::field('tgname'), |
||
242 | 'icon' => 'Trigger', |
||
243 | ]; |
||
244 | |||
245 | return $this->printTree($triggers, $attrs, 'triggers'); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Function to save after altering a trigger. |
||
250 | */ |
||
251 | public function doSaveAlter(): void |
||
252 | { |
||
253 | $data = $this->misc->getDatabaseAccessor(); |
||
254 | |||
255 | $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']); |
||
256 | |||
257 | if (0 === $status) { |
||
258 | $this->doDefault($this->lang['strtriggeraltered']); |
||
259 | } else { |
||
260 | $this->doAlter($this->lang['strtriggeralteredbad']); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Function to allow altering of a trigger. |
||
266 | * |
||
267 | * @param mixed $msg |
||
268 | */ |
||
269 | public function doAlter($msg = ''): void |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Show confirmation of drop and perform actual drop. |
||
323 | * |
||
324 | * @param mixed $confirm |
||
325 | */ |
||
326 | public function doDrop($confirm): void |
||
327 | { |
||
328 | $data = $this->misc->getDatabaseAccessor(); |
||
329 | |||
330 | if ($confirm) { |
||
331 | $this->printTrail('trigger'); |
||
332 | $this->printTitle($this->lang['strdrop'], 'pg.trigger.drop'); |
||
333 | |||
334 | echo '<p>', \sprintf( |
||
335 | $this->lang['strconfdroptrigger'], |
||
336 | $this->misc->printVal($_REQUEST['trigger']), |
||
337 | $this->misc->printVal($_REQUEST['table']) |
||
338 | ), '</p>' . \PHP_EOL; |
||
339 | |||
340 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL; |
||
341 | echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL; |
||
342 | echo \sprintf( |
||
343 | '<input type="hidden" name="table" value="%s" />%s', |
||
344 | \htmlspecialchars($_REQUEST['table']), |
||
345 | \PHP_EOL |
||
346 | ); |
||
347 | echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL; |
||
348 | echo $this->view->form; |
||
349 | echo \sprintf( |
||
350 | '<p><input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label></p>', |
||
351 | $this->lang['strcascade'] |
||
352 | ) . \PHP_EOL; |
||
353 | echo \sprintf( |
||
354 | '<input type="submit" name="yes" value="%s" />', |
||
355 | $this->lang['stryes'] |
||
356 | ) . \PHP_EOL; |
||
357 | echo \sprintf( |
||
358 | '<input type="submit" name="no" value="%s" />', |
||
359 | $this->lang['strno'] |
||
360 | ) . \PHP_EOL; |
||
361 | echo '</form>' . \PHP_EOL; |
||
362 | } else { |
||
363 | $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade'])); |
||
364 | |||
365 | if (0 === $status) { |
||
366 | $this->doDefault($this->lang['strtriggerdropped']); |
||
367 | } else { |
||
368 | $this->doDefault($this->lang['strtriggerdroppedbad']); |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Show confirmation of enable trigger and perform enabling the trigger. |
||
375 | * |
||
376 | * @param mixed $confirm |
||
377 | */ |
||
378 | public function doEnable($confirm): void |
||
379 | { |
||
380 | $data = $this->misc->getDatabaseAccessor(); |
||
381 | |||
382 | if ($confirm) { |
||
383 | $this->printTrail('trigger'); |
||
384 | $this->printTitle($this->lang['strenable'], 'pg.table.alter'); |
||
385 | |||
386 | echo '<p>', \sprintf( |
||
387 | $this->lang['strconfenabletrigger'], |
||
388 | $this->misc->printVal($_REQUEST['trigger']), |
||
389 | $this->misc->printVal($_REQUEST['table']) |
||
390 | ), '</p>' . \PHP_EOL; |
||
391 | |||
392 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL; |
||
393 | echo '<input type="hidden" name="action" value="enable" />' . \PHP_EOL; |
||
394 | echo \sprintf( |
||
395 | '<input type="hidden" name="table" value="%s" />%s', |
||
396 | \htmlspecialchars($_REQUEST['table']), |
||
397 | \PHP_EOL |
||
398 | ); |
||
399 | echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL; |
||
400 | echo $this->view->form; |
||
401 | echo \sprintf( |
||
402 | '<input type="submit" name="yes" value="%s" />', |
||
403 | $this->lang['stryes'] |
||
404 | ) . \PHP_EOL; |
||
405 | echo \sprintf( |
||
406 | '<input type="submit" name="no" value="%s" />', |
||
407 | $this->lang['strno'] |
||
408 | ) . \PHP_EOL; |
||
409 | echo '</form>' . \PHP_EOL; |
||
410 | } else { |
||
411 | $status = $data->enableTrigger($_POST['trigger'], $_POST['table']); |
||
412 | |||
413 | if (0 === $status) { |
||
414 | $this->doDefault($this->lang['strtriggerenabled']); |
||
415 | } else { |
||
416 | $this->doDefault($this->lang['strtriggerenabledbad']); |
||
417 | } |
||
418 | } |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Show confirmation of disable trigger and perform disabling the trigger. |
||
423 | * |
||
424 | * @param mixed $confirm |
||
425 | */ |
||
426 | public function doDisable($confirm): void |
||
427 | { |
||
428 | $data = $this->misc->getDatabaseAccessor(); |
||
429 | |||
430 | if ($confirm) { |
||
431 | $this->printTrail('trigger'); |
||
432 | $this->printTitle($this->lang['strdisable'], 'pg.table.alter'); |
||
433 | |||
434 | echo '<p>', \sprintf( |
||
435 | $this->lang['strconfdisabletrigger'], |
||
436 | $this->misc->printVal($_REQUEST['trigger']), |
||
437 | $this->misc->printVal($_REQUEST['table']) |
||
438 | ), '</p>' . \PHP_EOL; |
||
439 | |||
440 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL; |
||
441 | echo '<input type="hidden" name="action" value="disable" />' . \PHP_EOL; |
||
442 | echo \sprintf( |
||
443 | '<input type="hidden" name="table" value="%s" />%s', |
||
444 | \htmlspecialchars($_REQUEST['table']), |
||
445 | \PHP_EOL |
||
446 | ); |
||
447 | echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL; |
||
448 | echo $this->view->form; |
||
449 | echo \sprintf( |
||
450 | '<input type="submit" name="yes" value="%s" />', |
||
451 | $this->lang['stryes'] |
||
452 | ) . \PHP_EOL; |
||
453 | echo \sprintf( |
||
454 | '<input type="submit" name="no" value="%s" />', |
||
455 | $this->lang['strno'] |
||
456 | ) . \PHP_EOL; |
||
457 | echo '</form>' . \PHP_EOL; |
||
458 | } else { |
||
459 | $status = $data->disableTrigger($_POST['trigger'], $_POST['table']); |
||
460 | |||
461 | if (0 === $status) { |
||
462 | $this->doDefault($this->lang['strtriggerdisabled']); |
||
463 | } else { |
||
464 | $this->doDefault($this->lang['strtriggerdisabledbad']); |
||
465 | } |
||
466 | } |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Let them create s.th. |
||
471 | * |
||
472 | * @param mixed $msg |
||
473 | */ |
||
474 | public function doCreate($msg = ''): void |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Actually creates the new trigger in the database. |
||
573 | */ |
||
574 | public function doSaveCreate(): void |
||
601 | } |
||
602 | } |
||
603 | } |
||
604 | } |
||
605 |