| Total Complexity | 61 |
| Total Lines | 671 |
| 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 |
||
| 15 | class AlldbController extends BaseController |
||
| 16 | { |
||
| 17 | use ExportTrait; |
||
|
|
|||
| 18 | |||
| 19 | public $table_place = 'alldb-databases'; |
||
| 20 | |||
| 21 | public $controller_title = 'strdatabases'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Default method to render the controller according to the action parameter. |
||
| 25 | */ |
||
| 26 | public function render() |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Show default list of databases in the server. |
||
| 95 | * |
||
| 96 | * @param mixed $msg |
||
| 97 | */ |
||
| 98 | public function doDefault($msg = ''): void |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return \Slim\Http\Response|string |
||
| 241 | */ |
||
| 242 | public function doTree() |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Display a form for alter and perform actual alter. |
||
| 262 | * |
||
| 263 | * @param mixed $confirm |
||
| 264 | */ |
||
| 265 | public function doAlter($confirm): void |
||
| 266 | { |
||
| 267 | $data = $this->misc->getDatabaseAccessor(); |
||
| 268 | |||
| 269 | if ($confirm) { |
||
| 270 | $this->printTrail('database'); |
||
| 271 | $this->printTitle($this->lang['stralter'], 'pg.database.alter'); |
||
| 272 | |||
| 273 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/alldb" method="post">' . \PHP_EOL; |
||
| 274 | echo '<table>' . \PHP_EOL; |
||
| 275 | echo \sprintf( |
||
| 276 | '<tr><th class="data left required">%s</th>', |
||
| 277 | $this->lang['strname'] |
||
| 278 | ) . \PHP_EOL; |
||
| 279 | echo '<td class="data1">'; |
||
| 280 | echo \sprintf( |
||
| 281 | '<input name="newname" size="32" maxlength="%s" value="', |
||
| 282 | $data->_maxNameLen |
||
| 283 | ), |
||
| 284 | \htmlspecialchars($_REQUEST['alterdatabase']), '" /></td></tr>' . \PHP_EOL; |
||
| 285 | |||
| 286 | if ($data->hasAlterDatabaseOwner() && $data->isSuperUser()) { |
||
| 287 | // Fetch all users |
||
| 288 | |||
| 289 | $rs = $data->getDatabaseOwner($_REQUEST['alterdatabase']); |
||
| 290 | $owner = isset($rs->fields['usename']) ? $rs->fields['usename'] : ''; |
||
| 291 | $users = $data->getUsers(); |
||
| 292 | |||
| 293 | echo \sprintf( |
||
| 294 | '<tr><th class="data left required">%s</th>', |
||
| 295 | $this->lang['strowner'] |
||
| 296 | ) . \PHP_EOL; |
||
| 297 | echo '<td class="data1"><select name="owner">'; |
||
| 298 | |||
| 299 | while (!$users->EOF) { |
||
| 300 | $uname = $users->fields['usename']; |
||
| 301 | echo '<option value="', \htmlspecialchars($uname), '"', |
||
| 302 | ($uname === $owner) ? ' selected="selected"' : '', '>', \htmlspecialchars($uname), '</option>' . \PHP_EOL; |
||
| 303 | $users->MoveNext(); |
||
| 304 | } |
||
| 305 | echo '</select></td></tr>' . \PHP_EOL; |
||
| 306 | } |
||
| 307 | |||
| 308 | if ($data->hasSharedComments()) { |
||
| 309 | $rs = $data->getDatabaseComment($_REQUEST['alterdatabase']); |
||
| 310 | $comment = isset($rs->fields['description']) ? $rs->fields['description'] : ''; |
||
| 311 | echo \sprintf( |
||
| 312 | '<tr><th class="data left">%s</th>', |
||
| 313 | $this->lang['strcomment'] |
||
| 314 | ) . \PHP_EOL; |
||
| 315 | echo '<td class="data1">'; |
||
| 316 | echo '<textarea rows="3" cols="32" name="dbcomment">', |
||
| 317 | \htmlspecialchars($comment), '</textarea></td></tr>' . \PHP_EOL; |
||
| 318 | } |
||
| 319 | echo '</table>' . \PHP_EOL; |
||
| 320 | echo '<input type="hidden" name="action" value="alter" />' . \PHP_EOL; |
||
| 321 | echo $this->view->form; |
||
| 322 | echo '<input type="hidden" name="oldname" value="', |
||
| 323 | \htmlspecialchars($_REQUEST['alterdatabase']), '" />' . \PHP_EOL; |
||
| 324 | echo \sprintf( |
||
| 325 | '<input type="submit" name="alter" value="%s" />', |
||
| 326 | $this->lang['stralter'] |
||
| 327 | ) . \PHP_EOL; |
||
| 328 | echo \sprintf( |
||
| 329 | '<input type="submit" name="cancel" value="%s" />', |
||
| 330 | $this->lang['strcancel'] |
||
| 331 | ) . \PHP_EOL; |
||
| 332 | echo '</form>' . \PHP_EOL; |
||
| 333 | } else { |
||
| 334 | $this->coalesceArr($_POST, 'owner', ''); |
||
| 335 | |||
| 336 | $this->coalesceArr($_POST, 'dbcomment', ''); |
||
| 337 | |||
| 338 | if (0 === $data->alterDatabase($_POST['oldname'], $_POST['newname'], $_POST['owner'], $_POST['dbcomment'])) { |
||
| 339 | $this->view->setReloadBrowser(true); |
||
| 340 | $this->doDefault($this->lang['strdatabasealtered']); |
||
| 341 | } else { |
||
| 342 | $this->doDefault($this->lang['strdatabasealteredbad']); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Show confirmation of drop and perform actual drop. |
||
| 349 | * |
||
| 350 | * @param mixed $confirm |
||
| 351 | */ |
||
| 352 | public function doDrop($confirm) |
||
| 432 | } |
||
| 433 | } |
||
| 434 | //END DROP |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | // END FUNCTION |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Displays a screen where they can enter a new database. |
||
| 442 | * |
||
| 443 | * @param mixed $msg |
||
| 444 | */ |
||
| 445 | public function doCreate($msg = ''): void |
||
| 446 | { |
||
| 447 | $data = $this->misc->getDatabaseAccessor(); |
||
| 448 | |||
| 449 | $this->printTrail('server'); |
||
| 450 | $this->printTitle($this->lang['strcreatedatabase'], 'pg.database.create'); |
||
| 451 | $this->printMsg($msg); |
||
| 452 | |||
| 453 | $this->coalesceArr($_POST, 'formName', ''); |
||
| 454 | |||
| 455 | // Default encoding is that in language file |
||
| 456 | $this->coalesceArr($_POST, 'formEncoding', ''); |
||
| 457 | $this->coalesceArr($_POST, 'formTemplate', 'template1'); |
||
| 458 | |||
| 459 | $this->coalesceArr($_POST, 'formSpc', ''); |
||
| 460 | |||
| 461 | $this->coalesceArr($_POST, 'formComment', ''); |
||
| 462 | |||
| 463 | // Fetch a list of databases in the cluster |
||
| 464 | $templatedbs = $data->getDatabases(false); |
||
| 465 | |||
| 466 | $tablespaces = null; |
||
| 467 | // Fetch all tablespaces from the database |
||
| 468 | if ($data->hasTablespaces()) { |
||
| 469 | $tablespaces = $data->getTablespaces(); |
||
| 470 | } |
||
| 471 | |||
| 472 | echo '<form action="' . \containerInstance()->subFolder . '/src/views/alldb" method="post">' . \PHP_EOL; |
||
| 473 | echo '<table>' . \PHP_EOL; |
||
| 474 | echo \sprintf( |
||
| 475 | ' <tr> |
||
| 476 | <th class="data left required">%s</th>', |
||
| 477 | $this->lang['strname'] |
||
| 478 | ) . \PHP_EOL; |
||
| 479 | echo \sprintf( |
||
| 480 | ' <td class="data1"><input name="formName" size="32" maxlength="%s" value="', |
||
| 481 | $data->_maxNameLen |
||
| 482 | ), |
||
| 483 | \htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>" . \PHP_EOL; |
||
| 484 | |||
| 485 | echo \sprintf( |
||
| 486 | ' <tr> |
||
| 487 | <th class="data left required">%s</th>', |
||
| 488 | $this->lang['strtemplatedb'] |
||
| 489 | ) . \PHP_EOL; |
||
| 490 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
| 491 | echo "\t\t\t<select name=\"formTemplate\">" . \PHP_EOL; |
||
| 492 | // Always offer template0 and template1 |
||
| 493 | echo "\t\t\t\t<option value=\"template0\"", |
||
| 494 | ('template0' === $_POST['formTemplate']) ? ' selected="selected"' : '', '>template0</option>' . \PHP_EOL; |
||
| 495 | echo "\t\t\t\t<option value=\"template1\"", |
||
| 496 | ('template1' === $_POST['formTemplate']) ? ' selected="selected"' : '', '>template1</option>' . \PHP_EOL; |
||
| 497 | |||
| 498 | while (!$templatedbs->EOF) { |
||
| 499 | $dbname = \htmlspecialchars($templatedbs->fields['datname']); |
||
| 500 | |||
| 501 | if ('template1' !== $dbname) { |
||
| 502 | // filter out for $this->conf[show_system] users so we dont get duplicates |
||
| 503 | echo \sprintf( |
||
| 504 | ' <option value="%s"', |
||
| 505 | $dbname |
||
| 506 | ), |
||
| 507 | ($dbname === $_POST['formTemplate']) ? ' selected="selected"' : '', \sprintf( |
||
| 508 | '>%s</option>', |
||
| 509 | $dbname |
||
| 510 | ) . \PHP_EOL; |
||
| 511 | } |
||
| 512 | $templatedbs->MoveNext(); |
||
| 513 | } |
||
| 514 | echo "\t\t\t</select>" . \PHP_EOL; |
||
| 515 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
| 516 | |||
| 517 | // ENCODING |
||
| 518 | echo \sprintf( |
||
| 519 | ' <tr> |
||
| 520 | <th class="data left required">%s</th>', |
||
| 521 | $this->lang['strencoding'] |
||
| 522 | ) . \PHP_EOL; |
||
| 523 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
| 524 | echo "\t\t\t<select name=\"formEncoding\">" . \PHP_EOL; |
||
| 525 | echo "\t\t\t\t<option value=\"\"></option>" . \PHP_EOL; |
||
| 526 | |||
| 527 | foreach ($data->codemap as $key) { |
||
| 528 | echo "\t\t\t\t<option value=\"", \htmlspecialchars($key), '"', |
||
| 529 | ($key === $_POST['formEncoding']) ? ' selected="selected"' : '', '>', |
||
| 530 | $this->misc->printVal($key), '</option>' . \PHP_EOL; |
||
| 531 | } |
||
| 532 | echo "\t\t\t</select>" . \PHP_EOL; |
||
| 533 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
| 534 | |||
| 535 | if ($data->hasDatabaseCollation()) { |
||
| 536 | $this->coalesceArr($_POST, 'formCollate', ''); |
||
| 537 | |||
| 538 | $this->coalesceArr($_POST, 'formCType', ''); |
||
| 539 | |||
| 540 | // LC_COLLATE |
||
| 541 | echo \sprintf( |
||
| 542 | ' <tr> |
||
| 543 | <th class="data left">%s</th>', |
||
| 544 | $this->lang['strcollation'] |
||
| 545 | ) . \PHP_EOL; |
||
| 546 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
| 547 | echo "\t\t\t<input name=\"formCollate\" value=\"", \htmlspecialchars($_POST['formCollate']), '" />' . \PHP_EOL; |
||
| 548 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
| 549 | |||
| 550 | // LC_CTYPE |
||
| 551 | echo \sprintf( |
||
| 552 | ' <tr> |
||
| 553 | <th class="data left">%s</th>', |
||
| 554 | $this->lang['strctype'] |
||
| 555 | ) . \PHP_EOL; |
||
| 556 | echo "\t\t<td class=\"data1\">" . \PHP_EOL; |
||
| 557 | echo "\t\t\t<input name=\"formCType\" value=\"", \htmlspecialchars($_POST['formCType']), '" />' . \PHP_EOL; |
||
| 558 | echo "\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
| 559 | } |
||
| 560 | |||
| 561 | // Tablespace (if there are any) |
||
| 562 | if ($data->hasTablespaces() && 0 < $tablespaces->RecordCount()) { |
||
| 563 | echo \sprintf( |
||
| 564 | ' <tr> |
||
| 565 | <th class="data left">%s</th>', |
||
| 566 | $this->lang['strtablespace'] |
||
| 567 | ) . \PHP_EOL; |
||
| 568 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">" . \PHP_EOL; |
||
| 569 | // Always offer the default (empty) option |
||
| 570 | echo "\t\t\t\t<option value=\"\"", |
||
| 571 | ('' === $_POST['formSpc']) ? ' selected="selected"' : '', '></option>' . \PHP_EOL; |
||
| 572 | // Display all other tablespaces |
||
| 573 | while (!$tablespaces->EOF) { |
||
| 574 | $spcname = \htmlspecialchars($tablespaces->fields['spcname'] ?? ''); |
||
| 575 | echo \sprintf( |
||
| 576 | ' <option value="%s"', |
||
| 577 | $spcname |
||
| 578 | ), |
||
| 579 | ($spcname === $_POST['formSpc']) ? ' selected="selected"' : '', \sprintf( |
||
| 580 | '>%s</option>', |
||
| 581 | $spcname |
||
| 582 | ) . \PHP_EOL; |
||
| 583 | $tablespaces->MoveNext(); |
||
| 584 | } |
||
| 585 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>" . \PHP_EOL; |
||
| 586 | } |
||
| 587 | |||
| 588 | // Comments (if available) |
||
| 589 | if ($data->hasSharedComments()) { |
||
| 590 | echo \sprintf( |
||
| 591 | ' <tr> |
||
| 592 | <th class="data left">%s</th>', |
||
| 593 | $this->lang['strcomment'] |
||
| 594 | ) . \PHP_EOL; |
||
| 595 | echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
| 596 | \htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>" . \PHP_EOL; |
||
| 597 | } |
||
| 598 | |||
| 599 | echo '</table>' . \PHP_EOL; |
||
| 600 | echo '<p><input type="hidden" name="action" value="save_create" />' . \PHP_EOL; |
||
| 601 | echo $this->view->form; |
||
| 602 | echo \sprintf( |
||
| 603 | '<input type="submit" value="%s" />', |
||
| 604 | $this->lang['strcreate'] |
||
| 605 | ) . \PHP_EOL; |
||
| 606 | echo \sprintf( |
||
| 607 | '<input type="submit" name="cancel" value="%s" /></p>%s', |
||
| 608 | $this->lang['strcancel'], |
||
| 609 | \PHP_EOL |
||
| 610 | ); |
||
| 611 | echo '</form>' . \PHP_EOL; |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Actually creates the new view in the database. |
||
| 616 | */ |
||
| 617 | public function doSaveCreate(): void |
||
| 618 | { |
||
| 619 | $data = $this->misc->getDatabaseAccessor(); |
||
| 620 | |||
| 621 | // Default tablespace to null if it isn't set |
||
| 622 | $this->coalesceArr($_POST, 'formSpc', ''); |
||
| 623 | |||
| 624 | // Default comment to blank if it isn't set |
||
| 625 | $this->coalesceArr($_POST, 'formComment', ''); |
||
| 626 | |||
| 627 | // Default collate to blank if it isn't set |
||
| 628 | $this->coalesceArr($_POST, 'formCollate', ''); |
||
| 629 | |||
| 630 | // Default ctype to blank if it isn't set |
||
| 631 | $this->coalesceArr($_POST, 'formCType', ''); |
||
| 632 | |||
| 633 | // Check that they've given a name and a definition |
||
| 634 | if ('' === $_POST['formName']) { |
||
| 635 | $this->doCreate($this->lang['strdatabaseneedsname']); |
||
| 636 | } else { |
||
| 637 | $status = $data->createDatabase( |
||
| 638 | $_POST['formName'], |
||
| 639 | $_POST['formEncoding'], |
||
| 640 | $_POST['formSpc'], |
||
| 641 | $_POST['formComment'], |
||
| 642 | $_POST['formTemplate'], |
||
| 643 | $_POST['formCollate'], |
||
| 644 | $_POST['formCType'] |
||
| 645 | ); |
||
| 646 | |||
| 647 | if (0 === $status) { |
||
| 648 | $this->view->setReloadBrowser(true); |
||
| 649 | $this->doDefault($this->lang['strdatabasecreated']); |
||
| 650 | } else { |
||
| 651 | $this->doCreate($this->lang['strdatabasecreatedbad']); |
||
| 652 | } |
||
| 653 | } |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Displays options for cluster download. |
||
| 658 | * |
||
| 659 | * @param mixed $msg |
||
| 660 | */ |
||
| 661 | public function doExport($msg = ''): void |
||
| 686 | } |
||
| 687 | } |
||
| 688 |