Total Complexity | 43 |
Total Lines | 526 |
Duplicated Lines | 0 % |
Changes | 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 |
||
14 | class TriggersController extends BaseController |
||
15 | { |
||
16 | public $controller_name = 'TriggersController'; |
||
17 | |||
18 | /** |
||
19 | * Default method to render the controller according to the action parameter. |
||
20 | */ |
||
21 | public function render() |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * List all the triggers on the table. |
||
107 | * |
||
108 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
109 | */ |
||
110 | public function doDefault($msg = '') |
||
111 | { |
||
112 | $conf = $this->conf; |
||
113 | |||
114 | $lang = $this->lang; |
||
115 | $data = $this->misc->getDatabaseAccessor(); |
||
116 | |||
117 | $tgPre = function (&$rowdata, $actions) use ($data) { |
||
118 | // toggle enable/disable trigger per trigger |
||
119 | if (!$data->phpBool($rowdata->fields['tgenabled'])) { |
||
120 | unset($actions['disable']); |
||
121 | } else { |
||
122 | unset($actions['enable']); |
||
123 | } |
||
124 | |||
125 | return $actions; |
||
126 | }; |
||
127 | |||
128 | $this->printTrail('table'); |
||
129 | $this->printTabs('table', 'triggers'); |
||
130 | $this->printMsg($msg); |
||
131 | |||
132 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
133 | |||
134 | $columns = [ |
||
135 | 'trigger' => [ |
||
136 | 'title' => $lang['strname'], |
||
137 | 'field' => Decorator::field('tgname'), |
||
138 | ], |
||
139 | 'definition' => [ |
||
140 | 'title' => $lang['strdefinition'], |
||
141 | 'field' => Decorator::field('tgdef'), |
||
142 | ], |
||
143 | 'function' => [ |
||
144 | 'title' => $lang['strfunction'], |
||
145 | 'field' => Decorator::field('proproto'), |
||
146 | 'url' => "functions.php?action=properties&server={$_REQUEST['server']}&database={$_REQUEST['database']}&", |
||
147 | 'vars' => [ |
||
148 | 'schema' => 'pronamespace', |
||
149 | 'function' => 'proproto', |
||
150 | 'function_oid' => 'prooid', |
||
151 | ], |
||
152 | ], |
||
153 | 'actions' => [ |
||
154 | 'title' => $lang['stractions'], |
||
155 | ], |
||
156 | ]; |
||
157 | |||
158 | $actions = [ |
||
159 | 'alter' => [ |
||
160 | 'content' => $lang['stralter'], |
||
161 | 'attr' => [ |
||
162 | 'href' => [ |
||
163 | 'url' => 'triggers.php', |
||
164 | 'urlvars' => [ |
||
165 | 'action' => 'confirm_alter', |
||
166 | 'table' => $_REQUEST['table'], |
||
167 | 'trigger' => Decorator::field('tgname'), |
||
168 | ], |
||
169 | ], |
||
170 | ], |
||
171 | ], |
||
172 | 'drop' => [ |
||
173 | 'content' => $lang['strdrop'], |
||
174 | 'attr' => [ |
||
175 | 'href' => [ |
||
176 | 'url' => 'triggers.php', |
||
177 | 'urlvars' => [ |
||
178 | 'action' => 'confirm_drop', |
||
179 | 'table' => $_REQUEST['table'], |
||
180 | 'trigger' => Decorator::field('tgname'), |
||
181 | ], |
||
182 | ], |
||
183 | ], |
||
184 | ], |
||
185 | ]; |
||
186 | if ($data->hasDisableTriggers()) { |
||
187 | $actions['enable'] = [ |
||
188 | 'content' => $lang['strenable'], |
||
189 | 'attr' => [ |
||
190 | 'href' => [ |
||
191 | 'url' => 'triggers.php', |
||
192 | 'urlvars' => [ |
||
193 | 'action' => 'confirm_enable', |
||
194 | 'table' => $_REQUEST['table'], |
||
195 | 'trigger' => Decorator::field('tgname'), |
||
196 | ], |
||
197 | ], |
||
198 | ], |
||
199 | ]; |
||
200 | $actions['disable'] = [ |
||
201 | 'content' => $lang['strdisable'], |
||
202 | 'attr' => [ |
||
203 | 'href' => [ |
||
204 | 'url' => 'triggers.php', |
||
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', $lang['strnotriggers'], $tgPre); |
||
216 | |||
217 | $this->printNavLinks(['create' => [ |
||
1 ignored issue
–
show
|
|||
218 | 'attr' => [ |
||
219 | 'href' => [ |
||
220 | 'url' => 'triggers.php', |
||
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' => $lang['strcreatetrigger'], |
||
231 | ]], 'triggers-triggers', get_defined_vars()); |
||
1 ignored issue
–
show
|
|||
232 | } |
||
233 | |||
234 | public function doTree() |
||
1 ignored issue
–
show
|
|||
235 | { |
||
236 | $conf = $this->conf; |
||
237 | |||
238 | $lang = $this->lang; |
||
239 | $data = $this->misc->getDatabaseAccessor(); |
||
240 | |||
241 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
242 | |||
243 | $reqvars = $this->misc->getRequestVars('table'); |
||
244 | |||
245 | $attrs = [ |
||
246 | 'text' => Decorator::field('tgname'), |
||
247 | 'icon' => 'Trigger', |
||
248 | ]; |
||
249 | |||
250 | return $this->printTree($triggers, $attrs, 'triggers'); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Function to save after altering a trigger. |
||
255 | */ |
||
256 | public function doSaveAlter() |
||
257 | { |
||
258 | $conf = $this->conf; |
||
259 | |||
260 | $lang = $this->lang; |
||
261 | $data = $this->misc->getDatabaseAccessor(); |
||
262 | |||
263 | $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']); |
||
264 | if (0 == $status) { |
||
265 | $this->doDefault($lang['strtriggeraltered']); |
||
266 | } else { |
||
267 | $this->doAlter($lang['strtriggeralteredbad']); |
||
268 | } |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Function to allow altering of a trigger. |
||
273 | * |
||
274 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
275 | */ |
||
276 | public function doAlter($msg = '') |
||
310 | } |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Show confirmation of drop and perform actual drop. |
||
315 | * |
||
316 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
317 | */ |
||
318 | public function doDrop($confirm) |
||
319 | { |
||
320 | $conf = $this->conf; |
||
321 | |||
322 | $lang = $this->lang; |
||
323 | $data = $this->misc->getDatabaseAccessor(); |
||
324 | |||
325 | if ($confirm) { |
||
326 | $this->printTrail('trigger'); |
||
327 | $this->printTitle($lang['strdrop'], 'pg.trigger.drop'); |
||
328 | |||
329 | echo '<p>', sprintf( |
||
330 | $lang['strconfdroptrigger'], |
||
331 | $this->misc->printVal($_REQUEST['trigger']), |
||
332 | $this->misc->printVal($_REQUEST['table']) |
||
333 | ), "</p>\n"; |
||
334 | |||
335 | echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n"; |
||
336 | echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
||
337 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
338 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
339 | echo $this->misc->form; |
||
340 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n"; |
||
341 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
342 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
343 | echo "</form>\n"; |
||
344 | } else { |
||
345 | $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade'])); |
||
346 | if (0 == $status) { |
||
347 | $this->doDefault($lang['strtriggerdropped']); |
||
348 | } else { |
||
349 | $this->doDefault($lang['strtriggerdroppedbad']); |
||
350 | } |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Show confirmation of enable trigger and perform enabling the trigger. |
||
356 | * |
||
357 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
358 | */ |
||
359 | public function doEnable($confirm) |
||
360 | { |
||
361 | $conf = $this->conf; |
||
362 | |||
363 | $lang = $this->lang; |
||
364 | $data = $this->misc->getDatabaseAccessor(); |
||
365 | |||
366 | if ($confirm) { |
||
367 | $this->printTrail('trigger'); |
||
368 | $this->printTitle($lang['strenable'], 'pg.table.alter'); |
||
369 | |||
370 | echo '<p>', sprintf( |
||
371 | $lang['strconfenabletrigger'], |
||
372 | $this->misc->printVal($_REQUEST['trigger']), |
||
373 | $this->misc->printVal($_REQUEST['table']) |
||
374 | ), "</p>\n"; |
||
375 | |||
376 | echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n"; |
||
377 | echo "<input type=\"hidden\" name=\"action\" value=\"enable\" />\n"; |
||
378 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
379 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
380 | echo $this->misc->form; |
||
381 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
382 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
383 | echo "</form>\n"; |
||
384 | } else { |
||
385 | $status = $data->enableTrigger($_POST['trigger'], $_POST['table']); |
||
386 | if (0 == $status) { |
||
387 | $this->doDefault($lang['strtriggerenabled']); |
||
388 | } else { |
||
389 | $this->doDefault($lang['strtriggerenabledbad']); |
||
390 | } |
||
391 | } |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Show confirmation of disable trigger and perform disabling the trigger. |
||
396 | * |
||
397 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
398 | */ |
||
399 | public function doDisable($confirm) |
||
400 | { |
||
401 | $conf = $this->conf; |
||
402 | |||
403 | $lang = $this->lang; |
||
404 | $data = $this->misc->getDatabaseAccessor(); |
||
405 | |||
406 | if ($confirm) { |
||
407 | $this->printTrail('trigger'); |
||
408 | $this->printTitle($lang['strdisable'], 'pg.table.alter'); |
||
409 | |||
410 | echo '<p>', sprintf( |
||
411 | $lang['strconfdisabletrigger'], |
||
412 | $this->misc->printVal($_REQUEST['trigger']), |
||
413 | $this->misc->printVal($_REQUEST['table']) |
||
414 | ), "</p>\n"; |
||
415 | |||
416 | echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n"; |
||
417 | echo "<input type=\"hidden\" name=\"action\" value=\"disable\" />\n"; |
||
418 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
419 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
420 | echo $this->misc->form; |
||
421 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
422 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
423 | echo "</form>\n"; |
||
424 | } else { |
||
425 | $status = $data->disableTrigger($_POST['trigger'], $_POST['table']); |
||
426 | if (0 == $status) { |
||
427 | $this->doDefault($lang['strtriggerdisabled']); |
||
428 | } else { |
||
429 | $this->doDefault($lang['strtriggerdisabledbad']); |
||
430 | } |
||
431 | } |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Let them create s.th. |
||
436 | * |
||
437 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
438 | */ |
||
439 | public function doCreate($msg = '') |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Actually creates the new trigger in the database. |
||
510 | */ |
||
511 | public function doSaveCreate() |
||
540 | } |
||
541 | } |
||
542 | } |
||
543 | } |
||
544 |