Total Complexity | 73 |
Total Lines | 607 |
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 |
||
14 | class AlldbController extends BaseController |
||
15 | { |
||
16 | public $controller_name = 'AlldbController'; |
||
17 | public $table_place = 'alldb-databases'; |
||
18 | |||
19 | /** |
||
20 | * Default method to render the controller according to the action parameter. |
||
21 | */ |
||
22 | public function render() |
||
23 | { |
||
24 | $lang = $this->lang; |
||
25 | $action = $this->action; |
||
26 | |||
27 | if ('tree' == $action) { |
||
28 | return $this->doTree(); |
||
29 | } |
||
30 | |||
31 | $this->printHeader($lang['strdatabases']); |
||
32 | $this->printBody(); |
||
33 | |||
34 | switch ($action) { |
||
35 | case 'export': |
||
36 | $this->doExport(); |
||
37 | |||
38 | break; |
||
39 | case 'save_create': |
||
40 | if (isset($_POST['cancel'])) { |
||
41 | $this->doDefault(); |
||
42 | } else { |
||
43 | $this->doSaveCreate(); |
||
44 | } |
||
45 | |||
46 | break; |
||
47 | case 'create': |
||
48 | $this->doCreate(); |
||
49 | |||
50 | break; |
||
51 | case 'drop': |
||
52 | if (isset($_REQUEST['drop'])) { |
||
53 | $this->doDrop(false); |
||
54 | } else { |
||
55 | $this->doDefault(); |
||
56 | } |
||
57 | |||
58 | break; |
||
59 | case 'confirm_drop': |
||
60 | $this->doDrop(true); |
||
61 | |||
62 | break; |
||
63 | case 'alter': |
||
64 | if (isset($_POST['oldname'], $_POST['newname']) && !isset($_POST['cancel'])) { |
||
65 | $this->doAlter(false); |
||
66 | } else { |
||
67 | $this->doDefault(); |
||
68 | } |
||
69 | |||
70 | break; |
||
71 | case 'confirm_alter': |
||
72 | $this->doAlter(true); |
||
73 | |||
74 | break; |
||
75 | default: |
||
76 | $this->doDefault(); |
||
77 | |||
78 | break; |
||
79 | } |
||
80 | |||
81 | return $this->printFooter(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Show default list of databases in the server. |
||
86 | * |
||
87 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
88 | */ |
||
89 | public function doDefault($msg = '') |
||
90 | { |
||
91 | $lang = $this->lang; |
||
92 | |||
93 | $this->printTrail('server'); |
||
94 | $this->printTabs('server', 'databases'); |
||
95 | $this->printMsg($msg); |
||
96 | $data = $this->misc->getDatabaseAccessor(); |
||
97 | $databases = $data->getDatabases(); |
||
98 | |||
99 | $columns = [ |
||
100 | 'database' => [ |
||
101 | 'title' => $lang['strdatabase'], |
||
102 | 'field' => Decorator::field('datname'), |
||
103 | 'url' => \SUBFOLDER . "/redirect/database?{$this->misc->href}&", |
||
104 | 'vars' => ['database' => 'datname'], |
||
105 | ], |
||
106 | 'owner' => [ |
||
107 | 'title' => $lang['strowner'], |
||
108 | 'field' => Decorator::field('datowner'), |
||
109 | ], |
||
110 | 'encoding' => [ |
||
111 | 'title' => $lang['strencoding'], |
||
112 | 'field' => Decorator::field('datencoding'), |
||
113 | ], |
||
114 | 'lc_collate' => [ |
||
115 | 'title' => $lang['strcollation'], |
||
116 | 'field' => Decorator::field('datcollate'), |
||
117 | ], |
||
118 | 'lc_ctype' => [ |
||
119 | 'title' => $lang['strctype'], |
||
120 | 'field' => Decorator::field('datctype'), |
||
121 | ], |
||
122 | 'tablespace' => [ |
||
123 | 'title' => $lang['strtablespace'], |
||
124 | 'field' => Decorator::field('tablespace'), |
||
125 | ], |
||
126 | 'dbsize' => [ |
||
127 | 'title' => $lang['strsize'], |
||
128 | 'field' => Decorator::field('dbsize'), |
||
129 | 'type' => 'prettysize', |
||
130 | ], |
||
131 | 'actions' => [ |
||
132 | 'title' => $lang['stractions'], |
||
133 | ], |
||
134 | 'comment' => [ |
||
135 | 'title' => $lang['strcomment'], |
||
136 | 'field' => Decorator::field('datcomment'), |
||
137 | ], |
||
138 | ]; |
||
139 | |||
140 | $actions = [ |
||
141 | 'multiactions' => [ |
||
142 | 'keycols' => ['database' => 'datname'], |
||
143 | 'url' => 'alldb.php', |
||
144 | 'default' => null, |
||
145 | ], |
||
146 | 'drop' => [ |
||
147 | 'content' => $lang['strdrop'], |
||
148 | 'attr' => [ |
||
149 | 'href' => [ |
||
150 | 'url' => 'alldb.php', |
||
151 | 'urlvars' => [ |
||
152 | 'subject' => 'database', |
||
153 | 'action' => 'confirm_drop', |
||
154 | 'dropdatabase' => Decorator::field('datname'), |
||
155 | ], |
||
156 | ], |
||
157 | ], |
||
158 | 'multiaction' => 'confirm_drop', |
||
159 | ], |
||
160 | 'privileges' => [ |
||
161 | 'content' => $lang['strprivileges'], |
||
162 | 'attr' => [ |
||
163 | 'href' => [ |
||
164 | 'url' => 'privileges.php', |
||
165 | 'urlvars' => [ |
||
166 | 'subject' => 'database', |
||
167 | 'database' => Decorator::field('datname'), |
||
168 | ], |
||
169 | ], |
||
170 | ], |
||
171 | ], |
||
172 | ]; |
||
173 | if ($data->hasAlterDatabase()) { |
||
174 | $actions['alter'] = [ |
||
175 | 'content' => $lang['stralter'], |
||
176 | 'attr' => [ |
||
177 | 'href' => [ |
||
178 | 'url' => 'alldb.php', |
||
179 | 'urlvars' => [ |
||
180 | 'subject' => 'database', |
||
181 | 'action' => 'confirm_alter', |
||
182 | 'alterdatabase' => Decorator::field('datname'), |
||
183 | ], |
||
184 | ], |
||
185 | ], |
||
186 | ]; |
||
187 | } |
||
188 | |||
189 | if (!$data->hasTablespaces()) { |
||
190 | unset($columns['tablespace']); |
||
191 | } |
||
192 | |||
193 | if (!$data->hasServerAdminFuncs()) { |
||
194 | unset($columns['dbsize']); |
||
195 | } |
||
196 | |||
197 | if (!$data->hasDatabaseCollation()) { |
||
198 | unset($columns['lc_collate'], $columns['lc_ctype']); |
||
199 | } |
||
200 | |||
201 | if (!isset($data->privlist['database'])) { |
||
202 | unset($actions['privileges']); |
||
203 | } |
||
204 | |||
205 | echo $this->printTable($databases, $columns, $actions, $this->table_place, $lang['strnodatabases']); |
||
206 | |||
207 | $navlinks = [ |
||
208 | 'create' => [ |
||
209 | 'attr' => [ |
||
210 | 'href' => [ |
||
211 | 'url' => 'alldb.php', |
||
212 | 'urlvars' => [ |
||
213 | 'action' => 'create', |
||
214 | 'server' => $_REQUEST['server'], |
||
215 | ], |
||
216 | ], |
||
217 | ], |
||
218 | 'content' => $lang['strcreatedatabase'], |
||
219 | ], |
||
220 | ]; |
||
221 | $this->printNavLinks($navlinks, $this->table_place, get_defined_vars()); |
||
222 | } |
||
223 | |||
224 | public function doTree() |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Display a form for alter and perform actual alter. |
||
248 | * |
||
249 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
250 | */ |
||
251 | public function doAlter($confirm) |
||
252 | { |
||
253 | $lang = $this->lang; |
||
254 | $data = $this->misc->getDatabaseAccessor(); |
||
255 | |||
256 | if ($confirm) { |
||
257 | $this->printTrail('database'); |
||
258 | $this->printTitle($lang['stralter'], 'pg.database.alter'); |
||
259 | |||
260 | echo '<form action="' . \SUBFOLDER . "/src/views/alldb.php\" method=\"post\">\n"; |
||
261 | echo "<table>\n"; |
||
262 | echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
263 | echo '<td class="data1">'; |
||
264 | echo "<input name=\"newname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
265 | htmlspecialchars($_REQUEST['alterdatabase']), "\" /></td></tr>\n"; |
||
266 | |||
267 | if ($data->hasAlterDatabaseOwner() && $data->isSuperUser()) { |
||
268 | // Fetch all users |
||
269 | |||
270 | $rs = $data->getDatabaseOwner($_REQUEST['alterdatabase']); |
||
271 | $owner = isset($rs->fields['usename']) ? $rs->fields['usename'] : ''; |
||
272 | $users = $data->getUsers(); |
||
273 | |||
274 | echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n"; |
||
275 | echo '<td class="data1"><select name="owner">'; |
||
276 | while (!$users->EOF) { |
||
277 | $uname = $users->fields['usename']; |
||
278 | echo '<option value="', htmlspecialchars($uname), '"', |
||
279 | ($uname == $owner) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n"; |
||
280 | $users->moveNext(); |
||
281 | } |
||
282 | echo "</select></td></tr>\n"; |
||
283 | } |
||
284 | if ($data->hasSharedComments()) { |
||
285 | $rs = $data->getDatabaseComment($_REQUEST['alterdatabase']); |
||
286 | $comment = isset($rs->fields['description']) ? $rs->fields['description'] : ''; |
||
287 | echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
288 | echo '<td class="data1">'; |
||
289 | echo '<textarea rows="3" cols="32" name="dbcomment">', |
||
290 | htmlspecialchars($comment), "</textarea></td></tr>\n"; |
||
291 | } |
||
292 | echo "</table>\n"; |
||
293 | echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n"; |
||
294 | echo $this->misc->form; |
||
295 | echo '<input type="hidden" name="oldname" value="', |
||
296 | htmlspecialchars($_REQUEST['alterdatabase']), "\" />\n"; |
||
297 | echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n"; |
||
298 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
||
299 | echo "</form>\n"; |
||
300 | } else { |
||
301 | if (!isset($_POST['owner'])) { |
||
302 | $_POST['owner'] = ''; |
||
303 | } |
||
304 | |||
305 | if (!isset($_POST['dbcomment'])) { |
||
306 | $_POST['dbcomment'] = ''; |
||
307 | } |
||
308 | |||
309 | if (0 == $data->alterDatabase($_POST['oldname'], $_POST['newname'], $_POST['owner'], $_POST['dbcomment'])) { |
||
310 | $this->misc->setReloadBrowser(true); |
||
311 | $this->doDefault($lang['strdatabasealtered']); |
||
312 | } else { |
||
313 | $this->doDefault($lang['strdatabasealteredbad']); |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Show confirmation of drop and perform actual drop. |
||
320 | * |
||
321 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
322 | */ |
||
323 | public function doDrop($confirm) |
||
378 | } |
||
379 | } |
||
380 | } //END DROP |
||
381 | } |
||
382 | |||
383 | // END FUNCTION |
||
384 | |||
385 | /** |
||
386 | * Displays a screen where they can enter a new database. |
||
387 | * |
||
388 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
389 | */ |
||
390 | public function doCreate($msg = '') |
||
391 | { |
||
392 | $lang = $this->lang; |
||
393 | $data = $this->misc->getDatabaseAccessor(); |
||
394 | |||
395 | $this->printTrail('server'); |
||
396 | $this->printTitle($lang['strcreatedatabase'], 'pg.database.create'); |
||
397 | $this->printMsg($msg); |
||
398 | |||
399 | if (!isset($_POST['formName'])) { |
||
400 | $_POST['formName'] = ''; |
||
401 | } |
||
402 | |||
403 | // Default encoding is that in language file |
||
404 | if (!isset($_POST['formEncoding'])) { |
||
405 | $_POST['formEncoding'] = ''; |
||
406 | } |
||
407 | if (!isset($_POST['formTemplate'])) { |
||
408 | $_POST['formTemplate'] = 'template1'; |
||
409 | } |
||
410 | |||
411 | if (!isset($_POST['formSpc'])) { |
||
412 | $_POST['formSpc'] = ''; |
||
413 | } |
||
414 | |||
415 | if (!isset($_POST['formComment'])) { |
||
416 | $_POST['formComment'] = ''; |
||
417 | } |
||
418 | |||
419 | // Fetch a list of databases in the cluster |
||
420 | $templatedbs = $data->getDatabases(false); |
||
421 | |||
422 | // Fetch all tablespaces from the database |
||
423 | if ($data->hasTablespaces()) { |
||
424 | $tablespaces = $data->getTablespaces(); |
||
425 | } |
||
426 | |||
427 | echo '<form action="' . \SUBFOLDER . "/src/views/alldb.php\" method=\"post\">\n"; |
||
428 | echo "<table>\n"; |
||
429 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
430 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
431 | htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n"; |
||
432 | |||
433 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strtemplatedb']}</th>\n"; |
||
434 | echo "\t\t<td class=\"data1\">\n"; |
||
435 | echo "\t\t\t<select name=\"formTemplate\">\n"; |
||
436 | // Always offer template0 and template1 |
||
437 | echo "\t\t\t\t<option value=\"template0\"", |
||
438 | ('template0' == $_POST['formTemplate']) ? ' selected="selected"' : '', ">template0</option>\n"; |
||
439 | echo "\t\t\t\t<option value=\"template1\"", |
||
440 | ('template1' == $_POST['formTemplate']) ? ' selected="selected"' : '', ">template1</option>\n"; |
||
441 | while (!$templatedbs->EOF) { |
||
442 | $dbname = htmlspecialchars($templatedbs->fields['datname']); |
||
443 | if ('template1' != $dbname) { |
||
444 | // filter out for $conf[show_system] users so we dont get duplicates |
||
445 | echo "\t\t\t\t<option value=\"{$dbname}\"", |
||
446 | ($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n"; |
||
447 | } |
||
448 | $templatedbs->moveNext(); |
||
449 | } |
||
450 | echo "\t\t\t</select>\n"; |
||
451 | echo "\t\t</td>\n\t</tr>\n"; |
||
452 | |||
453 | // ENCODING |
||
454 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strencoding']}</th>\n"; |
||
455 | echo "\t\t<td class=\"data1\">\n"; |
||
456 | echo "\t\t\t<select name=\"formEncoding\">\n"; |
||
457 | echo "\t\t\t\t<option value=\"\"></option>\n"; |
||
458 | |||
459 | foreach ($data->codemap as $index => $key) { |
||
460 | echo "\t\t\t\t<option value=\"", htmlspecialchars($key), '"', |
||
461 | ($key == $_POST['formEncoding']) ? ' selected="selected"' : '', '>', |
||
462 | $this->misc->printVal($key), "</option>\n"; |
||
463 | } |
||
464 | echo "\t\t\t</select>\n"; |
||
465 | echo "\t\t</td>\n\t</tr>\n"; |
||
466 | |||
467 | if ($data->hasDatabaseCollation()) { |
||
468 | if (!isset($_POST['formCollate'])) { |
||
469 | $_POST['formCollate'] = ''; |
||
470 | } |
||
471 | |||
472 | if (!isset($_POST['formCType'])) { |
||
473 | $_POST['formCType'] = ''; |
||
474 | } |
||
475 | |||
476 | // LC_COLLATE |
||
477 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcollation']}</th>\n"; |
||
478 | echo "\t\t<td class=\"data1\">\n"; |
||
479 | echo "\t\t\t<input name=\"formCollate\" value=\"", htmlspecialchars($_POST['formCollate']), "\" />\n"; |
||
480 | echo "\t\t</td>\n\t</tr>\n"; |
||
481 | |||
482 | // LC_CTYPE |
||
483 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strctype']}</th>\n"; |
||
484 | echo "\t\t<td class=\"data1\">\n"; |
||
485 | echo "\t\t\t<input name=\"formCType\" value=\"", htmlspecialchars($_POST['formCType']), "\" />\n"; |
||
486 | echo "\t\t</td>\n\t</tr>\n"; |
||
487 | } |
||
488 | |||
489 | // Tablespace (if there are any) |
||
490 | if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) { |
||
491 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n"; |
||
492 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n"; |
||
493 | // Always offer the default (empty) option |
||
494 | echo "\t\t\t\t<option value=\"\"", |
||
495 | ('' == $_POST['formSpc']) ? ' selected="selected"' : '', "></option>\n"; |
||
496 | // Display all other tablespaces |
||
497 | while (!$tablespaces->EOF) { |
||
498 | $spcname = htmlspecialchars($tablespaces->fields['spcname']); |
||
499 | echo "\t\t\t\t<option value=\"{$spcname}\"", |
||
500 | ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n"; |
||
501 | $tablespaces->moveNext(); |
||
502 | } |
||
503 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
||
504 | } |
||
505 | |||
506 | // Comments (if available) |
||
507 | if ($data->hasSharedComments()) { |
||
508 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
509 | echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
510 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
511 | } |
||
512 | |||
513 | echo "</table>\n"; |
||
514 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
515 | echo $this->misc->form; |
||
516 | echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
||
517 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
518 | echo "</form>\n"; |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Actually creates the new view in the database. |
||
523 | */ |
||
524 | public function doSaveCreate() |
||
525 | { |
||
526 | $lang = $this->lang; |
||
527 | $data = $this->misc->getDatabaseAccessor(); |
||
528 | |||
529 | // Default tablespace to null if it isn't set |
||
530 | if (!isset($_POST['formSpc'])) { |
||
531 | $_POST['formSpc'] = null; |
||
532 | } |
||
533 | |||
534 | // Default comment to blank if it isn't set |
||
535 | if (!isset($_POST['formComment'])) { |
||
536 | $_POST['formComment'] = null; |
||
537 | } |
||
538 | |||
539 | // Default collate to blank if it isn't set |
||
540 | if (!isset($_POST['formCollate'])) { |
||
541 | $_POST['formCollate'] = null; |
||
542 | } |
||
543 | |||
544 | // Default ctype to blank if it isn't set |
||
545 | if (!isset($_POST['formCType'])) { |
||
546 | $_POST['formCType'] = null; |
||
547 | } |
||
548 | |||
549 | // Check that they've given a name and a definition |
||
550 | if ('' == $_POST['formName']) { |
||
551 | $this->doCreate($lang['strdatabaseneedsname']); |
||
552 | } else { |
||
553 | $status = $data->createDatabase( |
||
554 | $_POST['formName'], |
||
555 | $_POST['formEncoding'], |
||
556 | $_POST['formSpc'], |
||
557 | $_POST['formComment'], |
||
558 | $_POST['formTemplate'], |
||
559 | $_POST['formCollate'], |
||
560 | $_POST['formCType'] |
||
561 | ); |
||
562 | if (0 == $status) { |
||
563 | $this->misc->setReloadBrowser(true); |
||
564 | $this->doDefault($lang['strdatabasecreated']); |
||
565 | } else { |
||
566 | $this->doCreate($lang['strdatabasecreatedbad']); |
||
567 | } |
||
568 | } |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Displays options for cluster download. |
||
573 | * |
||
574 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
575 | */ |
||
576 | public function doExport($msg = '') |
||
621 | } |
||
622 | } |
||
623 |