Total Complexity | 56 |
Total Lines | 657 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DomainsController 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 DomainsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class DomainsController extends BaseController |
||
17 | { |
||
18 | public $controller_title = 'strdomains'; |
||
19 | |||
20 | /** |
||
21 | * Default method to render the controller according to the action parameter. |
||
22 | */ |
||
23 | public function render() |
||
24 | { |
||
25 | if ('tree' == $this->action) { |
||
26 | return $this->doTree(); |
||
27 | } |
||
28 | |||
29 | $this->printHeader(); |
||
30 | $this->printBody(); |
||
31 | |||
32 | switch ($this->action) { |
||
33 | case 'add_check': |
||
34 | $this->addCheck(true); |
||
35 | |||
36 | break; |
||
37 | case 'save_add_check': |
||
38 | if (isset($_POST['cancel'])) { |
||
39 | $this->doProperties(); |
||
40 | } else { |
||
41 | $this->addCheck(false); |
||
42 | } |
||
43 | |||
44 | break; |
||
45 | case 'drop_con': |
||
46 | if (isset($_POST['drop'])) { |
||
47 | $this->doDropConstraint(false); |
||
48 | } else { |
||
49 | $this->doProperties(); |
||
50 | } |
||
51 | |||
52 | break; |
||
53 | case 'confirm_drop_con': |
||
54 | $this->doDropConstraint(true); |
||
55 | |||
56 | break; |
||
57 | case 'save_create': |
||
58 | if (isset($_POST['cancel'])) { |
||
59 | $this->doDefault(); |
||
60 | } else { |
||
61 | $this->doSaveCreate(); |
||
62 | } |
||
63 | |||
64 | break; |
||
65 | case 'create': |
||
66 | $this->doCreate(); |
||
67 | |||
68 | break; |
||
69 | case 'drop': |
||
70 | if (isset($_POST['drop'])) { |
||
71 | $this->doDrop(false); |
||
72 | } else { |
||
73 | $this->doDefault(); |
||
74 | } |
||
75 | |||
76 | break; |
||
77 | case 'confirm_drop': |
||
78 | $this->doDrop(true); |
||
79 | |||
80 | break; |
||
81 | case 'save_alter': |
||
82 | if (isset($_POST['alter'])) { |
||
83 | $this->doSaveAlter(); |
||
84 | } else { |
||
85 | $this->doProperties(); |
||
86 | } |
||
87 | |||
88 | break; |
||
89 | case 'alter': |
||
90 | $this->doAlter(); |
||
91 | |||
92 | break; |
||
93 | case 'properties': |
||
94 | $this->doProperties(); |
||
95 | |||
96 | break; |
||
97 | default: |
||
98 | $this->doDefault(); |
||
99 | |||
100 | break; |
||
101 | } |
||
102 | |||
103 | return $this->printFooter(); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Show default list of domains in the database. |
||
108 | * |
||
109 | * @param mixed $msg |
||
110 | */ |
||
111 | public function doDefault($msg = '') |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Generate XML for the browser tree. |
||
209 | */ |
||
210 | public function doTree() |
||
211 | { |
||
212 | $data = $this->misc->getDatabaseAccessor(); |
||
213 | |||
214 | $domains = $data->getDomains(); |
||
215 | |||
216 | $reqvars = $this->misc->getRequestVars('domain'); |
||
217 | |||
218 | $attrs = [ |
||
219 | 'text' => Decorator::field('domname'), |
||
220 | 'icon' => 'Domain', |
||
221 | 'toolTip' => Decorator::field('domcomment'), |
||
222 | 'action' => Decorator::actionurl( |
||
223 | 'domains', |
||
224 | $reqvars, |
||
225 | [ |
||
226 | 'action' => 'properties', |
||
227 | 'domain' => Decorator::field('domname'), |
||
228 | ] |
||
229 | ), |
||
230 | ]; |
||
231 | |||
232 | return $this->printTree($domains, $attrs, 'domains'); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Function to save after altering a domain. |
||
237 | */ |
||
238 | public function doSaveAlter() |
||
239 | { |
||
240 | $data = $this->misc->getDatabaseAccessor(); |
||
241 | |||
242 | $status = $data->alterDomain( |
||
243 | $_POST['domain'], |
||
244 | $_POST['domdefault'], |
||
245 | isset($_POST['domnotnull']), |
||
246 | $_POST['domowner'] |
||
247 | ); |
||
248 | if (0 == $status) { |
||
249 | $this->doProperties($this->lang['strdomainaltered']); |
||
250 | } else { |
||
251 | $this->doAlter($this->lang['strdomainalteredbad']); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Allow altering a domain. |
||
257 | * |
||
258 | * @param mixed $msg |
||
259 | */ |
||
260 | public function doAlter($msg = '') |
||
315 | } |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Confirm and then actually add a CHECK constraint. |
||
320 | * |
||
321 | * @param mixed $confirm |
||
322 | * @param mixed $msg |
||
323 | */ |
||
324 | public function addCheck($confirm, $msg = '') |
||
325 | { |
||
326 | $data = $this->misc->getDatabaseAccessor(); |
||
327 | |||
328 | $this->coalesceArr($_POST, 'name', ''); |
||
329 | |||
330 | $this->coalesceArr($_POST, 'definition', ''); |
||
331 | |||
332 | if ($confirm) { |
||
333 | $this->printTrail('domain'); |
||
334 | $this->printTitle($this->lang['straddcheck'], 'pg.constraint.check'); |
||
335 | $this->printMsg($msg); |
||
336 | |||
337 | echo '<form action="' . \SUBFOLDER . "/src/views/domains\" method=\"post\">\n"; |
||
338 | echo "<table>\n"; |
||
339 | echo "<tr><th class=\"data\">{$this->lang['strname']}</th>\n"; |
||
340 | echo "<th class=\"data required\">{$this->lang['strdefinition']}</th></tr>\n"; |
||
341 | |||
342 | echo "<tr><td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
343 | htmlspecialchars($_POST['name']), "\" /></td>\n"; |
||
344 | |||
345 | echo '<td class="data1">(<input name="definition" size="32" value="', |
||
346 | htmlspecialchars($_POST['definition']), "\" />)</td></tr>\n"; |
||
347 | echo "</table>\n"; |
||
348 | |||
349 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_check\" />\n"; |
||
350 | echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n"; |
||
351 | echo $this->misc->form; |
||
352 | echo "<input type=\"submit\" name=\"add\" value=\"{$this->lang['stradd']}\" />\n"; |
||
353 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
354 | echo "</form>\n"; |
||
355 | } else { |
||
356 | if ('' == trim($_POST['definition'])) { |
||
357 | $this->addCheck(true, $this->lang['strcheckneedsdefinition']); |
||
358 | } else { |
||
359 | $status = $data->addDomainCheckConstraint( |
||
360 | $_POST['domain'], |
||
361 | $_POST['definition'], |
||
362 | $_POST['name'] |
||
363 | ); |
||
364 | if (0 == $status) { |
||
365 | $this->doProperties($this->lang['strcheckadded']); |
||
366 | } else { |
||
367 | $this->addCheck(true, $this->lang['strcheckaddedbad']); |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Show confirmation of drop constraint and perform actual drop. |
||
375 | * |
||
376 | * @param mixed $confirm |
||
377 | * @param mixed $msg |
||
378 | */ |
||
379 | public function doDropConstraint($confirm, $msg = '') |
||
380 | { |
||
381 | $data = $this->misc->getDatabaseAccessor(); |
||
382 | |||
383 | if ($confirm) { |
||
384 | $this->printTrail('domain'); |
||
385 | $this->printTitle($this->lang['strdrop'], 'pg.constraint.drop'); |
||
386 | $this->printMsg($msg); |
||
387 | |||
388 | echo '<p>', sprintf( |
||
389 | $this->lang['strconfdropconstraint'], |
||
390 | $this->misc->printVal($_REQUEST['constraint']), |
||
391 | $this->misc->printVal($_REQUEST['domain']) |
||
392 | ), "</p>\n"; |
||
393 | echo '<form action="' . \SUBFOLDER . "/src/views/domains\" method=\"post\">\n"; |
||
394 | echo "<input type=\"hidden\" name=\"action\" value=\"drop_con\" />\n"; |
||
395 | echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n"; |
||
396 | echo '<input type="hidden" name="constraint" value="', htmlspecialchars($_REQUEST['constraint']), "\" />\n"; |
||
397 | echo $this->misc->form; |
||
398 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>\n"; |
||
399 | echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n"; |
||
400 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n"; |
||
401 | echo "</form>\n"; |
||
402 | } else { |
||
403 | $status = $data->dropDomainConstraint($_POST['domain'], $_POST['constraint'], isset($_POST['cascade'])); |
||
404 | if (0 == $status) { |
||
405 | $this->doProperties($this->lang['strconstraintdropped']); |
||
406 | } else { |
||
407 | $this->doDropConstraint(true, $this->lang['strconstraintdroppedbad']); |
||
408 | } |
||
409 | } |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Show properties for a domain. Allow manipulating constraints as well. |
||
414 | * |
||
415 | * @param mixed $msg |
||
416 | */ |
||
417 | public function doProperties($msg = '') |
||
418 | { |
||
419 | $data = $this->misc->getDatabaseAccessor(); |
||
420 | |||
421 | $this->printTrail('domain'); |
||
422 | $this->printTitle($this->lang['strproperties'], 'pg.domain'); |
||
423 | $this->printMsg($msg); |
||
424 | |||
425 | $domaindata = $data->getDomain($_REQUEST['domain']); |
||
426 | |||
427 | if ($domaindata->recordCount() > 0) { |
||
428 | // Show comment if any |
||
429 | if (null !== $domaindata->fields['domcomment']) { |
||
430 | echo '<p class="comment">', $this->misc->printVal($domaindata->fields['domcomment']), "</p>\n"; |
||
431 | } |
||
432 | |||
433 | // Display domain info |
||
434 | $domaindata->fields['domnotnull'] = $data->phpBool($domaindata->fields['domnotnull']); |
||
435 | echo "<table>\n"; |
||
436 | echo "<tr><th class=\"data left\" style=\"width: 70px\">{$this->lang['strname']}</th>\n"; |
||
437 | echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domname']), "</td></tr>\n"; |
||
438 | echo "<tr><th class=\"data left\">{$this->lang['strtype']}</th>\n"; |
||
439 | echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domtype']), "</td></tr>\n"; |
||
440 | echo "<tr><th class=\"data left\">{$this->lang['strnotnull']}</th>\n"; |
||
441 | echo '<td class="data1">', ($domaindata->fields['domnotnull'] ? 'NOT NULL' : ''), "</td></tr>\n"; |
||
442 | echo "<tr><th class=\"data left\">{$this->lang['strdefault']}</th>\n"; |
||
443 | echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domdef']), "</td></tr>\n"; |
||
444 | echo "<tr><th class=\"data left\">{$this->lang['strowner']}</th>\n"; |
||
445 | echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domowner']), "</td></tr>\n"; |
||
446 | echo "</table>\n"; |
||
447 | |||
448 | // Display domain constraints |
||
449 | echo "<h3>{$this->lang['strconstraints']}</h3>\n"; |
||
450 | if ($data->hasDomainConstraints()) { |
||
451 | $domaincons = $data->getDomainConstraints($_REQUEST['domain']); |
||
452 | |||
453 | $columns = [ |
||
454 | 'name' => [ |
||
455 | 'title' => $this->lang['strname'], |
||
456 | 'field' => Decorator::field('conname'), |
||
457 | ], |
||
458 | 'definition' => [ |
||
459 | 'title' => $this->lang['strdefinition'], |
||
460 | 'field' => Decorator::field('consrc'), |
||
461 | ], |
||
462 | 'actions' => [ |
||
463 | 'title' => $this->lang['stractions'], |
||
464 | ], |
||
465 | ]; |
||
466 | |||
467 | $actions = [ |
||
468 | 'drop' => [ |
||
469 | 'content' => $this->lang['strdrop'], |
||
470 | 'attr' => [ |
||
471 | 'href' => [ |
||
472 | 'url' => 'domains', |
||
473 | 'urlvars' => [ |
||
474 | 'action' => 'confirm_drop_con', |
||
475 | 'domain' => $_REQUEST['domain'], |
||
476 | 'constraint' => Decorator::field('conname'), |
||
477 | 'type' => Decorator::field('contype'), |
||
478 | ], |
||
479 | ], |
||
480 | ], |
||
481 | ], |
||
482 | ]; |
||
483 | |||
484 | echo $this->printTable($domaincons, $columns, $actions, 'domains-properties', $this->lang['strnodata']); |
||
485 | } |
||
486 | } else { |
||
487 | echo "<p>{$this->lang['strnodata']}</p>\n"; |
||
488 | } |
||
489 | |||
490 | $navlinks = [ |
||
491 | 'drop' => [ |
||
492 | 'attr' => [ |
||
493 | 'href' => [ |
||
494 | 'url' => 'domains', |
||
495 | 'urlvars' => [ |
||
496 | 'action' => 'confirm_drop', |
||
497 | 'server' => $_REQUEST['server'], |
||
498 | 'database' => $_REQUEST['database'], |
||
499 | 'schema' => $_REQUEST['schema'], |
||
500 | 'domain' => $_REQUEST['domain'], |
||
501 | ], |
||
502 | ], |
||
503 | ], |
||
504 | 'content' => $this->lang['strdrop'], |
||
505 | ], |
||
506 | ]; |
||
507 | if ($data->hasAlterDomains()) { |
||
508 | $navlinks['addcheck'] = [ |
||
509 | 'attr' => [ |
||
510 | 'href' => [ |
||
511 | 'url' => 'domains', |
||
512 | 'urlvars' => [ |
||
513 | 'action' => 'add_check', |
||
514 | 'server' => $_REQUEST['server'], |
||
515 | 'database' => $_REQUEST['database'], |
||
516 | 'schema' => $_REQUEST['schema'], |
||
517 | 'domain' => $_REQUEST['domain'], |
||
518 | ], |
||
519 | ], |
||
520 | ], |
||
521 | 'content' => $this->lang['straddcheck'], |
||
522 | ]; |
||
523 | $navlinks['alter'] = [ |
||
524 | 'attr' => [ |
||
525 | 'href' => [ |
||
526 | 'url' => 'domains', |
||
527 | 'urlvars' => [ |
||
528 | 'action' => 'alter', |
||
529 | 'server' => $_REQUEST['server'], |
||
530 | 'database' => $_REQUEST['database'], |
||
531 | 'schema' => $_REQUEST['schema'], |
||
532 | 'domain' => $_REQUEST['domain'], |
||
533 | ], |
||
534 | ], |
||
535 | ], |
||
536 | 'content' => $this->lang['stralter'], |
||
537 | ]; |
||
538 | } |
||
539 | |||
540 | $this->printNavLinks($navlinks, 'domains-properties', get_defined_vars()); |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Show confirmation of drop and perform actual drop. |
||
545 | * |
||
546 | * @param mixed $confirm |
||
547 | */ |
||
548 | public function doDrop($confirm) |
||
549 | { |
||
550 | $data = $this->misc->getDatabaseAccessor(); |
||
551 | |||
552 | if ($confirm) { |
||
553 | $this->printTrail('domain'); |
||
554 | $this->printTitle($this->lang['strdrop'], 'pg.domain.drop'); |
||
555 | |||
556 | echo '<p>', sprintf($this->lang['strconfdropdomain'], $this->misc->printVal($_REQUEST['domain'])), "</p>\n"; |
||
557 | echo '<form action="' . \SUBFOLDER . "/src/views/domains\" method=\"post\">\n"; |
||
558 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /><label for=\"cascade\">{$this->lang['strcascade']}</label></p>\n"; |
||
559 | echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n"; |
||
560 | echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n"; |
||
561 | echo $this->misc->form; |
||
562 | echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n"; |
||
563 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
564 | echo "</form>\n"; |
||
565 | } else { |
||
566 | $status = $data->dropDomain($_POST['domain'], isset($_POST['cascade'])); |
||
567 | if (0 == $status) { |
||
568 | $this->doDefault($this->lang['strdomaindropped']); |
||
569 | } else { |
||
570 | $this->doDefault($this->lang['strdomaindroppedbad']); |
||
571 | } |
||
572 | } |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Displays a screen where they can enter a new domain. |
||
577 | * |
||
578 | * @param mixed $msg |
||
579 | */ |
||
580 | public function doCreate($msg = '') |
||
581 | { |
||
582 | $data = $this->misc->getDatabaseAccessor(); |
||
583 | |||
584 | $this->coalesceArr($_POST, 'domname', ''); |
||
585 | |||
586 | $this->coalesceArr($_POST, 'domtype', ''); |
||
587 | |||
588 | $this->coalesceArr($_POST, 'domlength', ''); |
||
589 | |||
590 | $this->coalesceArr($_POST, 'domarray', ''); |
||
591 | |||
592 | $this->coalesceArr($_POST, 'domdefault', ''); |
||
593 | |||
594 | $this->coalesceArr($_POST, 'domcheck', ''); |
||
595 | |||
596 | $types = $data->getTypes(true); |
||
597 | |||
598 | $this->printTrail('schema'); |
||
599 | $this->printTitle($this->lang['strcreatedomain'], 'pg.domain.create'); |
||
600 | $this->printMsg($msg); |
||
601 | |||
602 | echo '<form action="' . \SUBFOLDER . "/src/views/domains\" method=\"post\">\n"; |
||
603 | echo "<table>\n"; |
||
604 | echo "<tr><th class=\"data left required\" style=\"width: 70px\">{$this->lang['strname']}</th>\n"; |
||
605 | echo "<td class=\"data1\"><input name=\"domname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
606 | htmlspecialchars($_POST['domname']), "\" /></td></tr>\n"; |
||
607 | echo "<tr><th class=\"data left required\">{$this->lang['strtype']}</th>\n"; |
||
608 | echo "<td class=\"data1\">\n"; |
||
609 | // Output return type list |
||
610 | echo "<select name=\"domtype\">\n"; |
||
611 | while (!$types->EOF) { |
||
612 | echo '<option value="', htmlspecialchars($types->fields['typname']), '"', |
||
613 | ($types->fields['typname'] == $_POST['domtype']) ? ' selected="selected"' : '', '>', |
||
614 | $this->misc->printVal($types->fields['typname']), "</option>\n"; |
||
615 | $types->moveNext(); |
||
616 | } |
||
617 | echo "</select>\n"; |
||
618 | |||
619 | // Type length |
||
620 | echo '<input type="text" size="4" name="domlength" value="', htmlspecialchars($_POST['domlength']), '" />'; |
||
621 | |||
622 | // Output array type selector |
||
623 | echo "<select name=\"domarray\">\n"; |
||
624 | echo '<option value=""', ('' == $_POST['domarray']) ? ' selected="selected"' : '', "></option>\n"; |
||
625 | echo '<option value="[]"', ('[]' == $_POST['domarray']) ? ' selected="selected"' : '', ">[ ]</option>\n"; |
||
626 | echo "</select></td></tr>\n"; |
||
627 | |||
628 | echo "<tr><th class=\"data left\"><label for=\"domnotnull\">{$this->lang['strnotnull']}</label></th>\n"; |
||
629 | echo '<td class="data1"><input type="checkbox" id="domnotnull" name="domnotnull"', |
||
630 | (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), " /></td></tr>\n"; |
||
631 | echo "<tr><th class=\"data left\">{$this->lang['strdefault']}</th>\n"; |
||
632 | echo '<td class="data1"><input name="domdefault" size="32" value="', |
||
633 | htmlspecialchars($_POST['domdefault']), "\" /></td></tr>\n"; |
||
634 | if ($data->hasDomainConstraints()) { |
||
635 | echo "<tr><th class=\"data left\">{$this->lang['strconstraints']}</th>\n"; |
||
636 | echo '<td class="data1">CHECK (<input name="domcheck" size="32" value="', |
||
637 | htmlspecialchars($_POST['domcheck']), "\" />)</td></tr>\n"; |
||
638 | } |
||
639 | echo "</table>\n"; |
||
640 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
641 | echo $this->misc->form; |
||
642 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n"; |
||
643 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
644 | echo "</form>\n"; |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Actually creates the new domain in the database. |
||
649 | */ |
||
650 | public function doSaveCreate() |
||
673 | } |
||
674 | } |
||
675 | } |
||
676 | } |
||
677 |