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

MaterializedviewsController::doDrop()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 6.0561
c 0
b 0
f 0
cc 12
eloc 46
nc 10
nop 1

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 MaterializedviewsController extends BaseController
17
{
18
    use \PHPPgAdmin\Traits\ViewsMatviewsTrait;
19
20
    public $table_place      = 'matviews-matviews';
21
    public $controller_title = 'strviews';
22
23
    // this member variable is view for views and matview for materialized views
24
    public $keystring = 'matview';
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('M '.$this->lang['strviews']);
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(true);
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
        $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', 'matviews');
118
        $this->printMsg($msg);
119
120
        $matviews = $data->getMaterializedViews();
121
122
        $columns = [
123
            $this->keystring => [
124
                'title' => 'M '.$this->lang['strview'],
125
                'field' => Decorator::field('relname'),
126
                'url'   => \SUBFOLDER."/redirect/matview?{$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'     => 'materializedviews',
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'     => 'materializedviews',
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 matview has been created.
175
            //            'insert' => array(
176
            //                'title'    => $this->lang['strinsert'],
177
            //                'url'    => "materializedviews?action=confinsertrow&amp;{$this->misc->href}&amp;",
178
            //                'vars'    => array('matview' => 'relname'),
179
            //            ),
180
181
            'alter'        => [
182
                'content' => $this->lang['stralter'],
183
                'attr'    => [
184
                    'href' => [
185
                        'url'     => 'materializedviewproperties',
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'     => 'materializedviews',
199
                        'urlvars' => [
200
                            'action'         => 'confirm_drop',
201
                            $this->keystring => Decorator::field('relname'),
202
                        ],
203
                    ],
204
                ],
205
            ],
206
        ];
207
208
        echo $this->printTable($matviews, $columns, $actions, $this->table_place, $this->lang['strnoviews']);
209
210
        $navlinks = [
211
            'create'    => [
212
                'attr'    => [
213
                    'href' => [
214
                        'url'     => 'materializedviews',
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'     => 'materializedviews',
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['strcreatematviewwiz'],
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
        $matviews = $data->getMaterializedViews();
251
252
        $reqvars = $this->misc->getRequestVars($this->keystring);
253
254
        $attrs = [
255
            'text'       => Decorator::field('relname'),
256
            'icon'       => 'MViews',
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('materializedviews', $reqvars, ['action' => 'subtree', $this->keystring => Decorator::field('relname')]),
261
        ];
262
263
        return $this->printTree($matviews, $attrs, 'matviews');
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['matview']) && empty($_REQUEST['ma'])) {
276
            return $this->doDefault($this->lang['strspecifyviewtodrop']);
277
        }
278
279
        if ($confirm) {
280
            $this->printTrail('getTrail');
281
            $this->printTitle($this->lang['strdrop'], 'pg.matview.drop');
282
283
            echo '<form action="'.\SUBFOLDER."/src/views/materializedviews\" 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['matview'])), "</p>\n";
294
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['matview']), "\" />\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
        // Check that they've chosen tables for the view definition
348
        if (!isset($_POST['formTables'])) {
349
            return $this->doWizardCreate($this->lang['strviewneedsdef']);
350
        }
351
        // Initialise variables
352
        $this->coalesceArr($_REQUEST, 'formView', '');
353
354
        $this->coalesceArr($_REQUEST, 'formComment', '');
355
356
        $this->printTrail('schema');
357
        $this->printTitle($this->lang['strcreatematviewwiz'], 'pg.matview.create');
358
        $this->printMsg($msg);
359
360
        $this->printParamsCreateForm();
361
    }
362
363
    /**
364
     * Display a wizard where they can enter a new view.
365
     *
366
     * @param mixed $msg
367
     */
368
    public function doWizardCreate($msg = '')
369
    {
370
        $this->printTrail('schema');
371
        $this->printTitle($this->lang['strcreatematviewwiz'], 'pg.matview.create');
372
        $this->printMsg($msg);
373
374
        $this->printWizardCreateForm();
375
    }
376
377
    /**
378
     * Displays a screen where they can enter a new view.
379
     *
380
     * @param mixed $msg
381
     */
382
    public function doCreate($msg = '')
383
    {
384
        $data = $this->misc->getDatabaseAccessor();
385
386
        $this->coalesceArr($_REQUEST, 'formView', '');
387
388
        if (!isset($_REQUEST['formDefinition'])) {
389
            if (isset($_SESSION['sqlquery'])) {
390
                $_REQUEST['formDefinition'] = $_SESSION['sqlquery'];
391
            } else {
392
                $_REQUEST['formDefinition'] = 'SELECT ';
393
            }
394
        }
395
        $this->coalesceArr($_REQUEST, 'formComment', '');
396
397
        $this->printTrail('schema');
398
        $this->printTitle($this->lang['strcreateview'], 'pg.matview.create');
399
        $this->printMsg($msg);
400
401
        echo '<form action="'.\SUBFOLDER."/src/views/{$this->view_name}\" method=\"post\">\n";
402
        echo "<table style=\"width: 100%\">\n";
403
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n";
404
        echo "\t<td class=\"data1\"><input name=\"formView\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
405
        htmlspecialchars($_REQUEST['formView']), "\" /></td>\n\t</tr>\n";
406
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>\n";
407
        echo "\t<td class=\"data1\"><textarea style=\"width:100%;\" rows=\"10\" cols=\"50\" name=\"formDefinition\">",
408
        htmlspecialchars($_REQUEST['formDefinition']), "</textarea></td>\n\t</tr>\n";
409
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
410
        echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
411
        htmlspecialchars($_REQUEST['formComment']), "</textarea></td>\n\t</tr>\n";
412
        echo "</table>\n";
413
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
414
        echo $this->misc->form;
415
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n";
416
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
417
        echo "</form>\n";
418
    }
419
420
    /**
421
     * Actually creates the new view in the database.
422
     */
423
    public function doSaveCreate()
424
    {
425
        $data = $this->misc->getDatabaseAccessor();
426
427
        // Check that they've given a name and a definition
428
        if ('' == $_POST['formView']) {
429
            $this->doCreate($this->lang['strviewneedsname']);
430
        } elseif ('' == $_POST['formDefinition']) {
431
            $this->doCreate($this->lang['strviewneedsdef']);
432
        } else {
433
            $status = $data->createView($_POST['formView'], $_POST['formDefinition'], false, $_POST['formComment'], true);
434
            if (0 == $status) {
435
                $this->misc->setReloadBrowser(true);
436
                $this->doDefault($this->lang['strviewcreated']);
437
            } else {
438
                $this->doCreate($this->lang['strviewcreatedbad']);
439
            }
440
        }
441
    }
442
}
443