| Total Complexity | 55 |
| Total Lines | 546 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MaterializedviewsController 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 MaterializedviewsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class MaterializedviewsController extends BaseController |
||
| 17 | { |
||
| 18 | use \PHPPgAdmin\Traits\ViewsMatviewsTrait; |
||
| 19 | |||
| 20 | public $table_place = 'matviews-matviews'; |
||
| 21 | public $controller_title = 'strviews'; |
||
| 22 | |||
| 23 | // this member variable is view for views and matview for materialized views |
||
| 24 | public $keystring = 'matview'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Default method to render the controller according to the action parameter. |
||
| 28 | */ |
||
| 29 | public function render() |
||
| 30 | { |
||
| 31 | if ('tree' == $this->action) { |
||
| 32 | return $this->doTree(); |
||
| 33 | } |
||
| 34 | if ('subtree' == $this->action) { |
||
| 35 | return $this->doSubTree(); |
||
| 36 | } |
||
| 37 | |||
| 38 | $this->printHeader('M '.$this->lang['strviews']); |
||
| 39 | $this->printBody(); |
||
| 40 | |||
| 41 | switch ($this->action) { |
||
| 42 | case 'selectrows': |
||
| 43 | if (!isset($_REQUEST['cancel'])) { |
||
| 44 | $this->doSelectRows(false); |
||
| 45 | } else { |
||
| 46 | $this->doDefault(); |
||
| 47 | } |
||
| 48 | |||
| 49 | break; |
||
| 50 | case 'confselectrows': |
||
| 51 | $this->doSelectRows(true); |
||
| 52 | |||
| 53 | break; |
||
| 54 | case 'save_create_wiz': |
||
| 55 | if (isset($_REQUEST['cancel'])) { |
||
| 56 | $this->doDefault(); |
||
| 57 | } else { |
||
| 58 | $this->doSaveCreateWiz(true); |
||
| 59 | } |
||
| 60 | |||
| 61 | break; |
||
| 62 | case 'wiz_create': |
||
| 63 | $this->doWizardCreate(); |
||
| 64 | |||
| 65 | break; |
||
| 66 | case 'set_params_create': |
||
| 67 | if (isset($_POST['cancel'])) { |
||
| 68 | $this->doDefault(); |
||
| 69 | } else { |
||
| 70 | $this->doSetParamsCreate(); |
||
| 71 | } |
||
| 72 | |||
| 73 | break; |
||
| 74 | case 'save_create': |
||
| 75 | if (isset($_REQUEST['cancel'])) { |
||
| 76 | $this->doDefault(); |
||
| 77 | } else { |
||
| 78 | $this->doSaveCreate(); |
||
| 79 | } |
||
| 80 | |||
| 81 | break; |
||
| 82 | case 'create': |
||
| 83 | $this->doCreate(); |
||
| 84 | |||
| 85 | break; |
||
| 86 | case 'drop': |
||
| 87 | if (isset($_POST['drop'])) { |
||
| 88 | $this->doDrop(false); |
||
| 89 | } else { |
||
| 90 | $this->doDefault(); |
||
| 91 | } |
||
| 92 | |||
| 93 | break; |
||
| 94 | case 'confirm_drop': |
||
| 95 | $this->doDrop(true); |
||
| 96 | |||
| 97 | break; |
||
| 98 | default: |
||
| 99 | $this->doDefault(); |
||
| 100 | |||
| 101 | break; |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->printFooter(); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Show default list of views in the database. |
||
| 109 | * |
||
| 110 | * @param mixed $msg |
||
| 111 | */ |
||
| 112 | public function doDefault($msg = '') |
||
| 113 | { |
||
| 114 | $data = $this->misc->getDatabaseAccessor(); |
||
| 115 | |||
| 116 | $this->printTrail('schema'); |
||
| 117 | $this->printTabs('schema', 'matviews'); |
||
| 118 | $this->printMsg($msg); |
||
| 119 | |||
| 120 | $matviews = $data->getMaterializedViews(); |
||
| 121 | |||
| 122 | $columns = [ |
||
| 123 | $this->keystring => [ |
||
| 124 | 'title' => 'M '.$this->lang['strview'], |
||
| 125 | 'field' => Decorator::field('relname'), |
||
| 126 | 'url' => \SUBFOLDER."/redirect/matview?{$this->misc->href}&", |
||
| 127 | 'vars' => [$this->keystring => 'relname'], |
||
| 128 | ], |
||
| 129 | 'owner' => [ |
||
| 130 | 'title' => $this->lang['strowner'], |
||
| 131 | 'field' => Decorator::field('relowner'), |
||
| 132 | ], |
||
| 133 | 'actions' => [ |
||
| 134 | 'title' => $this->lang['stractions'], |
||
| 135 | ], |
||
| 136 | 'comment' => [ |
||
| 137 | 'title' => $this->lang['strcomment'], |
||
| 138 | 'field' => Decorator::field('relcomment'), |
||
| 139 | ], |
||
| 140 | ]; |
||
| 141 | |||
| 142 | $actions = [ |
||
| 143 | 'multiactions' => [ |
||
| 144 | 'keycols' => [$this->keystring => 'relname'], |
||
| 145 | 'url' => 'materializedviews', |
||
| 146 | ], |
||
| 147 | 'browse' => [ |
||
| 148 | 'content' => $this->lang['strbrowse'], |
||
| 149 | 'attr' => [ |
||
| 150 | 'href' => [ |
||
| 151 | 'url' => 'display', |
||
| 152 | 'urlvars' => [ |
||
| 153 | 'action' => 'confselectrows', |
||
| 154 | 'subject' => $this->keystring, |
||
| 155 | 'return' => 'schema', |
||
| 156 | $this->keystring => Decorator::field('relname'), |
||
| 157 | ], |
||
| 158 | ], |
||
| 159 | ], |
||
| 160 | ], |
||
| 161 | 'select' => [ |
||
| 162 | 'content' => $this->lang['strselect'], |
||
| 163 | 'attr' => [ |
||
| 164 | 'href' => [ |
||
| 165 | 'url' => 'materializedviews', |
||
| 166 | 'urlvars' => [ |
||
| 167 | 'action' => 'confselectrows', |
||
| 168 | $this->keystring => Decorator::field('relname'), |
||
| 169 | ], |
||
| 170 | ], |
||
| 171 | ], |
||
| 172 | ], |
||
| 173 | |||
| 174 | // Insert is possible if the relevant rule for the matview has been created. |
||
| 175 | // 'insert' => array( |
||
| 176 | // 'title' => $this->lang['strinsert'], |
||
| 177 | // 'url' => "materializedviews?action=confinsertrow&{$this->misc->href}&", |
||
| 178 | // 'vars' => array('matview' => 'relname'), |
||
| 179 | // ), |
||
| 180 | |||
| 181 | 'alter' => [ |
||
| 182 | 'content' => $this->lang['stralter'], |
||
| 183 | 'attr' => [ |
||
| 184 | 'href' => [ |
||
| 185 | 'url' => 'materializedviewproperties', |
||
| 186 | 'urlvars' => [ |
||
| 187 | 'action' => 'confirm_alter', |
||
| 188 | $this->keystring => Decorator::field('relname'), |
||
| 189 | ], |
||
| 190 | ], |
||
| 191 | ], |
||
| 192 | ], |
||
| 193 | 'drop' => [ |
||
| 194 | 'multiaction' => 'confirm_drop', |
||
| 195 | 'content' => $this->lang['strdrop'], |
||
| 196 | 'attr' => [ |
||
| 197 | 'href' => [ |
||
| 198 | 'url' => 'materializedviews', |
||
| 199 | 'urlvars' => [ |
||
| 200 | 'action' => 'confirm_drop', |
||
| 201 | $this->keystring => Decorator::field('relname'), |
||
| 202 | ], |
||
| 203 | ], |
||
| 204 | ], |
||
| 205 | ], |
||
| 206 | ]; |
||
| 207 | |||
| 208 | echo $this->printTable($matviews, $columns, $actions, $this->table_place, $this->lang['strnoviews']); |
||
| 209 | |||
| 210 | $navlinks = [ |
||
| 211 | 'create' => [ |
||
| 212 | 'attr' => [ |
||
| 213 | 'href' => [ |
||
| 214 | 'url' => 'materializedviews', |
||
| 215 | 'urlvars' => [ |
||
| 216 | 'action' => 'create', |
||
| 217 | 'server' => $_REQUEST['server'], |
||
| 218 | 'database' => $_REQUEST['database'], |
||
| 219 | 'schema' => $_REQUEST['schema'], |
||
| 220 | ], |
||
| 221 | ], |
||
| 222 | ], |
||
| 223 | 'content' => $this->lang['strcreateview'], |
||
| 224 | ], |
||
| 225 | 'createwiz' => [ |
||
| 226 | 'attr' => [ |
||
| 227 | 'href' => [ |
||
| 228 | 'url' => 'materializedviews', |
||
| 229 | 'urlvars' => [ |
||
| 230 | 'action' => 'wiz_create', |
||
| 231 | 'server' => $_REQUEST['server'], |
||
| 232 | 'database' => $_REQUEST['database'], |
||
| 233 | 'schema' => $_REQUEST['schema'], |
||
| 234 | ], |
||
| 235 | ], |
||
| 236 | ], |
||
| 237 | 'content' => $this->lang['strcreatematviewwiz'], |
||
| 238 | ], |
||
| 239 | ]; |
||
| 240 | $this->printNavLinks($navlinks, $this->table_place, get_defined_vars()); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Generate XML for the browser tree. |
||
| 245 | */ |
||
| 246 | public function doTree() |
||
| 247 | { |
||
| 248 | $data = $this->misc->getDatabaseAccessor(); |
||
| 249 | |||
| 250 | $matviews = $data->getMaterializedViews(); |
||
| 251 | |||
| 252 | $reqvars = $this->misc->getRequestVars($this->keystring); |
||
| 253 | |||
| 254 | $attrs = [ |
||
| 255 | 'text' => Decorator::field('relname'), |
||
| 256 | 'icon' => 'MViews', |
||
| 257 | 'iconAction' => Decorator::url('display', $reqvars, [$this->keystring => Decorator::field('relname')]), |
||
| 258 | 'toolTip' => Decorator::field('relcomment'), |
||
| 259 | 'action' => Decorator::redirecturl('redirect', $reqvars, [$this->keystring => Decorator::field('relname')]), |
||
| 260 | 'branch' => Decorator::url('materializedviews', $reqvars, ['action' => 'subtree', $this->keystring => Decorator::field('relname')]), |
||
| 261 | ]; |
||
| 262 | |||
| 263 | return $this->printTree($matviews, $attrs, 'matviews'); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Show confirmation of drop and perform actual drop. |
||
| 268 | * |
||
| 269 | * @param mixed $confirm |
||
| 270 | */ |
||
| 271 | public function doDrop($confirm) |
||
| 335 | } |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Sets up choices for table linkage, and which fields to select for the view we're creating. |
||
| 342 | * |
||
| 343 | * @param mixed $msg |
||
| 344 | */ |
||
| 345 | public function doSetParamsCreate($msg = '') |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Display a wizard where they can enter a new view. |
||
| 488 | * |
||
| 489 | * @param mixed $msg |
||
| 490 | */ |
||
| 491 | public function doWizardCreate($msg = '') |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Displays a screen where they can enter a new view. |
||
| 502 | * |
||
| 503 | * @param mixed $msg |
||
| 504 | */ |
||
| 505 | public function doCreate($msg = '') |
||
| 506 | { |
||
| 507 | $data = $this->misc->getDatabaseAccessor(); |
||
| 508 | |||
| 509 | $this->coalesceArr($_REQUEST, 'formView', ''); |
||
| 510 | |||
| 511 | if (!isset($_REQUEST['formDefinition'])) { |
||
| 512 | if (isset($_SESSION['sqlquery'])) { |
||
| 513 | $_REQUEST['formDefinition'] = $_SESSION['sqlquery']; |
||
| 514 | } else { |
||
| 515 | $_REQUEST['formDefinition'] = 'SELECT '; |
||
| 516 | } |
||
| 517 | } |
||
| 518 | $this->coalesceArr($_REQUEST, 'formComment', ''); |
||
| 519 | |||
| 520 | $this->printTrail('schema'); |
||
| 521 | $this->printTitle($this->lang['strcreateview'], 'pg.matview.create'); |
||
| 522 | $this->printMsg($msg); |
||
| 523 | |||
| 524 | echo '<form action="'.\SUBFOLDER."/src/views/{$this->view_name}\" method=\"post\">\n"; |
||
| 525 | echo "<table style=\"width: 100%\">\n"; |
||
| 526 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n"; |
||
| 527 | echo "\t<td class=\"data1\"><input name=\"formView\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 528 | htmlspecialchars($_REQUEST['formView']), "\" /></td>\n\t</tr>\n"; |
||
| 529 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>\n"; |
||
| 530 | echo "\t<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"10\" cols=\"50\" name=\"formDefinition\">", |
||
| 531 | htmlspecialchars($_REQUEST['formDefinition']), "</textarea></td>\n\t</tr>\n"; |
||
| 532 | echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n"; |
||
| 533 | echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
| 534 | htmlspecialchars($_REQUEST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
| 535 | echo "</table>\n"; |
||
| 536 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
| 537 | echo $this->misc->form; |
||
| 538 | echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n"; |
||
| 539 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n"; |
||
| 540 | echo "</form>\n"; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Actually creates the new view in the database. |
||
| 545 | */ |
||
| 546 | public function doSaveCreate() |
||
| 562 | } |
||
| 563 | } |
||
| 566 |