Total Complexity | 62 |
Total Lines | 585 |
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) |
||
319 | } |
||
320 | } |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Show confirmation of drop and perform actual drop. |
||
325 | * |
||
326 | * @param mixed $confirm |
||
327 | */ |
||
328 | public function doDrop($confirm) |
||
383 | } |
||
384 | } |
||
385 | //END DROP |
||
386 | } |
||
387 | } |
||
388 | |||
389 | // END FUNCTION |
||
390 | |||
391 | /** |
||
392 | * Displays a screen where they can enter a new database. |
||
393 | * |
||
394 | * @param mixed $msg |
||
395 | */ |
||
396 | public function doCreate($msg = '') |
||
397 | { |
||
398 | $data = $this->misc->getDatabaseAccessor(); |
||
399 | |||
400 | $this->printTrail('server'); |
||
401 | $this->printTitle($this->lang['strcreatedatabase'], 'pg.database.create'); |
||
402 | $this->printMsg($msg); |
||
403 | |||
404 | $this->coalesceArr($_POST, 'formName', ''); |
||
405 | |||
406 | // Default encoding is that in language file |
||
407 | $this->coalesceArr($_POST, 'formEncoding', ''); |
||
408 | $this->coalesceArr($_POST, 'formTemplate', 'template1'); |
||
409 | |||
410 | $this->coalesceArr($_POST, 'formSpc', ''); |
||
411 | |||
412 | $this->coalesceArr($_POST, 'formComment', ''); |
||
413 | |||
414 | // Fetch a list of databases in the cluster |
||
415 | $templatedbs = $data->getDatabases(false); |
||
416 | |||
417 | $tablespaces = null; |
||
418 | // Fetch all tablespaces from the database |
||
419 | if ($data->hasTablespaces()) { |
||
420 | $tablespaces = $data->getTablespaces(); |
||
421 | } |
||
422 | |||
423 | echo '<form action="' . \SUBFOLDER . "/src/views/alldb\" method=\"post\">\n"; |
||
424 | echo "<table>\n"; |
||
425 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n"; |
||
426 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
427 | htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n"; |
||
428 | |||
429 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strtemplatedb']}</th>\n"; |
||
430 | echo "\t\t<td class=\"data1\">\n"; |
||
431 | echo "\t\t\t<select name=\"formTemplate\">\n"; |
||
432 | // Always offer template0 and template1 |
||
433 | echo "\t\t\t\t<option value=\"template0\"", |
||
434 | ('template0' == $_POST['formTemplate']) ? ' selected="selected"' : '', ">template0</option>\n"; |
||
435 | echo "\t\t\t\t<option value=\"template1\"", |
||
436 | ('template1' == $_POST['formTemplate']) ? ' selected="selected"' : '', ">template1</option>\n"; |
||
437 | while (!$templatedbs->EOF) { |
||
438 | $dbname = htmlspecialchars($templatedbs->fields['datname']); |
||
439 | if ('template1' != $dbname) { |
||
440 | // filter out for $this->conf[show_system] users so we dont get duplicates |
||
441 | echo "\t\t\t\t<option value=\"{$dbname}\"", |
||
442 | ($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n"; |
||
443 | } |
||
444 | $templatedbs->moveNext(); |
||
445 | } |
||
446 | echo "\t\t\t</select>\n"; |
||
447 | echo "\t\t</td>\n\t</tr>\n"; |
||
448 | |||
449 | // ENCODING |
||
450 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strencoding']}</th>\n"; |
||
451 | echo "\t\t<td class=\"data1\">\n"; |
||
452 | echo "\t\t\t<select name=\"formEncoding\">\n"; |
||
453 | echo "\t\t\t\t<option value=\"\"></option>\n"; |
||
454 | |||
455 | foreach ($data->codemap as $key) { |
||
456 | echo "\t\t\t\t<option value=\"", htmlspecialchars($key), '"', |
||
457 | ($key == $_POST['formEncoding']) ? ' selected="selected"' : '', '>', |
||
458 | $this->misc->printVal($key), "</option>\n"; |
||
459 | } |
||
460 | echo "\t\t\t</select>\n"; |
||
461 | echo "\t\t</td>\n\t</tr>\n"; |
||
462 | |||
463 | if ($data->hasDatabaseCollation()) { |
||
464 | $this->coalesceArr($_POST, 'formCollate', ''); |
||
465 | |||
466 | $this->coalesceArr($_POST, 'formCType', ''); |
||
467 | |||
468 | // LC_COLLATE |
||
469 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcollation']}</th>\n"; |
||
470 | echo "\t\t<td class=\"data1\">\n"; |
||
471 | echo "\t\t\t<input name=\"formCollate\" value=\"", htmlspecialchars($_POST['formCollate']), "\" />\n"; |
||
472 | echo "\t\t</td>\n\t</tr>\n"; |
||
473 | |||
474 | // LC_CTYPE |
||
475 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strctype']}</th>\n"; |
||
476 | echo "\t\t<td class=\"data1\">\n"; |
||
477 | echo "\t\t\t<input name=\"formCType\" value=\"", htmlspecialchars($_POST['formCType']), "\" />\n"; |
||
478 | echo "\t\t</td>\n\t</tr>\n"; |
||
479 | } |
||
480 | |||
481 | // Tablespace (if there are any) |
||
482 | if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) { |
||
483 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strtablespace']}</th>\n"; |
||
484 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n"; |
||
485 | // Always offer the default (empty) option |
||
486 | echo "\t\t\t\t<option value=\"\"", |
||
487 | ('' == $_POST['formSpc']) ? ' selected="selected"' : '', "></option>\n"; |
||
488 | // Display all other tablespaces |
||
489 | while (!$tablespaces->EOF) { |
||
490 | $spcname = htmlspecialchars($tablespaces->fields['spcname']); |
||
491 | echo "\t\t\t\t<option value=\"{$spcname}\"", |
||
492 | ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n"; |
||
493 | $tablespaces->moveNext(); |
||
494 | } |
||
495 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
||
496 | } |
||
497 | |||
498 | // Comments (if available) |
||
499 | if ($data->hasSharedComments()) { |
||
500 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n"; |
||
501 | echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
502 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
503 | } |
||
504 | |||
505 | echo "</table>\n"; |
||
506 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
507 | echo $this->misc->form; |
||
508 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n"; |
||
509 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
510 | echo "</form>\n"; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Actually creates the new view in the database. |
||
515 | */ |
||
516 | public function doSaveCreate() |
||
517 | { |
||
518 | $data = $this->misc->getDatabaseAccessor(); |
||
519 | |||
520 | // Default tablespace to null if it isn't set |
||
521 | $this->coalesceArr($_POST, 'formSpc', null); |
||
522 | |||
523 | // Default comment to blank if it isn't set |
||
524 | $this->coalesceArr($_POST, 'formComment', null); |
||
525 | |||
526 | // Default collate to blank if it isn't set |
||
527 | $this->coalesceArr($_POST, 'formCollate', null); |
||
528 | |||
529 | // Default ctype to blank if it isn't set |
||
530 | $this->coalesceArr($_POST, 'formCType', null); |
||
531 | |||
532 | // Check that they've given a name and a definition |
||
533 | if ('' == $_POST['formName']) { |
||
534 | $this->doCreate($this->lang['strdatabaseneedsname']); |
||
535 | } else { |
||
536 | $status = $data->createDatabase( |
||
537 | $_POST['formName'], |
||
538 | $_POST['formEncoding'], |
||
539 | $_POST['formSpc'], |
||
540 | $_POST['formComment'], |
||
541 | $_POST['formTemplate'], |
||
542 | $_POST['formCollate'], |
||
543 | $_POST['formCType'] |
||
544 | ); |
||
545 | if (0 == $status) { |
||
546 | $this->misc->setReloadBrowser(true); |
||
547 | $this->doDefault($this->lang['strdatabasecreated']); |
||
548 | } else { |
||
549 | $this->doCreate($this->lang['strdatabasecreatedbad']); |
||
550 | } |
||
551 | } |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Displays options for cluster download. |
||
556 | * |
||
557 | * @param mixed $msg |
||
558 | */ |
||
559 | public function doExport($msg = '') |
||
601 | } |
||
602 | } |
||
603 |