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