| Total Complexity | 51 |
| Total Lines | 558 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SchemasController 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 SchemasController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SchemasController extends BaseController |
||
| 15 | { |
||
| 16 | public $controller_name = 'SchemasController'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Default method to render the controller according to the action parameter. |
||
| 20 | */ |
||
| 21 | public function render() |
||
| 22 | { |
||
| 23 | $lang = $this->lang; |
||
| 24 | $action = $this->action; |
||
| 25 | |||
| 26 | if ('tree' == $action) { |
||
| 27 | return $this->doTree(); |
||
| 28 | } |
||
| 29 | if ('subtree' == $action) { |
||
| 30 | return $this->doSubTree(); |
||
| 31 | } |
||
| 32 | |||
| 33 | if (isset($_POST['cancel'])) { |
||
| 34 | $action = ''; |
||
| 35 | } |
||
| 36 | |||
| 37 | $header_template = 'header.twig'; |
||
| 38 | $footer_template = 'footer.twig'; |
||
| 39 | |||
| 40 | ob_start(); |
||
| 41 | switch ($action) { |
||
| 42 | case 'create': |
||
| 43 | if (isset($_POST['create'])) { |
||
| 44 | $this->doSaveCreate(); |
||
| 45 | } else { |
||
| 46 | $this->doCreate(); |
||
| 47 | } |
||
| 48 | |||
| 49 | break; |
||
| 50 | case 'alter': |
||
| 51 | if (isset($_POST['alter'])) { |
||
| 52 | $this->doSaveAlter(); |
||
| 53 | } else { |
||
| 54 | $this->doAlter(); |
||
| 55 | } |
||
| 56 | |||
| 57 | break; |
||
| 58 | case 'drop': |
||
| 59 | if (isset($_POST['drop'])) { |
||
| 60 | $this->doDrop(false); |
||
| 61 | } else { |
||
| 62 | $this->doDrop(true); |
||
| 63 | } |
||
| 64 | |||
| 65 | break; |
||
| 66 | case 'export': |
||
| 67 | $this->doExport(); |
||
| 68 | |||
| 69 | break; |
||
| 70 | default: |
||
| 71 | $header_template = 'header_datatables.twig'; |
||
| 72 | $this->doDefault(); |
||
| 73 | |||
| 74 | break; |
||
| 75 | } |
||
| 76 | |||
| 77 | $output = ob_get_clean(); |
||
| 78 | |||
| 79 | $this->printHeader($lang['strschemas'], null, true, $header_template); |
||
| 80 | $this->printBody(); |
||
| 81 | |||
| 82 | echo $output; |
||
| 83 | |||
| 84 | return $this->printFooter(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Show default list of schemas in the database. |
||
| 89 | * |
||
| 90 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 91 | */ |
||
| 92 | public function doDefault($msg = '') |
||
| 93 | { |
||
| 94 | $lang = $this->lang; |
||
| 95 | $data = $this->misc->getDatabaseAccessor(); |
||
| 96 | |||
| 97 | $this->printTrail('database'); |
||
| 98 | $this->printTabs('database', 'schemas'); |
||
| 99 | $this->printMsg($msg); |
||
| 100 | |||
| 101 | // Check that the DB actually supports schemas |
||
| 102 | $schemas = $data->getSchemas(); |
||
| 103 | |||
| 104 | $columns = [ |
||
| 105 | 'schema' => [ |
||
| 106 | 'title' => $lang['strschema'], |
||
| 107 | 'field' => Decorator::field('nspname'), |
||
| 108 | 'url' => \SUBFOLDER . "/redirect/schema?{$this->misc->href}&", |
||
| 109 | 'vars' => ['schema' => 'nspname'], |
||
| 110 | ], |
||
| 111 | 'owner' => [ |
||
| 112 | 'title' => $lang['strowner'], |
||
| 113 | 'field' => Decorator::field('nspowner'), |
||
| 114 | ], |
||
| 115 | 'actions' => [ |
||
| 116 | 'title' => $lang['stractions'], |
||
| 117 | ], |
||
| 118 | 'comment' => [ |
||
| 119 | 'title' => $lang['strcomment'], |
||
| 120 | 'field' => Decorator::field('nspcomment'), |
||
| 121 | ], |
||
| 122 | ]; |
||
| 123 | |||
| 124 | $actions = [ |
||
| 125 | 'multiactions' => [ |
||
| 126 | 'keycols' => ['nsp' => 'nspname'], |
||
| 127 | 'url' => 'schemas.php', |
||
| 128 | ], |
||
| 129 | 'drop' => [ |
||
| 130 | 'content' => $lang['strdrop'], |
||
| 131 | 'attr' => [ |
||
| 132 | 'href' => [ |
||
| 133 | 'url' => 'schemas.php', |
||
| 134 | 'urlvars' => [ |
||
| 135 | 'action' => 'drop', |
||
| 136 | 'nsp' => Decorator::field('nspname'), |
||
| 137 | ], |
||
| 138 | ], |
||
| 139 | ], |
||
| 140 | 'multiaction' => 'drop', |
||
| 141 | ], |
||
| 142 | 'privileges' => [ |
||
| 143 | 'content' => $lang['strprivileges'], |
||
| 144 | 'attr' => [ |
||
| 145 | 'href' => [ |
||
| 146 | 'url' => 'privileges.php', |
||
| 147 | 'urlvars' => [ |
||
| 148 | 'subject' => 'schema', |
||
| 149 | 'schema' => Decorator::field('nspname'), |
||
| 150 | ], |
||
| 151 | ], |
||
| 152 | ], |
||
| 153 | ], |
||
| 154 | 'alter' => [ |
||
| 155 | 'content' => $lang['stralter'], |
||
| 156 | 'attr' => [ |
||
| 157 | 'href' => [ |
||
| 158 | 'url' => 'schemas.php', |
||
| 159 | 'urlvars' => [ |
||
| 160 | 'action' => 'alter', |
||
| 161 | 'schema' => Decorator::field('nspname'), |
||
| 162 | ], |
||
| 163 | ], |
||
| 164 | ], |
||
| 165 | ], |
||
| 166 | ]; |
||
| 167 | |||
| 168 | if (!$data->hasAlterSchema()) { |
||
| 169 | unset($actions['alter']); |
||
| 170 | } |
||
| 171 | |||
| 172 | echo $this->printTable($schemas, $columns, $actions, 'schemas-schemas', $lang['strnoschemas']); |
||
| 173 | |||
| 174 | $this->printNavLinks(['create' => [ |
||
|
1 ignored issue
–
show
|
|||
| 175 | 'attr' => [ |
||
| 176 | 'href' => [ |
||
| 177 | 'url' => 'schemas.php', |
||
| 178 | 'urlvars' => [ |
||
| 179 | 'action' => 'create', |
||
| 180 | 'server' => $_REQUEST['server'], |
||
| 181 | 'database' => $_REQUEST['database'], |
||
| 182 | ], |
||
| 183 | ], |
||
| 184 | ], |
||
| 185 | 'content' => $lang['strcreateschema'], |
||
| 186 | ]], 'schemas-schemas', get_defined_vars()); |
||
|
1 ignored issue
–
show
|
|||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Generate XML for the browser tree. |
||
| 191 | */ |
||
| 192 | public function doTree() |
||
| 193 | { |
||
| 194 | $lang = $this->lang; |
||
| 195 | $data = $this->misc->getDatabaseAccessor(); |
||
| 196 | |||
| 197 | $schemas = $data->getSchemas(); |
||
| 198 | |||
| 199 | $reqvars = $this->misc->getRequestVars('schema'); |
||
| 200 | |||
| 201 | //$this->prtrace($reqvars); |
||
| 202 | |||
| 203 | $attrs = [ |
||
| 204 | 'text' => Decorator::field('nspname'), |
||
| 205 | 'icon' => 'Schema', |
||
| 206 | 'toolTip' => Decorator::field('nspcomment'), |
||
| 207 | 'action' => Decorator::redirecturl( |
||
| 208 | 'redirect.php', |
||
| 209 | $reqvars, |
||
| 210 | [ |
||
| 211 | 'subject' => 'schema', |
||
| 212 | 'schema' => Decorator::field('nspname'), |
||
| 213 | ] |
||
| 214 | ), |
||
| 215 | 'branch' => Decorator::url( |
||
| 216 | 'schemas.php', |
||
| 217 | $reqvars, |
||
| 218 | [ |
||
| 219 | 'action' => 'subtree', |
||
| 220 | 'schema' => Decorator::field('nspname'), |
||
| 221 | ] |
||
| 222 | ), |
||
| 223 | ]; |
||
| 224 | |||
| 225 | $this->printTree($schemas, $attrs, 'schemas'); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function doSubTree() |
||
|
1 ignored issue
–
show
|
|||
| 229 | { |
||
| 230 | $lang = $this->lang; |
||
| 231 | $data = $this->misc->getDatabaseAccessor(); |
||
| 232 | |||
| 233 | $tabs = $this->misc->getNavTabs('schema'); |
||
| 234 | |||
| 235 | $items = $this->adjustTabsForTree($tabs); |
||
| 236 | |||
| 237 | $reqvars = $this->misc->getRequestVars('schema'); |
||
| 238 | |||
| 239 | //$this->prtrace($reqvars); |
||
| 240 | |||
| 241 | $attrs = [ |
||
| 242 | 'text' => Decorator::field('title'), |
||
| 243 | 'icon' => Decorator::field('icon'), |
||
| 244 | 'action' => Decorator::actionurl( |
||
| 245 | Decorator::field('url'), |
||
| 246 | $reqvars, |
||
| 247 | Decorator::field('urlvars', []) |
||
| 248 | ), |
||
| 249 | 'branch' => Decorator::url( |
||
| 250 | Decorator::field('url'), |
||
| 251 | $reqvars, |
||
| 252 | Decorator::field('urlvars'), |
||
| 253 | ['action' => 'tree'] |
||
| 254 | ), |
||
| 255 | ]; |
||
| 256 | |||
| 257 | $this->printTree($items, $attrs, 'schema'); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Displays a screen where they can enter a new schema. |
||
| 262 | * |
||
| 263 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 264 | */ |
||
| 265 | public function doCreate($msg = '') |
||
| 266 | { |
||
| 267 | $lang = $this->lang; |
||
| 268 | $data = $this->misc->getDatabaseAccessor(); |
||
| 269 | |||
| 270 | $server_info = $this->misc->getServerInfo(); |
||
| 271 | |||
| 272 | if (!isset($_POST['formName'])) { |
||
| 273 | $_POST['formName'] = ''; |
||
| 274 | } |
||
| 275 | |||
| 276 | if (!isset($_POST['formAuth'])) { |
||
| 277 | $_POST['formAuth'] = $server_info['username']; |
||
| 278 | } |
||
| 279 | |||
| 280 | if (!isset($_POST['formSpc'])) { |
||
| 281 | $_POST['formSpc'] = ''; |
||
| 282 | } |
||
| 283 | |||
| 284 | if (!isset($_POST['formComment'])) { |
||
| 285 | $_POST['formComment'] = ''; |
||
| 286 | } |
||
| 287 | |||
| 288 | // Fetch all users from the database |
||
| 289 | $users = $data->getUsers(); |
||
| 290 | |||
| 291 | $this->printTrail('database'); |
||
| 292 | $this->printTitle($lang['strcreateschema'], 'pg.schema.create'); |
||
| 293 | $this->printMsg($msg); |
||
| 294 | |||
| 295 | echo '<form action="' . \SUBFOLDER . '/src/views/schemas.php" method="post">' . "\n"; |
||
| 296 | echo "<table style=\"width: 100%\">\n"; |
||
| 297 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
| 298 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 299 | htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n"; |
||
| 300 | // Owner |
||
| 301 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strowner']}</th>\n"; |
||
| 302 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formAuth\">\n"; |
||
| 303 | while (!$users->EOF) { |
||
| 304 | $uname = htmlspecialchars($users->fields['usename']); |
||
| 305 | echo "\t\t\t\t<option value=\"{$uname}\"", |
||
| 306 | ($uname == $_POST['formAuth']) ? ' selected="selected"' : '', ">{$uname}</option>\n"; |
||
| 307 | $users->moveNext(); |
||
| 308 | } |
||
| 309 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
||
| 310 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
| 311 | echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
| 312 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
| 313 | |||
| 314 | echo "</table>\n"; |
||
| 315 | echo "<p>\n"; |
||
| 316 | echo "<input type=\"hidden\" name=\"action\" value=\"create\" />\n"; |
||
| 317 | echo '<input type="hidden" name="database" value="', htmlspecialchars($_REQUEST['database']), "\" />\n"; |
||
| 318 | echo $this->misc->form; |
||
| 319 | echo "<input type=\"submit\" name=\"create\" value=\"{$lang['strcreate']}\" />\n"; |
||
| 320 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
||
| 321 | echo "</p>\n"; |
||
| 322 | echo "</form>\n"; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Actually creates the new schema in the database. |
||
| 327 | */ |
||
| 328 | public function doSaveCreate() |
||
| 329 | { |
||
| 330 | $lang = $this->lang; |
||
| 331 | $data = $this->misc->getDatabaseAccessor(); |
||
| 332 | |||
| 333 | // Check that they've given a name |
||
| 334 | if ('' == $_POST['formName']) { |
||
| 335 | $this->doCreate($lang['strschemaneedsname']); |
||
| 336 | } else { |
||
| 337 | $status = $data->createSchema($_POST['formName'], $_POST['formAuth'], $_POST['formComment']); |
||
| 338 | if (0 == $status) { |
||
| 339 | $this->misc->setReloadBrowser(true); |
||
| 340 | $this->doDefault($lang['strschemacreated']); |
||
| 341 | } else { |
||
| 342 | $this->doCreate($lang['strschemacreatedbad']); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Display a form to permit editing schema properies. |
||
| 349 | * TODO: permit changing owner. |
||
| 350 | * |
||
| 351 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 352 | */ |
||
| 353 | public function doAlter($msg = '') |
||
| 354 | { |
||
| 355 | $lang = $this->lang; |
||
| 356 | $data = $this->misc->getDatabaseAccessor(); |
||
| 357 | |||
| 358 | $this->printTrail('schema'); |
||
| 359 | $this->printTitle($lang['stralter'], 'pg.schema.alter'); |
||
| 360 | $this->printMsg($msg); |
||
| 361 | |||
| 362 | $schema = $data->getSchemaByName($_REQUEST['schema']); |
||
| 363 | if ($schema->recordCount() > 0) { |
||
| 364 | if (!isset($_POST['comment'])) { |
||
| 365 | $_POST['comment'] = $schema->fields['nspcomment']; |
||
| 366 | } |
||
| 367 | |||
| 368 | if (!isset($_POST['schema'])) { |
||
| 369 | $_POST['schema'] = $_REQUEST['schema']; |
||
| 370 | } |
||
| 371 | |||
| 372 | if (!isset($_POST['name'])) { |
||
| 373 | $_POST['name'] = $_REQUEST['schema']; |
||
| 374 | } |
||
| 375 | |||
| 376 | if (!isset($_POST['owner'])) { |
||
| 377 | $_POST['owner'] = $schema->fields['ownername']; |
||
| 378 | } |
||
| 379 | |||
| 380 | echo '<form action="' . \SUBFOLDER . '/src/views/schemas.php" method="post">' . "\n"; |
||
| 381 | echo "<table>\n"; |
||
| 382 | |||
| 383 | echo "\t<tr>\n"; |
||
| 384 | echo "\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
| 385 | echo "\t\t<td class=\"data1\">"; |
||
| 386 | echo "\t\t\t<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 387 | htmlspecialchars($_POST['name']), "\" />\n"; |
||
| 388 | echo "\t\t</td>\n"; |
||
| 389 | echo "\t</tr>\n"; |
||
| 390 | |||
| 391 | if ($data->hasAlterSchemaOwner()) { |
||
| 392 | $users = $data->getUsers(); |
||
| 393 | echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n"; |
||
| 394 | echo '<td class="data2"><select name="owner">'; |
||
| 395 | while (!$users->EOF) { |
||
| 396 | $uname = $users->fields['usename']; |
||
| 397 | echo '<option value="', htmlspecialchars($uname), '"', |
||
| 398 | ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n"; |
||
| 399 | $users->moveNext(); |
||
| 400 | } |
||
| 401 | echo "</select></td></tr>\n"; |
||
| 402 | } else { |
||
| 403 | echo "<input name=\"owner\" value=\"{$_POST['owner']}\" type=\"hidden\" />"; |
||
| 404 | } |
||
| 405 | |||
| 406 | echo "\t<tr>\n"; |
||
| 407 | echo "\t\t<th class=\"data\">{$lang['strcomment']}</th>\n"; |
||
| 408 | echo "\t\t<td class=\"data1\"><textarea cols=\"32\" rows=\"3\" name=\"comment\">", htmlspecialchars($_POST['comment']), "</textarea></td>\n"; |
||
| 409 | echo "\t</tr>\n"; |
||
| 410 | echo "</table>\n"; |
||
| 411 | echo "<p><input type=\"hidden\" name=\"action\" value=\"alter\" />\n"; |
||
| 412 | echo '<input type="hidden" name="schema" value="', htmlspecialchars($_POST['schema']), "\" />\n"; |
||
| 413 | echo $this->misc->form; |
||
| 414 | echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n"; |
||
| 415 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 416 | echo "</form>\n"; |
||
| 417 | } else { |
||
| 418 | echo "<p>{$lang['strnodata']}</p>\n"; |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Save the form submission containing changes to a schema. |
||
| 424 | * |
||
| 425 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 426 | */ |
||
| 427 | public function doSaveAlter($msg = '') |
||
|
1 ignored issue
–
show
|
|||
| 428 | { |
||
| 429 | $lang = $this->lang; |
||
| 430 | $data = $this->misc->getDatabaseAccessor(); |
||
| 431 | |||
| 432 | $status = $data->updateSchema($_POST['schema'], $_POST['comment'], $_POST['name'], $_POST['owner']); |
||
| 433 | if (0 == $status) { |
||
| 434 | $this->misc->setReloadBrowser(true); |
||
| 435 | $this->doDefault($lang['strschemaaltered']); |
||
| 436 | } else { |
||
| 437 | $this->doAlter($lang['strschemaalteredbad']); |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Show confirmation of drop and perform actual drop. |
||
| 443 | * |
||
| 444 | * @param mixed $confirm |
||
|
1 ignored issue
–
show
|
|||
| 445 | */ |
||
| 446 | public function doDrop($confirm) |
||
| 510 | } |
||
| 511 | } |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Displays options for database download. |
||
| 517 | * |
||
| 518 | * @param mixed $msg |
||
|
1 ignored issue
–
show
|
|||
| 519 | */ |
||
| 520 | public function doExport($msg = '') |
||
| 572 | } |
||
| 573 | } |
||
| 574 |