Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

IndexesController::doDefault()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 122
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 84
nc 2
nop 1
dl 0
loc 122
rs 8.1935
c 0
b 0
f 0

How to fix   Long Method   

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
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class IndexesController extends BaseController
15
{
16
    public $controller_name = 'IndexesController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $lang = $this->lang;
24
25
        $action = $this->action;
26
        if ('tree' == $action) {
27
            return $this->doTree();
28
        }
29
30
        $this->printHeader($lang['strindexes'], '<script src="' . \SUBFOLDER . '/js/indexes.js" type="text/javascript"></script>');
31
32
        if ('create_index' == $action || 'save_create_index' == $action) {
33
            echo '<body onload="init();">';
34
        } else {
35
            $this->printBody();
36
        }
37
38
        switch ($action) {
39
            case 'cluster_index':
40
                if (isset($_POST['cluster'])) {
41
                    $this->doClusterIndex(false);
42
                } else {
43
                    $this->doDefault();
44
                }
45
46
                break;
47
            case 'confirm_cluster_index':
48
                $this->doClusterIndex(true);
49
50
                break;
51
            case 'reindex':
52
                $this->doReindex();
53
54
                break;
55
            case 'save_create_index':
56
                if (isset($_POST['cancel'])) {
57
                    $this->doDefault();
58
                } else {
59
                    $this->doSaveCreateIndex();
60
                }
61
62
                break;
63
            case 'create_index':
64
                $this->doCreateIndex();
65
66
                break;
67
            case 'drop_index':
68
                if (isset($_POST['drop'])) {
69
                    $this->doDropIndex(false);
70
                } else {
71
                    $this->doDefault();
72
                }
73
74
                break;
75
            case 'confirm_drop_index':
76
                $this->doDropIndex(true);
77
78
                break;
79
            default:
80
                $this->doDefault();
81
82
                break;
83
        }
84
85
        return $this->printFooter();
86
    }
87
88
    public function doDefault($msg = '')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
89
    {
90
        $lang = $this->lang;
91
        $data = $this->misc->getDatabaseAccessor();
92
93
        $indPre = function (&$rowdata, $actions) use ($data, $lang) {
94
            if ($data->phpBool($rowdata->fields['indisprimary'])) {
95
                $rowdata->fields['+constraints'] = $lang['strprimarykey'];
96
                $actions['drop']['disable']      = true;
97
            } elseif ($data->phpBool($rowdata->fields['indisunique'])) {
98
                $rowdata->fields['+constraints'] = $lang['struniquekey'];
99
                $actions['drop']['disable']      = true;
100
            } else {
101
                $rowdata->fields['+constraints'] = '';
102
            }
103
104
            return $actions;
105
        };
106
        if (!isset($_REQUEST['subject'])) {
107
            $_REQUEST['subject'] = 'table';
108
        }
109
110
        $subject = urlencode($_REQUEST['subject']);
111
        $object  = urlencode($_REQUEST[$_REQUEST['subject']]);
112
113
        $this->printTrail($subject);
114
        $this->printTabs($subject, 'indexes');
115
        $this->printMsg($msg);
116
117
        $indexes = $data->getIndexes($_REQUEST[$_REQUEST['subject']]);
118
119
        $columns = [
120
            'index'       => [
121
                'title' => $lang['strname'],
122
                'field' => Decorator::field('indname'),
123
            ],
124
            'definition'  => [
125
                'title' => $lang['strdefinition'],
126
                'field' => Decorator::field('inddef'),
127
            ],
128
            'constraints' => [
129
                'title'  => $lang['strconstraints'],
130
                'field'  => Decorator::field('+constraints'),
131
                'type'   => 'verbatim',
132
                'params' => ['align' => 'center'],
133
            ],
134
            'clustered'   => [
135
                'title' => $lang['strclustered'],
136
                'field' => Decorator::field('indisclustered'),
137
                'type'  => 'yesno',
138
            ],
139
            'actions'     => [
140
                'title' => $lang['stractions'],
141
            ],
142
            'comment'     => [
143
                'title' => $lang['strcomment'],
144
                'field' => Decorator::field('idxcomment'),
145
            ],
146
        ];
147
148
        $actions = [
149
            'cluster' => [
150
                'content' => $lang['strclusterindex'],
151
                'attr'    => [
152
                    'href' => [
153
                        'url'     => 'indexes.php',
154
                        'urlvars' => [
155
                            'action' => 'confirm_cluster_index',
156
                            $subject => $object,
157
                            'index'  => Decorator::field('indname'),
158
                        ],
159
                    ],
160
                ],
161
            ],
162
            'reindex' => [
163
                'content' => $lang['strreindex'],
164
                'attr'    => [
165
                    'href' => [
166
                        'url'     => 'indexes.php',
167
                        'urlvars' => [
168
                            'action' => 'reindex',
169
                            $subject => $object,
170
                            'index'  => Decorator::field('indname'),
171
                        ],
172
                    ],
173
                ],
174
            ],
175
            'drop'    => [
176
                'content' => $lang['strdrop'],
177
                'attr'    => [
178
                    'href' => [
179
                        'url'     => 'indexes.php',
180
                        'urlvars' => [
181
                            'action' => 'confirm_drop_index',
182
                            $subject => $object,
183
                            'index'  => Decorator::field('indname'),
184
                        ],
185
                    ],
186
                ],
187
            ],
188
        ];
189
190
        echo $this->printTable($indexes, $columns, $actions, 'indexes-indexes', $lang['strnoindexes'], $indPre);
191
192
        $this->printNavLinks([
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
193
            'create' => [
194
                'attr'    => [
195
                    'href' => [
196
                        'url'     => 'indexes.php',
197
                        'urlvars' => [
198
                            'action'   => 'create_index',
199
                            'server'   => $_REQUEST['server'],
200
                            'database' => $_REQUEST['database'],
201
                            'schema'   => $_REQUEST['schema'],
202
                            $subject   => $object,
203
                            'subject'  => $subject,
204
                        ],
205
                    ],
206
                ],
207
                'content' => $lang['strcreateindex'],
208
            ],
209
        ], 'indexes-indexes', get_defined_vars());
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
210
    }
211
212
    public function doTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
213
    {
214
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
215
        $data = $this->misc->getDatabaseAccessor();
216
        if (!isset($_REQUEST['subject'])) {
217
            $_REQUEST['subject'] = 'table';
218
        }
219
220
        $subject = urlencode($_REQUEST['subject']);
221
        $object  = urlencode($_REQUEST[$_REQUEST['subject']]);
222
223
        $indexes = $data->getIndexes($object);
224
225
        $reqvars = $this->misc->getRequestVars($subject);
0 ignored issues
show
Unused Code introduced by
The assignment to $reqvars is dead and can be removed.
Loading history...
226
227
        $getIcon = function ($f) {
228
            if ('t' == $f['indisprimary']) {
229
                return 'PrimaryKey';
230
            }
231
232
            if ('t' == $f['indisunique']) {
233
                return 'UniqueConstraint';
234
            }
235
236
            return 'Index';
237
        };
238
239
        $attrs = [
240
            'text' => Decorator::field('indname'),
241
            'icon' => Decorator::callback($getIcon),
242
        ];
243
244
        return $this->printTree($indexes, $attrs, 'indexes');
245
    }
246
247
    /**
248
     * Show confirmation of cluster index and perform actual cluster.
249
     *
250
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
251
     */
252
    public function doClusterIndex($confirm)
253
    {
254
        $lang = $this->lang;
255
        $data = $this->misc->getDatabaseAccessor();
256
257
        if ($confirm) {
258
            // Default analyze to on
259
            $_REQUEST['analyze'] = true;
260
261
            $this->printTrail('index');
262
            $this->printTitle($lang['strclusterindex'], 'pg.index.cluster');
263
264
            echo '<p>', sprintf($lang['strconfcluster'], $this->misc->printVal($_REQUEST['index'])), '</p>' . "\n";
265
266
            echo '<form action="' . \SUBFOLDER . '/src/views/indexes.php" method="post">' . "\n";
267
            echo '<p><input type="checkbox" id="analyze" name="analyze"', (isset($_REQUEST['analyze']) ? ' checked="checked"' : ''), ' />';
268
            echo "<label for=\"analyze\">{$lang['stranalyze']}</label></p>" . "\n";
269
            echo '<input type="hidden" name="action" value="cluster_index" />' . "\n";
270
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), '" />' . "\n";
271
            echo '<input type="hidden" name="index" value="', htmlspecialchars($_REQUEST['index']), '" />' . "\n";
272
            echo $this->misc->form;
273
            echo "<input type=\"submit\" name=\"cluster\" value=\"{$lang['strclusterindex']}\" />" . "\n";
274
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />" . "\n";
275
            echo '</form>' . "\n";
276
        } else {
277
            $status = $data->clusterIndex($_POST['table'], $_POST['index']);
278
            if (0 == $status) {
279
                if (isset($_POST['analyze'])) {
280
                    $status = $data->analyzeDB($_POST['table']);
281
                    if (0 == $status) {
282
                        $this->doDefault($lang['strclusteredgood'] . ' ' . $lang['stranalyzegood']);
283
                    } else {
284
                        $this->doDefault($lang['stranalyzebad']);
285
                    }
286
                } else {
287
                    $this->doDefault($lang['strclusteredgood']);
288
                }
289
            } else {
290
                $this->doDefault($lang['strclusteredbad']);
291
            }
292
        }
293
    }
294
295
    public function doReindex()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
296
    {
297
        $lang   = $this->lang;
298
        $data   = $this->misc->getDatabaseAccessor();
299
        $status = $data->reindex('INDEX', $_REQUEST['index']);
300
        if (0 == $status) {
301
            $this->doDefault($lang['strreindexgood']);
302
        } else {
303
            $this->doDefault($lang['strreindexbad']);
304
        }
305
    }
306
307
    /**
308
     * Displays a screen where they can enter a new index.
309
     *
310
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
311
     */
312
    public function doCreateIndex($msg = '')
313
    {
314
        $lang = $this->lang;
315
        $data = $this->misc->getDatabaseAccessor();
316
317
        if (!isset($_REQUEST['subject'])) {
318
            $_REQUEST['subject'] = 'table';
319
        }
320
        $subject = urlencode($_REQUEST['subject']);
321
        $object  = urlencode($_REQUEST[$subject]);
322
323
        if (!isset($_POST['formIndexName'])) {
324
            $_POST['formIndexName'] = '';
325
        }
326
327
        if (!isset($_POST['formIndexType'])) {
328
            $_POST['formIndexType'] = null;
329
        }
330
331
        if (!isset($_POST['formCols'])) {
332
            $_POST['formCols'] = '';
333
        }
334
335
        if (!isset($_POST['formWhere'])) {
336
            $_POST['formWhere'] = '';
337
        }
338
339
        if (!isset($_POST['formSpc'])) {
340
            $_POST['formSpc'] = '';
341
        }
342
343
        $attrs = $data->getTableAttributes($object);
344
        // Fetch all tablespaces from the database
345
        if ($data->hasTablespaces()) {
346
            $tablespaces = $data->getTablespaces();
347
        }
348
349
        $this->printTrail($subject);
350
        $this->printTitle($lang['strcreateindex'], 'pg.index.create');
351
        $this->printMsg($msg);
352
353
        $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10);
354
        $selColumns->set_style('width: 10em;');
355
356
        if ($attrs->recordCount() > 0) {
357
            while (!$attrs->EOF) {
358
                $attname = new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname']);
359
                $selColumns->add($attname);
360
                $attrs->moveNext();
361
            }
362
        }
363
364
        $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10);
365
        $selIndex->set_style('width: 10em;');
366
        $selIndex->set_attribute('id', 'IndexColumnList');
367
        $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>');
368
        $buttonAdd->set_attribute('onclick', 'buttonPressed(this);');
369
        $buttonAdd->set_attribute('type', 'button');
370
371
        $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<');
372
        $buttonRemove->set_attribute('onclick', 'buttonPressed(this);');
373
        $buttonRemove->set_attribute('type', 'button');
374
375
        echo '<form onsubmit="doSelectAll();" name="formIndex" action="indexes.php" method="post">' . "\n";
376
377
        echo '<table>' . "\n";
378
        echo '<tr><th class="data required" colspan="3">' . $lang['strindexname'] . '</th></tr>';
379
        echo '<tr>';
380
        echo '<td class="data1" colspan="3">';
381
        echo '<input type="text" name="formIndexName" size="32" maxlength="' . $data->_maxNameLen . '" value="' . htmlspecialchars($_POST['formIndexName']) . '" />';
382
        echo '</td></tr>';
383
        echo '<tr><th class="data">' . $lang['strtablecolumnlist'] . '</th><th class="data">&nbsp;</th>';
384
        echo '<th class="data required">' . $lang['strindexcolumnlist'] . '</th></tr>' . "\n";
385
        echo '<tr><td class="data1">' . $selColumns->fetch() . '</td>' . "\n";
386
        echo '<td class="data1">' . $buttonRemove->fetch() . $buttonAdd->fetch() . '</td>';
387
        echo '<td class="data1">' . $selIndex->fetch() . '</td></tr>' . "\n";
388
        echo '</table>' . "\n";
389
390
        echo '<table> ' . "\n";
391
        echo '<tr>';
392
        echo '<th class="data left required" scope="row">' . $lang['strindextype'] . '</th>';
393
        echo '<td class="data1"><select name="formIndexType">';
394
        foreach ($data->typIndexes as $v) {
395
            echo '<option value="', htmlspecialchars($v), '"',
396
            ($v == $_POST['formIndexType']) ? ' selected="selected"' : '', '>', htmlspecialchars($v), '</option>' . "\n";
397
        }
398
        echo '</select></td></tr>' . "\n";
399
        echo '<tr>';
400
        echo "<th class=\"data left\" scope=\"row\"><label for=\"formUnique\">{$lang['strunique']}</label></th>";
401
        echo '<td class="data1"><input type="checkbox" id="formUnique" name="formUnique"', (isset($_POST['formUnique']) ? 'checked="checked"' : ''), ' /></td>';
402
        echo '</tr>';
403
        echo '<tr>';
404
        echo "<th class=\"data left\" scope=\"row\">{$lang['strwhere']}</th>";
405
        echo '<td class="data1">(<input name="formWhere" size="32" maxlength="' . $data->_maxNameLen . '" value="' . htmlspecialchars($_POST['formWhere']) . '" />)</td>';
406
        echo '</tr>';
407
408
        // Tablespace (if there are any)
409
        if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tablespaces does not seem to be defined for all execution paths leading up to this point.
Loading history...
410
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>" . "\n";
411
            echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">" . "\n";
412
            // Always offer the default (empty) option
413
            echo "\t\t\t\t<option value=\"\"",
414
            ('' == $_POST['formSpc']) ? ' selected="selected"' : '', '></option>' . "\n";
415
            // Display all other tablespaces
416
            while (!$tablespaces->EOF) {
417
                $spcname = htmlspecialchars($tablespaces->fields['spcname']);
418
                echo "\t\t\t\t<option value=\"{$spcname}\"",
419
                ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>" . "\n";
420
                $tablespaces->moveNext();
421
            }
422
            echo "\t\t\t</select>\n\t\t</td>\n\t</tr>" . "\n";
423
        }
424
425
        if ($data->hasConcurrentIndexBuild()) {
426
            echo '<tr>';
427
            echo "<th class=\"data left\" scope=\"row\"><label for=\"formConcur\">{$lang['strconcurrently']}</label></th>";
428
            echo '<td class="data1"><input type="checkbox" id="formConcur" name="formConcur"', (isset($_POST['formConcur']) ? 'checked="checked"' : ''), ' /></td>';
429
            echo '</tr>';
430
        }
431
432
        echo '</table>';
433
434
        echo '<p><input type="hidden" name="action" value="save_create_index" />' . "\n";
435
        echo $this->misc->form;
436
        echo '<input type="hidden" name="subject" value="', htmlspecialchars($subject), '" />' . "\n";
437
        echo '<input type="hidden" name="' . $subject . '" value="', htmlspecialchars($object), '" />' . "\n";
438
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />" . "\n";
439
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>" . "\n";
440
        echo '</form>' . "\n";
441
    }
442
443
    /**
444
     * Actually creates the new index in the database.
445
     *
446
     * @@ Note: this function can't handle columns with commas in them
447
     */
448
    public function doSaveCreateIndex()
449
    {
450
        $lang = $this->lang;
451
        $data = $this->misc->getDatabaseAccessor();
452
453
        if (!isset($_POST['subject'])) {
454
            $_POST['subject'] = 'table';
455
        }
456
        $subject = urlencode($_POST['subject']);
457
        $object  = urlencode($_POST[$subject]);
458
459
        // Handle databases that don't have partial indexes
460
        if (!isset($_POST['formWhere'])) {
461
            $_POST['formWhere'] = '';
462
        }
463
464
        // Default tablespace to null if it isn't set
465
        if (!isset($_POST['formSpc'])) {
466
            $_POST['formSpc'] = null;
467
        }
468
469
        // Check that they've given a name and at least one column
470
        if ('' == $_POST['formIndexName']) {
471
            $this->doCreateIndex($lang['strindexneedsname']);
472
        } elseif (!isset($_POST['IndexColumnList']) || '' == $_POST['IndexColumnList']) {
473
            $this->doCreateIndex($lang['strindexneedscols']);
474
        } else {
475
            $status = $data->createIndex(
476
                $_POST['formIndexName'],
477
                $object,
478
                $_POST['IndexColumnList'],
479
                $_POST['formIndexType'],
480
                isset($_POST['formUnique']),
481
                $_POST['formWhere'],
482
                $_POST['formSpc'],
483
                isset($_POST['formConcur'])
484
            );
485
            if (0 == $status) {
486
                $this->doDefault($lang['strindexcreated']);
487
            } else {
488
                $this->doCreateIndex($lang['strindexcreatedbad']);
489
            }
490
        }
491
    }
492
493
    /**
494
     * Show confirmation of drop index and perform actual drop.
495
     *
496
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
497
     */
498
    public function doDropIndex($confirm)
499
    {
500
        $lang = $this->lang;
501
        $data = $this->misc->getDatabaseAccessor();
502
503
        if (!isset($_REQUEST['subject'])) {
504
            $_REQUEST['subject'] = 'table';
505
        }
506
        $subject = urlencode($_REQUEST['subject']);
0 ignored issues
show
Unused Code introduced by
The assignment to $subject is dead and can be removed.
Loading history...
507
        $object  = urlencode($_REQUEST[$_REQUEST['subject']]);
508
509
        if ($confirm) {
510
            $this->printTrail('index');
511
            $this->printTitle($lang['strdrop'], 'pg.index.drop');
512
513
            echo '<p>', sprintf($lang['strconfdropindex'], $this->misc->printVal($_REQUEST['index'])), '</p>' . "\n";
514
515
            echo '<form action="' . \SUBFOLDER . '/src/views/indexes.php" method="post">' . "\n";
516
            echo '<input type="hidden" name="action" value="drop_index" />' . "\n";
517
            echo '<input type="hidden" name="table" value="', htmlspecialchars($object), '" />' . "\n";
518
            echo '<input type="hidden" name="index" value="', htmlspecialchars($_REQUEST['index']), '" />' . "\n";
519
            echo $this->misc->form;
520
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>" . "\n";
521
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />" . "\n";
522
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />" . "\n";
523
            echo '</form>' . "\n";
524
        } else {
525
            $status = $data->dropIndex($_POST['index'], isset($_POST['cascade']));
526
            if (0 == $status) {
527
                $this->doDefault($lang['strindexdropped']);
528
            } else {
529
                $this->doDefault($lang['strindexdroppedbad']);
530
            }
531
        }
532
    }
533
}
534