| Total Complexity | 43 |
| Total Lines | 517 |
| 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 | public function render() |
||
|
1 ignored issue
–
show
|
|||
| 19 | { |
||
| 20 | $conf = $this->conf; |
||
| 21 | |||
| 22 | $lang = $this->lang; |
||
| 23 | |||
| 24 | $action = $this->action; |
||
| 25 | if ('tree' == $action) { |
||
| 26 | return $this->doTree(); |
||
| 27 | } |
||
| 28 | |||
| 29 | $this->printHeader($lang['strtables'] . ' - ' . $_REQUEST['table'] . ' - ' . $lang['strtriggers']); |
||
| 30 | $this->printBody(); |
||
| 31 | |||
| 32 | switch ($action) { |
||
| 33 | case 'alter': |
||
| 34 | if (isset($_POST['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 (isset($_POST['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 | default: |
||
| 94 | $this->doDefault(); |
||
| 95 | |||
| 96 | break; |
||
| 97 | } |
||
| 98 | |||
| 99 | return $this->printFooter(); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * List all the triggers on the table |
||
| 104 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 105 | */ |
||
| 106 | public function doDefault($msg = '') |
||
| 107 | { |
||
| 108 | $conf = $this->conf; |
||
| 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 | $conf = $this->conf; |
||
| 233 | |||
| 234 | $lang = $this->lang; |
||
| 235 | $data = $this->misc->getDatabaseAccessor(); |
||
| 236 | |||
| 237 | $triggers = $data->getTriggers($_REQUEST['table']); |
||
| 238 | |||
| 239 | $reqvars = $this->misc->getRequestVars('table'); |
||
| 240 | |||
| 241 | $attrs = [ |
||
| 242 | 'text' => Decorator::field('tgname'), |
||
| 243 | 'icon' => 'Trigger', |
||
| 244 | ]; |
||
| 245 | |||
| 246 | return $this->printTree($triggers, $attrs, 'triggers'); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Function to save after altering a trigger |
||
| 251 | */ |
||
| 252 | public function doSaveAlter() |
||
| 253 | { |
||
| 254 | $conf = $this->conf; |
||
| 255 | |||
| 256 | $lang = $this->lang; |
||
| 257 | $data = $this->misc->getDatabaseAccessor(); |
||
| 258 | |||
| 259 | $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']); |
||
| 260 | if (0 == $status) { |
||
| 261 | $this->doDefault($lang['strtriggeraltered']); |
||
| 262 | } else { |
||
| 263 | $this->doAlter($lang['strtriggeralteredbad']); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Function to allow altering of a trigger |
||
| 269 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 270 | */ |
||
| 271 | public function doAlter($msg = '') |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Show confirmation of drop and perform actual drop |
||
| 310 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 311 | */ |
||
| 312 | public function doDrop($confirm) |
||
| 313 | { |
||
| 314 | $conf = $this->conf; |
||
| 315 | |||
| 316 | $lang = $this->lang; |
||
| 317 | $data = $this->misc->getDatabaseAccessor(); |
||
| 318 | |||
| 319 | if ($confirm) { |
||
| 320 | $this->printTrail('trigger'); |
||
| 321 | $this->printTitle($lang['strdrop'], 'pg.trigger.drop'); |
||
| 322 | |||
| 323 | echo '<p>', sprintf( |
||
| 324 | $lang['strconfdroptrigger'], |
||
| 325 | $this->misc->printVal($_REQUEST['trigger']), |
||
| 326 | $this->misc->printVal($_REQUEST['table']) |
||
| 327 | ), "</p>\n"; |
||
| 328 | |||
| 329 | echo '<form action="' . SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n"; |
||
| 330 | echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
||
| 331 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 332 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
| 333 | echo $this->misc->form; |
||
| 334 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n"; |
||
| 335 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
| 336 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
| 337 | echo "</form>\n"; |
||
| 338 | } else { |
||
| 339 | $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade'])); |
||
| 340 | if (0 == $status) { |
||
| 341 | $this->doDefault($lang['strtriggerdropped']); |
||
| 342 | } else { |
||
| 343 | $this->doDefault($lang['strtriggerdroppedbad']); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Show confirmation of enable trigger and perform enabling the trigger |
||
| 350 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 351 | */ |
||
| 352 | public function doEnable($confirm) |
||
| 353 | { |
||
| 354 | $conf = $this->conf; |
||
| 355 | |||
| 356 | $lang = $this->lang; |
||
| 357 | $data = $this->misc->getDatabaseAccessor(); |
||
| 358 | |||
| 359 | if ($confirm) { |
||
| 360 | $this->printTrail('trigger'); |
||
| 361 | $this->printTitle($lang['strenable'], 'pg.table.alter'); |
||
| 362 | |||
| 363 | echo '<p>', sprintf( |
||
| 364 | $lang['strconfenabletrigger'], |
||
| 365 | $this->misc->printVal($_REQUEST['trigger']), |
||
| 366 | $this->misc->printVal($_REQUEST['table']) |
||
| 367 | ), "</p>\n"; |
||
| 368 | |||
| 369 | echo '<form action="' . SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n"; |
||
| 370 | echo "<input type=\"hidden\" name=\"action\" value=\"enable\" />\n"; |
||
| 371 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 372 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
| 373 | echo $this->misc->form; |
||
| 374 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
| 375 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
| 376 | echo "</form>\n"; |
||
| 377 | } else { |
||
| 378 | $status = $data->enableTrigger($_POST['trigger'], $_POST['table']); |
||
| 379 | if (0 == $status) { |
||
| 380 | $this->doDefault($lang['strtriggerenabled']); |
||
| 381 | } else { |
||
| 382 | $this->doDefault($lang['strtriggerenabledbad']); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Show confirmation of disable trigger and perform disabling the trigger |
||
| 389 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 390 | */ |
||
| 391 | public function doDisable($confirm) |
||
| 392 | { |
||
| 393 | $conf = $this->conf; |
||
| 394 | |||
| 395 | $lang = $this->lang; |
||
| 396 | $data = $this->misc->getDatabaseAccessor(); |
||
| 397 | |||
| 398 | if ($confirm) { |
||
| 399 | $this->printTrail('trigger'); |
||
| 400 | $this->printTitle($lang['strdisable'], 'pg.table.alter'); |
||
| 401 | |||
| 402 | echo '<p>', sprintf( |
||
| 403 | $lang['strconfdisabletrigger'], |
||
| 404 | $this->misc->printVal($_REQUEST['trigger']), |
||
| 405 | $this->misc->printVal($_REQUEST['table']) |
||
| 406 | ), "</p>\n"; |
||
| 407 | |||
| 408 | echo '<form action="' . SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n"; |
||
| 409 | echo "<input type=\"hidden\" name=\"action\" value=\"disable\" />\n"; |
||
| 410 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 411 | echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n"; |
||
| 412 | echo $this->misc->form; |
||
| 413 | echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n"; |
||
| 414 | echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n"; |
||
| 415 | echo "</form>\n"; |
||
| 416 | } else { |
||
| 417 | $status = $data->disableTrigger($_POST['trigger'], $_POST['table']); |
||
| 418 | if (0 == $status) { |
||
| 419 | $this->doDefault($lang['strtriggerdisabled']); |
||
| 420 | } else { |
||
| 421 | $this->doDefault($lang['strtriggerdisabledbad']); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Let them create s.th. |
||
| 428 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 429 | */ |
||
| 430 | public function doCreate($msg = '') |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Actually creates the new trigger in the database |
||
| 501 | */ |
||
| 502 | public function doSaveCreate() |
||
| 531 | } |
||
| 532 | } |
||
| 533 | } |
||
| 534 | } |
||
| 535 |