TablesController   F
last analyzed

Complexity

Total Complexity 149

Size/Duplication

Total Lines 1354
Duplicated Lines 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 848
c 7
b 1
f 0
dl 0
loc 1354
rs 1.752
wmc 149

14 Methods

Rating   Name   Duplication   Size   Complexity  
D render() 0 113 22
A doSubTree() 0 30 1
A doTree() 0 18 1
C doDrop() 0 97 12
A doInsertRow() 0 29 4
A doDefault() 0 60 3
C doCreateLike() 0 116 15
A _getColumns() 0 32 1
B doEmpty() 0 82 10
A displayJson() 0 13 1
C doSelectRows() 0 124 14
F doCreate() 0 285 47
B _getActions() 0 118 1
D formInsertRow() 0 143 17

How to fix   Complexity   

Complex Class

Complex classes like TablesController 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 TablesController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class TablesController extends BaseController
15
{
16
    use \PHPPgAdmin\Traits\AdminTrait;
0 ignored issues
show
Bug introduced by
The trait PHPPgAdmin\Traits\AdminTrait requires the property $subFolder which is not provided by PHPPgAdmin\Controller\TablesController.
Loading history...
17
    use \PHPPgAdmin\Traits\InsertEditRowTrait;
18
19
    public $table_place = 'tables-tables';
20
21
    public $controller_title = 'strtables';
22
23
    /**
24
     * Default method to render the controller according to the action parameter.
25
     */
26
    public function render()
27
    {
28
        if ('tree' === $this->action) {
29
            return $this->doTree();
30
        }
31
32
        if ('subtree' === $this->action) {
33
            return $this->doSubTree();
34
        }
35
36
        if ('json' === $this->action) {
37
            return $this->displayJson();
38
        }
39
40
        $header_template = 'header.twig';
41
42
        \ob_start();
43
44
        switch ($this->action) {
45
            case 'create':
46
                if (null !== $this->getPostParam('cancel')) {
47
                    $this->doDefault();
48
                } else {
49
                    $header_template = 'header_select2.twig';
50
                    $this->doCreate();
51
                }
52
53
                break;
54
            case 'createlike':
55
                $header_template = 'header_select2.twig';
56
                $this->doCreateLike(false);
57
58
                break;
59
            case 'confcreatelike':
60
                if (null !== $this->getPostParam('cancel')) {
61
                    $header_template = 'header_datatables.twig';
62
                    $this->doDefault();
63
                } else {
64
                    //$header_template = 'header_select2.twig';
65
                    $this->doCreateLike(true);
66
                }
67
68
                break;
69
            case 'selectrows':
70
                if (!isset($_POST['cancel'])) {
71
                    $this->doSelectRows(false);
72
                } else {
73
                    $header_template = 'header_datatables.twig';
74
                    $this->doDefault();
75
                }
76
77
                break;
78
            case 'confselectrows':
79
                $this->doSelectRows(true);
80
81
                break;
82
            case 'insertrow':
83
                if (!isset($_POST['cancel'])) {
84
                    $this->doInsertRow();
85
                } else {
86
                    $header_template = 'header_datatables.twig';
87
                    $this->doDefault();
88
                }
89
90
                break;
91
            case 'confinsertrow':
92
                $this->formInsertRow();
93
94
                break;
95
            case 'empty':
96
                if (isset($_POST['empty'])) {
97
                    $this->doEmpty(false);
98
                } else {
99
                    $header_template = 'header_datatables.twig';
100
                    $this->doDefault();
101
                }
102
103
                break;
104
            case 'confirm_empty':
105
                $this->doEmpty(true);
106
107
                break;
108
            case 'drop':
109
                if (null !== $this->getPostParam('drop')) {
110
                    $this->doDrop(false);
111
                } else {
112
                    $header_template = 'header_datatables.twig';
113
                    $this->doDefault();
114
                }
115
116
                break;
117
            case 'confirm_drop':
118
                $this->doDrop(true);
119
120
                break;
121
122
            default:
123
                if (false === $this->adminActions($this->action, 'table')) {
124
                    $header_template = 'header_datatables.twig';
125
                    $this->doDefault();
126
                }
127
128
                break;
129
        }
130
131
        $output = \ob_get_clean();
132
133
        $this->printHeader($this->headerTitle(), null, true, $header_template);
134
        $this->printBody();
135
136
        echo $output;
137
138
        return $this->printFooter();
139
    }
140
141
    /**
142
     * Show default list of tables in the database.
143
     *
144
     * @param mixed $msg
145
     */
146
    public function doDefault($msg = ''): void
147
    {
148
        $data = $this->misc->getDatabaseAccessor();
149
150
        $this->printTrail('schema');
151
        $this->printTabs('schema', 'tables');
152
        $this->printMsg($msg);
153
154
        $tables = $data->getTables();
155
156
        $columns = $this->_getColumns();
157
158
        $actions = $this->_getActions();
159
160
        echo $this->printTable($tables, $columns, $actions, $this->table_place, $this->lang['strnotables']);
161
        $attr = [
162
            'href' => [
163
                'url' => 'tables',
164
                'urlvars' => [
165
                    'action' => 'createlike',
166
                    'server' => $this->getRequestParam('server'),
167
                    'database' => $this->getRequestParam('database'),
168
                    'schema' => $this->getRequestParam('schema'),
169
                ],
170
            ],
171
        ];
172
        $navlinks = [
173
            'create' => [
174
                'attr' => [
175
                    'href' => [
176
                        'url' => 'tables',
177
                        'urlvars' => [
178
                            'action' => 'create',
179
                            'server' => $this->getRequestParam('server'),
180
                            'database' => $this->getRequestParam('database'),
181
                            'schema' => $this->getRequestParam('schema'),
182
                        ],
183
                    ],
184
                ],
185
                'content' => $this->lang['strcreatetable'],
186
            ],
187
        ];
188
189
        if ((0 < $tables->recordCount()) && $data->hasCreateTableLike()) {
190
            $navlinks['createlike'] = [
191
                'attr' => [
192
                    'href' => [
193
                        'url' => 'tables',
194
                        'urlvars' => [
195
                            'action' => 'createlike',
196
                            'server' => $this->getRequestParam('server'),
197
                            'database' => $this->getRequestParam('database'),
198
                            'schema' => $this->getRequestParam('schema'),
199
                        ],
200
                    ],
201
                ],
202
                'content' => $this->lang['strcreatetablelike'],
203
            ];
204
        }
205
        $this->printNavLinks($navlinks, 'tables-tables', \get_defined_vars());
206
    }
207
208
    public function displayJson()
209
    {
210
        $data = $this->misc->getDatabaseAccessor();
211
212
        $tables = $data->getTables();
213
214
        $all_tables = $tables->getAll();
215
216
        return $this
217
            ->container
218
            ->response
219
            ->withStatus(200)
220
            ->withJson($all_tables);
221
    }
222
223
    /**
224
     * Generate XML for the browser tree.
225
     */
226
    public function doTree()
227
    {
228
        $data = $this->misc->getDatabaseAccessor();
229
230
        $tables = $data->getTables();
231
232
        $reqvars = $this->misc->getRequestVars('table');
233
234
        $attrs = [
235
            'text' => Decorator::field('relname'),
236
            'icon' => 'Table',
237
            'iconAction' => Decorator::url('display', $reqvars, ['table' => Decorator::field('relname')]),
238
            'toolTip' => Decorator::field('relcomment'),
239
            'action' => Decorator::redirecturl('redirect', $reqvars, ['table' => Decorator::field('relname')]),
240
            'branch' => Decorator::url('tables', $reqvars, ['action' => 'subtree', 'table' => Decorator::field('relname')]),
241
        ];
242
243
        return $this->printTree($tables, $attrs, 'tables');
244
    }
245
246
    public function doSubTree()
247
    {
248
        $tabs = $this->misc->getNavTabs('table');
249
        $items = $this->adjustTabsForTree($tabs);
250
        $reqvars = $this->misc->getRequestVars('table');
251
252
        $attrs = [
253
            'text' => Decorator::field('title'),
254
            'icon' => Decorator::field('icon'),
255
            'action' => Decorator::actionurl(
256
                Decorator::field('url'),
257
                $reqvars,
258
                Decorator::field('urlvars'),
259
                ['table' => $_REQUEST['table']]
260
            ),
261
            'branch' => Decorator::ifempty(
262
                Decorator::field('branch'),
263
                '',
264
                Decorator::url(
265
                    Decorator::field('url'),
266
                    $reqvars,
267
                    [
268
                        'action' => 'tree',
269
                        'table' => $_REQUEST['table'],
270
                    ]
271
                )
272
            ),
273
        ];
274
275
        return $this->printTree($items, $attrs, 'table');
276
    }
277
278
    /**
279
     * Displays a screen where they can enter a new table.
280
     *
281
     * @param mixed $msg
282
     */
283
    public function doCreate($msg = ''): void
284
    {
285
        $data = $this->misc->getDatabaseAccessor();
286
287
        if (!isset($_REQUEST['stage'])) {
288
            $_REQUEST['stage'] = 1;
289
            $default_with_oids = $data->getDefaultWithOid();
290
291
            if ('off' === $default_with_oids) {
292
                $_REQUEST['withoutoids'] = 'on';
293
            }
294
        }
295
296
        $this->coalesceArr($_REQUEST, 'name', '');
297
298
        $this->coalesceArr($_REQUEST, 'fields', '');
299
300
        $this->coalesceArr($_REQUEST, 'tblcomment', '');
301
302
        $this->coalesceArr($_REQUEST, 'spcname', '');
303
        $tablespaces = null;
304
305
        switch ($_REQUEST['stage']) {
306
            case 1:
307
                // You are presented with a form in which you describe the table, pick the tablespace and state how many fields it will have
308
                // Fetch all tablespaces from the database
309
                if ($data->hasTablespaces()) {
310
                    $tablespaces = $data->getTablespaces();
311
                }
312
313
                $this->printTrail('schema');
314
                $this->printTitle($this->lang['strcreatetable'], 'pg.table.create');
315
                $this->printMsg($msg);
316
317
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/' . $this->script . '" method="post">';
318
                echo \PHP_EOL;
319
                echo '<table>' . \PHP_EOL;
320
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
321
                echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
322
                    \htmlspecialchars($_REQUEST['name']),
323
                    "\" /></td>\n\t</tr>" . \PHP_EOL;
324
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strnumcols']}</th>" . \PHP_EOL;
325
                echo "\t\t<td class=\"data\"><input name=\"fields\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"",
326
                    \htmlspecialchars($_REQUEST['fields']),
327
                    "\" /></td>\n\t</tr>" . \PHP_EOL;
328
                echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stroptions']}</th>" . \PHP_EOL;
329
                echo "\t\t<td class=\"data\"><label for=\"withoutoids\"><input type=\"checkbox\" id=\"withoutoids\" name=\"withoutoids\"", isset($_REQUEST['withoutoids']) ? ' checked="checked"' : '', " />WITHOUT OIDS</label></td>\n\t</tr>" . \PHP_EOL;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 190 characters; contains 251 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
330
331
                // Tablespace (if there are any)
332
                if ($data->hasTablespaces() && 0 < $tablespaces->recordCount()) {
333
                    echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strtablespace']}</th>" . \PHP_EOL;
334
                    echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"spcname\">" . \PHP_EOL;
335
                    // Always offer the default (empty) option
336
                    echo "\t\t\t\t<option value=\"\"",
337
                        ('' === $_REQUEST['spcname']) ? ' selected="selected"' : '',
338
                        '></option>' . \PHP_EOL;
339
                    // Display all other tablespaces
340
                    while (!$tablespaces->EOF) {
341
                        $spcname = \htmlspecialchars($tablespaces->fields['spcname']);
342
                        echo "\t\t\t\t<option value=\"{$spcname}\"",
343
                            ($tablespaces->fields['spcname'] === $_REQUEST['spcname']) ? ' selected="selected"' : '',
344
                            ">{$spcname}</option>" . \PHP_EOL;
345
                        $tablespaces->moveNext();
346
                    }
347
                    echo "\t\t\t</select>\n\t\t</td>\n\t</tr>" . \PHP_EOL;
348
                }
349
350
                echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL;
351
                echo "\t\t<td><textarea name=\"tblcomment\" rows=\"3\" cols=\"32\">",
352
                    \htmlspecialchars($_REQUEST['tblcomment']),
353
                    "</textarea></td>\n\t</tr>" . \PHP_EOL;
354
355
                echo '</table>' . \PHP_EOL;
356
                echo '<p><input type="hidden" name="action" value="create" />' . \PHP_EOL;
357
                echo '<input type="hidden" name="stage" value="2" />' . \PHP_EOL;
358
                echo $this->view->form;
359
                echo "<input type=\"submit\" value=\"{$this->lang['strnext']}\" />" . \PHP_EOL;
360
                echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
361
                echo '</form>' . \PHP_EOL;
362
363
                break;
364
            case 2:
365
                // Check inputs
366
                $fields = (int) (\trim($_REQUEST['fields']));
367
368
                if ('' === \trim($_REQUEST['name'])) {
369
                    $_REQUEST['stage'] = 1;
370
                    $this->doCreate($this->lang['strtableneedsname']);
371
372
                    return;
373
                }
374
375
                if ('' === $fields || !\is_numeric($fields) || (int) $fields !== $fields || 1 > $fields) {
0 ignored issues
show
introduced by
The condition is_numeric($fields) is always true.
Loading history...
376
                    $_REQUEST['stage'] = 1;
377
                    $this->doCreate($this->lang['strtableneedscols']);
378
379
                    return;
380
                }
381
382
                $types = $data->getTypes(true, false, true);
383
                $types_for_js = [];
384
385
                $this->printTrail('schema');
386
                $this->printTitle($this->lang['strcreatetable'], 'pg.table.create');
387
                $this->printMsg($msg);
388
389
                echo '<script src="' . \containerInstance()->subFolder . '/assets/js/tables.js" type="text/javascript"></script>';
390
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
391
392
                // Output table header
393
                echo '<table>' . \PHP_EOL;
394
                echo "\t<tr><th colspan=\"2\" class=\"data required\">{$this->lang['strcolumn']}</th><th colspan=\"2\" class=\"data required\">{$this->lang['strtype']}</th>";
395
                echo "<th class=\"data\">{$this->lang['strlength']}</th><th class=\"data\">{$this->lang['strnotnull']}</th>";
396
                echo "<th class=\"data\">{$this->lang['struniquekey']}</th><th class=\"data\">{$this->lang['strprimarykey']}</th>";
397
                echo "<th class=\"data\">{$this->lang['strdefault']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>" . \PHP_EOL;
398
399
                for ($i = 0; $i < $_REQUEST['fields']; ++$i) {
400
                    if (!isset($_REQUEST['field'][$i])) {
401
                        $_REQUEST['field'][$i] = '';
402
                    }
403
404
                    if (!isset($_REQUEST['length'][$i])) {
405
                        $_REQUEST['length'][$i] = '';
406
                    }
407
408
                    if (!isset($_REQUEST['default'][$i])) {
409
                        $_REQUEST['default'][$i] = '';
410
                    }
411
412
                    if (!isset($_REQUEST['colcomment'][$i])) {
413
                        $_REQUEST['colcomment'][$i] = '';
414
                    }
415
416
                    echo "\t<tr>\n\t\t<td>", $i + 1, '.&nbsp;</td>' . \PHP_EOL;
417
                    echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
418
                        \htmlspecialchars($_REQUEST['field'][$i]),
419
                        '" /></td>' . \PHP_EOL;
420
                    echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\" class=\"select2\" id=\"types{$i}\" onchange=\"checkLengths(this.options[this.selectedIndex].value,{$i});\">" . \PHP_EOL;
421
                    // Output any "magic" types
422
                    foreach ($data->extraTypes as $v) {
423
                        $types_for_js[\mb_strtolower($v)] = 1;
424
                        echo "\t\t\t\t<option value=\"", \htmlspecialchars($v), '"',
425
                            (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] === $v) ? ' selected="selected"' : '',
426
                            '>',
427
                            $this->misc->printVal($v),
428
                            '</option>' . \PHP_EOL;
429
                    }
430
                    $types->moveFirst();
431
432
                    while (!$types->EOF) {
433
                        $typname = $types->fields['typname'];
434
                        $types_for_js[$typname] = 1;
435
                        echo "\t\t\t\t<option value=\"", \htmlspecialchars($typname), '"',
436
                            (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] === $typname) ? ' selected="selected"' : '',
437
                            '>',
438
                            $this->misc->printVal($typname),
439
                            '</option>' . \PHP_EOL;
440
                        $types->moveNext();
441
                    }
442
                    echo "\t\t\t</select>\n\t\t\n";
443
444
                    if (0 === $i) {
445
                        // only define js types array once
446
                        $predefined_size_types = \array_intersect($data->predefined_size_types, \array_keys($types_for_js));
447
                        $escaped_predef_types = []; // the JS escaped array elements
448
449
                        foreach ($predefined_size_types as $value) {
450
                            $escaped_predef_types[] = "'{$value}'";
451
                        }
452
                        echo '<script type="text/javascript">predefined_lengths = new Array(' . \implode(',', $escaped_predef_types) . ");</script>\n\t</td>";
453
                    }
454
455
                    // Output array type selector
456
                    echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">" . \PHP_EOL;
457
                    echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && '' === $_REQUEST['array'][$i]) ? ' selected="selected"' : '', '></option>' . \PHP_EOL;
458
                    echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && '[]' === $_REQUEST['array'][$i]) ? ' selected="selected"' : '', '>[ ]</option>' . \PHP_EOL;
459
                    echo "\t\t\t</select>\n\t\t</td>" . \PHP_EOL;
460
461
                    echo "\t\t<td><input name=\"length[{$i}]\" id=\"lengths{$i}\" size=\"10\" value=\"",
462
                        \htmlspecialchars($_REQUEST['length'][$i]),
463
                        '" /></td>' . \PHP_EOL;
464
                    echo "\t\t<td><input type=\"checkbox\" name=\"notnull[{$i}]\"", (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', ' /></td>' . \PHP_EOL;
465
                    echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"uniquekey[{$i}]\""
466
                        . (isset($_REQUEST['uniquekey'][$i]) ? ' checked="checked"' : '') . ' /></td>' . \PHP_EOL;
467
                    echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"primarykey[{$i}]\" "
468
                        . (isset($_REQUEST['primarykey'][$i]) ? ' checked="checked"' : '')
469
                        . ' /></td>' . \PHP_EOL;
470
                    echo "\t\t<td><input name=\"default[{$i}]\" size=\"20\" value=\"",
471
                        \htmlspecialchars($_REQUEST['default'][$i]),
472
                        '" /></td>' . \PHP_EOL;
473
                    echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"",
474
                        \htmlspecialchars($_REQUEST['colcomment'][$i]),
475
                        "\" />
476
						<script type=\"text/javascript\">checkLengths(document.getElementById('types{$i}').value,{$i});</script>
477
						</td>\n\t</tr>" . \PHP_EOL;
478
                }
479
                echo '</table>' . \PHP_EOL;
480
                echo '<p><input type="hidden" name="action" value="create" />' . \PHP_EOL;
481
                echo '<input type="hidden" name="stage" value="3" />' . \PHP_EOL;
482
                echo $this->view->form;
483
                echo '<input type="hidden" name="name" value="', \htmlspecialchars($_REQUEST['name']), '" />' . \PHP_EOL;
484
                echo '<input type="hidden" name="fields" value="', \htmlspecialchars($_REQUEST['fields']), '" />' . \PHP_EOL;
485
486
                if (isset($_REQUEST['withoutoids'])) {
487
                    echo '<input type="hidden" name="withoutoids" value="true" />' . \PHP_EOL;
488
                }
489
                echo '<input type="hidden" name="tblcomment" value="', \htmlspecialchars($_REQUEST['tblcomment']), '" />' . \PHP_EOL;
490
491
                if (isset($_REQUEST['spcname'])) {
492
                    echo '<input type="hidden" name="spcname" value="', \htmlspecialchars($_REQUEST['spcname']), '" />' . \PHP_EOL;
493
                }
494
                echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL;
495
                echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
496
                echo '</form>' . \PHP_EOL;
497
498
                break;
499
            case 3:
500
                $this->coalesceArr($_REQUEST, 'notnull', []);
501
502
                $this->coalesceArr($_REQUEST, 'uniquekey', []);
503
504
                $this->coalesceArr($_REQUEST, 'primarykey', []);
505
506
                $this->coalesceArr($_REQUEST, 'length', []);
507
508
                // Default tablespace to null if it isn't set
509
                $this->coalesceArr($_REQUEST, 'spcname', null);
510
511
                // Check inputs
512
                $fields = (int) (\trim($_REQUEST['fields']));
513
514
                if ('' === \trim($_REQUEST['name'])) {
515
                    $_REQUEST['stage'] = 1;
516
                    $this->doCreate($this->lang['strtableneedsname']);
517
518
                    return;
519
                }
520
521
                if ('' === $fields || !\is_numeric($fields) || (int) $fields !== $fields || 0 >= $fields) {
0 ignored issues
show
introduced by
The condition is_numeric($fields) is always true.
Loading history...
522
                    $_REQUEST['stage'] = 1;
523
                    $this->doCreate($this->lang['strtableneedscols']);
524
525
                    return;
526
                }
527
528
                $status = $data->createTable(
529
                    $_REQUEST['name'],
530
                    $_REQUEST['fields'],
531
                    $_REQUEST['field'],
532
                    $_REQUEST['type'],
533
                    $_REQUEST['array'],
534
                    $_REQUEST['length'],
535
                    $_REQUEST['notnull'],
536
                    $_REQUEST['default'],
537
                    isset($_REQUEST['withoutoids']),
538
                    $_REQUEST['colcomment'],
539
                    $_REQUEST['tblcomment'],
540
                    $_REQUEST['spcname'],
541
                    $_REQUEST['uniquekey'],
542
                    $_REQUEST['primarykey']
543
                );
544
545
                if (0 === $status) {
546
                    $this->view->setReloadBrowser(true);
547
548
                    $this->doDefault($this->lang['strtablecreated']);
549
550
                    return;
551
                }
552
553
                if (-1 === $status) {
554
                    $_REQUEST['stage'] = 2;
555
                    $this->doCreate($this->lang['strtableneedsfield']);
556
557
                    return;
558
                }
559
                $_REQUEST['stage'] = 2;
560
                $this->doCreate($this->lang['strtablecreatedbad']);
561
562
                return;
563
564
                break;
565
566
            default:
567
                echo "<p>{$this->lang['strinvalidparam']}</p>" . \PHP_EOL;
568
        }
569
    }
570
571
    /**
572
     * Dsiplay a screen where user can create a table from an existing one.
573
     * We don't have to check if pg supports schema cause create table like
574
     * is available under pg 7.4+ which has schema.
575
     *
576
     * @param mixed $confirm
577
     * @param mixed $msg
578
     */
579
    public function doCreateLike($confirm, $msg = ''): void
580
    {
581
        $data = $this->misc->getDatabaseAccessor();
582
583
        if (!$confirm) {
584
            $this->coalesceArr($_REQUEST, 'name', '');
585
586
            $this->coalesceArr($_REQUEST, 'like', '');
587
588
            $this->coalesceArr($_REQUEST, 'tablespace', '');
589
590
            $this->printTrail('schema');
591
            $this->printTitle($this->lang['strcreatetable'], 'pg.table.create');
592
            $this->printMsg($msg);
593
594
            $tbltmp = $data->getAllTables();
595
            $tbltmp = $tbltmp->getArray();
596
597
            $tables = [];
598
            $tblsel = '';
599
600
            foreach ($tbltmp as $a) {
601
                $data->fieldClean($a['nspname']);
602
                $data->fieldClean($a['relname']);
603
                $tables["\"{$a['nspname']}\".\"{$a['relname']}\""] = \serialize(['schema' => $a['nspname'], 'table' => $a['relname']]);
604
605
                if ($_REQUEST['like'] === $tables["\"{$a['nspname']}\".\"{$a['relname']}\""]) {
606
                    $tblsel = \htmlspecialchars($tables["\"{$a['nspname']}\".\"{$a['relname']}\""]);
607
                }
608
            }
609
610
            unset($tbltmp);
611
612
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
613
            echo "<table>\n\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
614
            echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", \htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>" . \PHP_EOL;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 190 characters; contains 191 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
615
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strcreatetablelikeparent']}</th>" . \PHP_EOL;
616
            echo "\t\t<td class=\"data\">";
617
            echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tables, 'like', true, $tblsel, false);
618
            echo "</td>\n\t</tr>" . \PHP_EOL;
619
620
            if ($data->hasTablespaces()) {
621
                $tblsp_ = $data->getTablespaces();
622
623
                if (0 < $tblsp_->recordCount()) {
624
                    $tblsp_ = $tblsp_->getArray();
625
                    $tblsp = [];
626
627
                    foreach ($tblsp_ as $a) {
628
                        $tblsp[$a['spcname']] = $a['spcname'];
629
                    }
630
631
                    echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strtablespace']}</th>" . \PHP_EOL;
632
                    echo "\t\t<td class=\"data\">";
633
                    echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false);
634
                    echo "</td>\n\t</tr>" . \PHP_EOL;
635
                }
636
            }
637
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['stroptions']}</th>\n\t\t<td class=\"data\">";
638
            echo '<label for="withdefaults"><input type="checkbox" id="withdefaults" name="withdefaults"',
639
                isset($_REQUEST['withdefaults']) ? ' checked="checked"' : '',
640
                "/>{$this->lang['strcreatelikewithdefaults']}</label>";
641
642
            if ($data->hasCreateTableLikeWithConstraints()) {
643
                echo '<br /><label for="withconstraints"><input type="checkbox" id="withconstraints" name="withconstraints"',
644
                    isset($_REQUEST['withconstraints']) ? ' checked="checked"' : '',
645
                    "/>{$this->lang['strcreatelikewithconstraints']}</label>";
646
            }
647
648
            if ($data->hasCreateTableLikeWithIndexes()) {
649
                echo '<br /><label for="withindexes"><input type="checkbox" id="withindexes" name="withindexes"',
650
                    isset($_REQUEST['withindexes']) ? ' checked="checked"' : '',
651
                    "/>{$this->lang['strcreatelikewithindexes']}</label>";
652
            }
653
            echo "</td>\n\t</tr>" . \PHP_EOL;
654
            echo '</table>';
655
656
            echo '<input type="hidden" name="action" value="confcreatelike" />' . \PHP_EOL;
657
            echo $this->view->form;
658
            echo "<p><input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL;
659
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
660
            echo '</form>' . \PHP_EOL;
661
        } else {
662
            if ('' === \trim($_REQUEST['name'])) {
663
                $this->doCreateLike(false, $this->lang['strtableneedsname']);
664
665
                return;
666
            }
667
668
            if ('' === \trim($_REQUEST['like'])) {
669
                $this->doCreateLike(false, $this->lang['strtablelikeneedslike']);
670
671
                return;
672
            }
673
674
            $this->coalesceArr($_REQUEST, 'tablespace', '');
675
676
            $status = $data->createTableLike(
677
                $_REQUEST['name'],
678
                \unserialize($_REQUEST['like']),
679
                isset($_REQUEST['withdefaults']),
680
                isset($_REQUEST['withconstraints']),
681
                isset($_REQUEST['withindexes']),
682
                $_REQUEST['tablespace']
683
            );
684
685
            if (0 === $status) {
686
                $this->view->setReloadBrowser(true);
687
688
                $this->doDefault($this->lang['strtablecreated']);
689
690
                return;
691
            }
692
            $this->doCreateLike(false, $this->lang['strtablecreatedbad']);
693
694
            return;
695
        }
696
    }
697
698
    /**
699
     * Ask for select parameters and perform select.
700
     *
701
     * @param mixed $confirm
702
     * @param mixed $msg
703
     */
704
    public function doSelectRows($confirm, $msg = '')
705
    {
706
        $data = $this->misc->getDatabaseAccessor();
707
708
        if ($confirm) {
709
            $this->printTrail('table');
710
            $this->printTabs('table', 'select');
711
            $this->printMsg($msg);
712
713
            $attrs = $data->getTableAttributes($_REQUEST['table']);
714
715
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/display" method="post" id="selectform">' . \PHP_EOL;
716
717
            if (0 < $attrs->recordCount()) {
718
                // JavaScript for select all feature
719
                echo '<script type="text/javascript">' . \PHP_EOL;
720
                echo "//<![CDATA[\n";
721
                echo "	function selectAll() {\n";
722
                echo "		for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n";
723
                echo "			var e = document.getElementById('selectform').elements[i];\n";
724
                echo "			if (e.name.indexOf('show') == 0) e.checked = document.getElementById('selectform').selectall.checked;\n";
725
                echo "		}\n";
726
                echo "	}\n";
727
                echo '//]]>' . \PHP_EOL;
728
                echo '</script>' . \PHP_EOL;
729
730
                echo '<table>' . \PHP_EOL;
731
732
                // Output table header
733
                echo "<tr><th class=\"data\">{$this->lang['strshow']}</th><th class=\"data\">{$this->lang['strcolumn']}</th>";
734
                echo "<th class=\"data\">{$this->lang['strtype']}</th><th class=\"data\">{$this->lang['stroperator']}</th>";
735
                echo "<th class=\"data\">{$this->lang['strvalue']}</th></tr>";
736
737
                $i = 0;
738
739
                while (!$attrs->EOF) {
740
                    $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']);
741
                    // Set up default value if there isn't one already
742
                    if (!isset($_REQUEST['values'][$attrs->fields['attname']])) {
743
                        $_REQUEST['values'][$attrs->fields['attname']] = null;
744
                    }
745
746
                    if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) {
747
                        $_REQUEST['ops'][$attrs->fields['attname']] = null;
748
                    }
749
750
                    // Continue drawing row
751
                    $id = (0 === ($i % 2) ? '1' : '2');
752
                    echo "<tr class=\"data{$id}\">" . \PHP_EOL;
753
                    echo '<td style="white-space:nowrap;">';
754
                    echo '<input type="checkbox" name="show[', \htmlspecialchars($attrs->fields['attname']), ']"',
755
                        isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '',
756
                        ' /></td>';
757
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
758
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>';
759
                    echo '<td style="white-space:nowrap;">';
760
                    echo "<select name=\"ops[{$attrs->fields['attname']}]\">" . \PHP_EOL;
761
762
                    foreach (\array_keys($data->selectOps) as $v) {
763
                        echo '<option value="', \htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] === $v) ? ' selected="selected"' : '',
764
                            '>',
765
                            \htmlspecialchars($v),
766
                            '</option>' . \PHP_EOL;
767
                    }
768
                    echo "</select>\n</td>" . \PHP_EOL;
769
                    echo '<td style="white-space:nowrap;">', $data->printField(
770
                        "values[{$attrs->fields['attname']}]",
771
                        $_REQUEST['values'][$attrs->fields['attname']],
772
                        $attrs->fields['type']
773
                    ), '</td>';
774
                    echo '</tr>' . \PHP_EOL;
775
                    ++$i;
776
                    $attrs->moveNext();
777
                }
778
                // Select all checkbox
779
                echo "<tr><td colspan=\"5\"><input type=\"checkbox\" id=\"selectall\" name=\"selectall\" accesskey=\"a\" onclick=\"javascript:selectAll()\" /><label for=\"selectall\">{$this->lang['strselectallfields']}</label></td>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 190 characters; contains 233 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
780
                echo '</tr></table>' . \PHP_EOL;
781
            } else {
782
                echo "<p>{$this->lang['strinvalidparam']}</p>" . \PHP_EOL;
783
            }
784
785
            echo '<p><input type="hidden" name="action" value="selectrows" />' . \PHP_EOL;
786
            echo \sprintf('<input type="hidden" name="table" value="%s"  />%s', \htmlspecialchars($_REQUEST['table']), \PHP_EOL);
787
            echo '<input type="hidden" name="subject" value="table" />' . \PHP_EOL;
788
            echo $this->view->form;
789
            echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$this->lang['strselect']}\" />" . \PHP_EOL;
790
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
791
            echo '</form>' . \PHP_EOL;
792
793
            return;
794
        }
795
        $this->coalesceArr($_POST, 'show', []);
796
797
        $this->coalesceArr($_POST, 'values', []);
798
799
        $this->coalesceArr($_POST, 'nulls', []);
800
801
        // Verify that they haven't supplied a value for unary operators
802
        foreach ($_POST['ops'] as $k => $v) {
803
            if ('p' === $data->selectOps[$v] && '' !== $_POST['values'][$k]) {
804
                $this->doSelectRows(true, $this->lang['strselectunary']);
805
806
                return;
807
            }
808
        }
809
810
        if (0 === \count($_POST['show'])) {
811
            $this->doSelectRows(true, $this->lang['strselectneedscol']);
812
        } else {
813
            // Generate query SQL
814
            $query = $data->getSelectSQL(
815
                $_REQUEST['table'],
816
                \array_keys($_POST['show']),
817
                $_POST['values'],
818
                $_POST['ops']
819
            );
820
            $_REQUEST['query'] = $query;
821
            $_REQUEST['return'] = 'selectrows';
822
823
            $this->setNoOutput(true);
824
825
            $display_controller = new DisplayController($this->getContainer());
826
827
            return $display_controller->render();
828
        }
829
    }
830
831
    /**
832
     * Ask for insert parameters and then actually insert row.
833
     *
834
     * @param mixed $msg
835
     */
836
    public function formInsertRow($msg = ''): void
837
    {
838
        $data = $this->misc->getDatabaseAccessor();
839
840
        $this->printTrail('table');
841
        $this->printTabs('table', 'insert');
842
843
        $this->printMsg($msg);
844
845
        $attrs = $data->getTableAttributes($_REQUEST['table']);
846
847
        $fksprops = $this->_getFKProps();
848
849
        $this->coalesceArr($_REQUEST, 'values', []);
850
        $this->coalesceArr($_REQUEST, 'nulls', []);
851
        $this->coalesceArr($_REQUEST, 'format', []);
852
853
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post" id="ac_form">' . \PHP_EOL;
854
855
        if (0 < $attrs->recordCount()) {
856
            echo '<table>' . \PHP_EOL;
857
858
            // Output table header
859
            echo "<tr><th class=\"data\">{$this->lang['strcolumn']}</th><th class=\"data\">{$this->lang['strtype']}</th>";
860
            echo "<th class=\"data\">{$this->lang['strformat']}</th>";
861
            echo "<th class=\"data\">{$this->lang['strnull']}</th><th class=\"data\">{$this->lang['strvalue']}</th></tr>";
862
863
            $i = 0;
864
            $fields = [];
865
866
            while (!$attrs->EOF) {
867
                $fields[$attrs->fields['attnum']] = $attrs->fields['attname'];
868
                $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']);
869
                // Set up default value if there isn't one already
870
                if (!isset($_REQUEST['values'][$attrs->fields['attnum']])) {
871
                    $_REQUEST['values'][$attrs->fields['attnum']] = $attrs->fields['adsrc'];
872
873
                    if (null === $attrs->fields['adsrc'] && !$attrs->fields['attnotnull']) {
874
                        $_REQUEST['nulls'][$attrs->fields['attnum']] = true;
875
                    }
876
                }
877
878
                // Default format to 'VALUE' if there is no default,
879
                // otherwise default to 'EXPRESSION'
880
                if (!isset($_REQUEST['format'][$attrs->fields['attnum']])) {
881
                    $_REQUEST['format'][$attrs->fields['attnum']] = (null === $attrs->fields['adsrc']) ? 'VALUE' : 'EXPRESSION';
882
                }
883
884
                $requested_format = $_REQUEST['format'][$attrs->fields['attnum']];
885
                // Continue drawing row
886
                $id = (0 === ($i % 2) ? '1' : '2');
887
                echo "<tr class=\"data{$id}\">" . \PHP_EOL;
888
                echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
889
                echo '<td style="white-space:nowrap;">' . \PHP_EOL;
890
                echo $this->misc->printVal(
891
                    $data->formatType(
892
                        $attrs->fields['type'],
893
                        $attrs->fields['atttypmod']
894
                    )
895
                );
896
                echo "<input type=\"hidden\" name=\"types[{$attrs->fields['attnum']}]\" value=\"",
897
                    \htmlspecialchars($attrs->fields['type']),
898
                    '" /></td>';
899
                echo '<td style="white-space:nowrap;">' . \PHP_EOL;
900
901
                echo "<select name=\"format[{$attrs->fields['attnum']}]\">" . \PHP_EOL;
902
                echo \sprintf(
903
                    '<option value="VALUE" %s >%s</option> %s',
904
                    ('VALUE' === $requested_format) ? ' selected="selected" ' : '',
905
                    $this->lang['strvalue'],
906
                    \PHP_EOL
907
                );
908
                echo \sprintf(
909
                    '<option value="EXPRESSION" %s >%s</option> %s',
910
                    ('EXPRESSION' === $requested_format) ? ' selected="selected" ' : '',
911
                    $this->lang['strexpression'],
912
                    \PHP_EOL
913
                );
914
915
                echo "</select>\n</td>" . \PHP_EOL;
916
                echo '<td style="white-space:nowrap;">';
917
                // Output null box if the column allows nulls
918
                // Edit: if it can be null, then null it is.
919
                if (!$attrs->fields['attnotnull']) {
920
                    echo '<label><span>';
921
                    echo \sprintf(
922
                        '<input type="checkbox" class="nullcheckbox" name="nulls[%s]" %s />',
923
                        $attrs->fields['attnum'],
924
                        ' checked="checked"'
925
                    );
926
                    echo '</span></label>';
927
                }
928
                echo '</td>';
929
930
                echo "<td id=\"row_att_{$attrs->fields['attnum']}\" style=\"white-space:nowrap;\">";
931
932
                if ((false !== $fksprops) && isset($fksprops['byfield'][$attrs->fields['attnum']])) {
933
                    echo $data->printField(
934
                        "values[{$attrs->fields['attnum']}]",
935
                        $_REQUEST['values'][$attrs->fields['attnum']],
936
                        'fktype' /*force FK*/,
937
                        [
938
                            'id' => "attr_{$attrs->fields['attnum']}",
939
                            'autocomplete' => 'off',
940
                            'class' => 'insert_row_input',
941
                        ]
942
                    );
943
                } else {
944
                    echo $data->printField("values[{$attrs->fields['attnum']}]", $_REQUEST['values'][$attrs->fields['attnum']], $attrs->fields['type'], ['class' => 'insert_row_input']);
945
                }
946
                echo '</td>' . \PHP_EOL;
947
                echo '</tr>' . \PHP_EOL;
948
                ++$i;
949
                $attrs->moveNext();
950
            }
951
            echo '</table>' . \PHP_EOL;
952
953
            if (!isset($_SESSION['counter'])) {
954
                $_SESSION['counter'] = 0;
955
            }
956
957
            echo '<input type="hidden" name="action" value="insertrow" />' . \PHP_EOL;
958
            echo '<input type="hidden" name="fields" value="', \htmlentities(\serialize($fields), \ENT_QUOTES, 'UTF-8'), '" />' . \PHP_EOL;
959
            echo '<input type="hidden" name="protection_counter" value="' . $_SESSION['counter'] . '" />' . \PHP_EOL;
960
            echo \sprintf('<input type="hidden" name="table" value="%s"  />%s', \htmlspecialchars($_REQUEST['table']), \PHP_EOL);
961
            echo "<p><input type=\"submit\" name=\"insert\" value=\"{$this->lang['strinsert']}\" />" . \PHP_EOL;
962
            echo "<input type=\"submit\" name=\"insertandrepeat\" accesskey=\"r\" value=\"{$this->lang['strinsertandrepeat']}\" />" . \PHP_EOL;
963
964
            if (false !== $fksprops) {
965
                if ('default off' !== $this->conf['autocomplete']) {
966
                    echo "<input type=\"checkbox\" id=\"no_ac\" value=\"1\" checked=\"checked\" /><label for=\"no_ac\">{$this->lang['strac']}</label>" . \PHP_EOL;
967
                } else {
968
                    echo "<input type=\"checkbox\" id=\"no_ac\" value=\"0\" /><label for=\"no_ac\">{$this->lang['strac']}</label>" . \PHP_EOL;
969
                }
970
            }
971
            echo '</p>' . \PHP_EOL;
972
        } else {
973
            echo "<p>{$this->lang['strnofieldsforinsert']}</p>" . \PHP_EOL;
974
        }
975
        echo $this->view->form;
976
        echo '</form>' . \PHP_EOL;
977
        echo \sprintf('<button class="btn btn-mini btn_back" style="float: right;        margin-right: 4em;        margin-top: -3em;">%s</button>%s', $this->lang['strcancel'], \PHP_EOL);
978
        echo '<script src="' . \containerInstance()->subFolder . '/assets/js/insert_or_edit_row.js" type="text/javascript"></script>';
979
    }
980
981
    /**
982
     * Performs insertion of row according to request parameters.
983
     */
984
    public function doInsertRow()
985
    {
986
        $data = $this->misc->getDatabaseAccessor();
987
988
        $this->coalesceArr($_POST, 'values', []);
989
990
        $this->coalesceArr($_POST, 'nulls', []);
991
992
        $_POST['fields'] = \unserialize(\htmlspecialchars_decode($_POST['fields'], \ENT_QUOTES));
993
994
        if ($_SESSION['counter']++ === (int) ($_POST['protection_counter'])) {
995
            $status = $data->insertRow($_POST['table'], $_POST['fields'], $_POST['values'], $_POST['nulls'], $_POST['format'], $_POST['types']);
996
997
            if (0 === $status) {
998
                if (isset($_POST['insert'])) {
999
                    $this->doDefault($this->lang['strrowinserted']);
1000
1001
                    return;
1002
                }
1003
                $_REQUEST['values'] = [];
1004
                $_REQUEST['nulls'] = [];
1005
1006
                return $this->formInsertRow($this->lang['strrowinserted']);
1007
            }
1008
1009
            return $this->formInsertRow($this->lang['strrowinsertedbad']);
1010
        }
1011
1012
        return $this->formInsertRow($this->lang['strrowduplicate']);
1013
    }
1014
1015
    /**
1016
     * Show confirmation of empty and perform actual empty.
1017
     *
1018
     * @param mixed $confirm
1019
     */
1020
    public function doEmpty($confirm): void
1021
    {
1022
        $data = $this->misc->getDatabaseAccessor();
1023
1024
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1025
            $this->doDefault($this->lang['strspecifytabletoempty']);
1026
1027
            return;
1028
        }
1029
1030
        if ($confirm) {
1031
            if (isset($_REQUEST['ma'])) {
1032
                $this->printTrail('schema');
1033
                $this->printTitle($this->lang['strempty'], 'pg.table.empty');
1034
1035
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1036
1037
                foreach ($_REQUEST['ma'] as $v) {
1038
                    $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
1039
                    echo '<p>' . \sprintf($this->lang['strconfemptytable'], $this->misc->printVal($a['table']));
1040
1041
                    echo '</p>' . \PHP_EOL;
1042
                    \printf('<input type="hidden" name="table[]" value="%s" />', \htmlspecialchars($a['table']));
1043
                } //  END mutli empty
1044
            } else {
1045
                $this->printTrail('table');
1046
                $this->printTitle($this->lang['strempty'], 'pg.table.empty');
1047
1048
                echo '<p>', \sprintf($this->lang['strconfemptytable'], $this->misc->printVal($_REQUEST['table'])), '</p>' . \PHP_EOL;
1049
1050
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1051
1052
                echo \sprintf('<input type="hidden" name="table" value="%s"  />%s', \htmlspecialchars($_REQUEST['table']), \PHP_EOL);
1053
                // END not mutli empty
1054
            }
1055
            echo "<input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label>";
1056
            echo '<input type="hidden" name="action" value="empty" />' . \PHP_EOL;
1057
            echo $this->view->form;
1058
            echo "<input type=\"submit\" name=\"empty\" value=\"{$this->lang['strempty']}\" /> <input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
1059
            echo "</form>\n"; //  END if confirm
1060
        } else {
1061
            // Do Empty
1062
            $msg = '';
1063
1064
            if (\is_array($_REQUEST['table'])) {
1065
                foreach ($_REQUEST['table'] as $t) {
1066
                    [$status, $sql] = $data->emptyTable($t, isset($_POST['cascade']));
1067
1068
                    if (0 === $status) {
1069
                        $msg .= \sprintf('%s<br />', $sql);
1070
                        $msg .= \sprintf(
1071
                            '%s: %s<br />',
1072
                            \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1073
                            $this->lang['strtableemptied']
1074
                        );
1075
                    } else {
1076
                        $this->doDefault(\sprintf(
1077
                            '%s%s: %s<br />',
1078
                            $msg,
1079
                            \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1080
                            $this->lang['strtableemptiedbad']
1081
                        ));
1082
1083
                        return;
1084
                    }
1085
                }
1086
                $this->doDefault($msg); //  END mutli empty
1087
            } else {
1088
                [$status, $sql] = $data->emptyTable($_POST['table'], isset($_POST['cascade']));
1089
1090
                if (0 === $status) {
1091
                    $msg .= \sprintf('%s<br />', $sql);
1092
                    $msg .= \sprintf(
1093
                        '%s: %s<br />',
1094
                        \htmlentities($_POST['table'], \ENT_QUOTES, 'UTF-8'),
1095
                        $this->lang['strtableemptied']
1096
                    );
1097
1098
                    $this->doDefault($msg);
1099
                }
1100
1101
                $this->doDefault($sql . '<br>' . $this->lang['strtableemptiedbad']);
1102
                // END not mutli empty
1103
            }
1104
            // END do Empty
1105
        }
1106
    }
1107
1108
    /**
1109
     * Show confirmation of drop and perform actual drop.
1110
     *
1111
     * @param mixed $confirm
1112
     */
1113
    public function doDrop($confirm): void
1114
    {
1115
        $data = $this->misc->getDatabaseAccessor();
1116
1117
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1118
            $this->doDefault($this->lang['strspecifytabletodrop']);
1119
1120
            return;
1121
        }
1122
1123
        if ($confirm) {
1124
            //If multi drop
1125
            if (isset($_REQUEST['ma'])) {
1126
                $this->printTrail('schema');
1127
                $this->printTitle($this->lang['strdrop'], 'pg.table.drop');
1128
1129
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1130
1131
                foreach ($_REQUEST['ma'] as $v) {
1132
                    $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
1133
                    echo '<p>', \sprintf($this->lang['strconfdroptable'], $this->misc->printVal($a['table'])), '</p>' . \PHP_EOL;
1134
                    \printf('<input type="hidden" name="table[]" value="%s" />', \htmlspecialchars($a['table']));
1135
                }
1136
            } else {
1137
                $this->printTrail('table');
1138
                $this->printTitle($this->lang['strdrop'], 'pg.table.drop');
1139
1140
                echo '<p>', \sprintf($this->lang['strconfdroptable'], $this->misc->printVal($_REQUEST['table'])), '</p>' . \PHP_EOL;
1141
1142
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1143
                echo \sprintf('<input type="hidden" name="table" value="%s"  />%s', \htmlspecialchars($_REQUEST['table']), \PHP_EOL);
1144
                // END if multi drop
1145
            }
1146
1147
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
1148
            echo $this->view->form;
1149
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>" . \PHP_EOL;
1150
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL;
1151
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
1152
            echo "</form>\n"; //  END confirm
1153
        } else {
1154
            //If multi drop
1155
            if (\is_array($_REQUEST['table'])) {
1156
                $msg = '';
1157
                $status = $data->beginTransaction();
1158
1159
                if (0 === $status) {
1160
                    foreach ($_REQUEST['table'] as $t) {
1161
                        $status = $data->dropTable($t, isset($_POST['cascade']));
1162
1163
                        if (0 === $status) {
1164
                            $msg .= \sprintf(
1165
                                '%s: %s<br />',
1166
                                \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1167
                                $this->lang['strtabledropped']
1168
                            );
1169
                        } else {
1170
                            $data->endTransaction();
1171
1172
                            $this->doDefault(\sprintf(
1173
                                '%s%s: %s<br />',
1174
                                $msg,
1175
                                \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1176
                                $this->lang['strtabledroppedbad']
1177
                            ));
1178
1179
                            return;
1180
                        }
1181
                    }
1182
                }
1183
1184
                if (0 === $data->endTransaction()) {
1185
                    // Everything went fine, back to the Default page....
1186
                    $this->view->setReloadBrowser(true);
1187
1188
                    $this->doDefault($msg);
1189
1190
                    return;
1191
                }
1192
1193
                $this->doDefault($this->lang['strtabledroppedbad']);
1194
1195
                return;
1196
            }
1197
            $status = $data->dropTable($_POST['table'], isset($_POST['cascade']));
1198
1199
            if (0 === $status) {
1200
                $this->view->setReloadBrowser(true);
1201
1202
                $this->doDefault($this->lang['strtabledropped']);
1203
1204
                return;
1205
            }
1206
1207
            $this->doDefault($this->lang['strtabledroppedbad']);
1208
1209
            return;
1210
            // END DROP
1211
        }
1212
    }
1213
1214
    private function _getColumns()
1215
    {
1216
        return [
1217
            'table' => [
1218
                'title' => $this->lang['strtable'],
1219
                'field' => Decorator::field('relname'),
1220
                'url' => \containerInstance()->subFolder . "/redirect/table?{$this->misc->href}&amp;",
1221
                'vars' => ['table' => 'relname'],
1222
            ],
1223
            'owner' => [
1224
                'title' => $this->lang['strowner'],
1225
                'field' => Decorator::field('relowner'),
1226
            ],
1227
            'tablespace' => [
1228
                'title' => $this->lang['strtablespace'],
1229
                'field' => Decorator::field('tablespace'),
1230
            ],
1231
            'tuples' => [
1232
                'title' => $this->lang['strestimatedrowcount'],
1233
                'field' => Decorator::field('reltuples'),
1234
                'type' => 'numeric',
1235
            ],
1236
            'table_size' => [
1237
                'title' => $this->lang['strsize'],
1238
                'field' => Decorator::field('table_size'),
1239
            ],
1240
            'actions' => [
1241
                'title' => $this->lang['stractions'],
1242
            ],
1243
            'comment' => [
1244
                'title' => $this->lang['strcomment'],
1245
                'field' => Decorator::field('relcomment'),
1246
            ],
1247
        ];
1248
    }
1249
1250
    private function _getActions()
1251
    {
1252
        return [
1253
            'multiactions' => [
1254
                'keycols' => ['table' => 'relname'],
1255
                'url' => 'tables',
1256
                'default' => 'analyze',
1257
            ],
1258
            'browse' => [
1259
                'content' => $this->lang['strbrowse'],
1260
                'attr' => [
1261
                    'href' => [
1262
                        'url' => 'display',
1263
                        'urlvars' => [
1264
                            'subject' => 'table',
1265
                            'return' => 'table',
1266
                            'table' => Decorator::field('relname'),
1267
                        ],
1268
                    ],
1269
                ],
1270
            ],
1271
            'select' => [
1272
                'content' => $this->lang['strselect'],
1273
                'attr' => [
1274
                    'href' => [
1275
                        'url' => 'tables',
1276
                        'urlvars' => [
1277
                            'action' => 'confselectrows',
1278
                            'table' => Decorator::field('relname'),
1279
                        ],
1280
                    ],
1281
                ],
1282
            ],
1283
            'insert' => [
1284
                'content' => $this->lang['strinsert'],
1285
                'attr' => [
1286
                    'href' => [
1287
                        'url' => 'tables',
1288
                        'urlvars' => [
1289
                            'action' => 'confinsertrow',
1290
                            'table' => Decorator::field('relname'),
1291
                        ],
1292
                    ],
1293
                ],
1294
            ],
1295
            'empty' => [
1296
                'multiaction' => 'confirm_empty',
1297
                'content' => $this->lang['strempty'],
1298
                'attr' => [
1299
                    'href' => [
1300
                        'url' => 'tables',
1301
                        'urlvars' => [
1302
                            'action' => 'confirm_empty',
1303
                            'table' => Decorator::field('relname'),
1304
                        ],
1305
                    ],
1306
                ],
1307
            ],
1308
            'alter' => [
1309
                'content' => $this->lang['stralter'],
1310
                'attr' => [
1311
                    'href' => [
1312
                        'url' => 'tblproperties',
1313
                        'urlvars' => [
1314
                            'action' => 'confirm_alter',
1315
                            'table' => Decorator::field('relname'),
1316
                        ],
1317
                    ],
1318
                ],
1319
            ],
1320
            'drop' => [
1321
                'multiaction' => 'confirm_drop',
1322
                'content' => $this->lang['strdrop'],
1323
                'attr' => [
1324
                    'href' => [
1325
                        'url' => 'tables',
1326
                        'urlvars' => [
1327
                            'action' => 'confirm_drop',
1328
                            'table' => Decorator::field('relname'),
1329
                        ],
1330
                    ],
1331
                ],
1332
            ],
1333
            'vacuum' => [
1334
                'multiaction' => 'confirm_vacuum',
1335
                'content' => $this->lang['strvacuum'],
1336
                'attr' => [
1337
                    'href' => [
1338
                        'url' => 'tables',
1339
                        'urlvars' => [
1340
                            'action' => 'confirm_vacuum',
1341
                            'table' => Decorator::field('relname'),
1342
                        ],
1343
                    ],
1344
                ],
1345
            ],
1346
            'analyze' => [
1347
                'multiaction' => 'confirm_analyze',
1348
                'content' => $this->lang['stranalyze'],
1349
                'attr' => [
1350
                    'href' => [
1351
                        'url' => 'tables',
1352
                        'urlvars' => [
1353
                            'action' => 'confirm_analyze',
1354
                            'table' => Decorator::field('relname'),
1355
                        ],
1356
                    ],
1357
                ],
1358
            ],
1359
            'reindex' => [
1360
                'multiaction' => 'confirm_reindex',
1361
                'content' => $this->lang['strreindex'],
1362
                'attr' => [
1363
                    'href' => [
1364
                        'url' => 'tables',
1365
                        'urlvars' => [
1366
                            'action' => 'confirm_reindex',
1367
                            'table' => Decorator::field('relname'),
1368
                        ],
1369
                    ],
1370
                ],
1371
            ],
1372
            //'cluster' TODO ?
1373
        ];
1374
    }
1375
1376
    // END Function
1377
}
1378