| Conditions | 40 |
| Paths | 564 |
| Total Lines | 243 |
| Code Lines | 165 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 259 | public function addForeignKey($stage, $msg = '') |
||
| 260 | { |
||
| 261 | $lang = $this->lang; |
||
| 262 | $data = $this->misc->getDatabaseAccessor(); |
||
| 263 | |||
| 264 | if (!isset($_POST['name'])) { |
||
| 265 | $_POST['name'] = ''; |
||
| 266 | } |
||
| 267 | |||
| 268 | if (!isset($_POST['target'])) { |
||
| 269 | $_POST['target'] = ''; |
||
| 270 | } |
||
| 271 | |||
| 272 | switch ($stage) { |
||
| 273 | case 2: |
||
| 274 | // Check that they've given at least one source column |
||
| 275 | if (!isset($_REQUEST['SourceColumnList']) && (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || 0 == sizeof($_POST['IndexColumnList']))) { |
||
| 276 | $this->addForeignKey(1, $lang['strfkneedscols']); |
||
| 277 | } else { |
||
| 278 | // Copy the IndexColumnList variable from stage 1 |
||
| 279 | if (isset($_REQUEST['IndexColumnList']) && !isset($_REQUEST['SourceColumnList'])) { |
||
| 280 | $_REQUEST['SourceColumnList'] = serialize($_REQUEST['IndexColumnList']); |
||
| 281 | } |
||
| 282 | |||
| 283 | // Initialise variables |
||
| 284 | if (!isset($_POST['upd_action'])) { |
||
| 285 | $_POST['upd_action'] = null; |
||
| 286 | } |
||
| 287 | |||
| 288 | if (!isset($_POST['del_action'])) { |
||
| 289 | $_POST['del_action'] = null; |
||
| 290 | } |
||
| 291 | |||
| 292 | if (!isset($_POST['match'])) { |
||
| 293 | $_POST['match'] = null; |
||
| 294 | } |
||
| 295 | |||
| 296 | if (!isset($_POST['deferrable'])) { |
||
| 297 | $_POST['deferrable'] = null; |
||
| 298 | } |
||
| 299 | |||
| 300 | if (!isset($_POST['initially'])) { |
||
| 301 | $_POST['initially'] = null; |
||
| 302 | } |
||
| 303 | |||
| 304 | $_REQUEST['target'] = unserialize($_REQUEST['target']); |
||
| 305 | |||
| 306 | $this->printTrail('table'); |
||
| 307 | $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key'); |
||
| 308 | $this->printMsg($msg); |
||
| 309 | |||
| 310 | // Unserialize target and fetch appropriate table. This is a bit messy |
||
| 311 | // because the table could be in another schema. |
||
| 312 | $data->setSchema($_REQUEST['target']['schemaname']); |
||
| 313 | $attrs = $data->getTableAttributes($_REQUEST['target']['tablename']); |
||
| 314 | $data->setSchema($_REQUEST['schema']); |
||
| 315 | |||
| 316 | $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10); |
||
| 317 | $selColumns->set_style('width: 15em;'); |
||
| 318 | |||
| 319 | if ($attrs->recordCount() > 0) { |
||
| 320 | while (!$attrs->EOF) { |
||
| 321 | $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname'])); |
||
| 322 | $attrs->moveNext(); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10); |
||
| 327 | $selIndex->set_style('width: 15em;'); |
||
| 328 | $selIndex->set_attribute('id', 'IndexColumnList'); |
||
| 329 | $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>'); |
||
| 330 | $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 331 | $buttonAdd->set_attribute('type', 'button'); |
||
| 332 | |||
| 333 | $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<'); |
||
| 334 | $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 335 | $buttonRemove->set_attribute('type', 'button'); |
||
| 336 | |||
| 337 | echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n"; |
||
| 338 | |||
| 339 | echo "<table>\n"; |
||
| 340 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>"; |
||
| 341 | echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=data>{$lang['strfkcolumnlist']}</th></tr>\n"; |
||
| 342 | echo '<tr><td class="data1">' . $selColumns->fetch() . "</td>\n"; |
||
| 343 | echo '<td class="data1" style="text-align: center">' . $buttonRemove->fetch() . $buttonAdd->fetch() . '</td>'; |
||
| 344 | echo '<td class="data1">' . $selIndex->fetch() . "</td></tr>\n"; |
||
| 345 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['stractions']}</th></tr>"; |
||
| 346 | echo '<tr>'; |
||
| 347 | echo "<td class=\"data1\" colspan=\"3\">\n"; |
||
| 348 | // ON SELECT actions |
||
| 349 | echo "{$lang['stronupdate']} <select name=\"upd_action\">"; |
||
| 350 | foreach ($data->fkactions as $v) { |
||
| 351 | echo "<option value=\"{$v}\"", ($_POST['upd_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 352 | } |
||
| 353 | |||
| 354 | echo "</select><br />\n"; |
||
| 355 | |||
| 356 | // ON DELETE actions |
||
| 357 | echo "{$lang['strondelete']} <select name=\"del_action\">"; |
||
| 358 | foreach ($data->fkactions as $v) { |
||
| 359 | echo "<option value=\"{$v}\"", ($_POST['del_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 360 | } |
||
| 361 | |||
| 362 | echo "</select><br />\n"; |
||
| 363 | |||
| 364 | // MATCH options |
||
| 365 | echo '<select name="match">'; |
||
| 366 | foreach ($data->fkmatches as $v) { |
||
| 367 | echo "<option value=\"{$v}\"", ($_POST['match'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 368 | } |
||
| 369 | |||
| 370 | echo "</select><br />\n"; |
||
| 371 | |||
| 372 | // DEFERRABLE options |
||
| 373 | echo '<select name="deferrable">'; |
||
| 374 | foreach ($data->fkdeferrable as $v) { |
||
| 375 | echo "<option value=\"{$v}\"", ($_POST['deferrable'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 376 | } |
||
| 377 | |||
| 378 | echo "</select><br />\n"; |
||
| 379 | |||
| 380 | // INITIALLY options |
||
| 381 | echo '<select name="initially">'; |
||
| 382 | foreach ($data->fkinitial as $v) { |
||
| 383 | echo "<option value=\"{$v}\"", ($_POST['initially'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 384 | } |
||
| 385 | |||
| 386 | echo "</select>\n"; |
||
| 387 | echo "</td></tr>\n"; |
||
| 388 | echo "</table>\n"; |
||
| 389 | |||
| 390 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n"; |
||
| 391 | echo $this->misc->form; |
||
| 392 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 393 | echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n"; |
||
| 394 | echo '<input type="hidden" name="target" value="', htmlspecialchars(serialize($_REQUEST['target'])), "\" />\n"; |
||
| 395 | echo '<input type="hidden" name="SourceColumnList" value="', htmlspecialchars($_REQUEST['SourceColumnList']), "\" />\n"; |
||
| 396 | echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n"; |
||
| 397 | echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n"; |
||
| 398 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 399 | echo "</form>\n"; |
||
| 400 | } |
||
| 401 | |||
| 402 | break; |
||
| 403 | case 3: |
||
| 404 | // Unserialize target |
||
| 405 | $_POST['target'] = unserialize($_POST['target']); |
||
| 406 | |||
| 407 | // Check that they've given at least one column |
||
| 408 | if (isset($_POST['SourceColumnList'])) { |
||
| 409 | $temp = unserialize($_POST['SourceColumnList']); |
||
| 410 | } |
||
| 411 | |||
| 412 | if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) |
||
| 413 | || 0 == sizeof($_POST['IndexColumnList']) || !isset($temp) |
||
| 414 | || !is_array($temp) || 0 == sizeof($temp)) { |
||
|
1 ignored issue
–
show
|
|||
| 415 | $this->addForeignKey(2, $lang['strfkneedscols']); |
||
| 416 | } else { |
||
| 417 | $status = $data->addForeignKey( |
||
| 418 | $_POST['table'], |
||
| 419 | $_POST['target']['schemaname'], |
||
| 420 | $_POST['target']['tablename'], |
||
| 421 | unserialize($_POST['SourceColumnList']), |
||
| 422 | $_POST['IndexColumnList'], |
||
| 423 | $_POST['upd_action'], |
||
| 424 | $_POST['del_action'], |
||
| 425 | $_POST['match'], |
||
| 426 | $_POST['deferrable'], |
||
| 427 | $_POST['initially'], |
||
| 428 | $_POST['name'] |
||
| 429 | ); |
||
| 430 | if (0 == $status) { |
||
| 431 | $this->doDefault($lang['strfkadded']); |
||
| 432 | } else { |
||
| 433 | $this->addForeignKey(2, $lang['strfkaddedbad']); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | |||
| 437 | break; |
||
| 438 | default: |
||
| 439 | $this->printTrail('table'); |
||
| 440 | $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key'); |
||
| 441 | $this->printMsg($msg); |
||
| 442 | |||
| 443 | $attrs = $data->getTableAttributes($_REQUEST['table']); |
||
| 444 | $tables = $data->getTables(true); |
||
| 445 | |||
| 446 | $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10); |
||
| 447 | $selColumns->set_style('width: 15em;'); |
||
| 448 | |||
| 449 | if ($attrs->recordCount() > 0) { |
||
| 450 | while (!$attrs->EOF) { |
||
| 451 | $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname'])); |
||
| 452 | $attrs->moveNext(); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10); |
||
| 457 | $selIndex->set_style('width: 15em;'); |
||
| 458 | $selIndex->set_attribute('id', 'IndexColumnList'); |
||
| 459 | $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>'); |
||
| 460 | $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 461 | $buttonAdd->set_attribute('type', 'button'); |
||
| 462 | |||
| 463 | $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<'); |
||
| 464 | $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 465 | $buttonRemove->set_attribute('type', 'button'); |
||
| 466 | |||
| 467 | echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n"; |
||
| 468 | |||
| 469 | echo "<table>\n"; |
||
| 470 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strname']}</th></tr>\n"; |
||
| 471 | echo "<tr><td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" /></td></tr>\n"; |
||
| 472 | echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=\"data required\">{$lang['strfkcolumnlist']}</th></tr>\n"; |
||
| 473 | echo '<tr><td class="data1">' . $selColumns->fetch() . "</td>\n"; |
||
| 474 | echo '<td class="data1" style="text-align: center">' . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>\n"; |
||
| 475 | echo '<td class=data1>' . $selIndex->fetch() . "</td></tr>\n"; |
||
| 476 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>"; |
||
| 477 | echo '<tr>'; |
||
| 478 | echo '<td class="data1" colspan="3"><select class="select2" name="target">'; |
||
| 479 | while (!$tables->EOF) { |
||
| 480 | $key = ['schemaname' => $tables->fields['nspname'], 'tablename' => $tables->fields['relname']]; |
||
| 481 | $key = serialize($key); |
||
| 482 | echo '<option value="', htmlspecialchars($key), '">'; |
||
| 483 | if ($tables->fields['nspname'] != $_REQUEST['schema']) { |
||
| 484 | echo htmlspecialchars($tables->fields['nspname']), '.'; |
||
| 485 | } |
||
| 486 | echo htmlspecialchars($tables->fields['relname']), "</option>\n"; |
||
| 487 | $tables->moveNext(); |
||
| 488 | } |
||
| 489 | echo "</select>\n"; |
||
| 490 | echo '</td></tr>'; |
||
| 491 | echo "</table>\n"; |
||
| 492 | |||
| 493 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n"; |
||
| 494 | echo $this->misc->form; |
||
| 495 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 496 | echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; |
||
| 497 | echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n"; |
||
| 498 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 499 | echo "</form>\n"; |
||
| 500 | //echo "<script>jQuery('select[name=\"target\"]').select2()</script>"; |
||
| 501 | break; |
||
| 502 | } |
||
| 753 |