| Total Complexity | 43 |
| Total Lines | 506 |
| 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() |
||
| 22 | { |
||
| 23 | $lang = $this->lang; |
||
| 24 | |||
| 25 | $action = $this->action; |
||
| 26 | if ('tree' == $action) { |
||
| 27 | return $this->doTree(); |
||
| 28 | } |
||
| 29 | |||
| 30 | $this->printHeader($lang['strtables'] . ' - ' . $_REQUEST['table'] . ' - ' . $lang['strtriggers']); |
||
| 31 | $this->printBody(); |
||
| 32 | |||
| 33 | switch ($action) { |
||
| 34 | case 'alter': |
||
| 35 | if (isset($_POST['alter'])) { |
||
| 36 | $this->doSaveAlter(); |
||
| 37 | } else { |
||
| 38 | $this->doDefault(); |
||
| 39 | } |
||
| 40 | |||
| 41 | break; |
||
| 42 | case 'confirm_alter': |
||
| 43 | $this->doAlter(); |
||
| 44 | |||
| 45 | break; |
||
| 46 | case 'confirm_enable': |
||
| 47 | $this->doEnable(true); |
||
| 48 | |||
| 49 | break; |
||
| 50 | case 'confirm_disable': |
||
| 51 | $this->doDisable(true); |
||
| 52 | |||
| 53 | break; |
||
| 54 | case 'save_create': |
||
| 55 | if (isset($_POST['cancel'])) { |
||
| 56 | $this->doDefault(); |
||
| 57 | } else { |
||
| 58 | $this->doSaveCreate(); |
||
| 59 | } |
||
| 60 | |||
| 61 | break; |
||
| 62 | case 'create': |
||
| 63 | $this->doCreate(); |
||
| 64 | |||
| 65 | break; |
||
| 66 | case 'drop': |
||
| 67 | if (isset($_POST['yes'])) { |
||
| 68 | $this->doDrop(false); |
||
| 69 | } else { |
||
| 70 | $this->doDefault(); |
||
| 71 | } |
||
| 72 | |||
| 73 | break; |
||
| 74 | case 'confirm_drop': |
||
| 75 | $this->doDrop(true); |
||
| 76 | |||
| 77 | break; |
||
| 78 | case 'enable': |
||
| 79 | if (isset($_POST['yes'])) { |
||
| 80 | $this->doEnable(false); |
||
| 81 | } else { |
||
| 82 | $this->doDefault(); |
||
| 83 | } |
||
| 84 | |||
| 85 | break; |
||
| 86 | case 'disable': |
||
| 87 | if (isset($_POST['yes'])) { |
||
| 88 | $this->doDisable(false); |
||
| 89 | } else { |
||
| 90 | $this->doDefault(); |
||
| 91 | } |
||
| 92 | |||
| 93 | break; |
||
| 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 |
||
|
1 ignored issue
–
show
|
|||
| 107 | */ |
||
| 108 | public function doDefault($msg = '') |
||
| 109 | { |
||
| 110 | $lang = $this->lang; |
||
| 111 | $data = $this->misc->getDatabaseAccessor(); |
||
| 112 | |||
| 113 | $tgPre = function (&$rowdata, $actions) use ($data) { |
||
| 114 | // toggle enable/disable trigger per trigger |
||
| 115 | if (!$data->phpBool($rowdata->fields['tgenabled'])) { |
||
| 116 | unset($actions['disable']); |
||
| 117 | } else { |
||
| 118 | unset($actions['enable']); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $actions; |
||
| 122 | }; |
||
| 123 | |||
| 124 | $this->printTrail('table'); |
||
| 125 | $this->printTabs('table', 'triggers'); |
||
| 126 | $this->printMsg($msg); |
||
| 127 | |||
| 128 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
| 129 | |||
| 130 | $columns = [ |
||
| 131 | 'trigger' => [ |
||
| 132 | 'title' => $lang['strname'], |
||
| 133 | 'field' => Decorator::field('tgname'), |
||
| 134 | ], |
||
| 135 | 'definition' => [ |
||
| 136 | 'title' => $lang['strdefinition'], |
||
| 137 | 'field' => Decorator::field('tgdef'), |
||
| 138 | ], |
||
| 139 | 'function' => [ |
||
| 140 | 'title' => $lang['strfunction'], |
||
| 141 | 'field' => Decorator::field('proproto'), |
||
| 142 | 'url' => "functions.php?action=properties&server={$_REQUEST['server']}&database={$_REQUEST['database']}&", |
||
| 143 | 'vars' => [ |
||
| 144 | 'schema' => 'pronamespace', |
||
| 145 | 'function' => 'proproto', |
||
| 146 | 'function_oid' => 'prooid', |
||
| 147 | ], |
||
| 148 | ], |
||
| 149 | 'actions' => [ |
||
| 150 | 'title' => $lang['stractions'], |
||
| 151 | ], |
||
| 152 | ]; |
||
| 153 | |||
| 154 | $actions = [ |
||
| 155 | 'alter' => [ |
||
| 156 | 'content' => $lang['stralter'], |
||
| 157 | 'attr' => [ |
||
| 158 | 'href' => [ |
||
| 159 | 'url' => 'triggers.php', |
||
| 160 | 'urlvars' => [ |
||
| 161 | 'action' => 'confirm_alter', |
||
| 162 | 'table' => $_REQUEST['table'], |
||
| 163 | 'trigger' => Decorator::field('tgname'), |
||
| 164 | ], |
||
| 165 | ], |
||
| 166 | ], |
||
| 167 | ], |
||
| 168 | 'drop' => [ |
||
| 169 | 'content' => $lang['strdrop'], |
||
| 170 | 'attr' => [ |
||
| 171 | 'href' => [ |
||
| 172 | 'url' => 'triggers.php', |
||
| 173 | 'urlvars' => [ |
||
| 174 | 'action' => 'confirm_drop', |
||
| 175 | 'table' => $_REQUEST['table'], |
||
| 176 | 'trigger' => Decorator::field('tgname'), |
||
| 177 | ], |
||
| 178 | ], |
||
| 179 | ], |
||
| 180 | ], |
||
| 181 | ]; |
||
| 182 | if ($data->hasDisableTriggers()) { |
||
| 183 | $actions['enable'] = [ |
||
| 184 | 'content' => $lang['strenable'], |
||
| 185 | 'attr' => [ |
||
| 186 | 'href' => [ |
||
| 187 | 'url' => 'triggers.php', |
||
| 188 | 'urlvars' => [ |
||
| 189 | 'action' => 'confirm_enable', |
||
| 190 | 'table' => $_REQUEST['table'], |
||
| 191 | 'trigger' => Decorator::field('tgname'), |
||
| 192 | ], |
||
| 193 | ], |
||
| 194 | ], |
||
| 195 | ]; |
||
| 196 | $actions['disable'] = [ |
||
| 197 | 'content' => $lang['strdisable'], |
||
| 198 | 'attr' => [ |
||
| 199 | 'href' => [ |
||
| 200 | 'url' => 'triggers.php', |
||
| 201 | 'urlvars' => [ |
||
| 202 | 'action' => 'confirm_disable', |
||
| 203 | 'table' => $_REQUEST['table'], |
||
| 204 | 'trigger' => Decorator::field('tgname'), |
||
| 205 | ], |
||
| 206 | ], |
||
| 207 | ], |
||
| 208 | ]; |
||
| 209 | } |
||
| 210 | |||
| 211 | echo $this->printTable($triggers, $columns, $actions, 'triggers-triggers', $lang['strnotriggers'], $tgPre); |
||
| 212 | |||
| 213 | $this->printNavLinks(['create' => [ |
||
|
1 ignored issue
–
show
|
|||
| 214 | 'attr' => [ |
||
| 215 | 'href' => [ |
||
| 216 | 'url' => 'triggers.php', |
||
| 217 | 'urlvars' => [ |
||
| 218 | 'action' => 'create', |
||
| 219 | 'server' => $_REQUEST['server'], |
||
| 220 | 'database' => $_REQUEST['database'], |
||
| 221 | 'schema' => $_REQUEST['schema'], |
||
| 222 | 'table' => $_REQUEST['table'], |
||
| 223 | ], |
||
| 224 | ], |
||
| 225 | ], |
||
| 226 | 'content' => $lang['strcreatetrigger'], |
||
| 227 | ]], 'triggers-triggers', get_defined_vars()); |
||
|
1 ignored issue
–
show
|
|||
| 228 | } |
||
| 229 | |||
| 230 | public function doTree() |
||
|
1 ignored issue
–
show
|
|||
| 231 | { |
||
| 232 | $lang = $this->lang; |
||
| 233 | $data = $this->misc->getDatabaseAccessor(); |
||
| 234 | |||
| 235 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
| 236 | |||
| 237 | $reqvars = $this->misc->getRequestVars('table'); |
||
| 238 | |||
| 239 | $attrs = [ |
||
| 240 | 'text' => Decorator::field('tgname'), |
||
| 241 | 'icon' => 'Trigger', |
||
| 242 | ]; |
||
| 243 | |||
| 244 | return $this->printTree($triggers, $attrs, 'triggers'); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Function to save after altering a trigger. |
||
| 249 | */ |
||
| 250 | public function doSaveAlter() |
||
| 251 | { |
||
| 252 | $lang = $this->lang; |
||
| 253 | $data = $this->misc->getDatabaseAccessor(); |
||
| 254 | |||
| 255 | $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']); |
||
| 256 | if (0 == $status) { |
||
| 257 | $this->doDefault($lang['strtriggeraltered']); |
||
| 258 | } else { |
||
| 259 | $this->doAlter($lang['strtriggeralteredbad']); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Function to allow altering of a trigger. |
||
| 265 | * |
||
| 266 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 267 | */ |
||
| 268 | public function doAlter($msg = '') |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Show confirmation of drop and perform actual drop. |
||
| 305 | * |
||
| 306 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 307 | */ |
||
| 308 | public function doDrop($confirm) |
||
| 309 | { |
||
| 310 | $lang = $this->lang; |
||
| 311 | $data = $this->misc->getDatabaseAccessor(); |
||
| 312 | |||
| 313 | if ($confirm) { |
||
| 314 | $this->printTrail('trigger'); |
||
| 315 | $this->printTitle($lang['strdrop'], 'pg.trigger.drop'); |
||
| 316 | |||
| 317 | echo '<p>', sprintf( |
||
| 318 | $lang['strconfdroptrigger'], |
||
| 319 | $this->misc->printVal($_REQUEST['trigger']), |
||
| 320 | $this->misc->printVal($_REQUEST['table']) |
||
| 321 | ), "</p>\n"; |
||
| 322 | |||
| 323 | echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n"; |
||
| 324 | echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
||
| 325 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 326 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
| 327 | echo $this->misc->form; |
||
| 328 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n"; |
||
| 329 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
| 330 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
| 331 | echo "</form>\n"; |
||
| 332 | } else { |
||
| 333 | $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade'])); |
||
| 334 | if (0 == $status) { |
||
| 335 | $this->doDefault($lang['strtriggerdropped']); |
||
| 336 | } else { |
||
| 337 | $this->doDefault($lang['strtriggerdroppedbad']); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Show confirmation of enable trigger and perform enabling the trigger. |
||
| 344 | * |
||
| 345 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 346 | */ |
||
| 347 | public function doEnable($confirm) |
||
| 348 | { |
||
| 349 | $lang = $this->lang; |
||
| 350 | $data = $this->misc->getDatabaseAccessor(); |
||
| 351 | |||
| 352 | if ($confirm) { |
||
| 353 | $this->printTrail('trigger'); |
||
| 354 | $this->printTitle($lang['strenable'], 'pg.table.alter'); |
||
| 355 | |||
| 356 | echo '<p>', sprintf( |
||
| 357 | $lang['strconfenabletrigger'], |
||
| 358 | $this->misc->printVal($_REQUEST['trigger']), |
||
| 359 | $this->misc->printVal($_REQUEST['table']) |
||
| 360 | ), "</p>\n"; |
||
| 361 | |||
| 362 | echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n"; |
||
| 363 | echo "<input type=\"hidden\" name=\"action\" value=\"enable\" />\n"; |
||
| 364 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 365 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
| 366 | echo $this->misc->form; |
||
| 367 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
| 368 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
| 369 | echo "</form>\n"; |
||
| 370 | } else { |
||
| 371 | $status = $data->enableTrigger($_POST['trigger'], $_POST['table']); |
||
| 372 | if (0 == $status) { |
||
| 373 | $this->doDefault($lang['strtriggerenabled']); |
||
| 374 | } else { |
||
| 375 | $this->doDefault($lang['strtriggerenabledbad']); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Show confirmation of disable trigger and perform disabling the trigger. |
||
| 382 | * |
||
| 383 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 384 | */ |
||
| 385 | public function doDisable($confirm) |
||
| 386 | { |
||
| 387 | $lang = $this->lang; |
||
| 388 | $data = $this->misc->getDatabaseAccessor(); |
||
| 389 | |||
| 390 | if ($confirm) { |
||
| 391 | $this->printTrail('trigger'); |
||
| 392 | $this->printTitle($lang['strdisable'], 'pg.table.alter'); |
||
| 393 | |||
| 394 | echo '<p>', sprintf( |
||
| 395 | $lang['strconfdisabletrigger'], |
||
| 396 | $this->misc->printVal($_REQUEST['trigger']), |
||
| 397 | $this->misc->printVal($_REQUEST['table']) |
||
| 398 | ), "</p>\n"; |
||
| 399 | |||
| 400 | echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n"; |
||
| 401 | echo "<input type=\"hidden\" name=\"action\" value=\"disable\" />\n"; |
||
| 402 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 403 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
| 404 | echo $this->misc->form; |
||
| 405 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
| 406 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
| 407 | echo "</form>\n"; |
||
| 408 | } else { |
||
| 409 | $status = $data->disableTrigger($_POST['trigger'], $_POST['table']); |
||
| 410 | if (0 == $status) { |
||
| 411 | $this->doDefault($lang['strtriggerdisabled']); |
||
| 412 | } else { |
||
| 413 | $this->doDefault($lang['strtriggerdisabledbad']); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Let them create s.th. |
||
| 420 | * |
||
| 421 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 422 | */ |
||
| 423 | public function doCreate($msg = '') |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Actually creates the new trigger in the database. |
||
| 492 | */ |
||
| 493 | public function doSaveCreate() |
||
| 520 | } |
||
| 521 | } |
||
| 522 | } |
||
| 523 | } |
||
| 524 |