| Total Complexity | 60 |
| Total Lines | 581 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AlldbController 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 AlldbController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class AlldbController extends BaseController |
||
| 17 | { |
||
| 18 | public $table_place = 'alldb-databases'; |
||
| 19 | public $controller_title = 'strdatabases'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Default method to render the controller according to the action parameter. |
||
| 23 | */ |
||
| 24 | public function render() |
||
| 25 | { |
||
| 26 | if ('tree' == $this->action) { |
||
| 27 | return $this->doTree(); |
||
| 28 | } |
||
| 29 | |||
| 30 | $header_template = 'header.twig'; |
||
| 31 | |||
| 32 | ob_start(); |
||
| 33 | switch ($this->action) { |
||
| 34 | case 'export': |
||
| 35 | $this->doExport(); |
||
| 36 | |||
| 37 | break; |
||
| 38 | case 'save_create': |
||
| 39 | if (isset($_POST['cancel'])) { |
||
| 40 | $this->doDefault(); |
||
| 41 | } else { |
||
| 42 | $this->doSaveCreate(); |
||
| 43 | } |
||
| 44 | |||
| 45 | break; |
||
| 46 | case 'create': |
||
| 47 | $this->doCreate(); |
||
| 48 | |||
| 49 | break; |
||
| 50 | case 'drop': |
||
| 51 | if (isset($_REQUEST['drop'])) { |
||
| 52 | $this->doDrop(false); |
||
| 53 | } else { |
||
| 54 | $this->doDefault(); |
||
| 55 | } |
||
| 56 | |||
| 57 | break; |
||
| 58 | case 'confirm_drop': |
||
| 59 | $this->doDrop(true); |
||
| 60 | |||
| 61 | break; |
||
| 62 | case 'alter': |
||
| 63 | if (isset($_POST['oldname'], $_POST['newname']) && !isset($_POST['cancel'])) { |
||
| 64 | $this->doAlter(false); |
||
| 65 | } else { |
||
| 66 | $this->doDefault(); |
||
| 67 | } |
||
| 68 | |||
| 69 | break; |
||
| 70 | case 'confirm_alter': |
||
| 71 | $this->doAlter(true); |
||
| 72 | |||
| 73 | break; |
||
| 74 | default: |
||
| 75 | $header_template = 'header_datatables.twig'; |
||
| 76 | $this->doDefault(); |
||
| 77 | |||
| 78 | break; |
||
| 79 | } |
||
| 80 | $output = ob_get_clean(); |
||
| 81 | |||
| 82 | $this->printHeader($this->headerTitle(), null, true, $header_template); |
||
| 83 | $this->printBody(); |
||
| 84 | echo $output; |
||
| 85 | |||
| 86 | return $this->printFooter(); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Show default list of databases in the server. |
||
| 91 | * |
||
| 92 | * @param mixed $msg |
||
| 93 | */ |
||
| 94 | public function doDefault($msg = '') |
||
| 95 | { |
||
| 96 | $this->printTrail('server'); |
||
| 97 | $this->printTabs('server', 'databases'); |
||
| 98 | $this->printMsg($msg); |
||
| 99 | $data = $this->misc->getDatabaseAccessor(); |
||
| 100 | |||
| 101 | $databases = $data->getDatabases(); |
||
| 102 | |||
| 103 | $this->misc->setReloadBrowser(true); |
||
| 104 | |||
| 105 | $href = $this->misc->getHREF(); |
||
| 106 | |||
| 107 | $columns = [ |
||
| 108 | 'database' => [ |
||
| 109 | 'title' => $this->lang['strdatabase'], |
||
| 110 | 'field' => Decorator::field('datname'), |
||
| 111 | 'url' => \SUBFOLDER . "/redirect/database?{$href}&", |
||
| 112 | 'vars' => ['database' => 'datname'], |
||
| 113 | ], |
||
| 114 | 'owner' => [ |
||
| 115 | 'title' => $this->lang['strowner'], |
||
| 116 | 'field' => Decorator::field('datowner'), |
||
| 117 | ], |
||
| 118 | 'encoding' => [ |
||
| 119 | 'title' => $this->lang['strencoding'], |
||
| 120 | 'field' => Decorator::field('datencoding'), |
||
| 121 | ], |
||
| 122 | |||
| 123 | 'tablespace' => [ |
||
| 124 | 'title' => $this->lang['strtablespace'], |
||
| 125 | 'field' => Decorator::field('tablespace'), |
||
| 126 | ], |
||
| 127 | 'dbsize' => [ |
||
| 128 | 'title' => $this->lang['strsize'], |
||
| 129 | 'field' => Decorator::field('dbsize'), |
||
| 130 | 'type' => 'prettysize', |
||
| 131 | ], |
||
| 132 | 'lc_collate' => [ |
||
| 133 | 'title' => $this->lang['strcollation'], |
||
| 134 | 'field' => Decorator::field('datcollate'), |
||
| 135 | ], |
||
| 136 | 'lc_ctype' => [ |
||
| 137 | 'title' => $this->lang['strctype'], |
||
| 138 | 'field' => Decorator::field('datctype'), |
||
| 139 | ], |
||
| 140 | 'actions' => [ |
||
| 141 | 'title' => $this->lang['stractions'], |
||
| 142 | ], |
||
| 143 | 'comment' => [ |
||
| 144 | 'title' => $this->lang['strcomment'], |
||
| 145 | 'field' => Decorator::field('datcomment'), |
||
| 146 | ], |
||
| 147 | ]; |
||
| 148 | |||
| 149 | $actions = [ |
||
| 150 | 'multiactions' => [ |
||
| 151 | 'keycols' => ['database' => 'datname'], |
||
| 152 | 'url' => 'alldb', |
||
| 153 | 'default' => null, |
||
| 154 | ], |
||
| 155 | 'drop' => [ |
||
| 156 | 'content' => $this->lang['strdrop'], |
||
| 157 | 'attr' => [ |
||
| 158 | 'href' => [ |
||
| 159 | 'url' => 'alldb', |
||
| 160 | 'urlvars' => [ |
||
| 161 | 'subject' => 'database', |
||
| 162 | 'action' => 'confirm_drop', |
||
| 163 | 'dropdatabase' => Decorator::field('datname'), |
||
| 164 | ], |
||
| 165 | ], |
||
| 166 | ], |
||
| 167 | 'multiaction' => 'confirm_drop', |
||
| 168 | ], |
||
| 169 | 'privileges' => [ |
||
| 170 | 'content' => $this->lang['strprivileges'], |
||
| 171 | 'attr' => [ |
||
| 172 | 'href' => [ |
||
| 173 | 'url' => 'privileges', |
||
| 174 | 'urlvars' => [ |
||
| 175 | 'subject' => 'database', |
||
| 176 | 'database' => Decorator::field('datname'), |
||
| 177 | ], |
||
| 178 | ], |
||
| 179 | ], |
||
| 180 | ], |
||
| 181 | ]; |
||
| 182 | if ($data->hasAlterDatabase()) { |
||
| 183 | $actions['alter'] = [ |
||
| 184 | 'content' => $this->lang['stralter'], |
||
| 185 | 'attr' => [ |
||
| 186 | 'href' => [ |
||
| 187 | 'url' => 'alldb', |
||
| 188 | 'urlvars' => [ |
||
| 189 | 'subject' => 'database', |
||
| 190 | 'action' => 'confirm_alter', |
||
| 191 | 'alterdatabase' => Decorator::field('datname'), |
||
| 192 | ], |
||
| 193 | ], |
||
| 194 | ], |
||
| 195 | ]; |
||
| 196 | } |
||
| 197 | |||
| 198 | if (!$data->hasTablespaces()) { |
||
| 199 | unset($columns['tablespace']); |
||
| 200 | } |
||
| 201 | |||
| 202 | if (!$data->hasServerAdminFuncs()) { |
||
| 203 | unset($columns['dbsize']); |
||
| 204 | } |
||
| 205 | |||
| 206 | if (!$data->hasDatabaseCollation()) { |
||
| 207 | unset($columns['lc_collate'], $columns['lc_ctype']); |
||
| 208 | } |
||
| 209 | |||
| 210 | if (!isset($data->privlist['database'])) { |
||
| 211 | unset($actions['privileges']); |
||
| 212 | } |
||
| 213 | |||
| 214 | echo $this->printTable($databases, $columns, $actions, $this->table_place, $this->lang['strnodatabases']); |
||
| 215 | |||
| 216 | $navlinks = [ |
||
| 217 | 'create' => [ |
||
| 218 | 'attr' => [ |
||
| 219 | 'href' => [ |
||
| 220 | 'url' => 'alldb', |
||
| 221 | 'urlvars' => [ |
||
| 222 | 'action' => 'create', |
||
| 223 | 'server' => $_REQUEST['server'], |
||
| 224 | ], |
||
| 225 | ], |
||
| 226 | ], |
||
| 227 | 'content' => $this->lang['strcreatedatabase'], |
||
| 228 | ], |
||
| 229 | ]; |
||
| 230 | $this->printNavLinks($navlinks, $this->table_place, get_defined_vars()); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function doTree() |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Display a form for alter and perform actual alter. |
||
| 254 | * |
||
| 255 | * @param mixed $confirm |
||
| 256 | */ |
||
| 257 | public function doAlter($confirm) |
||
| 258 | { |
||
| 259 | $data = $this->misc->getDatabaseAccessor(); |
||
| 260 | |||
| 261 | if ($confirm) { |
||
| 262 | $this->printTrail('database'); |
||
| 263 | $this->printTitle($this->lang['stralter'], 'pg.database.alter'); |
||
| 264 | |||
| 265 | echo '<form action="' . \SUBFOLDER . "/src/views/alldb\" method=\"post\">\n"; |
||
| 266 | echo "<table>\n"; |
||
| 267 | echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n"; |
||
| 268 | echo '<td class="data1">'; |
||
| 269 | echo "<input name=\"newname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 270 | htmlspecialchars($_REQUEST['alterdatabase']), "\" /></td></tr>\n"; |
||
| 271 | |||
| 272 | if ($data->hasAlterDatabaseOwner() && $data->isSuperUser()) { |
||
| 273 | // Fetch all users |
||
| 274 | |||
| 275 | $rs = $data->getDatabaseOwner($_REQUEST['alterdatabase']); |
||
| 276 | $owner = isset($rs->fields['usename']) ? $rs->fields['usename'] : ''; |
||
| 277 | $users = $data->getUsers(); |
||
| 278 | |||
| 279 | echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>\n"; |
||
| 280 | echo '<td class="data1"><select name="owner">'; |
||
| 281 | while (!$users->EOF) { |
||
| 282 | $uname = $users->fields['usename']; |
||
| 283 | echo '<option value="', htmlspecialchars($uname), '"', |
||
| 284 | ($uname == $owner) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n"; |
||
| 285 | $users->moveNext(); |
||
| 286 | } |
||
| 287 | echo "</select></td></tr>\n"; |
||
| 288 | } |
||
| 289 | if ($data->hasSharedComments()) { |
||
| 290 | $rs = $data->getDatabaseComment($_REQUEST['alterdatabase']); |
||
| 291 | $comment = isset($rs->fields['description']) ? $rs->fields['description'] : ''; |
||
| 292 | echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>\n"; |
||
| 293 | echo '<td class="data1">'; |
||
| 294 | echo '<textarea rows="3" cols="32" name="dbcomment">', |
||
| 295 | htmlspecialchars($comment), "</textarea></td></tr>\n"; |
||
| 296 | } |
||
| 297 | echo "</table>\n"; |
||
| 298 | echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n"; |
||
| 299 | echo $this->misc->form; |
||
| 300 | echo '<input type="hidden" name="oldname" value="', |
||
| 301 | htmlspecialchars($_REQUEST['alterdatabase']), "\" />\n"; |
||
| 302 | echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n"; |
||
| 303 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n"; |
||
| 304 | echo "</form>\n"; |
||
| 305 | } else { |
||
| 306 | $this->coalesceArr($_POST, 'owner', ''); |
||
| 307 | |||
| 308 | $this->coalesceArr($_POST, 'dbcomment', ''); |
||
| 309 | |||
| 310 | if (0 == $data->alterDatabase($_POST['oldname'], $_POST['newname'], $_POST['owner'], $_POST['dbcomment'])) { |
||
| 311 | $this->misc->setReloadBrowser(true); |
||
| 312 | $this->doDefault($this->lang['strdatabasealtered']); |
||
| 313 | } else { |
||
| 314 | $this->doDefault($this->lang['strdatabasealteredbad']); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Show confirmation of drop and perform actual drop. |
||
| 321 | * |
||
| 322 | * @param mixed $confirm |
||
| 323 | */ |
||
| 324 | public function doDrop($confirm) |
||
| 379 | } |
||
| 380 | } |
||
| 381 | //END DROP |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | // END FUNCTION |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Displays a screen where they can enter a new database. |
||
| 389 | * |
||
| 390 | * @param mixed $msg |
||
| 391 | */ |
||
| 392 | public function doCreate($msg = '') |
||
| 393 | { |
||
| 394 | $data = $this->misc->getDatabaseAccessor(); |
||
| 395 | |||
| 396 | $this->printTrail('server'); |
||
| 397 | $this->printTitle($this->lang['strcreatedatabase'], 'pg.database.create'); |
||
| 398 | $this->printMsg($msg); |
||
| 399 | |||
| 400 | $this->coalesceArr($_POST, 'formName', ''); |
||
| 401 | |||
| 402 | // Default encoding is that in language file |
||
| 403 | $this->coalesceArr($_POST, 'formEncoding', ''); |
||
| 404 | $this->coalesceArr($_POST, 'formTemplate', 'template1'); |
||
| 405 | |||
| 406 | $this->coalesceArr($_POST, 'formSpc', ''); |
||
| 407 | |||
| 408 | $this->coalesceArr($_POST, 'formComment', ''); |
||
| 409 | |||
| 410 | // Fetch a list of databases in the cluster |
||
| 411 | $templatedbs = $data->getDatabases(false); |
||
| 412 | |||
| 413 | $tablespaces = null; |
||
| 414 | // Fetch all tablespaces from the database |
||
| 415 | if ($data->hasTablespaces()) { |
||
| 416 | $tablespaces = $data->getTablespaces(); |
||
| 417 | } |
||
| 418 | |||
| 419 | echo '<form action="' . \SUBFOLDER . "/src/views/alldb\" method=\"post\">\n"; |
||
| 420 | echo "<table>\n"; |
||
| 421 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n"; |
||
| 422 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 423 | htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n"; |
||
| 424 | |||
| 425 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strtemplatedb']}</th>\n"; |
||
| 426 | echo "\t\t<td class=\"data1\">\n"; |
||
| 427 | echo "\t\t\t<select name=\"formTemplate\">\n"; |
||
| 428 | // Always offer template0 and template1 |
||
| 429 | echo "\t\t\t\t<option value=\"template0\"", |
||
| 430 | ('template0' == $_POST['formTemplate']) ? ' selected="selected"' : '', ">template0</option>\n"; |
||
| 431 | echo "\t\t\t\t<option value=\"template1\"", |
||
| 432 | ('template1' == $_POST['formTemplate']) ? ' selected="selected"' : '', ">template1</option>\n"; |
||
| 433 | while (!$templatedbs->EOF) { |
||
| 434 | $dbname = htmlspecialchars($templatedbs->fields['datname']); |
||
| 435 | if ('template1' != $dbname) { |
||
| 436 | // filter out for $this->conf[show_system] users so we dont get duplicates |
||
| 437 | echo "\t\t\t\t<option value=\"{$dbname}\"", |
||
| 438 | ($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n"; |
||
| 439 | } |
||
| 440 | $templatedbs->moveNext(); |
||
| 441 | } |
||
| 442 | echo "\t\t\t</select>\n"; |
||
| 443 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 444 | |||
| 445 | // ENCODING |
||
| 446 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strencoding']}</th>\n"; |
||
| 447 | echo "\t\t<td class=\"data1\">\n"; |
||
| 448 | echo "\t\t\t<select name=\"formEncoding\">\n"; |
||
| 449 | echo "\t\t\t\t<option value=\"\"></option>\n"; |
||
| 450 | |||
| 451 | foreach ($data->codemap as $key) { |
||
| 452 | echo "\t\t\t\t<option value=\"", htmlspecialchars($key), '"', |
||
| 453 | ($key == $_POST['formEncoding']) ? ' selected="selected"' : '', '>', |
||
| 454 | $this->misc->printVal($key), "</option>\n"; |
||
| 455 | } |
||
| 456 | echo "\t\t\t</select>\n"; |
||
| 457 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 458 | |||
| 459 | if ($data->hasDatabaseCollation()) { |
||
| 460 | $this->coalesceArr($_POST, 'formCollate', ''); |
||
| 461 | |||
| 462 | $this->coalesceArr($_POST, 'formCType', ''); |
||
| 463 | |||
| 464 | // LC_COLLATE |
||
| 465 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcollation']}</th>\n"; |
||
| 466 | echo "\t\t<td class=\"data1\">\n"; |
||
| 467 | echo "\t\t\t<input name=\"formCollate\" value=\"", htmlspecialchars($_POST['formCollate']), "\" />\n"; |
||
| 468 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 469 | |||
| 470 | // LC_CTYPE |
||
| 471 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strctype']}</th>\n"; |
||
| 472 | echo "\t\t<td class=\"data1\">\n"; |
||
| 473 | echo "\t\t\t<input name=\"formCType\" value=\"", htmlspecialchars($_POST['formCType']), "\" />\n"; |
||
| 474 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 475 | } |
||
| 476 | |||
| 477 | // Tablespace (if there are any) |
||
| 478 | if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) { |
||
| 479 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strtablespace']}</th>\n"; |
||
| 480 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n"; |
||
| 481 | // Always offer the default (empty) option |
||
| 482 | echo "\t\t\t\t<option value=\"\"", |
||
| 483 | ('' == $_POST['formSpc']) ? ' selected="selected"' : '', "></option>\n"; |
||
| 484 | // Display all other tablespaces |
||
| 485 | while (!$tablespaces->EOF) { |
||
| 486 | $spcname = htmlspecialchars($tablespaces->fields['spcname']); |
||
| 487 | echo "\t\t\t\t<option value=\"{$spcname}\"", |
||
| 488 | ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n"; |
||
| 489 | $tablespaces->moveNext(); |
||
| 490 | } |
||
| 491 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
||
| 492 | } |
||
| 493 | |||
| 494 | // Comments (if available) |
||
| 495 | if ($data->hasSharedComments()) { |
||
| 496 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n"; |
||
| 497 | echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
| 498 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
| 499 | } |
||
| 500 | |||
| 501 | echo "</table>\n"; |
||
| 502 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
| 503 | echo $this->misc->form; |
||
| 504 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n"; |
||
| 505 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 506 | echo "</form>\n"; |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Actually creates the new view in the database. |
||
| 511 | */ |
||
| 512 | public function doSaveCreate() |
||
| 513 | { |
||
| 514 | $data = $this->misc->getDatabaseAccessor(); |
||
| 515 | |||
| 516 | // Default tablespace to null if it isn't set |
||
| 517 | $this->coalesceArr($_POST, 'formSpc', null); |
||
| 518 | |||
| 519 | // Default comment to blank if it isn't set |
||
| 520 | $this->coalesceArr($_POST, 'formComment', null); |
||
| 521 | |||
| 522 | // Default collate to blank if it isn't set |
||
| 523 | $this->coalesceArr($_POST, 'formCollate', null); |
||
| 524 | |||
| 525 | // Default ctype to blank if it isn't set |
||
| 526 | $this->coalesceArr($_POST, 'formCType', null); |
||
| 527 | |||
| 528 | // Check that they've given a name and a definition |
||
| 529 | if ('' == $_POST['formName']) { |
||
| 530 | $this->doCreate($this->lang['strdatabaseneedsname']); |
||
| 531 | } else { |
||
| 532 | $status = $data->createDatabase( |
||
| 533 | $_POST['formName'], |
||
| 534 | $_POST['formEncoding'], |
||
| 535 | $_POST['formSpc'], |
||
| 536 | $_POST['formComment'], |
||
| 537 | $_POST['formTemplate'], |
||
| 538 | $_POST['formCollate'], |
||
| 539 | $_POST['formCType'] |
||
| 540 | ); |
||
| 541 | if (0 == $status) { |
||
| 542 | $this->misc->setReloadBrowser(true); |
||
| 543 | $this->doDefault($this->lang['strdatabasecreated']); |
||
| 544 | } else { |
||
| 545 | $this->doCreate($this->lang['strdatabasecreatedbad']); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Displays options for cluster download. |
||
| 552 | * |
||
| 553 | * @param mixed $msg |
||
| 554 | */ |
||
| 555 | public function doExport($msg = '') |
||
| 597 | } |
||
| 598 | } |
||
| 599 |