Total Complexity | 112 |
Total Lines | 902 |
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 | public function render() |
||
1 ignored issue
–
show
|
|||
21 | { |
||
22 | $conf = $this->conf; |
||
23 | |||
24 | $lang = $this->lang; |
||
25 | $action = $this->action; |
||
26 | |||
27 | if ('tree' == $action) { |
||
28 | return $this->doTree(); |
||
29 | } |
||
30 | if ('subtree' == $action) { |
||
31 | return $this->doSubTree(); |
||
32 | } |
||
33 | |||
34 | $data = $this->misc->getDatabaseAccessor(); |
||
35 | |||
36 | $this->printHeader('M ' . $lang['strviews']); |
||
37 | $this->printBody(); |
||
38 | |||
39 | switch ($action) { |
||
40 | case 'selectrows': |
||
41 | if (!isset($_REQUEST['cancel'])) { |
||
42 | $this->doSelectRows(false); |
||
43 | } else { |
||
44 | $this->doDefault(); |
||
45 | } |
||
46 | |||
47 | break; |
||
48 | case 'confselectrows': |
||
49 | $this->doSelectRows(true); |
||
50 | |||
51 | break; |
||
52 | case 'save_create_wiz': |
||
53 | if (isset($_REQUEST['cancel'])) { |
||
54 | $this->doDefault(); |
||
55 | } else { |
||
56 | $this->doSaveCreateWiz(); |
||
57 | } |
||
58 | |||
59 | break; |
||
60 | case 'wiz_create': |
||
61 | $this->doWizardCreate(); |
||
62 | |||
63 | break; |
||
64 | case 'set_params_create': |
||
65 | if (isset($_POST['cancel'])) { |
||
66 | $this->doDefault(); |
||
67 | } else { |
||
68 | $this->doSetParamsCreate(); |
||
69 | } |
||
70 | |||
71 | break; |
||
72 | case 'save_create': |
||
73 | if (isset($_REQUEST['cancel'])) { |
||
74 | $this->doDefault(); |
||
75 | } else { |
||
76 | $this->doSaveCreate(); |
||
77 | } |
||
78 | |||
79 | break; |
||
80 | case 'create': |
||
81 | $this->doCreate(); |
||
82 | |||
83 | break; |
||
84 | case 'drop': |
||
85 | if (isset($_POST['drop'])) { |
||
86 | $this->doDrop(false); |
||
87 | } else { |
||
88 | $this->doDefault(); |
||
89 | } |
||
90 | |||
91 | break; |
||
92 | case 'confirm_drop': |
||
93 | $this->doDrop(true); |
||
94 | |||
95 | break; |
||
96 | default: |
||
97 | $this->doDefault(); |
||
98 | |||
99 | break; |
||
100 | } |
||
101 | |||
102 | $this->printFooter(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Show default list of views in the database |
||
107 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
108 | */ |
||
109 | public function doDefault($msg = '') |
||
110 | { |
||
111 | $conf = $this->conf; |
||
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 | $conf = $this->conf; |
||
250 | |||
251 | $lang = $this->lang; |
||
252 | $data = $this->misc->getDatabaseAccessor(); |
||
253 | |||
254 | $matviews = $data->getMaterializedViews(); |
||
255 | |||
256 | $reqvars = $this->misc->getRequestVars('matview'); |
||
257 | |||
258 | $attrs = [ |
||
259 | 'text' => Decorator::field('relname'), |
||
260 | 'icon' => 'MView', |
||
261 | 'iconAction' => Decorator::url('display.php', $reqvars, ['matview' => Decorator::field('relname')]), |
||
262 | 'toolTip' => Decorator::field('relcomment'), |
||
263 | 'action' => Decorator::redirecturl('redirect.php', $reqvars, ['matview' => Decorator::field('relname')]), |
||
264 | 'branch' => Decorator::url('materializedviews.php', $reqvars, ['action' => 'subtree', 'matview' => Decorator::field('relname')]), |
||
265 | ]; |
||
266 | |||
267 | return $this->printTree($matviews, $attrs, 'matviews'); |
||
268 | } |
||
269 | |||
270 | public function doSubTree() |
||
1 ignored issue
–
show
|
|||
271 | { |
||
272 | $conf = $this->conf; |
||
273 | |||
274 | $lang = $this->lang; |
||
275 | $data = $this->misc->getDatabaseAccessor(); |
||
276 | |||
277 | $tabs = $this->misc->getNavTabs('matview'); |
||
278 | $items = $this->adjustTabsForTree($tabs); |
||
279 | $reqvars = $this->misc->getRequestVars('matview'); |
||
280 | |||
281 | $attrs = [ |
||
282 | 'text' => Decorator::field('title'), |
||
283 | 'icon' => Decorator::field('icon'), |
||
284 | 'action' => Decorator::actionurl(Decorator::field('url'), $reqvars, Decorator::field('urlvars'), ['matview' => $_REQUEST['matview']]), |
||
285 | 'branch' => Decorator::ifempty( |
||
286 | Decorator::field('branch'), |
||
287 | '', |
||
288 | Decorator::url( |
||
289 | Decorator::field('url'), |
||
290 | Decorator::field('urlvars'), |
||
291 | $reqvars, |
||
292 | [ |
||
293 | 'action' => 'tree', |
||
294 | 'matview' => $_REQUEST['matview'], |
||
295 | ] |
||
296 | ) |
||
297 | ), |
||
298 | ]; |
||
299 | |||
300 | return $this->printTree($items, $attrs, 'matviews'); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Ask for select parameters and perform select |
||
305 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
306 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
307 | */ |
||
308 | public function doSelectRows($confirm, $msg = '') |
||
309 | { |
||
310 | $conf = $this->conf; |
||
311 | |||
312 | $lang = $this->lang; |
||
313 | $data = $this->misc->getDatabaseAccessor(); |
||
314 | |||
315 | if ($confirm) { |
||
316 | $this->printTrail('view'); |
||
317 | $this->printTabs('matview', 'select'); |
||
318 | $this->printMsg($msg); |
||
319 | |||
320 | $attrs = $data->getTableAttributes($_REQUEST['matview']); |
||
321 | |||
322 | echo '<form action="' . SUBFOLDER . '/src/views/' . $this->script . '" method="post" id="selectform">'; |
||
323 | echo "\n"; |
||
324 | |||
325 | if ($attrs->recordCount() > 0) { |
||
326 | // JavaScript for select all feature |
||
327 | echo "<script type=\"text/javascript\">\n"; |
||
328 | echo "//<![CDATA[\n"; |
||
329 | echo " function selectAll() {\n"; |
||
330 | echo " for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n"; |
||
331 | echo " var e = document.getElementById('selectform').elements[i];\n"; |
||
332 | echo " if (e.name.indexOf('show') == 0) { \n "; |
||
333 | echo " e.checked = document.getElementById('selectform').selectall.checked;\n"; |
||
334 | echo " }\n"; |
||
335 | echo " }\n"; |
||
336 | echo " }\n"; |
||
337 | echo "//]]>\n"; |
||
338 | echo "</script>\n"; |
||
339 | |||
340 | echo "<table>\n"; |
||
341 | |||
342 | // Output table header |
||
343 | echo "<tr><th class=\"data\">{$lang['strshow']}</th><th class=\"data\">{$lang['strcolumn']}</th>"; |
||
344 | echo "<th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['stroperator']}</th>"; |
||
345 | echo "<th class=\"data\">{$lang['strvalue']}</th></tr>"; |
||
346 | |||
347 | $i = 0; |
||
348 | while (!$attrs->EOF) { |
||
349 | $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']); |
||
350 | // Set up default value if there isn't one already |
||
351 | if (!isset($_REQUEST['values'][$attrs->fields['attname']])) { |
||
352 | $_REQUEST['values'][$attrs->fields['attname']] = null; |
||
353 | } |
||
354 | |||
355 | if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) { |
||
356 | $_REQUEST['ops'][$attrs->fields['attname']] = null; |
||
357 | } |
||
358 | |||
359 | // Continue drawing row |
||
360 | $id = (0 == ($i % 2) ? '1' : '2'); |
||
361 | echo "<tr class=\"data{$id}\">\n"; |
||
362 | echo '<td style="white-space:nowrap;">'; |
||
363 | echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"', |
||
364 | isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>'; |
||
365 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>'; |
||
366 | echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>'; |
||
367 | echo '<td style="white-space:nowrap;">'; |
||
368 | echo "<select name=\"ops[{$attrs->fields['attname']}]\">\n"; |
||
369 | foreach (array_keys($data->selectOps) as $v) { |
||
370 | echo '<option value="', htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] == $v) ? ' selected="selected"' : '', |
||
371 | '>', htmlspecialchars($v), "</option>\n"; |
||
372 | } |
||
373 | echo "</select></td>\n"; |
||
374 | echo '<td style="white-space:nowrap;">', $data->printField( |
||
375 | "values[{$attrs->fields['attname']}]", |
||
376 | $_REQUEST['values'][$attrs->fields['attname']], |
||
377 | $attrs->fields['type'] |
||
378 | ), '</td>'; |
||
379 | echo "</tr>\n"; |
||
380 | $i++; |
||
381 | $attrs->moveNext(); |
||
382 | } |
||
383 | // Select all checkbox |
||
384 | 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>"; |
||
385 | echo "</table>\n"; |
||
386 | } else { |
||
387 | echo "<p>{$lang['strinvalidparam']}</p>\n"; |
||
388 | } |
||
389 | |||
390 | echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n"; |
||
391 | echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['matview']), "\" />\n"; |
||
392 | echo "<input type=\"hidden\" name=\"subject\" value=\"view\" />\n"; |
||
393 | echo $this->misc->form; |
||
394 | echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$lang['strselect']}\" />\n"; |
||
395 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
396 | echo "</form>\n"; |
||
397 | |||
398 | return; |
||
399 | } |
||
400 | if (!isset($_POST['show'])) { |
||
401 | $_POST['show'] = []; |
||
402 | } |
||
403 | |||
404 | if (!isset($_POST['values'])) { |
||
405 | $_POST['values'] = []; |
||
406 | } |
||
407 | |||
408 | if (!isset($_POST['nulls'])) { |
||
409 | $_POST['nulls'] = []; |
||
410 | } |
||
411 | |||
412 | // Verify that they haven't supplied a value for unary operators |
||
413 | foreach ($_POST['ops'] as $k => $v) { |
||
414 | if ('p' == $data->selectOps[$v] && $_POST['values'][$k] != '') { |
||
415 | $this->doSelectRows(true, $lang['strselectunary']); |
||
416 | |||
417 | return; |
||
418 | } |
||
419 | } |
||
420 | |||
421 | if (0 == sizeof($_POST['show'])) { |
||
422 | return $this->doSelectRows(true, $lang['strselectneedscol']); |
||
423 | } |
||
424 | // Generate query SQL |
||
425 | $query = $data->getSelectSQL($_REQUEST['matview'], array_keys($_POST['show']), $_POST['values'], $_POST['ops']); |
||
426 | |||
427 | $_REQUEST['query'] = $query; |
||
428 | $_REQUEST['return'] = 'schema'; |
||
429 | |||
430 | $this->setNoOutput(true); |
||
431 | |||
432 | $display_controller = new DisplayController($this->getContainer()); |
||
433 | |||
434 | return $display_controller->render(); |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * Show confirmation of drop and perform actual drop |
||
439 | * @param mixed $confirm |
||
1 ignored issue
–
show
|
|||
440 | */ |
||
441 | public function doDrop($confirm) |
||
508 | } |
||
509 | } |
||
510 | } |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Sets up choices for table linkage, and which fields to select for the view we're creating |
||
515 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
516 | */ |
||
517 | public function doSetParamsCreate($msg = '') |
||
664 | } |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * Display a wizard where they can enter a new view |
||
669 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
670 | */ |
||
671 | public function doWizardCreate($msg = '') |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * Displays a screen where they can enter a new view |
||
710 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
711 | */ |
||
712 | public function doCreate($msg = '') |
||
713 | { |
||
714 | $conf = $this->conf; |
||
715 | |||
716 | $lang = $this->lang; |
||
717 | $data = $this->misc->getDatabaseAccessor(); |
||
718 | |||
719 | if (!isset($_REQUEST['formView'])) { |
||
720 | $_REQUEST['formView'] = ''; |
||
721 | } |
||
722 | |||
723 | if (!isset($_REQUEST['formDefinition'])) { |
||
724 | if (isset($_SESSION['sqlquery'])) { |
||
725 | $_REQUEST['formDefinition'] = $_SESSION['sqlquery']; |
||
726 | } else { |
||
727 | $_REQUEST['formDefinition'] = 'SELECT '; |
||
728 | } |
||
729 | } |
||
730 | if (!isset($_REQUEST['formComment'])) { |
||
731 | $_REQUEST['formComment'] = ''; |
||
732 | } |
||
733 | |||
734 | $this->printTrail('schema'); |
||
735 | $this->printTitle($lang['strcreateview'], 'pg.matview.create'); |
||
736 | $this->printMsg($msg); |
||
737 | |||
738 | echo '<form action="' . SUBFOLDER . "/src/views/materializedviews.php\" method=\"post\">\n"; |
||
739 | echo "<table style=\"width: 100%\">\n"; |
||
740 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
741 | echo "\t<td class=\"data1\"><input name=\"formView\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
742 | htmlspecialchars($_REQUEST['formView']), "\" /></td>\n\t</tr>\n"; |
||
743 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strdefinition']}</th>\n"; |
||
744 | echo "\t<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"10\" cols=\"50\" name=\"formDefinition\">", |
||
745 | htmlspecialchars($_REQUEST['formDefinition']), "</textarea></td>\n\t</tr>\n"; |
||
746 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
747 | echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
748 | htmlspecialchars($_REQUEST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
749 | echo "</table>\n"; |
||
750 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
751 | echo $this->misc->form; |
||
752 | echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
||
753 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
754 | echo "</form>\n"; |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * Actually creates the new view in the database |
||
759 | */ |
||
760 | public function doSaveCreate() |
||
779 | } |
||
780 | } |
||
781 | } |
||
782 | |||
783 | /** |
||
784 | * Actually creates the new wizard view in the database |
||
785 | */ |
||
786 | public function doSaveCreateWiz() |
||
920 |