Total Complexity | 112 |
Total Lines | 889 |
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() |
||
24 | { |
||
25 | $lang = $this->lang; |
||
26 | $action = $this->action; |
||
27 | |||
28 | if ('tree' == $action) { |
||
29 | return $this->doTree(); |
||
30 | } |
||
31 | if ('subtree' == $action) { |
||
32 | return $this->doSubTree(); |
||
33 | } |
||
34 | |||
35 | $data = $this->misc->getDatabaseAccessor(); |
||
36 | |||
37 | $this->printHeader('M ' . $lang['strviews']); |
||
38 | $this->printBody(); |
||
39 | |||
40 | switch ($action) { |
||
41 | case 'selectrows': |
||
42 | if (!isset($_REQUEST['cancel'])) { |
||
43 | $this->doSelectRows(false); |
||
44 | } else { |
||
45 | $this->doDefault(); |
||
46 | } |
||
47 | |||
48 | break; |
||
49 | case 'confselectrows': |
||
50 | $this->doSelectRows(true); |
||
51 | |||
52 | break; |
||
53 | case 'save_create_wiz': |
||
54 | if (isset($_REQUEST['cancel'])) { |
||
55 | $this->doDefault(); |
||
56 | } else { |
||
57 | $this->doSaveCreateWiz(); |
||
58 | } |
||
59 | |||
60 | break; |
||
61 | case 'wiz_create': |
||
62 | $this->doWizardCreate(); |
||
63 | |||
64 | break; |
||
65 | case 'set_params_create': |
||
66 | if (isset($_POST['cancel'])) { |
||
67 | $this->doDefault(); |
||
68 | } else { |
||
69 | $this->doSetParamsCreate(); |
||
70 | } |
||
71 | |||
72 | break; |
||
73 | case 'save_create': |
||
74 | if (isset($_REQUEST['cancel'])) { |
||
75 | $this->doDefault(); |
||
76 | } else { |
||
77 | $this->doSaveCreate(); |
||
78 | } |
||
79 | |||
80 | break; |
||
81 | case 'create': |
||
82 | $this->doCreate(); |
||
83 | |||
84 | break; |
||
85 | case 'drop': |
||
86 | if (isset($_POST['drop'])) { |
||
87 | $this->doDrop(false); |
||
88 | } else { |
||
89 | $this->doDefault(); |
||
90 | } |
||
91 | |||
92 | break; |
||
93 | case 'confirm_drop': |
||
94 | $this->doDrop(true); |
||
95 | |||
96 | break; |
||
97 | default: |
||
98 | $this->doDefault(); |
||
99 | |||
100 | break; |
||
101 | } |
||
102 | |||
103 | $this->printFooter(); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Show default list of views in the database. |
||
108 | * |
||
109 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
110 | */ |
||
111 | public function doDefault($msg = '') |
||
112 | { |
||
113 | $lang = $this->lang; |
||
114 | $data = $this->misc->getDatabaseAccessor(); |
||
115 | |||
116 | $this->printTrail('schema'); |
||
117 | $this->printTabs('schema', 'matviews'); |
||
118 | $this->printMsg($msg); |
||
119 | |||
120 | //$matviews = $data->getViews(); |
||
121 | $matviews = $data->getMaterializedViews(); |
||
122 | |||
123 | $columns = [ |
||
124 | 'matview' => [ |
||
125 | 'title' => 'M ' . $lang['strview'], |
||
126 | 'field' => Decorator::field('relname'), |
||
127 | 'url' => \SUBFOLDER . "/redirect/matview?{$this->misc->href}&", |
||
128 | 'vars' => ['matview' => 'relname'], |
||
129 | ], |
||
130 | 'owner' => [ |
||
131 | 'title' => $lang['strowner'], |
||
132 | 'field' => Decorator::field('relowner'), |
||
133 | ], |
||
134 | 'actions' => [ |
||
135 | 'title' => $lang['stractions'], |
||
136 | ], |
||
137 | 'comment' => [ |
||
138 | 'title' => $lang['strcomment'], |
||
139 | 'field' => Decorator::field('relcomment'), |
||
140 | ], |
||
141 | ]; |
||
142 | |||
143 | $actions = [ |
||
144 | 'multiactions' => [ |
||
145 | 'keycols' => ['matview' => 'relname'], |
||
146 | 'url' => 'materializedviews.php', |
||
147 | ], |
||
148 | 'browse' => [ |
||
149 | 'content' => $lang['strbrowse'], |
||
150 | 'attr' => [ |
||
151 | 'href' => [ |
||
152 | 'url' => 'display.php', |
||
153 | 'urlvars' => [ |
||
154 | 'action' => 'confselectrows', |
||
155 | 'subject' => 'matview', |
||
156 | 'return' => 'schema', |
||
157 | 'matview' => Decorator::field('relname'), |
||
158 | ], |
||
159 | ], |
||
160 | ], |
||
161 | ], |
||
162 | 'select' => [ |
||
163 | 'content' => $lang['strselect'], |
||
164 | 'attr' => [ |
||
165 | 'href' => [ |
||
166 | 'url' => 'materializedviews.php', |
||
167 | 'urlvars' => [ |
||
168 | 'action' => 'confselectrows', |
||
169 | 'matview' => Decorator::field('relname'), |
||
170 | ], |
||
171 | ], |
||
172 | ], |
||
173 | ], |
||
174 | |||
175 | // Insert is possible if the relevant rule for the view has been created. |
||
176 | // 'insert' => array( |
||
177 | // 'title' => $lang['strinsert'], |
||
178 | // 'url' => "materializedviews.php?action=confinsertrow&{$this->misc->href}&", |
||
179 | // 'vars' => array('view' => 'relname'), |
||
180 | // ), |
||
181 | |||
182 | 'alter' => [ |
||
183 | 'content' => $lang['stralter'], |
||
184 | 'attr' => [ |
||
185 | 'href' => [ |
||
186 | 'url' => 'materializedviewproperties.php', |
||
187 | 'urlvars' => [ |
||
188 | 'action' => 'confirm_alter', |
||
189 | 'matview' => Decorator::field('relname'), |
||
190 | ], |
||
191 | ], |
||
192 | ], |
||
193 | ], |
||
194 | 'drop' => [ |
||
195 | 'multiaction' => 'confirm_drop', |
||
196 | 'content' => $lang['strdrop'], |
||
197 | 'attr' => [ |
||
198 | 'href' => [ |
||
199 | 'url' => 'materializedviews.php', |
||
200 | 'urlvars' => [ |
||
201 | 'action' => 'confirm_drop', |
||
202 | 'matview' => Decorator::field('relname'), |
||
203 | ], |
||
204 | ], |
||
205 | ], |
||
206 | ], |
||
207 | ]; |
||
208 | |||
209 | echo $this->printTable($matviews, $columns, $actions, $this->table_place, $lang['strnoviews']); |
||
210 | |||
211 | $navlinks = [ |
||
212 | 'create' => [ |
||
213 | 'attr' => [ |
||
214 | 'href' => [ |
||
215 | 'url' => 'materializedviews.php', |
||
216 | 'urlvars' => [ |
||
217 | 'action' => 'create', |
||
218 | 'server' => $_REQUEST['server'], |
||
219 | 'database' => $_REQUEST['database'], |
||
220 | 'schema' => $_REQUEST['schema'], |
||
221 | ], |
||
222 | ], |
||
223 | ], |
||
224 | 'content' => $lang['strcreateview'], |
||
225 | ], |
||
226 | 'createwiz' => [ |
||
227 | 'attr' => [ |
||
228 | 'href' => [ |
||
229 | 'url' => 'materializedviews.php', |
||
230 | 'urlvars' => [ |
||
231 | 'action' => 'wiz_create', |
||
232 | 'server' => $_REQUEST['server'], |
||
233 | 'database' => $_REQUEST['database'], |
||
234 | 'schema' => $_REQUEST['schema'], |
||
235 | ], |
||
236 | ], |
||
237 | ], |
||
238 | 'content' => $lang['strcreateviewwiz'], |
||
239 | ], |
||
240 | ]; |
||
241 | $this->printNavLinks($navlinks, $this->table_place, get_defined_vars()); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Generate XML for the browser tree. |
||
246 | */ |
||
247 | public function doTree() |
||
248 | { |
||
249 | $lang = $this->lang; |
||
250 | $data = $this->misc->getDatabaseAccessor(); |
||
251 | |||
252 | $matviews = $data->getMaterializedViews(); |
||
253 | |||
254 | $reqvars = $this->misc->getRequestVars('matview'); |
||
255 | |||
256 | $attrs = [ |
||
257 | 'text' => Decorator::field('relname'), |
||
258 | 'icon' => 'MView', |
||
259 | 'iconAction' => Decorator::url('display.php', $reqvars, ['matview' => Decorator::field('relname')]), |
||
260 | 'toolTip' => Decorator::field('relcomment'), |
||
261 | 'action' => Decorator::redirecturl('redirect.php', $reqvars, ['matview' => Decorator::field('relname')]), |
||
262 | 'branch' => Decorator::url('materializedviews.php', $reqvars, ['action' => 'subtree', 'matview' => Decorator::field('relname')]), |
||
263 | ]; |
||
264 | |||
265 | return $this->printTree($matviews, $attrs, 'matviews'); |
||
266 | } |
||
267 | |||
268 | public function doSubTree() |
||
1 ignored issue
–
show
|
|||
269 | { |
||
270 | $lang = $this->lang; |
||
271 | $data = $this->misc->getDatabaseAccessor(); |
||
272 | |||
273 | $tabs = $this->misc->getNavTabs('matview'); |
||
274 | $items = $this->adjustTabsForTree($tabs); |
||
275 | $reqvars = $this->misc->getRequestVars('matview'); |
||
276 | |||
277 | $attrs = [ |
||
278 | 'text' => Decorator::field('title'), |
||
279 | 'icon' => Decorator::field('icon'), |
||
280 | 'action' => Decorator::actionurl(Decorator::field('url'), $reqvars, Decorator::field('urlvars'), ['matview' => $_REQUEST['matview']]), |
||
281 | 'branch' => Decorator::ifempty( |
||
282 | Decorator::field('branch'), |
||
283 | '', |
||
284 | Decorator::url( |
||
285 | Decorator::field('url'), |
||
286 | Decorator::field('urlvars'), |
||
287 | $reqvars, |
||
288 | [ |
||
289 | 'action' => 'tree', |
||
290 | 'matview' => $_REQUEST['matview'], |
||
291 | ] |
||
292 | ) |
||
293 | ), |
||
294 | ]; |
||
295 | |||
296 | return $this->printTree($items, $attrs, 'matviews'); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Ask for select parameters and perform select. |
||
301 | * |
||
302 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
303 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
304 | */ |
||
305 | public function doSelectRows($confirm, $msg = '') |
||
306 | { |
||
307 | $lang = $this->lang; |
||
308 | $data = $this->misc->getDatabaseAccessor(); |
||
309 | |||
310 | if ($confirm) { |
||
311 | $this->printTrail('view'); |
||
312 | $this->printTabs('matview', 'select'); |
||
313 | $this->printMsg($msg); |
||
314 | |||
315 | $attrs = $data->getTableAttributes($_REQUEST['matview']); |
||
316 | |||
317 | echo '<form action="' . \SUBFOLDER . '/src/views/' . $this->script . '" method="post" id="selectform">'; |
||
318 | echo "\n"; |
||
319 | |||
320 | if ($attrs->recordCount() > 0) { |
||
321 | // JavaScript for select all feature |
||
322 | echo "<script type=\"text/javascript\">\n"; |
||
323 | echo "//<![CDATA[\n"; |
||
324 | echo " function selectAll() {\n"; |
||
325 | echo " for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n"; |
||
326 | echo " var e = document.getElementById('selectform').elements[i];\n"; |
||
327 | echo " if (e.name.indexOf('show') == 0) { \n "; |
||
328 | echo " e.checked = document.getElementById('selectform').selectall.checked;\n"; |
||
329 | echo " }\n"; |
||
330 | echo " }\n"; |
||
331 | echo " }\n"; |
||
332 | echo "//]]>\n"; |
||
333 | echo "</script>\n"; |
||
334 | |||
335 | echo "<table>\n"; |
||
336 | |||
337 | // Output table header |
||
338 | echo "<tr><th class=\"data\">{$lang['strshow']}</th><th class=\"data\">{$lang['strcolumn']}</th>"; |
||
339 | echo "<th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['stroperator']}</th>"; |
||
340 | echo "<th class=\"data\">{$lang['strvalue']}</th></tr>"; |
||
341 | |||
342 | $i = 0; |
||
343 | while (!$attrs->EOF) { |
||
344 | $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
||
345 | // Set up default value if there isn't one already |
||
346 | if (!isset($_REQUEST['values'][$attrs->fields['attname']])) { |
||
347 | $_REQUEST['values'][$attrs->fields['attname']] = null; |
||
348 | } |
||
349 | |||
350 | if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) { |
||
351 | $_REQUEST['ops'][$attrs->fields['attname']] = null; |
||
352 | } |
||
353 | |||
354 | // Continue drawing row |
||
355 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
356 | echo "<tr class=\"data{$id}\">\n"; |
||
357 | echo '<td style="white-space:nowrap;">'; |
||
358 | echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"', |
||
359 | isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>'; |
||
360 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>'; |
||
361 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>'; |
||
362 | echo '<td style="white-space:nowrap;">'; |
||
363 | echo "<select name=\"ops[{$attrs->fields['attname']}]\">\n"; |
||
364 | foreach (array_keys($data->selectOps) as $v) { |
||
365 | echo '<option value="', htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] == $v) ? ' selected="selected"' : '', |
||
366 | '>', htmlspecialchars($v), "</option>\n"; |
||
367 | } |
||
368 | echo "</select></td>\n"; |
||
369 | echo '<td style="white-space:nowrap;">', $data->printField( |
||
370 | "values[{$attrs->fields['attname']}]", |
||
371 | $_REQUEST['values'][$attrs->fields['attname']], |
||
372 | $attrs->fields['type'] |
||
373 | ), '</td>'; |
||
374 | echo "</tr>\n"; |
||
375 | ++$i; |
||
376 | $attrs->moveNext(); |
||
377 | } |
||
378 | // Select all checkbox |
||
379 | 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>"; |
||
380 | echo "</table>\n"; |
||
381 | } else { |
||
382 | echo "<p>{$lang['strinvalidparam']}</p>\n"; |
||
383 | } |
||
384 | |||
385 | echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n"; |
||
386 | echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['matview']), "\" />\n"; |
||
387 | echo "<input type=\"hidden\" name=\"subject\" value=\"view\" />\n"; |
||
388 | echo $this->misc->form; |
||
389 | echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$lang['strselect']}\" />\n"; |
||
390 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
391 | echo "</form>\n"; |
||
392 | |||
393 | return; |
||
394 | } |
||
395 | if (!isset($_POST['show'])) { |
||
396 | $_POST['show'] = []; |
||
397 | } |
||
398 | |||
399 | if (!isset($_POST['values'])) { |
||
400 | $_POST['values'] = []; |
||
401 | } |
||
402 | |||
403 | if (!isset($_POST['nulls'])) { |
||
404 | $_POST['nulls'] = []; |
||
405 | } |
||
406 | |||
407 | // Verify that they haven't supplied a value for unary operators |
||
408 | foreach ($_POST['ops'] as $k => $v) { |
||
409 | if ('p' == $data->selectOps[$v] && $_POST['values'][$k] != '') { |
||
410 | $this->doSelectRows(true, $lang['strselectunary']); |
||
411 | |||
412 | return; |
||
413 | } |
||
414 | } |
||
415 | |||
416 | if (0 == sizeof($_POST['show'])) { |
||
417 | return $this->doSelectRows(true, $lang['strselectneedscol']); |
||
418 | } |
||
419 | // Generate query SQL |
||
420 | $query = $data->getSelectSQL($_REQUEST['matview'], array_keys($_POST['show']), $_POST['values'], $_POST['ops']); |
||
421 | |||
422 | $_REQUEST['query'] = $query; |
||
423 | $_REQUEST['return'] = 'schema'; |
||
424 | |||
425 | $this->setNoOutput(true); |
||
426 | |||
427 | $display_controller = new DisplayController($this->getContainer()); |
||
428 | |||
429 | return $display_controller->render(); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Show confirmation of drop and perform actual drop. |
||
434 | * |
||
435 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
436 | */ |
||
437 | public function doDrop($confirm) |
||
502 | } |
||
503 | } |
||
504 | } |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Sets up choices for table linkage, and which fields to select for the view we're creating. |
||
509 | * |
||
510 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
511 | */ |
||
512 | public function doSetParamsCreate($msg = '') |
||
657 | } |
||
658 | } |
||
659 | |||
660 | /** |
||
661 | * Display a wizard where they can enter a new view. |
||
662 | * |
||
663 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
664 | */ |
||
665 | public function doWizardCreate($msg = '') |
||
698 | } |
||
699 | |||
700 | /** |
||
701 | * Displays a screen where they can enter a new view. |
||
702 | * |
||
703 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
704 | */ |
||
705 | public function doCreate($msg = '') |
||
706 | { |
||
707 | $lang = $this->lang; |
||
708 | $data = $this->misc->getDatabaseAccessor(); |
||
709 | |||
710 | if (!isset($_REQUEST['formView'])) { |
||
711 | $_REQUEST['formView'] = ''; |
||
712 | } |
||
713 | |||
714 | if (!isset($_REQUEST['formDefinition'])) { |
||
715 | if (isset($_SESSION['sqlquery'])) { |
||
716 | $_REQUEST['formDefinition'] = $_SESSION['sqlquery']; |
||
717 | } else { |
||
718 | $_REQUEST['formDefinition'] = 'SELECT '; |
||
719 | } |
||
720 | } |
||
721 | if (!isset($_REQUEST['formComment'])) { |
||
722 | $_REQUEST['formComment'] = ''; |
||
723 | } |
||
724 | |||
725 | $this->printTrail('schema'); |
||
726 | $this->printTitle($lang['strcreateview'], 'pg.matview.create'); |
||
727 | $this->printMsg($msg); |
||
728 | |||
729 | echo '<form action="' . \SUBFOLDER . "/src/views/materializedviews.php\" method=\"post\">\n"; |
||
730 | echo "<table style=\"width: 100%\">\n"; |
||
731 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
732 | echo "\t<td class=\"data1\"><input name=\"formView\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
733 | htmlspecialchars($_REQUEST['formView']), "\" /></td>\n\t</tr>\n"; |
||
734 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strdefinition']}</th>\n"; |
||
735 | echo "\t<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"10\" cols=\"50\" name=\"formDefinition\">", |
||
736 | htmlspecialchars($_REQUEST['formDefinition']), "</textarea></td>\n\t</tr>\n"; |
||
737 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
738 | echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
739 | htmlspecialchars($_REQUEST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
740 | echo "</table>\n"; |
||
741 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
742 | echo $this->misc->form; |
||
743 | echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
||
744 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
745 | echo "</form>\n"; |
||
746 | } |
||
747 | |||
748 | /** |
||
749 | * Actually creates the new view in the database. |
||
750 | */ |
||
751 | public function doSaveCreate() |
||
768 | } |
||
769 | } |
||
770 | } |
||
771 | |||
772 | /** |
||
773 | * Actually creates the new wizard view in the database. |
||
774 | */ |
||
775 | public function doSaveCreateWiz() |
||
907 |