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

TablesController::doTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class TablesController extends BaseController
15
{
16
    use AdminTrait;
17
    public $script          = 'tables.php';
18
    public $controller_name = 'TablesController';
19
    public $table_place     = 'tables-tables';
20
21
    /**
22
     * Default method to render the controller according to the action parameter.
23
     */
24
    public function render()
25
    {
26
        $lang   = $this->lang;
27
        $action = $this->action;
28
29
        if ('tree' == $action) {
30
            return $this->doTree();
31
        }
32
        if ('subtree' == $action) {
33
            return $this->doSubTree();
34
        }
35
36
        $header_template = 'header.twig';
37
        $footer_template = 'footer.twig';
0 ignored issues
show
Unused Code introduced by
The assignment to $footer_template is dead and can be removed.
Loading history...
38
39
        ob_start();
40
41
        switch ($action) {
42
            case 'create':
43
44
                if (isset($_POST['cancel'])) {
45
                    $this->doDefault();
46
                } else {
47
                    $header_template = 'header_select2.twig';
48
                    $this->doCreate();
49
                }
50
51
                break;
52
            case 'createlike':
53
                $header_template = 'header_select2.twig';
54
                $this->doCreateLike(false);
55
56
                break;
57
            case 'confcreatelike':
58
                if (isset($_POST['cancel'])) {
59
                    $header_template = 'header_datatables.twig';
60
                    $this->doDefault();
61
                } else {
62
                    //$header_template = 'header_select2.twig';
63
                    $this->doCreateLike(true);
64
                }
65
66
                break;
67
            case 'selectrows':
68
                if (!isset($_POST['cancel'])) {
69
                    $this->doSelectRows(false);
70
                } else {
71
                    $header_template = 'header_datatables.twig';
72
                    $this->doDefault();
73
                }
74
75
                break;
76
            case 'confselectrows':
77
                $this->doSelectRows(true);
78
79
                break;
80
            case 'insertrow':
81
                if (!isset($_POST['cancel'])) {
82
                    $this->doInsertRow(false);
83
                } else {
84
                    $header_template = 'header_datatables.twig';
85
                    $this->doDefault();
86
                }
87
88
                break;
89
            case 'confinsertrow':
90
                $this->doInsertRow(true);
91
92
                break;
93
            case 'empty':
94
                if (isset($_POST['empty'])) {
95
                    $this->doEmpty(false);
96
                } else {
97
                    $header_template = 'header_datatables.twig';
98
                    $this->doDefault();
99
                }
100
101
                break;
102
            case 'confirm_empty':
103
                $this->doEmpty(true);
104
105
                break;
106
            case 'drop':
107
                if (isset($_POST['drop'])) {
108
                    $this->doDrop(false);
109
                } else {
110
                    $header_template = 'header_datatables.twig';
111
                    $this->doDefault();
112
                }
113
114
                break;
115
            case 'confirm_drop':
116
                $this->doDrop(true);
117
118
                break;
119
            default:
120
                if (false === $this->adminActions($action, 'table')) {
121
                    $header_template = 'header_datatables.twig';
122
                    $this->doDefault();
123
                }
124
125
                break;
126
        }
127
128
        $output = ob_get_clean();
129
130
        $this->printHeader($lang['strtables'], null, true, $header_template);
131
        $this->printBody();
132
133
        echo $output;
134
135
        return $this->printFooter();
136
    }
137
138
    /**
139
     * Show default list of tables in the database.
140
     *
141
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
142
     */
143
    public function doDefault($msg = '')
144
    {
145
        $lang = $this->lang;
146
        $data = $this->misc->getDatabaseAccessor();
147
148
        $this->printTrail('schema');
149
        $this->printTabs('schema', 'tables');
150
        $this->printMsg($msg);
151
152
        $tables = $data->getTables();
153
154
        $columns = [
155
            'table'      => [
156
                'title' => $lang['strtable'],
157
                'field' => Decorator::field('relname'),
158
                'url'   => \SUBFOLDER . "/redirect/table?{$this->misc->href}&amp;",
159
                'vars'  => ['table' => 'relname'],
160
            ],
161
            'owner'      => [
162
                'title' => $lang['strowner'],
163
                'field' => Decorator::field('relowner'),
164
            ],
165
            'tablespace' => [
166
                'title' => $lang['strtablespace'],
167
                'field' => Decorator::field('tablespace'),
168
            ],
169
            'tuples'     => [
170
                'title' => $lang['strestimatedrowcount'],
171
                'field' => Decorator::field('reltuples'),
172
                'type'  => 'numeric',
173
            ],
174
            'actions'    => [
175
                'title' => $lang['stractions'],
176
            ],
177
            'comment'    => [
178
                'title' => $lang['strcomment'],
179
                'field' => Decorator::field('relcomment'),
180
            ],
181
        ];
182
183
        $actions = [
184
            'multiactions' => [
185
                'keycols' => ['table' => 'relname'],
186
                'url'     => 'tables.php',
187
                'default' => 'analyze',
188
            ],
189
            'browse'       => [
190
                'content' => $lang['strbrowse'],
191
                'attr'    => [
192
                    'href' => [
193
                        'url'     => 'display.php',
194
                        'urlvars' => [
195
                            'subject' => 'table',
196
                            'return'  => 'table',
197
                            'table'   => Decorator::field('relname'),
198
                        ],
199
                    ],
200
                ],
201
            ],
202
            'select'       => [
203
                'content' => $lang['strselect'],
204
                'attr'    => [
205
                    'href' => [
206
                        'url'     => 'tables.php',
207
                        'urlvars' => [
208
                            'action' => 'confselectrows',
209
                            'table'  => Decorator::field('relname'),
210
                        ],
211
                    ],
212
                ],
213
            ],
214
            'insert'       => [
215
                'content' => $lang['strinsert'],
216
                'attr'    => [
217
                    'href' => [
218
                        'url'     => 'tables.php',
219
                        'urlvars' => [
220
                            'action' => 'confinsertrow',
221
                            'table'  => Decorator::field('relname'),
222
                        ],
223
                    ],
224
                ],
225
            ],
226
            'empty'        => [
227
                'multiaction' => 'confirm_empty',
228
                'content'     => $lang['strempty'],
229
                'attr'        => [
230
                    'href' => [
231
                        'url'     => 'tables.php',
232
                        'urlvars' => [
233
                            'action' => 'confirm_empty',
234
                            'table'  => Decorator::field('relname'),
235
                        ],
236
                    ],
237
                ],
238
            ],
239
            'alter'        => [
240
                'content' => $lang['stralter'],
241
                'attr'    => [
242
                    'href' => [
243
                        'url'     => 'tblproperties.php',
244
                        'urlvars' => [
245
                            'action' => 'confirm_alter',
246
                            'table'  => Decorator::field('relname'),
247
                        ],
248
                    ],
249
                ],
250
            ],
251
            'drop'         => [
252
                'multiaction' => 'confirm_drop',
253
                'content'     => $lang['strdrop'],
254
                'attr'        => [
255
                    'href' => [
256
                        'url'     => 'tables.php',
257
                        'urlvars' => [
258
                            'action' => 'confirm_drop',
259
                            'table'  => Decorator::field('relname'),
260
                        ],
261
                    ],
262
                ],
263
            ],
264
            'vacuum'       => [
265
                'multiaction' => 'confirm_vacuum',
266
                'content'     => $lang['strvacuum'],
267
                'attr'        => [
268
                    'href' => [
269
                        'url'     => 'tables.php',
270
                        'urlvars' => [
271
                            'action' => 'confirm_vacuum',
272
                            'table'  => Decorator::field('relname'),
273
                        ],
274
                    ],
275
                ],
276
            ],
277
            'analyze'      => [
278
                'multiaction' => 'confirm_analyze',
279
                'content'     => $lang['stranalyze'],
280
                'attr'        => [
281
                    'href' => [
282
                        'url'     => 'tables.php',
283
                        'urlvars' => [
284
                            'action' => 'confirm_analyze',
285
                            'table'  => Decorator::field('relname'),
286
                        ],
287
                    ],
288
                ],
289
            ],
290
            'reindex'      => [
291
                'multiaction' => 'confirm_reindex',
292
                'content'     => $lang['strreindex'],
293
                'attr'        => [
294
                    'href' => [
295
                        'url'     => 'tables.php',
296
                        'urlvars' => [
297
                            'action' => 'confirm_reindex',
298
                            'table'  => Decorator::field('relname'),
299
                        ],
300
                    ],
301
                ],
302
            ],
303
            //'cluster' TODO ?
304
        ];
305
306
        if (!$data->hasTablespaces()) {
307
            unset($columns['tablespace']);
308
        }
309
310
        //\Kint::dump($tables);
311
312
        echo $this->printTable($tables, $columns, $actions, $this->table_place, $lang['strnotables']);
313
314
        $navlinks = [
315
            'create' => [
316
                'attr'    => [
317
                    'href' => [
318
                        'url'     => 'tables.php',
319
                        'urlvars' => [
320
                            'action'   => 'create',
321
                            'server'   => $_REQUEST['server'],
322
                            'database' => $_REQUEST['database'],
323
                            'schema'   => $_REQUEST['schema'],
324
                        ],
325
                    ],
326
                ],
327
                'content' => $lang['strcreatetable'],
328
            ],
329
        ];
330
331
        if (($tables->recordCount() > 0) && $data->hasCreateTableLike()) {
332
            $navlinks['createlike'] = [
333
                'attr'    => [
334
                    'href' => [
335
                        'url'     => 'tables.php',
336
                        'urlvars' => [
337
                            'action'   => 'createlike',
338
                            'server'   => $_REQUEST['server'],
339
                            'database' => $_REQUEST['database'],
340
                            'schema'   => $_REQUEST['schema'],
341
                        ],
342
                    ],
343
                ],
344
                'content' => $lang['strcreatetablelike'],
345
            ];
346
        }
347
        $this->printNavLinks($navlinks, 'tables-tables', get_defined_vars());
348
    }
349
350
    /**
351
     * Generate XML for the browser tree.
352
     */
353
    public function doTree()
354
    {
355
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
356
        $data = $this->misc->getDatabaseAccessor();
357
358
        //\PC::debug($this->misc->getDatabase(), 'getDatabase');
359
360
        $tables = $data->getTables();
361
362
        $reqvars = $this->misc->getRequestVars('table');
363
364
        $attrs = [
365
            'text'       => Decorator::field('relname'),
366
            'icon'       => 'Table',
367
            'iconAction' => Decorator::url('display.php', $reqvars, ['table' => Decorator::field('relname')]),
368
            'toolTip'    => Decorator::field('relcomment'),
369
            'action'     => Decorator::redirecturl('redirect.php', $reqvars, ['table' => Decorator::field('relname')]),
370
            'branch'     => Decorator::url('tables.php', $reqvars, ['action' => 'subtree', 'table' => Decorator::field('relname')]),
371
        ];
372
373
        return $this->printTree($tables, $attrs, 'tables');
374
    }
375
376
    public function doSubTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
377
    {
378
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
379
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
380
381
        $tabs    = $this->misc->getNavTabs('table');
382
        $items   = $this->adjustTabsForTree($tabs);
383
        $reqvars = $this->misc->getRequestVars('table');
384
385
        $attrs = [
386
            'text'   => Decorator::field('title'),
387
            'icon'   => Decorator::field('icon'),
388
            'action' => Decorator::actionurl(
389
                Decorator::field('url'),
390
                $reqvars,
391
                Decorator::field('urlvars'),
392
                ['table' => $_REQUEST['table']]
393
            ),
394
            'branch' => Decorator::ifempty(
395
                Decorator::field('branch'),
396
                '',
397
                Decorator::url(
398
                    Decorator::field('url'),
399
                    $reqvars,
400
                    [
401
                        'action' => 'tree',
402
                        'table'  => $_REQUEST['table'],
403
                    ]
404
                )
405
            ),
406
        ];
407
408
        return $this->printTree($items, $attrs, 'table');
409
    }
410
411
    /**
412
     * Displays a screen where they can enter a new table.
413
     *
414
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
415
     */
416
    public function doCreate($msg = '')
417
    {
418
        $lang = $this->lang;
419
        $data = $this->misc->getDatabaseAccessor();
420
421
        if (!isset($_REQUEST['stage'])) {
422
            $_REQUEST['stage'] = 1;
423
            $default_with_oids = $data->getDefaultWithOid();
424
            if ('off' == $default_with_oids) {
425
                $_REQUEST['withoutoids'] = 'on';
426
            }
427
        }
428
429
        if (!isset($_REQUEST['name'])) {
430
            $_REQUEST['name'] = '';
431
        }
432
433
        if (!isset($_REQUEST['fields'])) {
434
            $_REQUEST['fields'] = '';
435
        }
436
437
        if (!isset($_REQUEST['tblcomment'])) {
438
            $_REQUEST['tblcomment'] = '';
439
        }
440
441
        if (!isset($_REQUEST['spcname'])) {
442
            $_REQUEST['spcname'] = '';
443
        }
444
445
        switch ($_REQUEST['stage']) {
446
            case 1:
447
                // You are presented with a form in which you describe the table, pick the tablespace and state how many fields it will have
448
                // Fetch all tablespaces from the database
449
                if ($data->hasTablespaces()) {
450
                    $tablespaces = $data->getTablespaces();
451
                }
452
453
                $this->printTrail('schema');
454
                $this->printTitle($lang['strcreatetable'], 'pg.table.create');
455
                $this->printMsg($msg);
456
457
                echo '<form action="' . \SUBFOLDER . '/src/views/' . $this->script . '" method="post">';
458
                echo "\n";
459
                echo "<table>\n";
460
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
461
                echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
462
                htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n";
463
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strnumcols']}</th>\n";
464
                echo "\t\t<td class=\"data\"><input name=\"fields\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"",
465
                htmlspecialchars($_REQUEST['fields']), "\" /></td>\n\t</tr>\n";
466
                echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stroptions']}</th>\n";
467
                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>\n";
468
469
                // Tablespace (if there are any)
470
                if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tablespaces does not seem to be defined for all execution paths leading up to this point.
Loading history...
471
                    echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
472
                    echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"spcname\">\n";
473
                    // Always offer the default (empty) option
474
                    echo "\t\t\t\t<option value=\"\"",
475
                    ('' == $_REQUEST['spcname']) ? ' selected="selected"' : '', "></option>\n";
476
                    // Display all other tablespaces
477
                    while (!$tablespaces->EOF) {
478
                        $spcname = htmlspecialchars($tablespaces->fields['spcname']);
479
                        echo "\t\t\t\t<option value=\"{$spcname}\"",
480
                        ($tablespaces->fields['spcname'] == $_REQUEST['spcname']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
481
                        $tablespaces->moveNext();
482
                    }
483
                    echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
484
                }
485
486
                echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
487
                echo "\t\t<td><textarea name=\"tblcomment\" rows=\"3\" cols=\"32\">",
488
                htmlspecialchars($_REQUEST['tblcomment']), "</textarea></td>\n\t</tr>\n";
489
490
                echo "</table>\n";
491
                echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n";
492
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
493
                echo $this->misc->form;
494
                echo "<input type=\"submit\" value=\"{$lang['strnext']}\" />\n";
495
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
496
                echo "</form>\n";
497
498
                break;
499
            case 2:
500
501
                // Check inputs
502
                $fields = trim($_REQUEST['fields']);
503
                if ('' == trim($_REQUEST['name'])) {
504
                    $_REQUEST['stage'] = 1;
505
                    $this->doCreate($lang['strtableneedsname']);
506
507
                    return;
508
                }
509
                if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields < 1) {
510
                    $_REQUEST['stage'] = 1;
511
                    $this->doCreate($lang['strtableneedscols']);
512
513
                    return;
514
                }
515
516
                $types        = $data->getTypes(true, false, true);
517
                $types_for_js = [];
518
519
                $this->printTrail('schema');
520
                $this->printTitle($lang['strcreatetable'], 'pg.table.create');
521
                $this->printMsg($msg);
522
523
                echo '<script src="' . \SUBFOLDER . '/js/tables.js" type="text/javascript"></script>';
524
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
525
526
                // Output table header
527
                echo "<table>\n";
528
                echo "\t<tr><th colspan=\"2\" class=\"data required\">{$lang['strcolumn']}</th><th colspan=\"2\" class=\"data required\">{$lang['strtype']}</th>";
529
                echo "<th class=\"data\">{$lang['strlength']}</th><th class=\"data\">{$lang['strnotnull']}</th>";
530
                echo "<th class=\"data\">{$lang['struniquekey']}</th><th class=\"data\">{$lang['strprimarykey']}</th>";
531
                echo "<th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>\n";
532
533
                for ($i = 0; $i < $_REQUEST['fields']; ++$i) {
534
                    if (!isset($_REQUEST['field'][$i])) {
535
                        $_REQUEST['field'][$i] = '';
536
                    }
537
538
                    if (!isset($_REQUEST['length'][$i])) {
539
                        $_REQUEST['length'][$i] = '';
540
                    }
541
542
                    if (!isset($_REQUEST['default'][$i])) {
543
                        $_REQUEST['default'][$i] = '';
544
                    }
545
546
                    if (!isset($_REQUEST['colcomment'][$i])) {
547
                        $_REQUEST['colcomment'][$i] = '';
548
                    }
549
550
                    echo "\t<tr>\n\t\t<td>", $i + 1, ".&nbsp;</td>\n";
551
                    echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
552
                    htmlspecialchars($_REQUEST['field'][$i]), "\" /></td>\n";
553
                    echo "\t\t<td>\n\t\t\t<select name=\"type[{$i}]\" class=\"select2\" id=\"types{$i}\" onchange=\"checkLengths(this.options[this.selectedIndex].value,{$i});\">\n";
554
                    // Output any "magic" types
555
                    foreach ($data->extraTypes as $v) {
556
                        $types_for_js[strtolower($v)] = 1;
557
                        echo "\t\t\t\t<option value=\"", htmlspecialchars($v), '"',
558
                        (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] == $v) ? ' selected="selected"' : '', '>',
559
                        $this->misc->printVal($v), "</option>\n";
560
                    }
561
                    $types->moveFirst();
562
                    while (!$types->EOF) {
563
                        $typname                = $types->fields['typname'];
564
                        $types_for_js[$typname] = 1;
565
                        echo "\t\t\t\t<option value=\"", htmlspecialchars($typname), '"',
566
                        (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] == $typname) ? ' selected="selected"' : '', '>',
567
                        $this->misc->printVal($typname), "</option>\n";
568
                        $types->moveNext();
569
                    }
570
                    echo "\t\t\t</select>\n\t\t\n";
571
                    if (0 == $i) {
572
                        // only define js types array once
573
                        $predefined_size_types = array_intersect($data->predefined_size_types, array_keys($types_for_js));
574
                        $escaped_predef_types  = []; // the JS escaped array elements
575
                        foreach ($predefined_size_types as $value) {
576
                            $escaped_predef_types[] = "'{$value}'";
577
                        }
578
                        echo '<script type="text/javascript">predefined_lengths = new Array(' . implode(',', $escaped_predef_types) . ");</script>\n\t</td>";
579
                    }
580
581
                    // Output array type selector
582
                    echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">\n";
583
                    echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '') ? ' selected="selected"' : '', "></option>\n";
584
                    echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
585
                    echo "\t\t\t</select>\n\t\t</td>\n";
586
587
                    echo "\t\t<td><input name=\"length[{$i}]\" id=\"lengths{$i}\" size=\"10\" value=\"",
588
                    htmlspecialchars($_REQUEST['length'][$i]), "\" /></td>\n";
589
                    echo "\t\t<td><input type=\"checkbox\" name=\"notnull[{$i}]\"", (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', " /></td>\n";
590
                    echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"uniquekey[{$i}]\""
591
                        . (isset($_REQUEST['uniquekey'][$i]) ? ' checked="checked"' : '') . " /></td>\n";
592
                    echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"primarykey[{$i}]\" "
593
                        . (isset($_REQUEST['primarykey'][$i]) ? ' checked="checked"' : '')
594
                        . " /></td>\n";
595
                    echo "\t\t<td><input name=\"default[{$i}]\" size=\"20\" value=\"",
596
                    htmlspecialchars($_REQUEST['default'][$i]), "\" /></td>\n";
597
                    echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"",
598
                    htmlspecialchars($_REQUEST['colcomment'][$i]), "\" />
599
						<script type=\"text/javascript\">checkLengths(document.getElementById('types{$i}').value,{$i});</script>
600
						</td>\n\t</tr>\n";
601
                }
602
                echo "</table>\n";
603
                echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n";
604
                echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n";
605
                echo $this->misc->form;
606
                echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n";
607
                echo '<input type="hidden" name="fields" value="', htmlspecialchars($_REQUEST['fields']), "\" />\n";
608
                if (isset($_REQUEST['withoutoids'])) {
609
                    echo "<input type=\"hidden\" name=\"withoutoids\" value=\"true\" />\n";
610
                }
611
                echo '<input type="hidden" name="tblcomment" value="', htmlspecialchars($_REQUEST['tblcomment']), "\" />\n";
612
                if (isset($_REQUEST['spcname'])) {
613
                    echo '<input type="hidden" name="spcname" value="', htmlspecialchars($_REQUEST['spcname']), "\" />\n";
614
                }
615
                echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
616
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
617
                echo "</form>\n";
618
619
                break;
620
            case 3:
621
622
                if (!isset($_REQUEST['notnull'])) {
623
                    $_REQUEST['notnull'] = [];
624
                }
625
626
                if (!isset($_REQUEST['uniquekey'])) {
627
                    $_REQUEST['uniquekey'] = [];
628
                }
629
630
                if (!isset($_REQUEST['primarykey'])) {
631
                    $_REQUEST['primarykey'] = [];
632
                }
633
634
                if (!isset($_REQUEST['length'])) {
635
                    $_REQUEST['length'] = [];
636
                }
637
638
                // Default tablespace to null if it isn't set
639
                if (!isset($_REQUEST['spcname'])) {
640
                    $_REQUEST['spcname'] = null;
641
                }
642
643
                // Check inputs
644
                $fields = trim($_REQUEST['fields']);
645
                if ('' == trim($_REQUEST['name'])) {
646
                    $_REQUEST['stage'] = 1;
647
                    $this->doCreate($lang['strtableneedsname']);
648
649
                    return;
650
                }
651
                if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields <= 0) {
652
                    $_REQUEST['stage'] = 1;
653
                    $this->doCreate($lang['strtableneedscols']);
654
655
                    return;
656
                }
657
658
                $status = $data->createTable(
659
                    $_REQUEST['name'],
660
                    $_REQUEST['fields'],
661
                    $_REQUEST['field'],
662
                    $_REQUEST['type'],
663
                    $_REQUEST['array'],
664
                    $_REQUEST['length'],
665
                    $_REQUEST['notnull'],
666
                    $_REQUEST['default'],
667
                    isset($_REQUEST['withoutoids']),
668
                    $_REQUEST['colcomment'],
669
                    $_REQUEST['tblcomment'],
670
                    $_REQUEST['spcname'],
671
                    $_REQUEST['uniquekey'],
672
                    $_REQUEST['primarykey']
673
                );
674
675
                if (0 == $status) {
676
                    $this->misc->setReloadBrowser(true);
677
678
                    return $this->doDefault($lang['strtablecreated']);
679
                }
680
                if ($status == -1) {
681
                    $_REQUEST['stage'] = 2;
682
                    $this->doCreate($lang['strtableneedsfield']);
683
684
                    return;
685
                }
686
                $_REQUEST['stage'] = 2;
687
                $this->doCreate($lang['strtablecreatedbad']);
688
689
                return;
690
                break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
691
            default:
692
                echo "<p>{$lang['strinvalidparam']}</p>\n";
693
        }
694
    }
695
696
    /**
697
     * Dsiplay a screen where user can create a table from an existing one.
698
     * We don't have to check if pg supports schema cause create table like
699
     * is available under pg 7.4+ which has schema.
700
     *
701
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
702
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
703
     */
704
    public function doCreateLike($confirm, $msg = '')
705
    {
706
        $lang = $this->lang;
707
        $data = $this->misc->getDatabaseAccessor();
708
709
        if (!$confirm) {
710
            if (!isset($_REQUEST['name'])) {
711
                $_REQUEST['name'] = '';
712
            }
713
714
            if (!isset($_REQUEST['like'])) {
715
                $_REQUEST['like'] = '';
716
            }
717
718
            if (!isset($_REQUEST['tablespace'])) {
719
                $_REQUEST['tablespace'] = '';
720
            }
721
722
            $this->printTrail('schema');
723
            $this->printTitle($lang['strcreatetable'], 'pg.table.create');
724
            $this->printMsg($msg);
725
726
            $tbltmp = $data->getTables(true);
727
            $tbltmp = $tbltmp->getArray();
728
729
            $tables = [];
730
            $tblsel = '';
731
            foreach ($tbltmp as $a) {
732
                $data->fieldClean($a['nspname']);
733
                $data->fieldClean($a['relname']);
734
                $tables["\"{$a['nspname']}\".\"{$a['relname']}\""] = serialize(['schema' => $a['nspname'], 'table' => $a['relname']]);
735
                if ($_REQUEST['like'] == $tables["\"{$a['nspname']}\".\"{$a['relname']}\""]) {
736
                    $tblsel = htmlspecialchars($tables["\"{$a['nspname']}\".\"{$a['relname']}\""]);
737
                }
738
            }
739
740
            unset($tbltmp);
741
742
            echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
743
            echo "<table>\n\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
744
            echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n";
745
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strcreatetablelikeparent']}</th>\n";
746
            echo "\t\t<td class=\"data\">";
747
            echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tables, 'like', true, $tblsel, false);
748
            echo "</td>\n\t</tr>\n";
749
            if ($data->hasTablespaces()) {
750
                $tblsp_ = $data->getTablespaces();
751
                if ($tblsp_->recordCount() > 0) {
752
                    $tblsp_ = $tblsp_->getArray();
753
                    $tblsp  = [];
754
                    foreach ($tblsp_ as $a) {
755
                        $tblsp[$a['spcname']] = $a['spcname'];
756
                    }
757
758
                    echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
759
                    echo "\t\t<td class=\"data\">";
760
                    echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false);
761
                    echo "</td>\n\t</tr>\n";
762
                }
763
            }
764
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stroptions']}</th>\n\t\t<td class=\"data\">";
765
            echo '<label for="withdefaults"><input type="checkbox" id="withdefaults" name="withdefaults"',
766
            isset($_REQUEST['withdefaults']) ? ' checked="checked"' : '',
767
                "/>{$lang['strcreatelikewithdefaults']}</label>";
768
            if ($data->hasCreateTableLikeWithConstraints()) {
769
                echo '<br /><label for="withconstraints"><input type="checkbox" id="withconstraints" name="withconstraints"',
770
                isset($_REQUEST['withconstraints']) ? ' checked="checked"' : '',
771
                    "/>{$lang['strcreatelikewithconstraints']}</label>";
772
            }
773
            if ($data->hasCreateTableLikeWithIndexes()) {
774
                echo '<br /><label for="withindexes"><input type="checkbox" id="withindexes" name="withindexes"',
775
                isset($_REQUEST['withindexes']) ? ' checked="checked"' : '',
776
                    "/>{$lang['strcreatelikewithindexes']}</label>";
777
            }
778
            echo "</td>\n\t</tr>\n";
779
            echo '</table>';
780
781
            echo "<input type=\"hidden\" name=\"action\" value=\"confcreatelike\" />\n";
782
            echo $this->misc->form;
783
            echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
784
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
785
            echo "</form>\n";
786
        } else {
787
            if ('' == trim($_REQUEST['name'])) {
788
                $this->doCreateLike(false, $lang['strtableneedsname']);
789
790
                return;
791
            }
792
            if ('' == trim($_REQUEST['like'])) {
793
                $this->doCreateLike(false, $lang['strtablelikeneedslike']);
794
795
                return;
796
            }
797
798
            if (!isset($_REQUEST['tablespace'])) {
799
                $_REQUEST['tablespace'] = '';
800
            }
801
802
            $status = $data->createTableLike(
803
                $_REQUEST['name'],
804
                unserialize($_REQUEST['like']),
805
                isset($_REQUEST['withdefaults']),
806
                isset($_REQUEST['withconstraints']),
807
                isset($_REQUEST['withindexes']),
808
                $_REQUEST['tablespace']
809
            );
810
811
            if (0 == $status) {
812
                $this->misc->setReloadBrowser(true);
813
814
                return $this->doDefault($lang['strtablecreated']);
815
            }
816
            $this->doCreateLike(false, $lang['strtablecreatedbad']);
817
818
            return;
819
        }
820
    }
821
822
    /**
823
     * Ask for select parameters and perform select.
824
     *
825
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
826
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
827
     */
828
    public function doSelectRows($confirm, $msg = '')
829
    {
830
        $lang = $this->lang;
831
        $data = $this->misc->getDatabaseAccessor();
832
833
        if ($confirm) {
834
            $this->printTrail('table');
835
            $this->printTabs('table', 'select');
836
            $this->printMsg($msg);
837
838
            $attrs = $data->getTableAttributes($_REQUEST['table']);
839
840
            echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\" id=\"selectform\">\n";
841
            if ($attrs->recordCount() > 0) {
842
                // JavaScript for select all feature
843
                echo "<script type=\"text/javascript\">\n";
844
                echo "//<![CDATA[\n";
845
                echo "	function selectAll() {\n";
846
                echo "		for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n";
847
                echo "			var e = document.getElementById('selectform').elements[i];\n";
848
                echo "			if (e.name.indexOf('show') == 0) e.checked = document.getElementById('selectform').selectall.checked;\n";
849
                echo "		}\n";
850
                echo "	}\n";
851
                echo "//]]>\n";
852
                echo "</script>\n";
853
854
                echo "<table>\n";
855
856
                // Output table header
857
                echo "<tr><th class=\"data\">{$lang['strshow']}</th><th class=\"data\">{$lang['strcolumn']}</th>";
858
                echo "<th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['stroperator']}</th>";
859
                echo "<th class=\"data\">{$lang['strvalue']}</th></tr>";
860
861
                $i = 0;
862
                while (!$attrs->EOF) {
863
                    $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']);
864
                    // Set up default value if there isn't one already
865
                    if (!isset($_REQUEST['values'][$attrs->fields['attname']])) {
866
                        $_REQUEST['values'][$attrs->fields['attname']] = null;
867
                    }
868
869
                    if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) {
870
                        $_REQUEST['ops'][$attrs->fields['attname']] = null;
871
                    }
872
873
                    // Continue drawing row
874
                    $id = (0 == ($i % 2) ? '1' : '2');
875
                    echo "<tr class=\"data{$id}\">\n";
876
                    echo '<td style="white-space:nowrap;">';
877
                    echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"',
878
                    isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>';
879
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
880
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>';
881
                    echo '<td style="white-space:nowrap;">';
882
                    echo "<select name=\"ops[{$attrs->fields['attname']}]\">\n";
883
                    foreach (array_keys($data->selectOps) as $v) {
884
                        echo '<option value="', htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] == $v) ? ' selected="selected"' : '',
885
                        '>', htmlspecialchars($v), "</option>\n";
886
                    }
887
                    echo "</select>\n</td>\n";
888
                    echo '<td style="white-space:nowrap;">', $data->printField(
889
                        "values[{$attrs->fields['attname']}]",
890
                        $_REQUEST['values'][$attrs->fields['attname']],
891
                        $attrs->fields['type']
892
                    ), '</td>';
893
                    echo "</tr>\n";
894
                    ++$i;
895
                    $attrs->moveNext();
896
                }
897
                // Select all checkbox
898
                echo "<tr><td colspan=\"5\"><input type=\"checkbox\" id=\"selectall\" name=\"selectall\" accesskey=\"a\" onclick=\"javascript:selectAll()\" /><label for=\"selectall\">{$lang['strselectallfields']}</label></td>";
899
                echo "</tr></table>\n";
900
            } else {
901
                echo "<p>{$lang['strinvalidparam']}</p>\n";
902
            }
903
904
            echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n";
905
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
906
            echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
907
            echo $this->misc->form;
908
            echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$lang['strselect']}\" />\n";
909
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
910
            echo "</form>\n";
911
912
            return;
913
        }
914
        if (!isset($_POST['show'])) {
915
            $_POST['show'] = [];
916
        }
917
918
        if (!isset($_POST['values'])) {
919
            $_POST['values'] = [];
920
        }
921
922
        if (!isset($_POST['nulls'])) {
923
            $_POST['nulls'] = [];
924
        }
925
926
        // Verify that they haven't supplied a value for unary operators
927
        foreach ($_POST['ops'] as $k => $v) {
928
            if ('p' == $data->selectOps[$v] && $_POST['values'][$k] != '') {
929
                $this->doSelectRows(true, $lang['strselectunary']);
930
931
                return;
932
            }
933
        }
934
935
        if (0 == sizeof($_POST['show'])) {
936
            $this->doSelectRows(true, $lang['strselectneedscol']);
937
        } else {
938
            // Generate query SQL
939
            $query = $data->getSelectSQL(
940
                $_REQUEST['table'],
941
                array_keys($_POST['show']),
942
                $_POST['values'],
943
                $_POST['ops']
944
            );
945
            $_REQUEST['query']  = $query;
946
            $_REQUEST['return'] = 'selectrows';
947
948
            $this->setNoOutput(true);
949
950
            $display_controller = new DisplayController($this->getContainer());
951
952
            return $display_controller->render();
953
        }
954
    }
955
956
    /**
957
     * Ask for insert parameters and then actually insert row.
958
     *
959
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
960
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
961
     */
962
    public function doInsertRow($confirm, $msg = '')
963
    {
964
        $lang = $this->lang;
965
        $data = $this->misc->getDatabaseAccessor();
966
967
        if ($confirm) {
968
            $this->printTrail('table');
969
            $this->printTabs('table', 'insert');
970
971
            $this->printMsg($msg);
972
973
            $attrs = $data->getTableAttributes($_REQUEST['table']);
974
975
            if (('disable' != $this->conf['autocomplete'])) {
976
                $fksprops = $this->misc->getAutocompleteFKProperties($_REQUEST['table']);
977
                if (false !== $fksprops) {
978
                    echo $fksprops['code'];
979
                }
980
            } else {
981
                $fksprops = false;
982
            }
983
984
            echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\" id=\"ac_form\">\n";
985
            if ($attrs->recordCount() > 0) {
986
                echo "<table>\n";
987
988
                // Output table header
989
                echo "<tr><th class=\"data\">{$lang['strcolumn']}</th><th class=\"data\">{$lang['strtype']}</th>";
990
                echo "<th class=\"data\">{$lang['strformat']}</th>";
991
                echo "<th class=\"data\">{$lang['strnull']}</th><th class=\"data\">{$lang['strvalue']}</th></tr>";
992
993
                $i      = 0;
994
                $fields = [];
995
                while (!$attrs->EOF) {
996
                    $fields[$attrs->fields['attnum']] = $attrs->fields['attname'];
997
                    $attrs->fields['attnotnull']      = $data->phpBool($attrs->fields['attnotnull']);
998
                    // Set up default value if there isn't one already
999
                    if (!isset($_REQUEST['values'][$attrs->fields['attnum']])) {
1000
                        $_REQUEST['values'][$attrs->fields['attnum']] = $attrs->fields['adsrc'];
1001
                    }
1002
1003
                    // Default format to 'VALUE' if there is no default,
1004
                    // otherwise default to 'EXPRESSION'
1005
                    if (!isset($_REQUEST['format'][$attrs->fields['attnum']])) {
1006
                        $_REQUEST['format'][$attrs->fields['attnum']] = (null === $attrs->fields['adsrc']) ? 'VALUE' : 'EXPRESSION';
1007
                    }
1008
1009
                    // Continue drawing row
1010
                    $id = (0 == ($i % 2) ? '1' : '2');
1011
                    echo "<tr class=\"data{$id}\">\n";
1012
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
1013
                    echo "<td style=\"white-space:nowrap;\">\n";
1014
                    echo $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod']));
1015
                    echo "<input type=\"hidden\" name=\"types[{$attrs->fields['attnum']}]\" value=\"",
1016
                    htmlspecialchars($attrs->fields['type']), '" /></td>';
1017
                    echo "<td style=\"white-space:nowrap;\">\n";
1018
                    echo "<select name=\"format[{$attrs->fields['attnum']}]\">\n";
1019
                    echo '<option value="VALUE"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'VALUE') ? ' selected="selected"' : '', ">{$lang['strvalue']}</option>\n";
1020
                    echo '<option value="EXPRESSION"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'EXPRESSION') ? ' selected="selected"' : '', ">{$lang['strexpression']}</option>\n";
1021
                    echo "</select>\n</td>\n";
1022
                    echo '<td style="white-space:nowrap;">';
1023
                    // Output null box if the column allows nulls (doesn't look at CHECKs or ASSERTIONS)
1024
                    if (!$attrs->fields['attnotnull']) {
1025
                        echo "<label><span><input type=\"checkbox\" name=\"nulls[{$attrs->fields['attnum']}]\"",
1026
                        isset($_REQUEST['nulls'][$attrs->fields['attnum']]) ? ' checked="checked"' : '', ' /></span></label></td>';
1027
                    } else {
1028
                        echo '&nbsp;</td>';
1029
                    }
1030
                    echo "<td id=\"row_att_{$attrs->fields['attnum']}\" style=\"white-space:nowrap;\">";
1031
                    if ((false !== $fksprops) && isset($fksprops['byfield'][$attrs->fields['attnum']])) {
1032
                        echo $data->printField(
1033
                            "values[{$attrs->fields['attnum']}]",
1034
                            $_REQUEST['values'][$attrs->fields['attnum']],
1035
                            'fktype' /*force FK*/,
1036
                            [
1037
                                'id'           => "attr_{$attrs->fields['attnum']}",
1038
                                'autocomplete' => 'off',
1039
                            ]
1040
                        );
1041
                    } else {
1042
                        echo $data->printField("values[{$attrs->fields['attnum']}]", $_REQUEST['values'][$attrs->fields['attnum']], $attrs->fields['type']);
1043
                    }
1044
                    echo "</td>\n";
1045
                    echo "</tr>\n";
1046
                    ++$i;
1047
                    $attrs->moveNext();
1048
                }
1049
                echo "</table>\n";
1050
1051
                if (!isset($_SESSION['counter'])) {
1052
                    $_SESSION['counter'] = 0;
1053
                }
1054
1055
                echo "<input type=\"hidden\" name=\"action\" value=\"insertrow\" />\n";
1056
                echo '<input type="hidden" name="fields" value="', htmlentities(serialize($fields), ENT_QUOTES, 'UTF-8'), "\" />\n";
1057
                echo '<input type="hidden" name="protection_counter" value="' . $_SESSION['counter'] . "\" />\n";
1058
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
1059
                echo "<p><input type=\"submit\" name=\"insert\" value=\"{$lang['strinsert']}\" />\n";
1060
                echo "<input type=\"submit\" name=\"insertandrepeat\" accesskey=\"r\" value=\"{$lang['strinsertandrepeat']}\" />\n";
1061
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1062
1063
                if (false !== $fksprops) {
1064
                    if ('default off' != $this->conf['autocomplete']) {
1065
                        echo "<input type=\"checkbox\" id=\"no_ac\" value=\"1\" checked=\"checked\" /><label for=\"no_ac\">{$lang['strac']}</label>\n";
1066
                    } else {
1067
                        echo "<input type=\"checkbox\" id=\"no_ac\" value=\"0\" /><label for=\"no_ac\">{$lang['strac']}</label>\n";
1068
                    }
1069
                }
1070
                echo "</p>\n";
1071
            } else {
1072
                echo "<p>{$lang['strnofieldsforinsert']}</p>\n";
1073
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1074
            }
1075
            echo $this->misc->form;
1076
            echo "</form>\n";
1077
        } else {
1078
            if (!isset($_POST['values'])) {
1079
                $_POST['values'] = [];
1080
            }
1081
1082
            if (!isset($_POST['nulls'])) {
1083
                $_POST['nulls'] = [];
1084
            }
1085
1086
            $_POST['fields'] = unserialize(htmlspecialchars_decode($_POST['fields'], ENT_QUOTES));
1087
1088
            if ($_SESSION['counter']++ == $_POST['protection_counter']) {
1089
                $status = $data->insertRow($_POST['table'], $_POST['fields'], $_POST['values'], $_POST['nulls'], $_POST['format'], $_POST['types']);
1090
                if (0 == $status) {
1091
                    if (isset($_POST['insert'])) {
1092
                        return $this->doDefault($lang['strrowinserted']);
1093
                    }
1094
                    $_REQUEST['values'] = [];
1095
                    $_REQUEST['nulls']  = [];
1096
                    $this->doInsertRow(true, $lang['strrowinserted']);
1097
                } else {
1098
                    $this->doInsertRow(true, $lang['strrowinsertedbad']);
1099
                }
1100
            } else {
1101
                $this->doInsertRow(true, $lang['strrowduplicate']);
1102
            }
1103
        }
1104
    }
1105
1106
    /**
1107
     * Show confirmation of empty and perform actual empty.
1108
     *
1109
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
1110
     */
1111
    public function doEmpty($confirm)
1112
    {
1113
        $lang = $this->lang;
1114
        $data = $this->misc->getDatabaseAccessor();
1115
1116
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1117
            return $this->doDefault($lang['strspecifytabletoempty']);
1118
        }
1119
1120
        if ($confirm) {
1121
            if (isset($_REQUEST['ma'])) {
1122
                $this->printTrail('schema');
1123
                $this->printTitle($lang['strempty'], 'pg.table.empty');
1124
1125
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
1126
                foreach ($_REQUEST['ma'] as $v) {
1127
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
1128
                    echo '<p>' . sprintf($lang['strconfemptytable'], $this->misc->printVal($a['table']));
1129
1130
                    echo "</p>\n";
1131
                    printf('<input type="hidden" name="table[]" value="%s" />', htmlspecialchars($a['table']));
1132
                }
1133
            } // END mutli empty
1134
            else {
0 ignored issues
show
Coding Style introduced by
Expected "} else \n"; found " // END mutli empty\n else {\n"
Loading history...
1135
                $this->printTrail('table');
1136
                $this->printTitle($lang['strempty'], 'pg.table.empty');
1137
1138
                echo '<p>', sprintf($lang['strconfemptytable'], $this->misc->printVal($_REQUEST['table'])), "</p>\n";
1139
1140
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
1141
1142
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
1143
            } // END not mutli empty
1144
            echo "<input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label>";
1145
            echo "<input type=\"hidden\" name=\"action\" value=\"empty\" />\n";
1146
            echo $this->misc->form;
1147
            echo "<input type=\"submit\" name=\"empty\" value=\"{$lang['strempty']}\" /> <input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1148
            echo "</form>\n";
1149
        } // END if confirm
1150
        else {
0 ignored issues
show
Coding Style introduced by
Expected "} else \n"; found " // END if confirm\n else {\n"
Loading history...
1151
            // Do Empty
1152
            $msg = '';
1153
            if (is_array($_REQUEST['table'])) {
1154
                foreach ($_REQUEST['table'] as $t) {
1155
                    list($status, $sql) = $data->emptyTable($t, isset($_POST['cascade']));
1156
                    if (0 == $status) {
1157
                        $msg .= sprintf('%s<br />', $sql);
1158
                        $msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtableemptied']);
1159
                    } else {
1160
                        $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtableemptiedbad']));
1161
1162
                        return;
1163
                    }
1164
                }
1165
                $this->doDefault($msg);
1166
            } // END mutli empty
1167
            else {
0 ignored issues
show
Coding Style introduced by
Expected "} else \n"; found " // END mutli empty\n else {\n"
Loading history...
1168
                list($status, $sql) = $data->emptyTable($_POST['table'], isset($_POST['cascade']));
1169
                if (0 == $status) {
1170
                    $msg .= sprintf('%s<br />', $sql);
1171
                    $msg .= sprintf('%s: %s<br />', htmlentities($_POST['table'], ENT_QUOTES, 'UTF-8'), $lang['strtableemptied']);
1172
                    return $this->doDefault($msg);
1173
                }
1174
1175
                return $this->doDefault($lang['strtableemptiedbad']);
1176
            } // END not mutli empty
1177
        } // END do Empty
1178
    }
1179
1180
    /**
1181
     * Show confirmation of drop and perform actual drop.
1182
     *
1183
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
1184
     */
1185
    public function doDrop($confirm)
1186
    {
1187
        $lang = $this->lang;
1188
        $data = $this->misc->getDatabaseAccessor();
1189
1190
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1191
            return $this->doDefault($lang['strspecifytabletodrop']);
1192
        }
1193
1194
        if ($confirm) {
1195
            //If multi drop
1196
            if (isset($_REQUEST['ma'])) {
1197
                $this->printTrail('schema');
1198
                $this->printTitle($lang['strdrop'], 'pg.table.drop');
1199
1200
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
1201
                foreach ($_REQUEST['ma'] as $v) {
1202
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
1203
                    echo '<p>', sprintf($lang['strconfdroptable'], $this->misc->printVal($a['table'])), "</p>\n";
1204
                    printf('<input type="hidden" name="table[]" value="%s" />', htmlspecialchars($a['table']));
1205
                }
1206
            } else {
1207
                $this->printTrail('table');
1208
                $this->printTitle($lang['strdrop'], 'pg.table.drop');
1209
1210
                echo '<p>', sprintf($lang['strconfdroptable'], $this->misc->printVal($_REQUEST['table'])), "</p>\n";
1211
1212
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
1213
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
1214
            } // END if multi drop
1215
1216
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
1217
            echo $this->misc->form;
1218
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
1219
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
1220
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1221
            echo "</form>\n";
1222
        } // END confirm
1223
        else {
0 ignored issues
show
Coding Style introduced by
Expected "} else \n"; found " // END confirm\n else {\n"
Loading history...
1224
            //If multi drop
1225
            if (is_array($_REQUEST['table'])) {
1226
                $msg    = '';
1227
                $status = $data->beginTransaction();
1228
                if (0 == $status) {
1229
                    foreach ($_REQUEST['table'] as $t) {
1230
                        $status = $data->dropTable($t, isset($_POST['cascade']));
1231
                        if (0 == $status) {
1232
                            $msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtabledropped']);
1233
                        } else {
1234
                            $data->endTransaction();
1235
1236
                            return $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtabledroppedbad']));
1237
                        }
1238
                    }
1239
                }
1240
                if (0 == $data->endTransaction()) {
1241
                    // Everything went fine, back to the Default page....
1242
                    $this->misc->setReloadBrowser(true);
1243
1244
                    return $this->doDefault($msg);
1245
                }
1246
1247
                return $this->doDefault($lang['strtabledroppedbad']);
1248
            }
1249
            $status = $data->dropTable($_POST['table'], isset($_POST['cascade']));
1250
            if (0 == $status) {
1251
                $this->misc->setReloadBrowser(true);
1252
1253
                return $this->doDefault($lang['strtabledropped']);
1254
            }
1255
1256
            return $this->doDefault($lang['strtabledroppedbad']);
1257
        } // END DROP
1258
    }
1259
1260
    // END Function
1261
}
1262