| Total Complexity | 103 |
| Total Lines | 747 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ConstraintsController 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 ConstraintsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class ConstraintsController extends BaseController |
||
| 15 | { |
||
| 16 | public $controller_name = 'ConstraintsController'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Default method to render the controller according to the action parameter. |
||
| 20 | */ |
||
| 21 | public function render() |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * List all the constraints on the table. |
||
| 129 | * |
||
| 130 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 131 | */ |
||
| 132 | public function doDefault($msg = '') |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Confirm and then actually add a FOREIGN KEY constraint. |
||
| 259 | * |
||
| 260 | * @param mixed $stage |
||
|
1 ignored issue
–
show
|
|||
| 261 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 262 | */ |
||
| 263 | public function addForeignKey($stage, $msg = '') |
||
| 264 | { |
||
| 265 | $conf = $this->conf; |
||
| 266 | |||
| 267 | $lang = $this->lang; |
||
| 268 | $data = $this->misc->getDatabaseAccessor(); |
||
| 269 | |||
| 270 | if (!isset($_POST['name'])) { |
||
| 271 | $_POST['name'] = ''; |
||
| 272 | } |
||
| 273 | |||
| 274 | if (!isset($_POST['target'])) { |
||
| 275 | $_POST['target'] = ''; |
||
| 276 | } |
||
| 277 | |||
| 278 | switch ($stage) { |
||
| 279 | case 2: |
||
| 280 | // Check that they've given at least one source column |
||
| 281 | if (!isset($_REQUEST['SourceColumnList']) && (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || 0 == sizeof($_POST['IndexColumnList']))) { |
||
| 282 | $this->addForeignKey(1, $lang['strfkneedscols']); |
||
| 283 | } else { |
||
| 284 | // Copy the IndexColumnList variable from stage 1 |
||
| 285 | if (isset($_REQUEST['IndexColumnList']) && !isset($_REQUEST['SourceColumnList'])) { |
||
| 286 | $_REQUEST['SourceColumnList'] = serialize($_REQUEST['IndexColumnList']); |
||
| 287 | } |
||
| 288 | |||
| 289 | // Initialise variables |
||
| 290 | if (!isset($_POST['upd_action'])) { |
||
| 291 | $_POST['upd_action'] = null; |
||
| 292 | } |
||
| 293 | |||
| 294 | if (!isset($_POST['del_action'])) { |
||
| 295 | $_POST['del_action'] = null; |
||
| 296 | } |
||
| 297 | |||
| 298 | if (!isset($_POST['match'])) { |
||
| 299 | $_POST['match'] = null; |
||
| 300 | } |
||
| 301 | |||
| 302 | if (!isset($_POST['deferrable'])) { |
||
| 303 | $_POST['deferrable'] = null; |
||
| 304 | } |
||
| 305 | |||
| 306 | if (!isset($_POST['initially'])) { |
||
| 307 | $_POST['initially'] = null; |
||
| 308 | } |
||
| 309 | |||
| 310 | $_REQUEST['target'] = unserialize($_REQUEST['target']); |
||
| 311 | |||
| 312 | $this->printTrail('table'); |
||
| 313 | $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key'); |
||
| 314 | $this->printMsg($msg); |
||
| 315 | |||
| 316 | // Unserialize target and fetch appropriate table. This is a bit messy |
||
| 317 | // because the table could be in another schema. |
||
| 318 | $data->setSchema($_REQUEST['target']['schemaname']); |
||
| 319 | $attrs = $data->getTableAttributes($_REQUEST['target']['tablename']); |
||
| 320 | $data->setSchema($_REQUEST['schema']); |
||
| 321 | |||
| 322 | $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10); |
||
| 323 | $selColumns->set_style('width: 15em;'); |
||
| 324 | |||
| 325 | if ($attrs->recordCount() > 0) { |
||
| 326 | while (!$attrs->EOF) { |
||
| 327 | $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname'])); |
||
| 328 | $attrs->moveNext(); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10); |
||
| 333 | $selIndex->set_style('width: 15em;'); |
||
| 334 | $selIndex->set_attribute('id', 'IndexColumnList'); |
||
| 335 | $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>'); |
||
| 336 | $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 337 | $buttonAdd->set_attribute('type', 'button'); |
||
| 338 | |||
| 339 | $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<'); |
||
| 340 | $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 341 | $buttonRemove->set_attribute('type', 'button'); |
||
| 342 | |||
| 343 | echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n"; |
||
| 344 | |||
| 345 | echo "<table>\n"; |
||
| 346 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>"; |
||
| 347 | echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=data>{$lang['strfkcolumnlist']}</th></tr>\n"; |
||
| 348 | echo '<tr><td class="data1">'.$selColumns->fetch()."</td>\n"; |
||
| 349 | echo '<td class="data1" style="text-align: center">'.$buttonRemove->fetch().$buttonAdd->fetch().'</td>'; |
||
| 350 | echo '<td class="data1">'.$selIndex->fetch()."</td></tr>\n"; |
||
| 351 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['stractions']}</th></tr>"; |
||
| 352 | echo '<tr>'; |
||
| 353 | echo "<td class=\"data1\" colspan=\"3\">\n"; |
||
| 354 | // ON SELECT actions |
||
| 355 | echo "{$lang['stronupdate']} <select name=\"upd_action\">"; |
||
| 356 | foreach ($data->fkactions as $v) { |
||
| 357 | echo "<option value=\"{$v}\"", ($_POST['upd_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 358 | } |
||
| 359 | |||
| 360 | echo "</select><br />\n"; |
||
| 361 | |||
| 362 | // ON DELETE actions |
||
| 363 | echo "{$lang['strondelete']} <select name=\"del_action\">"; |
||
| 364 | foreach ($data->fkactions as $v) { |
||
| 365 | echo "<option value=\"{$v}\"", ($_POST['del_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 366 | } |
||
| 367 | |||
| 368 | echo "</select><br />\n"; |
||
| 369 | |||
| 370 | // MATCH options |
||
| 371 | echo '<select name="match">'; |
||
| 372 | foreach ($data->fkmatches as $v) { |
||
| 373 | echo "<option value=\"{$v}\"", ($_POST['match'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 374 | } |
||
| 375 | |||
| 376 | echo "</select><br />\n"; |
||
| 377 | |||
| 378 | // DEFERRABLE options |
||
| 379 | echo '<select name="deferrable">'; |
||
| 380 | foreach ($data->fkdeferrable as $v) { |
||
| 381 | echo "<option value=\"{$v}\"", ($_POST['deferrable'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 382 | } |
||
| 383 | |||
| 384 | echo "</select><br />\n"; |
||
| 385 | |||
| 386 | // INITIALLY options |
||
| 387 | echo '<select name="initially">'; |
||
| 388 | foreach ($data->fkinitial as $v) { |
||
| 389 | echo "<option value=\"{$v}\"", ($_POST['initially'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 390 | } |
||
| 391 | |||
| 392 | echo "</select>\n"; |
||
| 393 | echo "</td></tr>\n"; |
||
| 394 | echo "</table>\n"; |
||
| 395 | |||
| 396 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n"; |
||
| 397 | echo $this->misc->form; |
||
| 398 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 399 | echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n"; |
||
| 400 | echo '<input type="hidden" name="target" value="', htmlspecialchars(serialize($_REQUEST['target'])), "\" />\n"; |
||
| 401 | echo '<input type="hidden" name="SourceColumnList" value="', htmlspecialchars($_REQUEST['SourceColumnList']), "\" />\n"; |
||
| 402 | echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n"; |
||
| 403 | echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n"; |
||
| 404 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 405 | echo "</form>\n"; |
||
| 406 | } |
||
| 407 | |||
| 408 | break; |
||
| 409 | case 3: |
||
| 410 | // Unserialize target |
||
| 411 | $_POST['target'] = unserialize($_POST['target']); |
||
| 412 | |||
| 413 | // Check that they've given at least one column |
||
| 414 | if (isset($_POST['SourceColumnList'])) { |
||
| 415 | $temp = unserialize($_POST['SourceColumnList']); |
||
| 416 | } |
||
| 417 | |||
| 418 | if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) |
||
| 419 | || 0 == sizeof($_POST['IndexColumnList']) || !isset($temp) |
||
| 420 | || !is_array($temp) || 0 == sizeof($temp)) { |
||
|
1 ignored issue
–
show
|
|||
| 421 | $this->addForeignKey(2, $lang['strfkneedscols']); |
||
| 422 | } else { |
||
| 423 | $status = $data->addForeignKey( |
||
| 424 | $_POST['table'], |
||
| 425 | $_POST['target']['schemaname'], |
||
| 426 | $_POST['target']['tablename'], |
||
| 427 | unserialize($_POST['SourceColumnList']), |
||
| 428 | $_POST['IndexColumnList'], |
||
| 429 | $_POST['upd_action'], |
||
| 430 | $_POST['del_action'], |
||
| 431 | $_POST['match'], |
||
| 432 | $_POST['deferrable'], |
||
| 433 | $_POST['initially'], |
||
| 434 | $_POST['name'] |
||
| 435 | ); |
||
| 436 | if (0 == $status) { |
||
| 437 | $this->doDefault($lang['strfkadded']); |
||
| 438 | } else { |
||
| 439 | $this->addForeignKey(2, $lang['strfkaddedbad']); |
||
| 440 | } |
||
| 441 | } |
||
| 442 | |||
| 443 | break; |
||
| 444 | default: |
||
| 445 | $this->printTrail('table'); |
||
| 446 | $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key'); |
||
| 447 | $this->printMsg($msg); |
||
| 448 | |||
| 449 | $attrs = $data->getTableAttributes($_REQUEST['table']); |
||
| 450 | $tables = $data->getTables(true); |
||
| 451 | |||
| 452 | $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10); |
||
| 453 | $selColumns->set_style('width: 15em;'); |
||
| 454 | |||
| 455 | if ($attrs->recordCount() > 0) { |
||
| 456 | while (!$attrs->EOF) { |
||
| 457 | $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname'])); |
||
| 458 | $attrs->moveNext(); |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10); |
||
| 463 | $selIndex->set_style('width: 15em;'); |
||
| 464 | $selIndex->set_attribute('id', 'IndexColumnList'); |
||
| 465 | $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>'); |
||
| 466 | $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 467 | $buttonAdd->set_attribute('type', 'button'); |
||
| 468 | |||
| 469 | $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<'); |
||
| 470 | $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 471 | $buttonRemove->set_attribute('type', 'button'); |
||
| 472 | |||
| 473 | echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n"; |
||
| 474 | |||
| 475 | echo "<table>\n"; |
||
| 476 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strname']}</th></tr>\n"; |
||
| 477 | echo "<tr><td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" /></td></tr>\n"; |
||
| 478 | echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=\"data required\">{$lang['strfkcolumnlist']}</th></tr>\n"; |
||
| 479 | echo '<tr><td class="data1">'.$selColumns->fetch()."</td>\n"; |
||
| 480 | echo '<td class="data1" style="text-align: center">'.$buttonRemove->fetch().$buttonAdd->fetch()."</td>\n"; |
||
| 481 | echo '<td class=data1>'.$selIndex->fetch()."</td></tr>\n"; |
||
| 482 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>"; |
||
| 483 | echo '<tr>'; |
||
| 484 | echo '<td class="data1" colspan="3"><select class="select2" name="target">'; |
||
| 485 | while (!$tables->EOF) { |
||
| 486 | $key = ['schemaname' => $tables->fields['nspname'], 'tablename' => $tables->fields['relname']]; |
||
| 487 | $key = serialize($key); |
||
| 488 | echo '<option value="', htmlspecialchars($key), '">'; |
||
| 489 | if ($tables->fields['nspname'] != $_REQUEST['schema']) { |
||
| 490 | echo htmlspecialchars($tables->fields['nspname']), '.'; |
||
| 491 | } |
||
| 492 | echo htmlspecialchars($tables->fields['relname']), "</option>\n"; |
||
| 493 | $tables->moveNext(); |
||
| 494 | } |
||
| 495 | echo "</select>\n"; |
||
| 496 | echo '</td></tr>'; |
||
| 497 | echo "</table>\n"; |
||
| 498 | |||
| 499 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n"; |
||
| 500 | echo $this->misc->form; |
||
| 501 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 502 | echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; |
||
| 503 | echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n"; |
||
| 504 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 505 | echo "</form>\n"; |
||
| 506 | //echo "<script>jQuery('select[name=\"target\"]').select2()</script>"; |
||
| 507 | break; |
||
| 508 | } |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Confirm and then actually add a PRIMARY KEY or UNIQUE constraint. |
||
| 513 | * |
||
| 514 | * @param mixed $type |
||
|
1 ignored issue
–
show
|
|||
| 515 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 516 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 517 | */ |
||
| 518 | public function addPrimaryOrUniqueKey($type, $confirm, $msg = '') |
||
| 657 | } |
||
| 658 | } |
||
| 659 | } |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Confirm and then actually add a CHECK constraint. |
||
| 663 | * |
||
| 664 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 665 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 666 | */ |
||
| 667 | public function addCheck($confirm, $msg = '') |
||
| 668 | { |
||
| 669 | $conf = $this->conf; |
||
| 670 | |||
| 671 | $lang = $this->lang; |
||
| 672 | $data = $this->misc->getDatabaseAccessor(); |
||
| 673 | |||
| 674 | if (!isset($_POST['name'])) { |
||
| 675 | $_POST['name'] = ''; |
||
| 676 | } |
||
| 677 | |||
| 678 | if (!isset($_POST['definition'])) { |
||
| 679 | $_POST['definition'] = ''; |
||
| 680 | } |
||
| 681 | |||
| 682 | if ($confirm) { |
||
| 683 | $this->printTrail('table'); |
||
| 684 | $this->printTitle($lang['straddcheck'], 'pg.constraint.check'); |
||
| 685 | $this->printMsg($msg); |
||
| 686 | |||
| 687 | echo '<form action="'.\SUBFOLDER."/src/views/constraints.php\" method=\"post\">\n"; |
||
| 688 | echo "<table>\n"; |
||
| 689 | echo "<tr><th class=\"data\">{$lang['strname']}</th>\n"; |
||
| 690 | echo "<th class=\"data required\">{$lang['strdefinition']}</th></tr>\n"; |
||
| 691 | |||
| 692 | echo "<tr><td class=\"data1\"><input name=\"name\" size=\"24\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 693 | htmlspecialchars($_POST['name']), "\" /></td>\n"; |
||
| 694 | |||
| 695 | echo '<td class="data1">(<input name="definition" size="64" value="', |
||
| 696 | htmlspecialchars($_POST['definition']), "\" />)</td></tr>\n"; |
||
| 697 | echo "</table>\n"; |
||
| 698 | |||
| 699 | echo "<input type=\"hidden\" name=\"action\" value=\"save_add_check\" />\n"; |
||
| 700 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 701 | echo $this->misc->form; |
||
| 702 | echo "<p><input type=\"submit\" name=\"ok\" value=\"{$lang['stradd']}\" />\n"; |
||
| 703 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 704 | echo "</form>\n"; |
||
| 705 | } else { |
||
| 706 | if ('' == trim($_POST['definition'])) { |
||
| 707 | $this->addCheck(true, $lang['strcheckneedsdefinition']); |
||
| 708 | } else { |
||
| 709 | $status = $data->addCheckConstraint( |
||
| 710 | $_POST['table'], |
||
| 711 | $_POST['definition'], |
||
| 712 | $_POST['name'] |
||
| 713 | ); |
||
| 714 | if (0 == $status) { |
||
| 715 | $this->doDefault($lang['strcheckadded']); |
||
| 716 | } else { |
||
| 717 | $this->addCheck(true, $lang['strcheckaddedbad']); |
||
| 718 | } |
||
| 719 | } |
||
| 720 | } |
||
| 721 | } |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Show confirmation of drop and perform actual drop. |
||
| 725 | * |
||
| 726 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 727 | */ |
||
| 728 | public function doDrop($confirm) |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 765 |