Total Complexity | 53 |
Total Lines | 653 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ViewpropertiesController 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 ViewpropertiesController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ViewpropertiesController extends BaseController |
||
15 | { |
||
16 | public $controller_name = 'ViewpropertiesController'; |
||
17 | |||
18 | /** |
||
19 | * Default method to render the controller according to the action parameter. |
||
20 | */ |
||
21 | public function render() |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Show view definition and virtual columns. |
||
99 | * |
||
100 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
101 | */ |
||
102 | public function doDefault($msg = '') |
||
103 | { |
||
104 | $conf = $this->conf; |
||
105 | |||
106 | $lang = $this->lang; |
||
107 | $data = $this->misc->getDatabaseAccessor(); |
||
108 | |||
109 | $attPre = function (&$rowdata) use ($data) { |
||
110 | $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']); |
||
111 | }; |
||
112 | |||
113 | $this->printTrail('view'); |
||
114 | $this->printTabs('view', 'columns'); |
||
115 | $this->printMsg($msg); |
||
116 | |||
117 | // Get view |
||
118 | $vdata = $data->getView($_REQUEST['view']); |
||
119 | // Get columns (using same method for getting a view) |
||
120 | $attrs = $data->getTableAttributes($_REQUEST['view']); |
||
121 | |||
122 | // Show comment if any |
||
123 | if (null !== $vdata->fields['relcomment']) { |
||
124 | echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n"; |
||
125 | } |
||
126 | |||
127 | $columns = [ |
||
128 | 'column' => [ |
||
129 | 'title' => $lang['strcolumn'], |
||
130 | 'field' => Decorator::field('attname'), |
||
131 | 'url' => "colproperties.php?subject=column&{$this->misc->href}&view=".urlencode($_REQUEST['view']).'&', |
||
132 | 'vars' => ['column' => 'attname'], |
||
133 | ], |
||
134 | 'type' => [ |
||
135 | 'title' => $lang['strtype'], |
||
136 | 'field' => Decorator::field('+type'), |
||
137 | ], |
||
138 | 'default' => [ |
||
139 | 'title' => $lang['strdefault'], |
||
140 | 'field' => Decorator::field('adsrc'), |
||
141 | ], |
||
142 | 'actions' => [ |
||
143 | 'title' => $lang['stractions'], |
||
144 | ], |
||
145 | 'comment' => [ |
||
146 | 'title' => $lang['strcomment'], |
||
147 | 'field' => Decorator::field('comment'), |
||
148 | ], |
||
149 | ]; |
||
150 | |||
151 | $actions = [ |
||
152 | 'alter' => [ |
||
153 | 'content' => $lang['stralter'], |
||
154 | 'attr' => [ |
||
155 | 'href' => [ |
||
156 | 'url' => 'viewproperties.php', |
||
157 | 'urlvars' => [ |
||
158 | 'action' => 'properties', |
||
159 | 'view' => $_REQUEST['view'], |
||
160 | 'column' => Decorator::field('attname'), |
||
161 | ], |
||
162 | ], |
||
163 | ], |
||
164 | ], |
||
165 | ]; |
||
166 | |||
167 | echo $this->printTable($attrs, $columns, $actions, 'viewproperties-viewproperties', null, $attPre); |
||
168 | |||
169 | echo "<br />\n"; |
||
170 | |||
171 | $navlinks = [ |
||
172 | 'browse' => [ |
||
173 | 'attr' => [ |
||
174 | 'href' => [ |
||
175 | 'url' => 'display.php', |
||
176 | 'urlvars' => [ |
||
177 | 'server' => $_REQUEST['server'], |
||
178 | 'database' => $_REQUEST['database'], |
||
179 | 'schema' => $_REQUEST['schema'], |
||
180 | 'view' => $_REQUEST['view'], |
||
181 | 'subject' => 'view', |
||
182 | 'return' => 'view', |
||
183 | ], |
||
184 | ], |
||
185 | ], |
||
186 | 'content' => $lang['strbrowse'], |
||
187 | ], |
||
188 | 'select' => [ |
||
189 | 'attr' => [ |
||
190 | 'href' => [ |
||
191 | 'url' => 'views.php', |
||
192 | 'urlvars' => [ |
||
193 | 'action' => 'confselectrows', |
||
194 | 'server' => $_REQUEST['server'], |
||
195 | 'database' => $_REQUEST['database'], |
||
196 | 'schema' => $_REQUEST['schema'], |
||
197 | 'view' => $_REQUEST['view'], |
||
198 | ], |
||
199 | ], |
||
200 | ], |
||
201 | 'content' => $lang['strselect'], |
||
202 | ], |
||
203 | 'drop' => [ |
||
204 | 'attr' => [ |
||
205 | 'href' => [ |
||
206 | 'url' => 'views.php', |
||
207 | 'urlvars' => [ |
||
208 | 'action' => 'confirm_drop', |
||
209 | 'server' => $_REQUEST['server'], |
||
210 | 'database' => $_REQUEST['database'], |
||
211 | 'schema' => $_REQUEST['schema'], |
||
212 | 'view' => $_REQUEST['view'], |
||
213 | ], |
||
214 | ], |
||
215 | ], |
||
216 | 'content' => $lang['strdrop'], |
||
217 | ], |
||
218 | 'alter' => [ |
||
219 | 'attr' => [ |
||
220 | 'href' => [ |
||
221 | 'url' => 'viewproperties.php', |
||
222 | 'urlvars' => [ |
||
223 | 'action' => 'confirm_alter', |
||
224 | 'server' => $_REQUEST['server'], |
||
225 | 'database' => $_REQUEST['database'], |
||
226 | 'schema' => $_REQUEST['schema'], |
||
227 | 'view' => $_REQUEST['view'], |
||
228 | ], |
||
229 | ], |
||
230 | ], |
||
231 | 'content' => $lang['stralter'], |
||
232 | ], |
||
233 | ]; |
||
234 | |||
235 | $this->printNavLinks($navlinks, 'viewproperties-viewproperties', get_defined_vars()); |
||
236 | } |
||
237 | |||
238 | public function doTree() |
||
1 ignored issue
–
show
|
|||
239 | { |
||
240 | $conf = $this->conf; |
||
241 | |||
242 | $lang = $this->lang; |
||
243 | $data = $this->misc->getDatabaseAccessor(); |
||
244 | |||
245 | $reqvars = $this->misc->getRequestVars('column'); |
||
246 | $columns = $data->getTableAttributes($_REQUEST['view']); |
||
247 | |||
248 | $attrs = [ |
||
249 | 'text' => Decorator::field('attname'), |
||
250 | 'action' => Decorator::actionurl( |
||
251 | 'colproperties.php', |
||
252 | $reqvars, |
||
253 | [ |
||
254 | 'view' => $_REQUEST['view'], |
||
255 | 'column' => Decorator::field('attname'), |
||
256 | ] |
||
257 | ), |
||
258 | 'icon' => 'Column', |
||
259 | 'iconAction' => Decorator::url( |
||
260 | 'display.php', |
||
261 | $reqvars, |
||
262 | [ |
||
263 | 'view' => $_REQUEST['view'], |
||
264 | 'column' => Decorator::field('attname'), |
||
265 | 'query' => Decorator::replace( |
||
266 | 'SELECT "%column%", count(*) AS "count" FROM %view% GROUP BY "%column%" ORDER BY "%column%"', |
||
267 | [ |
||
268 | '%column%' => Decorator::field('attname'), |
||
269 | '%view%' => $_REQUEST['view'], |
||
270 | ] |
||
271 | ), |
||
272 | ] |
||
273 | ), |
||
274 | 'toolTip' => Decorator::field('comment'), |
||
275 | ]; |
||
276 | |||
277 | return $this->printTree($columns, $attrs, 'viewcolumns'); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Function to save after editing a view. |
||
282 | */ |
||
283 | public function doSaveEdit() |
||
284 | { |
||
285 | $conf = $this->conf; |
||
286 | |||
287 | $lang = $this->lang; |
||
288 | $data = $this->misc->getDatabaseAccessor(); |
||
289 | |||
290 | $status = $data->setView($_POST['view'], $_POST['formDefinition'], $_POST['formComment']); |
||
291 | if (0 == $status) { |
||
292 | $this->doDefinition($lang['strviewupdated']); |
||
293 | } else { |
||
294 | $this->doEdit($lang['strviewupdatedbad']); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Function to allow editing of a view. |
||
300 | * |
||
301 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
302 | */ |
||
303 | public function doEdit($msg = '') |
||
304 | { |
||
305 | $conf = $this->conf; |
||
306 | |||
307 | $lang = $this->lang; |
||
308 | $data = $this->misc->getDatabaseAccessor(); |
||
309 | |||
310 | $this->printTrail('view'); |
||
311 | $this->printTitle($lang['stredit'], 'pg.view.alter'); |
||
312 | $this->printMsg($msg); |
||
313 | |||
314 | $viewdata = $data->getView($_REQUEST['view']); |
||
315 | |||
316 | if ($viewdata->recordCount() > 0) { |
||
317 | if (!isset($_POST['formDefinition'])) { |
||
318 | $_POST['formDefinition'] = $viewdata->fields['vwdefinition']; |
||
319 | $_POST['formComment'] = $viewdata->fields['relcomment']; |
||
320 | } |
||
321 | |||
322 | echo '<form action="'.\SUBFOLDER."/src/views/viewproperties.php\" method=\"post\">\n"; |
||
323 | echo "<table style=\"width: 100%\">\n"; |
||
324 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strdefinition']}</th>\n"; |
||
325 | echo "\t\t<td class=\"data1\"><textarea style=\"width: 100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">", |
||
326 | htmlspecialchars($_POST['formDefinition']), "</textarea></td>\n\t</tr>\n"; |
||
327 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
328 | echo "\t\t<td class=\"data1\"><textarea rows=\"3\" cols=\"32\" name=\"formComment\">", |
||
329 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
330 | echo "</table>\n"; |
||
331 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n"; |
||
332 | echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n"; |
||
333 | echo $this->misc->form; |
||
334 | echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n"; |
||
335 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
336 | echo "</form>\n"; |
||
337 | } else { |
||
338 | echo "<p>{$lang['strnodata']}</p>\n"; |
||
339 | } |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Allow the dumping of the data "in" a view |
||
344 | * NOTE:: PostgreSQL doesn't currently support dumping the data in a view |
||
345 | * so I have disabled the data related parts for now. In the future |
||
346 | * we should allow it conditionally if it becomes supported. This is |
||
347 | * a SMOP since it is based on pg_dump version not backend version. |
||
348 | * |
||
349 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
350 | */ |
||
351 | public function doExport($msg = '') |
||
352 | { |
||
353 | $conf = $this->conf; |
||
354 | |||
355 | $lang = $this->lang; |
||
356 | $data = $this->misc->getDatabaseAccessor(); |
||
357 | |||
358 | $this->printTrail('view'); |
||
359 | $this->printTabs('view', 'export'); |
||
360 | $this->printMsg($msg); |
||
361 | |||
362 | echo '<form action="'.\SUBFOLDER."/src/views/dataexport.php\" method=\"post\">\n"; |
||
363 | echo "<table>\n"; |
||
364 | echo "<tr><th class=\"data\">{$lang['strformat']}</th><th class=\"data\" colspan=\"2\">{$lang['stroptions']}</th></tr>\n"; |
||
365 | // Data only |
||
366 | echo "<!--\n"; |
||
367 | echo '<tr><th class="data left">'; |
||
368 | echo "<input type=\"radio\" id=\"what1\" name=\"what\" value=\"dataonly\" /><label for=\"what1\">{$lang['strdataonly']}</label></th>\n"; |
||
369 | echo "<td>{$lang['strformat']}</td>\n"; |
||
370 | echo "<td><select name=\"d_format\" >\n"; |
||
371 | echo "<option value=\"copy\">COPY</option>\n"; |
||
372 | echo "<option value=\"sql\">SQL</option>\n"; |
||
373 | echo "<option value=\"csv\">CSV</option>\n"; |
||
374 | echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n"; |
||
375 | echo "<option value=\"html\">XHTML</option>\n"; |
||
376 | echo "<option value=\"xml\">XML</option>\n"; |
||
377 | echo "</select>\n</td>\n</tr>\n"; |
||
378 | echo "-->\n"; |
||
379 | |||
380 | // Structure only |
||
381 | echo "<tr><th class=\"data left\"><input type=\"radio\" id=\"what2\" name=\"what\" value=\"structureonly\" checked=\"checked\" /><label for=\"what2\">{$lang['strstructureonly']}</label></th>\n"; |
||
382 | echo "<td><label for=\"s_clean\">{$lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"s_clean\" name=\"s_clean\" /></td>\n</tr>\n"; |
||
383 | // Structure and data |
||
384 | echo "<!--\n"; |
||
385 | echo '<tr><th class="data left" rowspan="2">'; |
||
386 | echo "<input type=\"radio\" id=\"what3\" name=\"what\" value=\"structureanddata\" /><label for=\"what3\">{$lang['strstructureanddata']}</label></th>\n"; |
||
387 | echo "<td>{$lang['strformat']}</td>\n"; |
||
388 | echo "<td><select name=\"sd_format\">\n"; |
||
389 | echo "<option value=\"copy\">COPY</option>\n"; |
||
390 | echo "<option value=\"sql\">SQL</option>\n"; |
||
391 | echo "</select>\n</td>\n</tr>\n"; |
||
392 | echo "<td><label for=\"sd_clean\">{$lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"sd_clean\" name=\"sd_clean\" /></td>\n</tr>\n"; |
||
393 | echo "-->\n"; |
||
394 | echo "</table>\n"; |
||
395 | |||
396 | echo "<h3>{$lang['stroptions']}</h3>\n"; |
||
397 | echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$lang['strshow']}</label>\n"; |
||
398 | echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$lang['strdownload']}</label></p>\n"; |
||
399 | |||
400 | echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n"; |
||
401 | echo $this->misc->form; |
||
402 | echo "<input type=\"hidden\" name=\"subject\" value=\"view\" />\n"; |
||
403 | echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n"; |
||
404 | echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n"; |
||
405 | echo "</form>\n"; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Show definition for a view. |
||
410 | * |
||
411 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
412 | */ |
||
413 | public function doDefinition($msg = '') |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Displays a screen where they can alter a column in a view. |
||
460 | * |
||
461 | * @param mixed $msg |
||
1 ignored issue
–
show
|
|||
462 | */ |
||
463 | public function doProperties($msg = '') |
||
554 | } |
||
555 | } |
||
556 | |||
557 | public function doAlter($confirm = false, $msg = '') |
||
667 | } |
||
668 | } |
||
671 |