Total Complexity | 112 |
Total Lines | 911 |
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 |
||
14 | class MaterializedviewsController extends BaseController |
||
15 | { |
||
16 | public $script = 'materializedviews.php'; |
||
17 | public $controller_name = 'MaterializedviewsController'; |
||
18 | public $table_place = 'matviews-matviews'; |
||
19 | |||
20 | /** |
||
21 | * Default method to render the controller according to the action parameter. |
||
22 | */ |
||
23 | public function render() |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Show default list of views in the database. |
||
110 | * |
||
111 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
112 | */ |
||
113 | public function doDefault($msg = '') |
||
114 | { |
||
115 | $conf = $this->conf; |
||
116 | |||
117 | $lang = $this->lang; |
||
118 | $data = $this->misc->getDatabaseAccessor(); |
||
119 | |||
120 | $this->printTrail('schema'); |
||
121 | $this->printTabs('schema', 'matviews'); |
||
122 | $this->printMsg($msg); |
||
123 | |||
124 | //$matviews = $data->getViews(); |
||
125 | $matviews = $data->getMaterializedViews(); |
||
126 | |||
127 | $columns = [ |
||
128 | 'matview' => [ |
||
129 | 'title' => 'M '.$lang['strview'], |
||
130 | 'field' => Decorator::field('relname'), |
||
131 | 'url' => \SUBFOLDER."/redirect/matview?{$this->misc->href}&", |
||
132 | 'vars' => ['matview' => 'relname'], |
||
133 | ], |
||
134 | 'owner' => [ |
||
135 | 'title' => $lang['strowner'], |
||
136 | 'field' => Decorator::field('relowner'), |
||
137 | ], |
||
138 | 'actions' => [ |
||
139 | 'title' => $lang['stractions'], |
||
140 | ], |
||
141 | 'comment' => [ |
||
142 | 'title' => $lang['strcomment'], |
||
143 | 'field' => Decorator::field('relcomment'), |
||
144 | ], |
||
145 | ]; |
||
146 | |||
147 | $actions = [ |
||
148 | 'multiactions' => [ |
||
149 | 'keycols' => ['matview' => 'relname'], |
||
150 | 'url' => 'materializedviews.php', |
||
151 | ], |
||
152 | 'browse' => [ |
||
153 | 'content' => $lang['strbrowse'], |
||
154 | 'attr' => [ |
||
155 | 'href' => [ |
||
156 | 'url' => 'display.php', |
||
157 | 'urlvars' => [ |
||
158 | 'action' => 'confselectrows', |
||
159 | 'subject' => 'matview', |
||
160 | 'return' => 'schema', |
||
161 | 'matview' => Decorator::field('relname'), |
||
162 | ], |
||
163 | ], |
||
164 | ], |
||
165 | ], |
||
166 | 'select' => [ |
||
167 | 'content' => $lang['strselect'], |
||
168 | 'attr' => [ |
||
169 | 'href' => [ |
||
170 | 'url' => 'materializedviews.php', |
||
171 | 'urlvars' => [ |
||
172 | 'action' => 'confselectrows', |
||
173 | 'matview' => Decorator::field('relname'), |
||
174 | ], |
||
175 | ], |
||
176 | ], |
||
177 | ], |
||
178 | |||
179 | // Insert is possible if the relevant rule for the view has been created. |
||
180 | // 'insert' => array( |
||
181 | // 'title' => $lang['strinsert'], |
||
182 | // 'url' => "materializedviews.php?action=confinsertrow&{$this->misc->href}&", |
||
183 | // 'vars' => array('view' => 'relname'), |
||
184 | // ), |
||
185 | |||
186 | 'alter' => [ |
||
187 | 'content' => $lang['stralter'], |
||
188 | 'attr' => [ |
||
189 | 'href' => [ |
||
190 | 'url' => 'materializedviewproperties.php', |
||
191 | 'urlvars' => [ |
||
192 | 'action' => 'confirm_alter', |
||
193 | 'matview' => Decorator::field('relname'), |
||
194 | ], |
||
195 | ], |
||
196 | ], |
||
197 | ], |
||
198 | 'drop' => [ |
||
199 | 'multiaction' => 'confirm_drop', |
||
200 | 'content' => $lang['strdrop'], |
||
201 | 'attr' => [ |
||
202 | 'href' => [ |
||
203 | 'url' => 'materializedviews.php', |
||
204 | 'urlvars' => [ |
||
205 | 'action' => 'confirm_drop', |
||
206 | 'matview' => Decorator::field('relname'), |
||
207 | ], |
||
208 | ], |
||
209 | ], |
||
210 | ], |
||
211 | ]; |
||
212 | |||
213 | echo $this->printTable($matviews, $columns, $actions, $this->table_place, $lang['strnoviews']); |
||
214 | |||
215 | $navlinks = [ |
||
216 | 'create' => [ |
||
217 | 'attr' => [ |
||
218 | 'href' => [ |
||
219 | 'url' => 'materializedviews.php', |
||
220 | 'urlvars' => [ |
||
221 | 'action' => 'create', |
||
222 | 'server' => $_REQUEST['server'], |
||
223 | 'database' => $_REQUEST['database'], |
||
224 | 'schema' => $_REQUEST['schema'], |
||
225 | ], |
||
226 | ], |
||
227 | ], |
||
228 | 'content' => $lang['strcreateview'], |
||
229 | ], |
||
230 | 'createwiz' => [ |
||
231 | 'attr' => [ |
||
232 | 'href' => [ |
||
233 | 'url' => 'materializedviews.php', |
||
234 | 'urlvars' => [ |
||
235 | 'action' => 'wiz_create', |
||
236 | 'server' => $_REQUEST['server'], |
||
237 | 'database' => $_REQUEST['database'], |
||
238 | 'schema' => $_REQUEST['schema'], |
||
239 | ], |
||
240 | ], |
||
241 | ], |
||
242 | 'content' => $lang['strcreateviewwiz'], |
||
243 | ], |
||
244 | ]; |
||
245 | $this->printNavLinks($navlinks, $this->table_place, get_defined_vars()); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Generate XML for the browser tree. |
||
250 | */ |
||
251 | public function doTree() |
||
252 | { |
||
253 | $conf = $this->conf; |
||
254 | |||
255 | $lang = $this->lang; |
||
256 | $data = $this->misc->getDatabaseAccessor(); |
||
257 | |||
258 | $matviews = $data->getMaterializedViews(); |
||
259 | |||
260 | $reqvars = $this->misc->getRequestVars('matview'); |
||
261 | |||
262 | $attrs = [ |
||
263 | 'text' => Decorator::field('relname'), |
||
264 | 'icon' => 'MView', |
||
265 | 'iconAction' => Decorator::url('display.php', $reqvars, ['matview' => Decorator::field('relname')]), |
||
266 | 'toolTip' => Decorator::field('relcomment'), |
||
267 | 'action' => Decorator::redirecturl('redirect.php', $reqvars, ['matview' => Decorator::field('relname')]), |
||
268 | 'branch' => Decorator::url('materializedviews.php', $reqvars, ['action' => 'subtree', 'matview' => Decorator::field('relname')]), |
||
269 | ]; |
||
270 | |||
271 | return $this->printTree($matviews, $attrs, 'matviews'); |
||
272 | } |
||
273 | |||
274 | public function doSubTree() |
||
1 ignored issue
–
show
|
|||
275 | { |
||
276 | $conf = $this->conf; |
||
277 | |||
278 | $lang = $this->lang; |
||
279 | $data = $this->misc->getDatabaseAccessor(); |
||
280 | |||
281 | $tabs = $this->misc->getNavTabs('matview'); |
||
282 | $items = $this->adjustTabsForTree($tabs); |
||
283 | $reqvars = $this->misc->getRequestVars('matview'); |
||
284 | |||
285 | $attrs = [ |
||
286 | 'text' => Decorator::field('title'), |
||
287 | 'icon' => Decorator::field('icon'), |
||
288 | 'action' => Decorator::actionurl(Decorator::field('url'), $reqvars, Decorator::field('urlvars'), ['matview' => $_REQUEST['matview']]), |
||
289 | 'branch' => Decorator::ifempty( |
||
290 | Decorator::field('branch'), |
||
291 | '', |
||
292 | Decorator::url( |
||
293 | Decorator::field('url'), |
||
294 | Decorator::field('urlvars'), |
||
295 | $reqvars, |
||
296 | [ |
||
297 | 'action' => 'tree', |
||
298 | 'matview' => $_REQUEST['matview'], |
||
299 | ] |
||
300 | ) |
||
301 | ), |
||
302 | ]; |
||
303 | |||
304 | return $this->printTree($items, $attrs, 'matviews'); |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Ask for select parameters and perform select. |
||
309 | * |
||
310 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
311 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
312 | */ |
||
313 | public function doSelectRows($confirm, $msg = '') |
||
314 | { |
||
315 | $conf = $this->conf; |
||
316 | |||
317 | $lang = $this->lang; |
||
318 | $data = $this->misc->getDatabaseAccessor(); |
||
319 | |||
320 | if ($confirm) { |
||
321 | $this->printTrail('view'); |
||
322 | $this->printTabs('matview', 'select'); |
||
323 | $this->printMsg($msg); |
||
324 | |||
325 | $attrs = $data->getTableAttributes($_REQUEST['matview']); |
||
326 | |||
327 | echo '<form action="'.\SUBFOLDER.'/src/views/'.$this->script.'" method="post" id="selectform">'; |
||
328 | echo "\n"; |
||
329 | |||
330 | if ($attrs->recordCount() > 0) { |
||
331 | // JavaScript for select all feature |
||
332 | echo "<script type=\"text/javascript\">\n"; |
||
333 | echo "//<![CDATA[\n"; |
||
334 | echo " function selectAll() {\n"; |
||
335 | echo " for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n"; |
||
336 | echo " var e = document.getElementById('selectform').elements[i];\n"; |
||
337 | echo " if (e.name.indexOf('show') == 0) { \n "; |
||
338 | echo " e.checked = document.getElementById('selectform').selectall.checked;\n"; |
||
339 | echo " }\n"; |
||
340 | echo " }\n"; |
||
341 | echo " }\n"; |
||
342 | echo "//]]>\n"; |
||
343 | echo "</script>\n"; |
||
344 | |||
345 | echo "<table>\n"; |
||
346 | |||
347 | // Output table header |
||
348 | echo "<tr><th class=\"data\">{$lang['strshow']}</th><th class=\"data\">{$lang['strcolumn']}</th>"; |
||
349 | echo "<th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['stroperator']}</th>"; |
||
350 | echo "<th class=\"data\">{$lang['strvalue']}</th></tr>"; |
||
351 | |||
352 | $i = 0; |
||
353 | while (!$attrs->EOF) { |
||
354 | $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
||
355 | // Set up default value if there isn't one already |
||
356 | if (!isset($_REQUEST['values'][$attrs->fields['attname']])) { |
||
357 | $_REQUEST['values'][$attrs->fields['attname']] = null; |
||
358 | } |
||
359 | |||
360 | if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) { |
||
361 | $_REQUEST['ops'][$attrs->fields['attname']] = null; |
||
362 | } |
||
363 | |||
364 | // Continue drawing row |
||
365 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
366 | echo "<tr class=\"data{$id}\">\n"; |
||
367 | echo '<td style="white-space:nowrap;">'; |
||
368 | echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"', |
||
369 | isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>'; |
||
370 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>'; |
||
371 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>'; |
||
372 | echo '<td style="white-space:nowrap;">'; |
||
373 | echo "<select name=\"ops[{$attrs->fields['attname']}]\">\n"; |
||
374 | foreach (array_keys($data->selectOps) as $v) { |
||
375 | echo '<option value="', htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] == $v) ? ' selected="selected"' : '', |
||
376 | '>', htmlspecialchars($v), "</option>\n"; |
||
377 | } |
||
378 | echo "</select></td>\n"; |
||
379 | echo '<td style="white-space:nowrap;">', $data->printField( |
||
380 | "values[{$attrs->fields['attname']}]", |
||
381 | $_REQUEST['values'][$attrs->fields['attname']], |
||
382 | $attrs->fields['type'] |
||
383 | ), '</td>'; |
||
384 | echo "</tr>\n"; |
||
385 | ++$i; |
||
386 | $attrs->moveNext(); |
||
387 | } |
||
388 | // Select all checkbox |
||
389 | echo "<tr><td colspan=\"5\"><input type=\"checkbox\" id=\"selectall\" name=\"selectall\" accesskey=\"a\" onclick=\"javascript:selectAll()\" /><label for=\"selectall\">{$lang['strselectallfields']}</label></td></tr>"; |
||
390 | echo "</table>\n"; |
||
391 | } else { |
||
392 | echo "<p>{$lang['strinvalidparam']}</p>\n"; |
||
393 | } |
||
394 | |||
395 | echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n"; |
||
396 | echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['matview']), "\" />\n"; |
||
397 | echo "<input type=\"hidden\" name=\"subject\" value=\"view\" />\n"; |
||
398 | echo $this->misc->form; |
||
399 | echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$lang['strselect']}\" />\n"; |
||
400 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
401 | echo "</form>\n"; |
||
402 | |||
403 | return; |
||
404 | } |
||
405 | if (!isset($_POST['show'])) { |
||
406 | $_POST['show'] = []; |
||
407 | } |
||
408 | |||
409 | if (!isset($_POST['values'])) { |
||
410 | $_POST['values'] = []; |
||
411 | } |
||
412 | |||
413 | if (!isset($_POST['nulls'])) { |
||
414 | $_POST['nulls'] = []; |
||
415 | } |
||
416 | |||
417 | // Verify that they haven't supplied a value for unary operators |
||
418 | foreach ($_POST['ops'] as $k => $v) { |
||
419 | if ('p' == $data->selectOps[$v] && $_POST['values'][$k] != '') { |
||
420 | $this->doSelectRows(true, $lang['strselectunary']); |
||
421 | |||
422 | return; |
||
423 | } |
||
424 | } |
||
425 | |||
426 | if (0 == sizeof($_POST['show'])) { |
||
427 | return $this->doSelectRows(true, $lang['strselectneedscol']); |
||
428 | } |
||
429 | // Generate query SQL |
||
430 | $query = $data->getSelectSQL($_REQUEST['matview'], array_keys($_POST['show']), $_POST['values'], $_POST['ops']); |
||
431 | |||
432 | $_REQUEST['query'] = $query; |
||
433 | $_REQUEST['return'] = 'schema'; |
||
434 | |||
435 | $this->setNoOutput(true); |
||
436 | |||
437 | $display_controller = new DisplayController($this->getContainer()); |
||
438 | |||
439 | return $display_controller->render(); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * Show confirmation of drop and perform actual drop. |
||
444 | * |
||
445 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
446 | */ |
||
447 | public function doDrop($confirm) |
||
514 | } |
||
515 | } |
||
516 | } |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Sets up choices for table linkage, and which fields to select for the view we're creating. |
||
521 | * |
||
522 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
523 | */ |
||
524 | public function doSetParamsCreate($msg = '') |
||
671 | } |
||
672 | } |
||
673 | |||
674 | /** |
||
675 | * Display a wizard where they can enter a new view. |
||
676 | * |
||
677 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
678 | */ |
||
679 | public function doWizardCreate($msg = '') |
||
714 | } |
||
715 | |||
716 | /** |
||
717 | * Displays a screen where they can enter a new view. |
||
718 | * |
||
719 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
720 | */ |
||
721 | public function doCreate($msg = '') |
||
722 | { |
||
723 | $conf = $this->conf; |
||
724 | |||
725 | $lang = $this->lang; |
||
726 | $data = $this->misc->getDatabaseAccessor(); |
||
727 | |||
728 | if (!isset($_REQUEST['formView'])) { |
||
729 | $_REQUEST['formView'] = ''; |
||
730 | } |
||
731 | |||
732 | if (!isset($_REQUEST['formDefinition'])) { |
||
733 | if (isset($_SESSION['sqlquery'])) { |
||
734 | $_REQUEST['formDefinition'] = $_SESSION['sqlquery']; |
||
735 | } else { |
||
736 | $_REQUEST['formDefinition'] = 'SELECT '; |
||
737 | } |
||
738 | } |
||
739 | if (!isset($_REQUEST['formComment'])) { |
||
740 | $_REQUEST['formComment'] = ''; |
||
741 | } |
||
742 | |||
743 | $this->printTrail('schema'); |
||
744 | $this->printTitle($lang['strcreateview'], 'pg.matview.create'); |
||
745 | $this->printMsg($msg); |
||
746 | |||
747 | echo '<form action="'.\SUBFOLDER."/src/views/materializedviews.php\" method=\"post\">\n"; |
||
748 | echo "<table style=\"width: 100%\">\n"; |
||
749 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
750 | echo "\t<td class=\"data1\"><input name=\"formView\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
751 | htmlspecialchars($_REQUEST['formView']), "\" /></td>\n\t</tr>\n"; |
||
752 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strdefinition']}</th>\n"; |
||
753 | echo "\t<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"10\" cols=\"50\" name=\"formDefinition\">", |
||
754 | htmlspecialchars($_REQUEST['formDefinition']), "</textarea></td>\n\t</tr>\n"; |
||
755 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
756 | echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
757 | htmlspecialchars($_REQUEST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
758 | echo "</table>\n"; |
||
759 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
760 | echo $this->misc->form; |
||
761 | echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
||
762 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
763 | echo "</form>\n"; |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * Actually creates the new view in the database. |
||
768 | */ |
||
769 | public function doSaveCreate() |
||
788 | } |
||
789 | } |
||
790 | } |
||
791 | |||
792 | /** |
||
793 | * Actually creates the new wizard view in the database. |
||
794 | */ |
||
795 | public function doSaveCreateWiz() |
||
929 |