Test Failed
Pull Request — develop (#380)
by Felipe
03:38
created

TablesController::doEmpty()   C

Complexity

Conditions 10
Paths 8

Size

Total Lines 105
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 10
eloc 72
nc 8
nop 1
dl 0
loc 105
rs 6.7442
c 6
b 1
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
use PHPPgAdmin\Traits\AdminTrait;
11
use PHPPgAdmin\Traits\InsertEditRowTrait;
12
use PHPPgAdmin\XHtml\HTMLController;
13
14
/**
15
 * Base controller class.
16
 */
17
class TablesController extends BaseController
18
{
19
    use 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...
20
    use InsertEditRowTrait;
21
22
    public $table_place = 'tables-tables';
23
24
    public $controller_title = 'strtables';
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
35
        if ('subtree' === $this->action) {
36
            return $this->doSubTree();
37
        }
38
39
        if ('json' === $this->action) {
40
            return $this->displayJson();
41
        }
42
43
        $header_template = 'header.twig';
44
45
        \ob_start();
46
47
        switch ($this->action) {
48
            case 'create':
49
                if (null !== $this->getPostParam('cancel')) {
50
                    $this->doDefault();
51
                } else {
52
                    $header_template = 'header_select2.twig';
53
                    $this->doCreate();
54
                }
55
56
                break;
57
            case 'createlike':
58
                $header_template = 'header_select2.twig';
59
                $this->doCreateLike(false);
60
61
                break;
62
            case 'confcreatelike':
63
                if (null !== $this->getPostParam('cancel')) {
64
                    $header_template = 'header_datatables.twig';
65
                    $this->doDefault();
66
                } else {
67
                    //$header_template = 'header_select2.twig';
68
                    $this->doCreateLike(true);
69
                }
70
71
                break;
72
            case 'selectrows':
73
                if (!isset($_POST['cancel'])) {
74
                    $this->doSelectRows(false);
75
                } else {
76
                    $header_template = 'header_datatables.twig';
77
                    $this->doDefault();
78
                }
79
80
                break;
81
            case 'confselectrows':
82
                $this->doSelectRows(true);
83
84
                break;
85
            case 'insertrow':
86
                if (!isset($_POST['cancel'])) {
87
                    $this->doInsertRow();
88
                } else {
89
                    $header_template = 'header_datatables.twig';
90
                    $this->doDefault();
91
                }
92
93
                break;
94
            case 'confinsertrow':
95
                $this->formInsertRow();
96
97
                break;
98
            case 'empty':
99
                if (isset($_POST['empty'])) {
100
                    $this->doEmpty(false);
101
                } else {
102
                    $header_template = 'header_datatables.twig';
103
                    $this->doDefault();
104
                }
105
106
                break;
107
            case 'confirm_empty':
108
                $this->doEmpty(true);
109
110
                break;
111
            case 'drop':
112
                if (null !== $this->getPostParam('drop')) {
113
                    $this->doDrop(false);
114
                } else {
115
                    $header_template = 'header_datatables.twig';
116
                    $this->doDefault();
117
                }
118
119
                break;
120
            case 'confirm_drop':
121
                $this->doDrop(true);
122
123
                break;
124
125
            default:
126
                if (false === $this->adminActions($this->action, 'table')) {
127
                    $header_template = 'header_datatables.twig';
128
                    $this->doDefault();
129
                }
130
131
                break;
132
        }
133
134
        $output = \ob_get_clean();
135
136
        $this->printHeader($this->headerTitle(), null, true, $header_template);
137
        $this->printBody();
138
139
        echo $output;
140
141
        return $this->printFooter();
142
    }
143
144
    /**
145
     * Show default list of tables in the database.
146
     *
147
     * @param mixed $msg
148
     */
149
    public function doDefault($msg = ''): void
150
    {
151
        $data = $this->misc->getDatabaseAccessor();
152
153
        $this->printTrail('schema');
154
        $this->printTabs('schema', 'tables');
155
        $this->printMsg($msg);
156
157
        $tables = $data->getTables();
158
159
        $columns = $this->_getColumns();
160
161
        $actions = $this->_getActions();
162
163
        echo $this->printTable($tables, $columns, $actions, $this->table_place, $this->lang['strnotables']);
164
        $attr = [
165
            'href' => [
166
                'url' => 'tables',
167
                'urlvars' => [
168
                    'action' => 'createlike',
169
                    'server' => $this->getRequestParam('server'),
170
                    'database' => $this->getRequestParam('database'),
171
                    'schema' => $this->getRequestParam('schema'),
172
                ],
173
            ],
174
        ];
175
        $navlinks = [
176
            'create' => [
177
                'attr' => [
178
                    'href' => [
179
                        'url' => 'tables',
180
                        'urlvars' => [
181
                            'action' => 'create',
182
                            'server' => $this->getRequestParam('server'),
183
                            'database' => $this->getRequestParam('database'),
184
                            'schema' => $this->getRequestParam('schema'),
185
                        ],
186
                    ],
187
                ],
188
                'content' => $this->lang['strcreatetable'],
189
            ],
190
        ];
191
192
        if ((0 < $tables->recordCount()) && $data->hasCreateTableLike()) {
193
            $navlinks['createlike'] = [
194
                'attr' => [
195
                    'href' => [
196
                        'url' => 'tables',
197
                        'urlvars' => [
198
                            'action' => 'createlike',
199
                            'server' => $this->getRequestParam('server'),
200
                            'database' => $this->getRequestParam('database'),
201
                            'schema' => $this->getRequestParam('schema'),
202
                        ],
203
                    ],
204
                ],
205
                'content' => $this->lang['strcreatetablelike'],
206
            ];
207
        }
208
        $this->printNavLinks($navlinks, 'tables-tables', \get_defined_vars());
209
    }
210
211
    public function displayJson()
212
    {
213
        $data = $this->misc->getDatabaseAccessor();
214
215
        $tables = $data->getTables();
216
217
        $all_tables = $tables->getAll();
218
219
        return $this
220
            ->container
221
            ->response
222
            ->withStatus(200)
223
            ->withJson($all_tables);
224
    }
225
226
    /**
227
     * Generate XML for the browser tree.
228
     *
229
     * @return \Slim\Http\Response|string
230
     */
231
    public function doTree()
232
    {
233
        $data = $this->misc->getDatabaseAccessor();
234
235
        $tables = $data->getTables();
236
237
        $reqvars = $this->misc->getRequestVars('table');
238
239
        $attrs = [
240
            'text' => Decorator::field('relname'),
241
            'icon' => 'Table',
242
            'iconAction' => Decorator::url('display', $reqvars, ['table' => Decorator::field('relname')]),
243
            'toolTip' => Decorator::field('relcomment'),
244
            'action' => Decorator::redirecturl('redirect', $reqvars, ['table' => Decorator::field('relname')]),
245
            'branch' => Decorator::url('tables', $reqvars, ['action' => 'subtree', 'table' => Decorator::field('relname')]),
246
        ];
247
248
        return $this->printTree($tables, $attrs, 'tables');
249
    }
250
251
    /**
252
     * @return \Slim\Http\Response|string
253
     */
254
    public function doSubTree()
255
    {
256
        $tabs = $this->misc->getNavTabs('table');
257
        $items = $this->adjustTabsForTree($tabs);
258
        $reqvars = $this->misc->getRequestVars('table');
259
260
        $attrs = [
261
            'text' => Decorator::field('title'),
262
            'icon' => Decorator::field('icon'),
263
            'action' => Decorator::actionurl(
264
                Decorator::field('url'),
265
                $reqvars,
266
                Decorator::field('urlvars'),
267
                ['table' => $_REQUEST['table']]
268
            ),
269
            'branch' => Decorator::ifempty(
270
                Decorator::field('branch'),
271
                '',
272
                Decorator::url(
273
                    Decorator::field('url'),
274
                    $reqvars,
275
                    [
276
                        'action' => 'tree',
277
                        'table' => $_REQUEST['table'],
278
                    ]
279
                )
280
            ),
281
        ];
282
283
        return $this->printTree($items, $attrs, 'table');
284
    }
285
286
    /**
287
     * Displays a screen where they can enter a new table.
288
     *
289
     * @param mixed $msg
290
     */
291
    public function doCreate($msg = ''): void
292
    {
293
        $data = $this->misc->getDatabaseAccessor();
294
295
        if (!isset($_REQUEST['stage'])) {
296
            $_REQUEST['stage'] = 1;
297
            $default_with_oids = $data->getDefaultWithOid();
298
299
            if ('off' === $default_with_oids) {
300
                $_REQUEST['withoutoids'] = 'on';
301
            }
302
        }
303
304
        $this->coalesceArr($_REQUEST, 'name', '');
305
306
        $this->coalesceArr($_REQUEST, 'fields', '');
307
308
        $this->coalesceArr($_REQUEST, 'tblcomment', '');
309
310
        $this->coalesceArr($_REQUEST, 'spcname', '');
311
        $tablespaces = null;
312
313
        switch ($_REQUEST['stage']) {
314
            case 1:
315
                // You are presented with a form in which you describe the table, pick the tablespace and state how many fields it will have
316
                // Fetch all tablespaces from the database
317
                if ($data->hasTablespaces()) {
318
                    $tablespaces = $data->getTablespaces();
319
                }
320
321
                $this->printTrail('schema');
322
                $this->printTitle($this->lang['strcreatetable'], 'pg.table.create');
323
                $this->printMsg($msg);
324
325
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/' . $this->script . '" method="post">';
326
                echo \PHP_EOL;
327
                echo '<table>' . \PHP_EOL;
328
                echo \sprintf(
329
                    '	<tr>
330
		<th class="data left required">%s</th>',
331
                    $this->lang['strname']
332
                ) . \PHP_EOL;
333
                echo \sprintf(
334
                    '		<td class="data"><input name="name" size="32" maxlength="%s" value="',
335
                    $data->_maxNameLen
336
                ),
337
                    \htmlspecialchars($_REQUEST['name']),
338
                    "\" /></td>\n\t</tr>" . \PHP_EOL;
339
                echo \sprintf(
340
                    '	<tr>
341
		<th class="data left required">%s</th>',
342
                    $this->lang['strnumcols']
343
                ) . \PHP_EOL;
344
                echo \sprintf(
345
                    '		<td class="data"><input name="fields" size="5" maxlength="%s" value="',
346
                    $data->_maxNameLen
347
                ),
348
                    \htmlspecialchars($_REQUEST['fields']),
349
                    "\" /></td>\n\t</tr>" . \PHP_EOL;
350
                echo \sprintf(
351
                    '	<tr>
352
		<th class="data left">%s</th>',
353
                    $this->lang['stroptions']
354
                ) . \PHP_EOL;
355
                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...
356
357
                // Tablespace (if there are any)
358
                if ($data->hasTablespaces() && 0 < $tablespaces->recordCount()) {
359
                    echo \sprintf(
360
                        '	<tr>
361
		<th class="data left">%s</th>',
362
                        $this->lang['strtablespace']
363
                    ) . \PHP_EOL;
364
                    echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"spcname\">" . \PHP_EOL;
365
                    // Always offer the default (empty) option
366
                    echo "\t\t\t\t<option value=\"\"",
367
                        ('' === $_REQUEST['spcname']) ? ' selected="selected"' : '',
368
                        '></option>' . \PHP_EOL;
369
                    // Display all other tablespaces
370
                    while (!$tablespaces->EOF) {
371
                        $spcname = \htmlspecialchars($tablespaces->fields['spcname']);
372
                        echo \sprintf(
373
                            '				<option value="%s"',
374
                            $spcname
375
                        ),
376
                            ($tablespaces->fields['spcname'] === $_REQUEST['spcname']) ? ' selected="selected"' : '',
377
                            \sprintf(
378
                                '>%s</option>',
379
                                $spcname
380
                            ) . \PHP_EOL;
381
                        $tablespaces->moveNext();
382
                    }
383
                    echo "\t\t\t</select>\n\t\t</td>\n\t</tr>" . \PHP_EOL;
384
                }
385
386
                echo \sprintf(
387
                    '	<tr>
388
		<th class="data left">%s</th>',
389
                    $this->lang['strcomment']
390
                ) . \PHP_EOL;
391
                echo "\t\t<td><textarea name=\"tblcomment\" rows=\"3\" cols=\"32\">",
392
                    \htmlspecialchars($_REQUEST['tblcomment']),
393
                    "</textarea></td>\n\t</tr>" . \PHP_EOL;
394
395
                echo '</table>' . \PHP_EOL;
396
                echo '<p><input type="hidden" name="action" value="create" />' . \PHP_EOL;
397
                echo '<input type="hidden" name="stage" value="2" />' . \PHP_EOL;
398
                echo $this->view->form;
399
                echo \sprintf(
400
                    '<input type="submit" value="%s" />',
401
                    $this->lang['strnext']
402
                ) . \PHP_EOL;
403
                echo \sprintf(
404
                    '<input type="submit" name="cancel" value="%s"  /></p>%s',
405
                    $this->lang['strcancel'],
406
                    \PHP_EOL
407
                );
408
                echo '</form>' . \PHP_EOL;
409
410
                break;
411
            case 2:
412
                // Check inputs
413
                $fields = (int) (\trim($_REQUEST['fields']));
414
415
                if ('' === \trim($_REQUEST['name'])) {
416
                    $_REQUEST['stage'] = 1;
417
                    $this->doCreate($this->lang['strtableneedsname']);
418
419
                    return;
420
                }
421
422
                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...
423
                    $_REQUEST['stage'] = 1;
424
                    $this->doCreate($this->lang['strtableneedscols']);
425
426
                    return;
427
                }
428
429
                $types = $data->getTypes(true, false, true);
430
                $types_for_js = [];
431
432
                $this->printTrail('schema');
433
                $this->printTitle($this->lang['strcreatetable'], 'pg.table.create');
434
                $this->printMsg($msg);
435
436
                echo '<script src="' . \containerInstance()->subFolder . '/assets/js/tables.js" type="text/javascript"></script>';
437
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
438
439
                // Output table header
440
                echo '<table>' . \PHP_EOL;
441
                echo \sprintf(
442
                    '	<tr><th colspan="2" class="data required">%s</th><th colspan="2" class="data required">%s</th>',
443
                    $this->lang['strcolumn'],
444
                    $this->lang['strtype']
445
                );
446
                echo \sprintf(
447
                    '<th class="data">%s</th><th class="data">%s</th>',
448
                    $this->lang['strlength'],
449
                    $this->lang['strnotnull']
450
                );
451
                echo \sprintf(
452
                    '<th class="data">%s</th><th class="data">%s</th>',
453
                    $this->lang['struniquekey'],
454
                    $this->lang['strprimarykey']
455
                );
456
                echo \sprintf(
457
                    '<th class="data">%s</th><th class="data">%s</th></tr>',
458
                    $this->lang['strdefault'],
459
                    $this->lang['strcomment']
460
                ) . \PHP_EOL;
461
462
                for ($i = 0; $i < $_REQUEST['fields']; ++$i) {
463
                    if (!isset($_REQUEST['field'][$i])) {
464
                        $_REQUEST['field'][$i] = '';
465
                    }
466
467
                    if (!isset($_REQUEST['length'][$i])) {
468
                        $_REQUEST['length'][$i] = '';
469
                    }
470
471
                    if (!isset($_REQUEST['default'][$i])) {
472
                        $_REQUEST['default'][$i] = '';
473
                    }
474
475
                    if (!isset($_REQUEST['colcomment'][$i])) {
476
                        $_REQUEST['colcomment'][$i] = '';
477
                    }
478
479
                    echo "\t<tr>\n\t\t<td>", $i + 1, '.&nbsp;</td>' . \PHP_EOL;
480
                    echo \sprintf(
481
                        '		<td><input name="field[%s]" size="16" maxlength="%s" value="',
482
                        $i,
483
                        $data->_maxNameLen
484
                    ),
485
                        \htmlspecialchars($_REQUEST['field'][$i]),
486
                        '" /></td>' . \PHP_EOL;
487
                    echo \sprintf(
488
                        '		<td>
489
			<select name="type[%s]" class="select2" id="types%s" onchange="checkLengths(this.options[this.selectedIndex].value,%s);">',
490
                        $i,
491
                        $i,
492
                        $i
493
                    ) . \PHP_EOL;
494
                    // Output any "magic" types
495
                    foreach ($data->extraTypes as $v) {
496
                        $types_for_js[\mb_strtolower($v)] = 1;
497
                        echo "\t\t\t\t<option value=\"", \htmlspecialchars($v), '"',
498
                            (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] === $v) ? ' selected="selected"' : '',
499
                            '>',
500
                            $this->misc->printVal($v),
501
                            '</option>' . \PHP_EOL;
502
                    }
503
                    $types->moveFirst();
504
505
                    while (!$types->EOF) {
506
                        $typname = $types->fields['typname'];
507
                        $types_for_js[$typname] = 1;
508
                        echo "\t\t\t\t<option value=\"", \htmlspecialchars($typname), '"',
509
                            (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] === $typname) ? ' selected="selected"' : '',
510
                            '>',
511
                            $this->misc->printVal($typname),
512
                            '</option>' . \PHP_EOL;
513
                        $types->moveNext();
514
                    }
515
                    echo "\t\t\t</select>\n\t\t\n";
516
517
                    if (0 === $i) {
518
                        // only define js types array once
519
                        $predefined_size_types = \array_intersect($data->predefined_size_types, \array_keys($types_for_js));
520
                        $escaped_predef_types = []; // the JS escaped array elements
521
522
                        foreach ($predefined_size_types as $value) {
523
                            $escaped_predef_types[] = \sprintf(
524
                                '\'%s\'',
525
                                $value
526
                            );
527
                        }
528
                        echo '<script type="text/javascript">predefined_lengths = new Array(' . \implode(',', $escaped_predef_types) . ");</script>\n\t</td>";
529
                    }
530
531
                    // Output array type selector
532
                    echo \sprintf(
533
                        '		<td>
534
			<select name="array[%s]">',
535
                        $i
536
                    ) . \PHP_EOL;
537
                    echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && '' === $_REQUEST['array'][$i]) ? ' selected="selected"' : '', '></option>' . \PHP_EOL;
538
                    echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && '[]' === $_REQUEST['array'][$i]) ? ' selected="selected"' : '', '>[ ]</option>' . \PHP_EOL;
539
                    echo "\t\t\t</select>\n\t\t</td>" . \PHP_EOL;
540
541
                    echo \sprintf(
542
                        '		<td><input name="length[%s]" id="lengths%s" size="10" value="',
543
                        $i,
544
                        $i
545
                    ),
546
                        \htmlspecialchars($_REQUEST['length'][$i]),
547
                        '" /></td>' . \PHP_EOL;
548
                    echo \sprintf(
549
                        '		<td><input type="checkbox" name="notnull[%s]"',
550
                        $i
551
                    ), (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', ' /></td>' . \PHP_EOL;
552
                    echo \sprintf(
553
                        '		<td style="text-align: center"><input type="checkbox" name="uniquekey[%s]"',
554
                        $i
555
                    )
556
                        . (isset($_REQUEST['uniquekey'][$i]) ? ' checked="checked"' : '') . ' /></td>' . \PHP_EOL;
557
                    echo \sprintf(
558
                        '		<td style="text-align: center"><input type="checkbox" name="primarykey[%s]" ',
559
                        $i
560
                    )
561
                        . (isset($_REQUEST['primarykey'][$i]) ? ' checked="checked"' : '')
562
                        . ' /></td>' . \PHP_EOL;
563
                    echo \sprintf(
564
                        '		<td><input name="default[%s]" size="20" value="',
565
                        $i
566
                    ),
567
                        \htmlspecialchars($_REQUEST['default'][$i]),
568
                        '" /></td>' . \PHP_EOL;
569
                    echo \sprintf(
570
                        '		<td><input name="colcomment[%s]" size="40" value="',
571
                        $i
572
                    ),
573
                        \htmlspecialchars($_REQUEST['colcomment'][$i]),
574
                        \sprintf(
575
                            '" />
576
						<script type="text/javascript">checkLengths(document.getElementById(\'types%s\').value,%s);</script>
577
						</td>
578
	</tr>',
579
                            $i,
580
                            $i
581
                        ) . \PHP_EOL;
582
                }
583
                echo '</table>' . \PHP_EOL;
584
                echo '<p><input type="hidden" name="action" value="create" />' . \PHP_EOL;
585
                echo '<input type="hidden" name="stage" value="3" />' . \PHP_EOL;
586
                echo $this->view->form;
587
                echo '<input type="hidden" name="name" value="', \htmlspecialchars($_REQUEST['name']), '" />' . \PHP_EOL;
588
                echo '<input type="hidden" name="fields" value="', \htmlspecialchars($_REQUEST['fields']), '" />' . \PHP_EOL;
589
590
                if (isset($_REQUEST['withoutoids'])) {
591
                    echo '<input type="hidden" name="withoutoids" value="true" />' . \PHP_EOL;
592
                }
593
                echo '<input type="hidden" name="tblcomment" value="', \htmlspecialchars($_REQUEST['tblcomment']), '" />' . \PHP_EOL;
594
595
                if (isset($_REQUEST['spcname'])) {
596
                    echo '<input type="hidden" name="spcname" value="', \htmlspecialchars($_REQUEST['spcname']), '" />' . \PHP_EOL;
597
                }
598
                echo \sprintf(
599
                    '<input type="submit" value="%s" />',
600
                    $this->lang['strcreate']
601
                ) . \PHP_EOL;
602
                echo \sprintf(
603
                    '<input type="submit" name="cancel" value="%s"  /></p>%s',
604
                    $this->lang['strcancel'],
605
                    \PHP_EOL
606
                );
607
                echo '</form>' . \PHP_EOL;
608
609
                break;
610
            case 3:
611
                $this->coalesceArr($_REQUEST, 'notnull', []);
612
613
                $this->coalesceArr($_REQUEST, 'uniquekey', []);
614
615
                $this->coalesceArr($_REQUEST, 'primarykey', []);
616
617
                $this->coalesceArr($_REQUEST, 'length', []);
618
619
                // Default tablespace to null if it isn't set
620
                $this->coalesceArr($_REQUEST, 'spcname', null);
621
622
                // Check inputs
623
                $fields = (int) (\trim($_REQUEST['fields']));
624
625
                if ('' === \trim($_REQUEST['name'])) {
626
                    $_REQUEST['stage'] = 1;
627
                    $this->doCreate($this->lang['strtableneedsname']);
628
629
                    return;
630
                }
631
632
                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...
633
                    $_REQUEST['stage'] = 1;
634
                    $this->doCreate($this->lang['strtableneedscols']);
635
636
                    return;
637
                }
638
639
                $status = $data->createTable(
640
                    $_REQUEST['name'],
641
                    $_REQUEST['fields'],
642
                    $_REQUEST['field'],
643
                    $_REQUEST['type'],
644
                    $_REQUEST['array'],
645
                    $_REQUEST['length'],
646
                    $_REQUEST['notnull'],
647
                    $_REQUEST['default'],
648
                    isset($_REQUEST['withoutoids']),
649
                    $_REQUEST['colcomment'],
650
                    $_REQUEST['tblcomment'],
651
                    $_REQUEST['spcname'],
652
                    $_REQUEST['uniquekey'],
653
                    $_REQUEST['primarykey']
654
                );
655
656
                if (0 === $status) {
657
                    $this->view->setReloadBrowser(true);
658
659
                    $this->doDefault($this->lang['strtablecreated']);
660
661
                    return;
662
                }
663
664
                if (-1 === $status) {
665
                    $_REQUEST['stage'] = 2;
666
                    $this->doCreate($this->lang['strtableneedsfield']);
667
668
                    return;
669
                }
670
                $_REQUEST['stage'] = 2;
671
                $this->doCreate($this->lang['strtablecreatedbad']);
672
673
                return;
674
675
                break;
676
677
            default:
678
                echo \sprintf(
679
                    '<p>%s</p>',
680
                    $this->lang['strinvalidparam']
681
                ) . \PHP_EOL;
682
        }
683
    }
684
685
    /**
686
     * Dsiplay a screen where user can create a table from an existing one.
687
     * We don't have to check if pg supports schema cause create table like
688
     * is available under pg 7.4+ which has schema.
689
     *
690
     * @param mixed $confirm
691
     * @param mixed $msg
692
     */
693
    public function doCreateLike($confirm, $msg = ''): void
694
    {
695
        $data = $this->misc->getDatabaseAccessor();
696
697
        if (!$confirm) {
698
            $this->coalesceArr($_REQUEST, 'name', '');
699
700
            $this->coalesceArr($_REQUEST, 'like', '');
701
702
            $this->coalesceArr($_REQUEST, 'tablespace', '');
703
704
            $this->printTrail('schema');
705
            $this->printTitle($this->lang['strcreatetable'], 'pg.table.create');
706
            $this->printMsg($msg);
707
708
            $tbltmp = $data->getAllTables();
709
            $tbltmp = $tbltmp->getArray();
710
711
            $tables = [];
712
            $tblsel = '';
713
714
            foreach ($tbltmp as $a) {
715
                $data->fieldClean($a['nspname']);
716
                $data->fieldClean($a['relname']);
717
                $tables[\sprintf(
718
                    '"%s"."%s"',
719
                    $a['nspname'],
720
                    $a['relname']
721
                )] = \serialize(['schema' => $a['nspname'], 'table' => $a['relname']]);
722
723
                if ($_REQUEST['like'] === $tables[\sprintf(
724
                    '"%s"."%s"',
725
                    $a['nspname'],
726
                    $a['relname']
727
                )]) {
728
                    $tblsel = \htmlspecialchars($tables[\sprintf(
729
                        '"%s"."%s"',
730
                        $a['nspname'],
731
                        $a['relname']
732
                    )]);
733
                }
734
            }
735
736
            unset($tbltmp);
737
738
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
739
            echo \sprintf(
740
                '<table>
741
	<tr>
742
		<th class="data left required">%s</th>',
743
                $this->lang['strname']
744
            ) . \PHP_EOL;
745
            echo \sprintf(
746
                '		<td class="data"><input name="name" size="32" maxlength="%s" value="',
747
                $data->_maxNameLen
748
            ), \htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>" . \PHP_EOL;
749
            echo \sprintf(
750
                '	<tr>
751
		<th class="data left required">%s</th>',
752
                $this->lang['strcreatetablelikeparent']
753
            ) . \PHP_EOL;
754
            echo "\t\t<td class=\"data\">";
755
            echo HTMLController::printCombo($tables, 'like', true, $tblsel, false);
756
            echo "</td>\n\t</tr>" . \PHP_EOL;
757
758
            if ($data->hasTablespaces()) {
759
                $tblsp_ = $data->getTablespaces();
760
761
                if (0 < $tblsp_->recordCount()) {
762
                    $tblsp_ = $tblsp_->getArray();
763
                    $tblsp = [];
764
765
                    foreach ($tblsp_ as $a) {
766
                        $tblsp[$a['spcname']] = $a['spcname'];
767
                    }
768
769
                    echo \sprintf(
770
                        '	<tr>
771
		<th class="data left">%s</th>',
772
                        $this->lang['strtablespace']
773
                    ) . \PHP_EOL;
774
                    echo "\t\t<td class=\"data\">";
775
                    echo HTMLController::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false);
776
                    echo "</td>\n\t</tr>" . \PHP_EOL;
777
                }
778
            }
779
            echo \sprintf(
780
                '	<tr>
781
		<th class="data left">%s</th>
782
		<td class="data">',
783
                $this->lang['stroptions']
784
            );
785
            echo '<label for="withdefaults"><input type="checkbox" id="withdefaults" name="withdefaults"',
786
                isset($_REQUEST['withdefaults']) ? ' checked="checked"' : '',
787
                \sprintf(
788
                    '/>%s</label>',
789
                    $this->lang['strcreatelikewithdefaults']
790
                );
791
792
            if ($data->hasCreateTableLikeWithConstraints()) {
793
                echo '<br /><label for="withconstraints"><input type="checkbox" id="withconstraints" name="withconstraints"',
794
                    isset($_REQUEST['withconstraints']) ? ' checked="checked"' : '',
795
                    \sprintf(
796
                        '/>%s</label>',
797
                        $this->lang['strcreatelikewithconstraints']
798
                    );
799
            }
800
801
            if ($data->hasCreateTableLikeWithIndexes()) {
802
                echo '<br /><label for="withindexes"><input type="checkbox" id="withindexes" name="withindexes"',
803
                    isset($_REQUEST['withindexes']) ? ' checked="checked"' : '',
804
                    \sprintf(
805
                        '/>%s</label>',
806
                        $this->lang['strcreatelikewithindexes']
807
                    );
808
            }
809
            echo "</td>\n\t</tr>" . \PHP_EOL;
810
            echo '</table>';
811
812
            echo '<input type="hidden" name="action" value="confcreatelike" />' . \PHP_EOL;
813
            echo $this->view->form;
814
            echo \sprintf(
815
                '<p><input type="submit" value="%s" />',
816
                $this->lang['strcreate']
817
            ) . \PHP_EOL;
818
            echo \sprintf(
819
                '<input type="submit" name="cancel" value="%s"  /></p>%s',
820
                $this->lang['strcancel'],
821
                \PHP_EOL
822
            );
823
            echo '</form>' . \PHP_EOL;
824
        } else {
825
            if ('' === \trim($_REQUEST['name'])) {
826
                $this->doCreateLike(false, $this->lang['strtableneedsname']);
827
828
                return;
829
            }
830
831
            if ('' === \trim($_REQUEST['like'])) {
832
                $this->doCreateLike(false, $this->lang['strtablelikeneedslike']);
833
834
                return;
835
            }
836
837
            $this->coalesceArr($_REQUEST, 'tablespace', '');
838
839
            $status = $data->createTableLike(
840
                $_REQUEST['name'],
841
                \unserialize($_REQUEST['like']),
842
                isset($_REQUEST['withdefaults']),
843
                isset($_REQUEST['withconstraints']),
844
                isset($_REQUEST['withindexes']),
845
                $_REQUEST['tablespace']
846
            );
847
848
            if (0 === $status) {
849
                $this->view->setReloadBrowser(true);
850
851
                $this->doDefault($this->lang['strtablecreated']);
852
853
                return;
854
            }
855
            $this->doCreateLike(false, $this->lang['strtablecreatedbad']);
856
857
            return;
858
        }
859
    }
860
861
    /**
862
     * Ask for select parameters and perform select.
863
     *
864
     * @param mixed $confirm
865
     * @param mixed $msg
866
     *
867
     * @return null|string
868
     */
869
    public function doSelectRows($confirm, $msg = '')
870
    {
871
        $data = $this->misc->getDatabaseAccessor();
872
873
        if ($confirm) {
874
            $this->printTrail('table');
875
            $this->printTabs('table', 'select');
876
            $this->printMsg($msg);
877
878
            $attrs = $data->getTableAttributes($_REQUEST['table']);
879
880
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/display" method="post" id="selectform">' . \PHP_EOL;
881
882
            if (0 < $attrs->recordCount()) {
883
                // JavaScript for select all feature
884
                echo '<script type="text/javascript">' . \PHP_EOL;
885
                echo "//<![CDATA[\n";
886
                echo "	function selectAll() {\n";
887
                echo "		for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n";
888
                echo "			var e = document.getElementById('selectform').elements[i];\n";
889
                echo "			if (e.name.indexOf('show') == 0) e.checked = document.getElementById('selectform').selectall.checked;\n";
890
                echo "		}\n";
891
                echo "	}\n";
892
                echo '//]]>' . \PHP_EOL;
893
                echo '</script>' . \PHP_EOL;
894
895
                echo '<table>' . \PHP_EOL;
896
897
                // Output table header
898
                echo \sprintf(
899
                    '<tr><th class="data">%s</th><th class="data">%s</th>',
900
                    $this->lang['strshow'],
901
                    $this->lang['strcolumn']
902
                );
903
                echo \sprintf(
904
                    '<th class="data">%s</th><th class="data">%s</th>',
905
                    $this->lang['strtype'],
906
                    $this->lang['stroperator']
907
                );
908
                echo \sprintf(
909
                    '<th class="data">%s</th></tr>',
910
                    $this->lang['strvalue']
911
                );
912
913
                $i = 0;
914
915
                while (!$attrs->EOF) {
916
                    $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']);
917
                    // Set up default value if there isn't one already
918
                    if (!isset($_REQUEST['values'][$attrs->fields['attname']])) {
919
                        $_REQUEST['values'][$attrs->fields['attname']] = null;
920
                    }
921
922
                    if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) {
923
                        $_REQUEST['ops'][$attrs->fields['attname']] = null;
924
                    }
925
926
                    // Continue drawing row
927
                    $id = (0 === ($i % 2) ? '1' : '2');
928
                    echo \sprintf(
929
                        '<tr class="data%s">',
930
                        $id
931
                    ) . \PHP_EOL;
932
                    echo '<td style="white-space:nowrap;">';
933
                    echo '<input type="checkbox" name="show[', \htmlspecialchars($attrs->fields['attname']), ']"',
934
                        isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '',
935
                        ' /></td>';
936
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
937
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>';
938
                    echo '<td style="white-space:nowrap;">';
939
                    echo \sprintf(
940
                        '<select name="ops[%s]">',
941
                        $attrs->fields['attname']
942
                    ) . \PHP_EOL;
943
944
                    foreach (\array_keys($data->selectOps) as $v) {
945
                        echo '<option value="', \htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] === $v) ? ' selected="selected"' : '',
946
                            '>',
947
                            \htmlspecialchars($v),
948
                            '</option>' . \PHP_EOL;
949
                    }
950
                    echo "</select>\n</td>" . \PHP_EOL;
951
                    echo '<td style="white-space:nowrap;">', $data->printField(
952
                        \sprintf(
953
                            'values[%s]',
954
                            $attrs->fields['attname']
955
                        ),
956
                        $_REQUEST['values'][$attrs->fields['attname']],
957
                        $attrs->fields['type']
958
                    ), '</td>';
959
                    echo '</tr>' . \PHP_EOL;
960
                    ++$i;
961
                    $attrs->moveNext();
962
                }
963
                // Select all checkbox
964
                echo \sprintf(
965
                    '<tr><td colspan="5"><input type="checkbox" id="selectall" name="selectall" accesskey="a" onclick="javascript:selectAll()" /><label for="selectall">%s</label></td>',
966
                    $this->lang['strselectallfields']
967
                );
968
                echo '</tr></table>' . \PHP_EOL;
969
            } else {
970
                echo \sprintf(
971
                    '<p>%s</p>',
972
                    $this->lang['strinvalidparam']
973
                ) . \PHP_EOL;
974
            }
975
976
            echo '<p><input type="hidden" name="action" value="selectrows" />' . \PHP_EOL;
977
            echo \sprintf(
978
                '<input type="hidden" name="table" value="%s"  />%s',
979
                \htmlspecialchars($_REQUEST['table']),
980
                \PHP_EOL
981
            );
982
            echo '<input type="hidden" name="subject" value="table" />' . \PHP_EOL;
983
            echo $this->view->form;
984
            echo \sprintf(
985
                '<input type="submit" name="select" accesskey="r" value="%s" />',
986
                $this->lang['strselect']
987
            ) . \PHP_EOL;
988
            echo \sprintf(
989
                '<input type="submit" name="cancel" value="%s"  /></p>%s',
990
                $this->lang['strcancel'],
991
                \PHP_EOL
992
            );
993
            echo '</form>' . \PHP_EOL;
994
995
            return;
996
        }
997
        $this->coalesceArr($_POST, 'show', []);
998
999
        $this->coalesceArr($_POST, 'values', []);
1000
1001
        $this->coalesceArr($_POST, 'nulls', []);
1002
1003
        // Verify that they haven't supplied a value for unary operators
1004
        foreach ($_POST['ops'] as $k => $v) {
1005
            if ('p' === $data->selectOps[$v] && '' !== $_POST['values'][$k]) {
1006
                $this->doSelectRows(true, $this->lang['strselectunary']);
1007
1008
                return;
1009
            }
1010
        }
1011
1012
        if (0 === \count($_POST['show'])) {
1013
            $this->doSelectRows(true, $this->lang['strselectneedscol']);
1014
        } else {
1015
            // Generate query SQL
1016
            $query = $data->getSelectSQL(
1017
                $_REQUEST['table'],
1018
                \array_keys($_POST['show']),
1019
                $_POST['values'],
1020
                $_POST['ops']
1021
            );
1022
            $_REQUEST['query'] = $query;
1023
            $_REQUEST['return'] = 'selectrows';
1024
1025
            $this->setNoOutput(true);
1026
1027
            $display_controller = new DisplayController($this->getContainer());
1028
1029
            return $display_controller->render();
1030
        }
1031
    }
1032
1033
    /**
1034
     * Ask for insert parameters and then actually insert row.
1035
     *
1036
     * @param mixed $msg
1037
     */
1038
    public function formInsertRow($msg = ''): void
1039
    {
1040
        $data = $this->misc->getDatabaseAccessor();
1041
1042
        $this->printTrail('table');
1043
        $this->printTabs('table', 'insert');
1044
1045
        $this->printMsg($msg);
1046
1047
        $attrs = $data->getTableAttributes($_REQUEST['table']);
1048
1049
        $fksprops = $this->_getFKProps();
1050
1051
        $this->coalesceArr($_REQUEST, 'values', []);
1052
        $this->coalesceArr($_REQUEST, 'nulls', []);
1053
        $this->coalesceArr($_REQUEST, 'format', []);
1054
1055
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post" id="ac_form">' . \PHP_EOL;
1056
1057
        if (0 < $attrs->recordCount()) {
1058
            echo '<table>' . \PHP_EOL;
1059
1060
            // Output table header
1061
            echo \sprintf(
1062
                '<tr><th class="data">%s</th><th class="data">%s</th>',
1063
                $this->lang['strcolumn'],
1064
                $this->lang['strtype']
1065
            );
1066
            echo \sprintf(
1067
                '<th class="data">%s</th>',
1068
                $this->lang['strformat']
1069
            );
1070
            echo \sprintf(
1071
                '<th class="data">%s</th><th class="data">%s</th></tr>',
1072
                $this->lang['strnull'],
1073
                $this->lang['strvalue']
1074
            );
1075
1076
            $i = 0;
1077
            $fields = [];
1078
1079
            while (!$attrs->EOF) {
1080
                $fields[$attrs->fields['attnum']] = $attrs->fields['attname'];
1081
                $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']);
1082
                // Set up default value if there isn't one already
1083
                if (!isset($_REQUEST['values'][$attrs->fields['attnum']])) {
1084
                    $_REQUEST['values'][$attrs->fields['attnum']] = $attrs->fields['adsrc'];
1085
1086
                    if (null === $attrs->fields['adsrc'] && !$attrs->fields['attnotnull']) {
1087
                        $_REQUEST['nulls'][$attrs->fields['attnum']] = true;
1088
                    }
1089
                }
1090
1091
                // Default format to 'VALUE' if there is no default,
1092
                // otherwise default to 'EXPRESSION'
1093
                if (!isset($_REQUEST['format'][$attrs->fields['attnum']])) {
1094
                    $_REQUEST['format'][$attrs->fields['attnum']] = (null === $attrs->fields['adsrc']) ? 'VALUE' : 'EXPRESSION';
1095
                }
1096
1097
                $requested_format = $_REQUEST['format'][$attrs->fields['attnum']];
1098
                // Continue drawing row
1099
                $id = (0 === ($i % 2) ? '1' : '2');
1100
                echo \sprintf(
1101
                    '<tr class="data%s">',
1102
                    $id
1103
                ) . \PHP_EOL;
1104
                echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
1105
                echo '<td style="white-space:nowrap;">' . \PHP_EOL;
1106
                echo $this->misc->printVal(
1107
                    $data->formatType(
1108
                        $attrs->fields['type'],
1109
                        $attrs->fields['atttypmod']
1110
                    )
1111
                );
1112
                echo \sprintf(
1113
                    '<input type="hidden" name="types[%s]" value="',
1114
                    $attrs->fields['attnum']
1115
                ),
1116
                    \htmlspecialchars($attrs->fields['type']),
1117
                    '" /></td>';
1118
                echo '<td style="white-space:nowrap;">' . \PHP_EOL;
1119
1120
                echo \sprintf(
1121
                    '<select name="format[%s]">',
1122
                    $attrs->fields['attnum']
1123
                ) . \PHP_EOL;
1124
                echo \sprintf(
1125
                    '<option value="VALUE" %s >%s</option> %s',
1126
                    ('VALUE' === $requested_format) ? ' selected="selected" ' : '',
1127
                    $this->lang['strvalue'],
1128
                    \PHP_EOL
1129
                );
1130
                echo \sprintf(
1131
                    '<option value="EXPRESSION" %s >%s</option> %s',
1132
                    ('EXPRESSION' === $requested_format) ? ' selected="selected" ' : '',
1133
                    $this->lang['strexpression'],
1134
                    \PHP_EOL
1135
                );
1136
1137
                echo "</select>\n</td>" . \PHP_EOL;
1138
                echo '<td style="white-space:nowrap;">';
1139
                // Output null box if the column allows nulls
1140
                // Edit: if it can be null, then null it is.
1141
                if (!$attrs->fields['attnotnull']) {
1142
                    echo '<label><span>';
1143
                    echo \sprintf(
1144
                        '<input type="checkbox" class="nullcheckbox" name="nulls[%s]" %s />',
1145
                        $attrs->fields['attnum'],
1146
                        ' checked="checked"'
1147
                    );
1148
                    echo '</span></label>';
1149
                }
1150
                echo '</td>';
1151
1152
                echo \sprintf(
1153
                    '<td id="row_att_%s" style="white-space:nowrap;">',
1154
                    $attrs->fields['attnum']
1155
                );
1156
1157
                if ((false !== $fksprops) && isset($fksprops['byfield'][$attrs->fields['attnum']])) {
1158
                    echo $data->printField(
1159
                        \sprintf(
1160
                            'values[%s]',
1161
                            $attrs->fields['attnum']
1162
                        ),
1163
                        $_REQUEST['values'][$attrs->fields['attnum']],
1164
                        'fktype' /*force FK*/,
1165
                        [
1166
                            'id' => \sprintf(
1167
                                'attr_%s',
1168
                                $attrs->fields['attnum']
1169
                            ),
1170
                            'autocomplete' => 'off',
1171
                            'class' => 'insert_row_input',
1172
                        ]
1173
                    );
1174
                } else {
1175
                    echo $data->printField(\sprintf(
1176
                        'values[%s]',
1177
                        $attrs->fields['attnum']
1178
                    ), $_REQUEST['values'][$attrs->fields['attnum']], $attrs->fields['type'], ['class' => 'insert_row_input']);
1179
                }
1180
                echo '</td>' . \PHP_EOL;
1181
                echo '</tr>' . \PHP_EOL;
1182
                ++$i;
1183
                $attrs->moveNext();
1184
            }
1185
            echo '</table>' . \PHP_EOL;
1186
1187
            if (!isset($_SESSION['counter'])) {
1188
                $_SESSION['counter'] = 0;
1189
            }
1190
1191
            echo '<input type="hidden" name="action" value="insertrow" />' . \PHP_EOL;
1192
            echo '<input type="hidden" name="fields" value="', \htmlentities(\serialize($fields), \ENT_QUOTES, 'UTF-8'), '" />' . \PHP_EOL;
1193
            echo '<input type="hidden" name="protection_counter" value="' . $_SESSION['counter'] . '" />' . \PHP_EOL;
1194
            echo \sprintf(
1195
                '<input type="hidden" name="table" value="%s"  />%s',
1196
                \htmlspecialchars($_REQUEST['table']),
1197
                \PHP_EOL
1198
            );
1199
            echo \sprintf(
1200
                '<p><input type="submit" name="insert" value="%s" />',
1201
                $this->lang['strinsert']
1202
            ) . \PHP_EOL;
1203
            echo \sprintf(
1204
                '<input type="submit" name="insertandrepeat" accesskey="r" value="%s" />',
1205
                $this->lang['strinsertandrepeat']
1206
            ) . \PHP_EOL;
1207
1208
            if (false !== $fksprops) {
1209
                if ('default off' !== $this->conf['autocomplete']) {
1210
                    echo \sprintf(
1211
                        '<input type="checkbox" id="no_ac" value="1" checked="checked" /><label for="no_ac">%s</label>',
1212
                        $this->lang['strac']
1213
                    ) . \PHP_EOL;
1214
                } else {
1215
                    echo \sprintf(
1216
                        '<input type="checkbox" id="no_ac" value="0" /><label for="no_ac">%s</label>',
1217
                        $this->lang['strac']
1218
                    ) . \PHP_EOL;
1219
                }
1220
            }
1221
            echo '</p>' . \PHP_EOL;
1222
        } else {
1223
            echo \sprintf(
1224
                '<p>%s</p>',
1225
                $this->lang['strnofieldsforinsert']
1226
            ) . \PHP_EOL;
1227
        }
1228
        echo $this->view->form;
1229
        echo '</form>' . \PHP_EOL;
1230
        echo \sprintf(
1231
            '<button class="btn btn-mini btn_back" style="float: right;        margin-right: 4em;        margin-top: -3em;">%s</button>%s',
1232
            $this->lang['strcancel'],
1233
            \PHP_EOL
1234
        );
1235
        echo '<script src="' . \containerInstance()->subFolder . '/assets/js/insert_or_edit_row.js" type="text/javascript"></script>';
1236
    }
1237
1238
    /**
1239
     * Performs insertion of row according to request parameters.
1240
     */
1241
    public function doInsertRow()
1242
    {
1243
        $data = $this->misc->getDatabaseAccessor();
1244
1245
        $this->coalesceArr($_POST, 'values', []);
1246
1247
        $this->coalesceArr($_POST, 'nulls', []);
1248
1249
        $_POST['fields'] = \unserialize(\htmlspecialchars_decode($_POST['fields'], \ENT_QUOTES));
1250
1251
        if ($_SESSION['counter']++ === (int) ($_POST['protection_counter'])) {
1252
            $status = $data->insertRow($_POST['table'], $_POST['fields'], $_POST['values'], $_POST['nulls'], $_POST['format'], $_POST['types']);
1253
1254
            if (0 === $status) {
1255
                if (isset($_POST['insert'])) {
1256
                    $this->doDefault($this->lang['strrowinserted']);
1257
1258
                    return;
1259
                }
1260
                $_REQUEST['values'] = [];
1261
                $_REQUEST['nulls'] = [];
1262
1263
                return $this->formInsertRow($this->lang['strrowinserted']);
1264
            }
1265
1266
            return $this->formInsertRow($this->lang['strrowinsertedbad']);
1267
        }
1268
1269
        return $this->formInsertRow($this->lang['strrowduplicate']);
1270
    }
1271
1272
    /**
1273
     * Show confirmation of empty and perform actual empty.
1274
     *
1275
     * @param mixed $confirm
1276
     */
1277
    public function doEmpty($confirm): void
1278
    {
1279
        $data = $this->misc->getDatabaseAccessor();
1280
1281
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1282
            $this->doDefault($this->lang['strspecifytabletoempty']);
1283
1284
            return;
1285
        }
1286
1287
        if ($confirm) {
1288
            if (isset($_REQUEST['ma'])) {
1289
                $this->printTrail('schema');
1290
                $this->printTitle($this->lang['strempty'], 'pg.table.empty');
1291
1292
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1293
1294
                foreach ($_REQUEST['ma'] as $v) {
1295
                    $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
1296
                    echo '<p>' . \sprintf(
1297
                        $this->lang['strconfemptytable'],
1298
                        $this->misc->printVal($a['table'])
1299
                    );
1300
1301
                    echo '</p>' . \PHP_EOL;
1302
                    \printf('<input type="hidden" name="table[]" value="%s" />', \htmlspecialchars($a['table']));
1303
                } //  END mutli empty
1304
            } else {
1305
                $this->printTrail('table');
1306
                $this->printTitle($this->lang['strempty'], 'pg.table.empty');
1307
1308
                echo '<p>', \sprintf(
1309
                    $this->lang['strconfemptytable'],
1310
                    $this->misc->printVal($_REQUEST['table'])
1311
                ), '</p>' . \PHP_EOL;
1312
1313
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1314
1315
                echo \sprintf(
1316
                    '<input type="hidden" name="table" value="%s"  />%s',
1317
                    \htmlspecialchars($_REQUEST['table']),
1318
                    \PHP_EOL
1319
                );
1320
                // END not mutli empty
1321
            }
1322
            echo \sprintf(
1323
                '<input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label>',
1324
                $this->lang['strcascade']
1325
            );
1326
            echo '<input type="hidden" name="action" value="empty" />' . \PHP_EOL;
1327
            echo $this->view->form;
1328
            echo \sprintf(
1329
                '<input type="submit" name="empty" value="%s" /> <input type="submit" name="cancel" value="%s" />',
1330
                $this->lang['strempty'],
1331
                $this->lang['strcancel']
1332
            ) . \PHP_EOL;
1333
            echo "</form>\n"; //  END if confirm
1334
        } else {
1335
            // Do Empty
1336
            $msg = '';
1337
1338
            if (\is_array($_REQUEST['table'])) {
1339
                foreach ($_REQUEST['table'] as $t) {
1340
                    [$status, $sql] = $data->emptyTable($t, isset($_POST['cascade']));
1341
1342
                    if (0 === $status) {
1343
                        $msg .= \sprintf(
1344
                            '%s<br />',
1345
                            $sql
1346
                        );
1347
                        $msg .= \sprintf(
1348
                            '%s: %s<br />',
1349
                            \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1350
                            $this->lang['strtableemptied']
1351
                        );
1352
                    } else {
1353
                        $this->doDefault(\sprintf(
1354
                            '%s%s: %s<br />',
1355
                            $msg,
1356
                            \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1357
                            $this->lang['strtableemptiedbad']
1358
                        ));
1359
1360
                        return;
1361
                    }
1362
                }
1363
                $this->doDefault($msg); //  END mutli empty
1364
            } else {
1365
                [$status, $sql] = $data->emptyTable($_POST['table'], isset($_POST['cascade']));
1366
1367
                if (0 === $status) {
1368
                    $msg .= \sprintf(
1369
                        '%s<br />',
1370
                        $sql
1371
                    );
1372
                    $msg .= \sprintf(
1373
                        '%s: %s<br />',
1374
                        \htmlentities($_POST['table'], \ENT_QUOTES, 'UTF-8'),
1375
                        $this->lang['strtableemptied']
1376
                    );
1377
1378
                    $this->doDefault($msg);
1379
                }
1380
1381
                $this->doDefault($sql . '<br>' . $this->lang['strtableemptiedbad']);
1382
                // END not mutli empty
1383
            }
1384
            // END do Empty
1385
        }
1386
    }
1387
1388
    /**
1389
     * Show confirmation of drop and perform actual drop.
1390
     *
1391
     * @param mixed $confirm
1392
     */
1393
    public function doDrop($confirm): void
1394
    {
1395
        $data = $this->misc->getDatabaseAccessor();
1396
1397
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1398
            $this->doDefault($this->lang['strspecifytabletodrop']);
1399
1400
            return;
1401
        }
1402
1403
        if ($confirm) {
1404
            //If multi drop
1405
            if (isset($_REQUEST['ma'])) {
1406
                $this->printTrail('schema');
1407
                $this->printTitle($this->lang['strdrop'], 'pg.table.drop');
1408
1409
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1410
1411
                foreach ($_REQUEST['ma'] as $v) {
1412
                    $a = \unserialize(\htmlspecialchars_decode($v, \ENT_QUOTES));
1413
                    echo '<p>', \sprintf(
1414
                        $this->lang['strconfdroptable'],
1415
                        $this->misc->printVal($a['table'])
1416
                    ), '</p>' . \PHP_EOL;
1417
                    \printf('<input type="hidden" name="table[]" value="%s" />', \htmlspecialchars($a['table']));
1418
                }
1419
            } else {
1420
                $this->printTrail('table');
1421
                $this->printTitle($this->lang['strdrop'], 'pg.table.drop');
1422
1423
                echo '<p>', \sprintf(
1424
                    $this->lang['strconfdroptable'],
1425
                    $this->misc->printVal($_REQUEST['table'])
1426
                ), '</p>' . \PHP_EOL;
1427
1428
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/tables" method="post">' . \PHP_EOL;
1429
                echo \sprintf(
1430
                    '<input type="hidden" name="table" value="%s"  />%s',
1431
                    \htmlspecialchars($_REQUEST['table']),
1432
                    \PHP_EOL
1433
                );
1434
                // END if multi drop
1435
            }
1436
1437
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
1438
            echo $this->view->form;
1439
            echo \sprintf(
1440
                '<p><input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label></p>',
1441
                $this->lang['strcascade']
1442
            ) . \PHP_EOL;
1443
            echo \sprintf(
1444
                '<input type="submit" name="drop" value="%s" />',
1445
                $this->lang['strdrop']
1446
            ) . \PHP_EOL;
1447
            echo \sprintf(
1448
                '<input type="submit" name="cancel" value="%s" />',
1449
                $this->lang['strcancel']
1450
            ) . \PHP_EOL;
1451
            echo "</form>\n"; //  END confirm
1452
        } else {
1453
            //If multi drop
1454
            if (\is_array($_REQUEST['table'])) {
1455
                $msg = '';
1456
                $status = $data->beginTransaction();
1457
1458
                if (0 === $status) {
1459
                    foreach ($_REQUEST['table'] as $t) {
1460
                        $status = $data->dropTable($t, isset($_POST['cascade']));
1461
1462
                        if (0 === $status) {
1463
                            $msg .= \sprintf(
1464
                                '%s: %s<br />',
1465
                                \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1466
                                $this->lang['strtabledropped']
1467
                            );
1468
                        } else {
1469
                            $data->endTransaction();
1470
1471
                            $this->doDefault(\sprintf(
1472
                                '%s%s: %s<br />',
1473
                                $msg,
1474
                                \htmlentities($t, \ENT_QUOTES, 'UTF-8'),
1475
                                $this->lang['strtabledroppedbad']
1476
                            ));
1477
1478
                            return;
1479
                        }
1480
                    }
1481
                }
1482
1483
                if (0 === $data->endTransaction()) {
1484
                    // Everything went fine, back to the Default page....
1485
                    $this->view->setReloadBrowser(true);
1486
1487
                    $this->doDefault($msg);
1488
1489
                    return;
1490
                }
1491
1492
                $this->doDefault($this->lang['strtabledroppedbad']);
1493
1494
                return;
1495
            }
1496
            $status = $data->dropTable($_POST['table'], isset($_POST['cascade']));
1497
1498
            if (0 === $status) {
1499
                $this->view->setReloadBrowser(true);
1500
1501
                $this->doDefault($this->lang['strtabledropped']);
1502
1503
                return;
1504
            }
1505
1506
            $this->doDefault($this->lang['strtabledroppedbad']);
1507
1508
            return;
1509
            // END DROP
1510
        }
1511
    }
1512
1513
    /**
1514
     * @return (mixed|string|string[])[][]
1515
     *
1516
     * @psalm-return array{table: array{title: mixed, field: mixed, url: string, vars: array{table: string}}, owner: array{title: mixed, field: mixed}, tablespace: array{title: mixed, field: mixed}, tuples: array{title: mixed, field: mixed, type: string}, table_size: array{title: mixed, field: mixed}, actions: array{title: mixed}, comment: array{title: mixed, field: mixed}}
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 190 characters; contains 376 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...
1517
     */
1518
    private function _getColumns()
1519
    {
1520
        return [
1521
            'table' => [
1522
                'title' => $this->lang['strtable'],
1523
                'field' => Decorator::field('relname'),
1524
                'url' => \containerInstance()->subFolder . \sprintf(
1525
                    '/redirect/table?%s&amp;',
1526
                    $this->misc->href
1527
                ),
1528
                'vars' => ['table' => 'relname'],
1529
            ],
1530
            'owner' => [
1531
                'title' => $this->lang['strowner'],
1532
                'field' => Decorator::field('relowner'),
1533
            ],
1534
            'tablespace' => [
1535
                'title' => $this->lang['strtablespace'],
1536
                'field' => Decorator::field('tablespace'),
1537
            ],
1538
            'tuples' => [
1539
                'title' => $this->lang['strestimatedrowcount'],
1540
                'field' => Decorator::field('reltuples'),
1541
                'type' => 'numeric',
1542
            ],
1543
            'table_size' => [
1544
                'title' => $this->lang['strsize'],
1545
                'field' => Decorator::field('table_size'),
1546
            ],
1547
            'actions' => [
1548
                'title' => $this->lang['stractions'],
1549
            ],
1550
            'comment' => [
1551
                'title' => $this->lang['strcomment'],
1552
                'field' => Decorator::field('relcomment'),
1553
            ],
1554
        ];
1555
    }
1556
1557
    /**
1558
     * @return ((((mixed|string)[]|string)[]|string)[]|mixed|string)[][]
1559
     *
1560
     * @psalm-return array{multiactions: array{keycols: array{table: string}, url: string, default: string}, browse: array{content: mixed, attr: array{href: array{url: string, urlvars: array{subject: string, return: string, table: mixed}}}}, select: array{content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}, insert: array{content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}, empty: array{multiaction: string, content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}, alter: array{content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}, drop: array{multiaction: string, content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}, vacuum: array{multiaction: string, content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}, analyze: array{multiaction: string, content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}, reindex: array{multiaction: string, content: mixed, attr: array{href: array{url: string, urlvars: array{action: string, table: mixed}}}}}
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 190 characters; contains 1272 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...
1561
     */
1562
    private function _getActions()
1563
    {
1564
        return [
1565
            'multiactions' => [
1566
                'keycols' => ['table' => 'relname'],
1567
                'url' => 'tables',
1568
                'default' => 'analyze',
1569
            ],
1570
            'browse' => [
1571
                'content' => $this->lang['strbrowse'],
1572
                'attr' => [
1573
                    'href' => [
1574
                        'url' => 'display',
1575
                        'urlvars' => [
1576
                            'subject' => 'table',
1577
                            'return' => 'table',
1578
                            'table' => Decorator::field('relname'),
1579
                        ],
1580
                    ],
1581
                ],
1582
            ],
1583
            'select' => [
1584
                'content' => $this->lang['strselect'],
1585
                'attr' => [
1586
                    'href' => [
1587
                        'url' => 'tables',
1588
                        'urlvars' => [
1589
                            'action' => 'confselectrows',
1590
                            'table' => Decorator::field('relname'),
1591
                        ],
1592
                    ],
1593
                ],
1594
            ],
1595
            'insert' => [
1596
                'content' => $this->lang['strinsert'],
1597
                'attr' => [
1598
                    'href' => [
1599
                        'url' => 'tables',
1600
                        'urlvars' => [
1601
                            'action' => 'confinsertrow',
1602
                            'table' => Decorator::field('relname'),
1603
                        ],
1604
                    ],
1605
                ],
1606
            ],
1607
            'empty' => [
1608
                'multiaction' => 'confirm_empty',
1609
                'content' => $this->lang['strempty'],
1610
                'attr' => [
1611
                    'href' => [
1612
                        'url' => 'tables',
1613
                        'urlvars' => [
1614
                            'action' => 'confirm_empty',
1615
                            'table' => Decorator::field('relname'),
1616
                        ],
1617
                    ],
1618
                ],
1619
            ],
1620
            'alter' => [
1621
                'content' => $this->lang['stralter'],
1622
                'attr' => [
1623
                    'href' => [
1624
                        'url' => 'tblproperties',
1625
                        'urlvars' => [
1626
                            'action' => 'confirm_alter',
1627
                            'table' => Decorator::field('relname'),
1628
                        ],
1629
                    ],
1630
                ],
1631
            ],
1632
            'drop' => [
1633
                'multiaction' => 'confirm_drop',
1634
                'content' => $this->lang['strdrop'],
1635
                'attr' => [
1636
                    'href' => [
1637
                        'url' => 'tables',
1638
                        'urlvars' => [
1639
                            'action' => 'confirm_drop',
1640
                            'table' => Decorator::field('relname'),
1641
                        ],
1642
                    ],
1643
                ],
1644
            ],
1645
            'vacuum' => [
1646
                'multiaction' => 'confirm_vacuum',
1647
                'content' => $this->lang['strvacuum'],
1648
                'attr' => [
1649
                    'href' => [
1650
                        'url' => 'tables',
1651
                        'urlvars' => [
1652
                            'action' => 'confirm_vacuum',
1653
                            'table' => Decorator::field('relname'),
1654
                        ],
1655
                    ],
1656
                ],
1657
            ],
1658
            'analyze' => [
1659
                'multiaction' => 'confirm_analyze',
1660
                'content' => $this->lang['stranalyze'],
1661
                'attr' => [
1662
                    'href' => [
1663
                        'url' => 'tables',
1664
                        'urlvars' => [
1665
                            'action' => 'confirm_analyze',
1666
                            'table' => Decorator::field('relname'),
1667
                        ],
1668
                    ],
1669
                ],
1670
            ],
1671
            'reindex' => [
1672
                'multiaction' => 'confirm_reindex',
1673
                'content' => $this->lang['strreindex'],
1674
                'attr' => [
1675
                    'href' => [
1676
                        'url' => 'tables',
1677
                        'urlvars' => [
1678
                            'action' => 'confirm_reindex',
1679
                            'table' => Decorator::field('relname'),
1680
                        ],
1681
                    ],
1682
                ],
1683
            ],
1684
            //'cluster' TODO ?
1685
        ];
1686
    }
1687
1688
    // END Function
1689
}
1690