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

TablesController::doInsertRow()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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