Passed
Push — develop ( 823958...5e1209 )
by Felipe
05:01
created

ViewsController::render()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 5.2392
c 0
b 0
f 0
cc 17
eloc 53
nc 17
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.48
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class ViewsController extends BaseController
17
{
18
    use \PHPPgAdmin\Traits\ViewsMatviewsTrait;
19
20
    public $table_place      = 'views-views';
21
    public $controller_title = 'strviews';
22
23
    // this member variable is view for views and matview for materialized views
24
    public $keystring = 'view';
25
26
    /**
27
     * Default method to render the controller according to the action parameter.
28
     */
29
    public function render()
30
    {
31
        if ('tree' == $this->action) {
32
            return $this->doTree();
33
        }
34
        if ('subtree' == $this->action) {
35
            return $this->doSubTree();
36
        }
37
38
        $this->printHeader();
39
        $this->printBody();
40
41
        switch ($this->action) {
42
            case 'selectrows':
43
                if (!isset($_REQUEST['cancel'])) {
44
                    $this->doSelectRows(false);
45
                } else {
46
                    $this->doDefault();
47
                }
48
49
                break;
50
            case 'confselectrows':
51
                $this->doSelectRows(true);
52
53
                break;
54
            case 'save_create_wiz':
55
                if (isset($_REQUEST['cancel'])) {
56
                    $this->doDefault();
57
                } else {
58
                    $this->doSaveCreateWiz(false);
59
                }
60
61
                break;
62
            case 'wiz_create':
63
                $this->doWizardCreate();
64
65
                break;
66
            case 'set_params_create':
67
                if (isset($_POST['cancel'])) {
68
                    $this->doDefault();
69
                } else {
70
                    $this->doSetParamsCreate();
71
                }
72
73
                break;
74
            case 'save_create':
75
                if (isset($_REQUEST['cancel'])) {
76
                    $this->doDefault();
77
                } else {
78
                    $this->doSaveCreate();
79
                }
80
81
                break;
82
            case 'create':
83
                $this->doCreate();
84
85
                break;
86
            case 'drop':
87
                if (isset($_POST['drop'])) {
88
                    $this->doDrop(false);
89
                } else {
90
                    $this->doDefault();
91
                }
92
93
                break;
94
            case 'confirm_drop':
95
                $this->doDrop(true);
96
97
                break;
98
            default:
99
                $this->doDefault();
100
101
                break;
102
        }
103
104
        return $this->printFooter();
105
    }
106
107
    /**
108
     * Show default list of views in the database.
109
     *
110
     * @param mixed $msg
111
     */
112
    public function doDefault($msg = '')
113
    {
114
        $data = $this->misc->getDatabaseAccessor();
115
116
        $this->printTrail('schema');
117
        $this->printTabs('schema', 'views');
118
        $this->printMsg($msg);
119
120
        $views = $data->getViews();
121
122
        $columns = [
123
            $this->keystring => [
124
                'title' => $this->lang['strview'],
125
                'field' => Decorator::field('relname'),
126
                'url'   => \SUBFOLDER."/redirect/view?{$this->misc->href}&amp;",
127
                'vars'  => [$this->keystring => 'relname'],
128
            ],
129
            'owner'          => [
130
                'title' => $this->lang['strowner'],
131
                'field' => Decorator::field('relowner'),
132
            ],
133
            'actions'        => [
134
                'title' => $this->lang['stractions'],
135
            ],
136
            'comment'        => [
137
                'title' => $this->lang['strcomment'],
138
                'field' => Decorator::field('relcomment'),
139
            ],
140
        ];
141
142
        $actions = [
143
            'multiactions' => [
144
                'keycols' => [$this->keystring => 'relname'],
145
                'url'     => 'views',
146
            ],
147
            'browse'       => [
148
                'content' => $this->lang['strbrowse'],
149
                'attr'    => [
150
                    'href' => [
151
                        'url'     => 'display',
152
                        'urlvars' => [
153
                            'action'         => 'confselectrows',
154
                            'subject'        => $this->keystring,
155
                            'return'         => 'schema',
156
                            $this->keystring => Decorator::field('relname'),
157
                        ],
158
                    ],
159
                ],
160
            ],
161
            'select'       => [
162
                'content' => $this->lang['strselect'],
163
                'attr'    => [
164
                    'href' => [
165
                        'url'     => 'views',
166
                        'urlvars' => [
167
                            'action'         => 'confselectrows',
168
                            $this->keystring => Decorator::field('relname'),
169
                        ],
170
                    ],
171
                ],
172
            ],
173
174
            // Insert is possible if the relevant rule for the view has been created.
175
            //            'insert' => array(
176
            //                'title'    => $this->lang['strinsert'],
177
            //                'url'    => "views?action=confinsertrow&amp;{$this->misc->href}&amp;",
178
            //                'vars'    => array($this->keystring => 'relname'),
179
            //            ),
180
181
            'alter'        => [
182
                'content' => $this->lang['stralter'],
183
                'attr'    => [
184
                    'href' => [
185
                        'url'     => 'viewproperties',
186
                        'urlvars' => [
187
                            'action'         => 'confirm_alter',
188
                            $this->keystring => Decorator::field('relname'),
189
                        ],
190
                    ],
191
                ],
192
            ],
193
            'drop'         => [
194
                'multiaction' => 'confirm_drop',
195
                'content'     => $this->lang['strdrop'],
196
                'attr'        => [
197
                    'href' => [
198
                        'url'     => 'views',
199
                        'urlvars' => [
200
                            'action'         => 'confirm_drop',
201
                            $this->keystring => Decorator::field('relname'),
202
                        ],
203
                    ],
204
                ],
205
            ],
206
        ];
207
208
        echo $this->printTable($views, $columns, $actions, $this->table_place, $this->lang['strnoviews']);
209
210
        $navlinks = [
211
            'create'    => [
212
                'attr'    => [
213
                    'href' => [
214
                        'url'     => 'views',
215
                        'urlvars' => [
216
                            'action'   => 'create',
217
                            'server'   => $_REQUEST['server'],
218
                            'database' => $_REQUEST['database'],
219
                            'schema'   => $_REQUEST['schema'],
220
                        ],
221
                    ],
222
                ],
223
                'content' => $this->lang['strcreateview'],
224
            ],
225
            'createwiz' => [
226
                'attr'    => [
227
                    'href' => [
228
                        'url'     => 'views',
229
                        'urlvars' => [
230
                            'action'   => 'wiz_create',
231
                            'server'   => $_REQUEST['server'],
232
                            'database' => $_REQUEST['database'],
233
                            'schema'   => $_REQUEST['schema'],
234
                        ],
235
                    ],
236
                ],
237
                'content' => $this->lang['strcreateviewwiz'],
238
            ],
239
        ];
240
        $this->printNavLinks($navlinks, $this->table_place, get_defined_vars());
241
    }
242
243
    /**
244
     * Generate XML for the browser tree.
245
     */
246
    public function doTree()
247
    {
248
        $data = $this->misc->getDatabaseAccessor();
249
250
        $views = $data->getViews();
251
252
        $reqvars = $this->misc->getRequestVars($this->keystring);
253
254
        $attrs = [
255
            'text'       => Decorator::field('relname'),
256
            'icon'       => 'View',
257
            'iconAction' => Decorator::url('display', $reqvars, [$this->keystring => Decorator::field('relname')]),
258
            'toolTip'    => Decorator::field('relcomment'),
259
            'action'     => Decorator::redirecturl('redirect', $reqvars, [$this->keystring => Decorator::field('relname')]),
260
            'branch'     => Decorator::url('views', $reqvars, ['action' => 'subtree', $this->keystring => Decorator::field('relname')]),
261
        ];
262
263
        return $this->printTree($views, $attrs, 'views');
264
    }
265
266
    /**
267
     * Show confirmation of drop and perform actual drop.
268
     *
269
     * @param mixed $confirm
270
     */
271
    public function doDrop($confirm)
272
    {
273
        $data = $this->misc->getDatabaseAccessor();
274
275
        if (empty($_REQUEST['view']) && empty($_REQUEST['ma'])) {
276
            return $this->doDefault($this->lang['strspecifyviewtodrop']);
277
        }
278
279
        if ($confirm) {
280
            $this->printTrail('view');
281
            $this->printTitle($this->lang['strdrop'], 'pg.view.drop');
282
283
            echo '<form action="'.\SUBFOLDER."/src/views/views\" method=\"post\">\n";
284
285
            //If multi drop
286
            if (isset($_REQUEST['ma'])) {
287
                foreach ($_REQUEST['ma'] as $v) {
288
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
289
                    echo '<p>', sprintf($this->lang['strconfdropview'], $this->misc->printVal($a['view'])), "</p>\n";
290
                    echo '<input type="hidden" name="view[]" value="', htmlspecialchars($a['view']), "\" />\n";
291
                }
292
            } else {
293
                echo '<p>', sprintf($this->lang['strconfdropview'], $this->misc->printVal($_REQUEST['view'])), "</p>\n";
294
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n";
295
            }
296
297
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
298
299
            echo $this->misc->form;
300
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>\n";
301
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n";
302
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />\n";
303
            echo "</form>\n";
304
        } else {
305
            if (is_array($_POST['view'])) {
306
                $msg    = '';
307
                $status = $data->beginTransaction();
308
                if (0 == $status) {
309
                    foreach ($_POST['view'] as $s) {
310
                        $status = $data->dropView($s, isset($_POST['cascade']));
311
                        if (0 == $status) {
312
                            $msg .= sprintf('%s: %s<br />', htmlentities($s, ENT_QUOTES, 'UTF-8'), $this->lang['strviewdropped']);
313
                        } else {
314
                            $data->endTransaction();
315
                            $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($s, ENT_QUOTES, 'UTF-8'), $this->lang['strviewdroppedbad']));
316
317
                            return;
318
                        }
319
                    }
320
                }
321
                if (0 == $data->endTransaction()) {
322
                    // Everything went fine, back to the Default page....
323
                    $this->misc->setReloadBrowser(true);
324
                    $this->doDefault($msg);
325
                } else {
326
                    $this->doDefault($this->lang['strviewdroppedbad']);
327
                }
328
            } else {
329
                $status = $data->dropView($_POST['view'], isset($_POST['cascade']));
330
                if (0 == $status) {
331
                    $this->misc->setReloadBrowser(true);
332
                    $this->doDefault($this->lang['strviewdropped']);
333
                } else {
334
                    $this->doDefault($this->lang['strviewdroppedbad']);
335
                }
336
            }
337
        }
338
    }
339
340
    /**
341
     * Sets up choices for table linkage, and which fields to select for the view we're creating.
342
     *
343
     * @param mixed $msg
344
     */
345
    public function doSetParamsCreate($msg = '')
346
    {
347
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
348
349
        // Check that they've chosen tables for the view definition
350
        if (!isset($_POST['formTables'])) {
351
            return $this->doWizardCreate($this->lang['strviewneedsdef']);
352
        }
353
        // Initialise variables
354
        $this->coalesceArr($_REQUEST, 'formView', '');
355
356
        $this->coalesceArr($_REQUEST, 'formComment', '');
357
358
        $this->printTrail('schema');
359
        $this->printTitle($this->lang['strcreateviewwiz'], 'pg.view.create');
360
        $this->printMsg($msg);
361
362
        $this->printParamsCreateForm();
363
    }
364
365
    /**
366
     * Display a wizard where they can enter a new view.
367
     *
368
     * @param mixed $msg
369
     */
370
    public function doWizardCreate($msg = '')
371
    {
372
        $this->printTrail('schema');
373
        $this->printTitle($this->lang['strcreateviewwiz'], 'pg.view.create');
374
        $this->printMsg($msg);
375
376
        $this->printWizardCreateForm();
377
    }
378
379
    /**
380
     * Displays a screen where they can enter a new view.
381
     *
382
     * @param mixed $msg
383
     */
384
    public function doCreate($msg = '')
385
    {
386
        $data = $this->misc->getDatabaseAccessor();
387
388
        $this->coalesceArr($_REQUEST, 'formView', '');
389
390
        if (!isset($_REQUEST['formDefinition'])) {
391
            if (isset($_SESSION['sqlquery'])) {
392
                $_REQUEST['formDefinition'] = $_SESSION['sqlquery'];
393
            } else {
394
                $_REQUEST['formDefinition'] = 'SELECT ';
395
            }
396
        }
397
        $this->coalesceArr($_REQUEST, 'formComment', '');
398
399
        $this->printTrail('schema');
400
        $this->printTitle($this->lang['strcreateview'], 'pg.view.create');
401
        $this->printMsg($msg);
402
403
        echo '<form action="'.\SUBFOLDER."/src/views/{$this->view_name}\" method=\"post\">\n";
404
        echo "<table style=\"width: 100%\">\n";
405
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n";
406
        echo "\t<td class=\"data1\"><input name=\"formView\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
407
        htmlspecialchars($_REQUEST['formView']), "\" /></td>\n\t</tr>\n";
408
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>\n";
409
        echo "\t<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"10\" cols=\"50\" name=\"formDefinition\">",
410
        htmlspecialchars($_REQUEST['formDefinition']), "</textarea></td>\n\t</tr>\n";
411
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
412
        echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
413
        htmlspecialchars($_REQUEST['formComment']), "</textarea></td>\n\t</tr>\n";
414
        echo "</table>\n";
415
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
416
        echo $this->misc->form;
417
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n";
418
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
419
        echo "</form>\n";
420
    }
421
422
    /**
423
     * Actually creates the new view in the database.
424
     */
425
    public function doSaveCreate()
426
    {
427
        $data = $this->misc->getDatabaseAccessor();
428
429
        // Check that they've given a name and a definition
430
        if ('' == $_POST['formView']) {
431
            $this->doCreate($this->lang['strviewneedsname']);
432
        } elseif ('' == $_POST['formDefinition']) {
433
            $this->doCreate($this->lang['strviewneedsdef']);
434
        } else {
435
            $status = $data->createView($_POST['formView'], $_POST['formDefinition'], false, $_POST['formComment']);
436
            if (0 == $status) {
437
                $this->misc->setReloadBrowser(true);
438
                $this->doDefault($this->lang['strviewcreated']);
439
            } else {
440
                $this->doCreate($this->lang['strviewcreatedbad']);
441
            }
442
        }
443
    }
444
}
445