Passed
Pull Request — develop (#92)
by Felipe
04:25
created

src/controllers/TablesController.php (13 issues)

1
<?php
2
0 ignored issues
show
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
 */
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
        $data = $this->misc->getDatabaseAccessor();
37
38
        $header_template = 'header.twig';
39
        $footer_template = 'footer.twig';
40
41
        ob_start();
42
43
        switch ($action) {
44
            case 'create':
45
46
                if (isset($_POST['cancel'])) {
47
                    $this->doDefault();
48
                } else {
49
                    $header_template = 'header_select2.twig';
50
                    $this->doCreate();
51
                }
52
53
                break;
54
            case 'createlike':
55
                $header_template = 'header_select2.twig';
56
                $this->doCreateLike(false);
57
58
                break;
59
            case 'confcreatelike':
60
                if (isset($_POST['cancel'])) {
61
                    $header_template = 'header_datatables.twig';
62
                    $this->doDefault();
63
                } else {
64
                    //$header_template = 'header_select2.twig';
65
                    $this->doCreateLike(true);
66
                }
67
68
                break;
69
            case 'selectrows':
70
                if (!isset($_POST['cancel'])) {
71
                    $this->doSelectRows(false);
72
                } else {
73
                    $header_template = 'header_datatables.twig';
74
                    $this->doDefault();
75
                }
76
77
                break;
78
            case 'confselectrows':
79
                $this->doSelectRows(true);
80
81
                break;
82
            case 'insertrow':
83
                if (!isset($_POST['cancel'])) {
84
                    $this->doInsertRow(false);
85
                } else {
86
                    $header_template = 'header_datatables.twig';
87
                    $this->doDefault();
88
                }
89
90
                break;
91
            case 'confinsertrow':
92
                $this->doInsertRow(true);
93
94
                break;
95
            case 'empty':
96
                if (isset($_POST['empty'])) {
97
                    $this->doEmpty(false);
98
                } else {
99
                    $header_template = 'header_datatables.twig';
100
                    $this->doDefault();
101
                }
102
103
                break;
104
            case 'confirm_empty':
105
                $this->doEmpty(true);
106
107
                break;
108
            case 'drop':
109
                if (isset($_POST['drop'])) {
110
                    $this->doDrop(false);
111
                } else {
112
                    $header_template = 'header_datatables.twig';
113
                    $this->doDefault();
114
                }
115
116
                break;
117
            case 'confirm_drop':
118
                $this->doDrop(true);
119
120
                break;
121
            default:
122
                if (false === $this->adminActions($action, 'table')) {
123
                    $header_template = 'header_datatables.twig';
124
                    $this->doDefault();
125
                }
126
127
                break;
128
        }
129
130
        $output = ob_get_clean();
131
132
        $this->printHeader($lang['strtables'], null, true, $header_template);
133
        $this->printBody();
134
135
        echo $output;
136
137
        return $this->printFooter();
138
    }
139
140
    /**
141
     * Show default list of tables in the database.
142
     *
143
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
144
     */
145
    public function doDefault($msg = '')
146
    {
147
        $lang = $this->lang;
148
        $data = $this->misc->getDatabaseAccessor();
149
150
        $this->printTrail('schema');
151
        $this->printTabs('schema', 'tables');
152
        $this->printMsg($msg);
153
154
        $tables = $data->getTables();
155
156
        $columns = [
157
            'table'      => [
158
                'title' => $lang['strtable'],
159
                'field' => Decorator::field('relname'),
160
                'url'   => \SUBFOLDER . "/redirect/table?{$this->misc->href}&amp;",
161
                'vars'  => ['table' => 'relname'],
162
            ],
163
            'owner'      => [
164
                'title' => $lang['strowner'],
165
                'field' => Decorator::field('relowner'),
166
            ],
167
            'tablespace' => [
168
                'title' => $lang['strtablespace'],
169
                'field' => Decorator::field('tablespace'),
170
            ],
171
            'tuples'     => [
172
                'title' => $lang['strestimatedrowcount'],
173
                'field' => Decorator::field('reltuples'),
174
                'type'  => 'numeric',
175
            ],
176
            'actions'    => [
177
                'title' => $lang['stractions'],
178
            ],
179
            'comment'    => [
180
                'title' => $lang['strcomment'],
181
                'field' => Decorator::field('relcomment'),
182
            ],
183
        ];
184
185
        $actions = [
186
            'multiactions' => [
187
                'keycols' => ['table' => 'relname'],
188
                'url'     => 'tables.php',
189
                'default' => 'analyze',
190
            ],
191
            'browse'       => [
192
                'content' => $lang['strbrowse'],
193
                'attr'    => [
194
                    'href' => [
195
                        'url'     => 'display.php',
196
                        'urlvars' => [
197
                            'subject' => 'table',
198
                            'return'  => 'table',
199
                            'table'   => Decorator::field('relname'),
200
                        ],
201
                    ],
202
                ],
203
            ],
204
            'select'       => [
205
                'content' => $lang['strselect'],
206
                'attr'    => [
207
                    'href' => [
208
                        'url'     => 'tables.php',
209
                        'urlvars' => [
210
                            'action' => 'confselectrows',
211
                            'table'  => Decorator::field('relname'),
212
                        ],
213
                    ],
214
                ],
215
            ],
216
            'insert'       => [
217
                'content' => $lang['strinsert'],
218
                'attr'    => [
219
                    'href' => [
220
                        'url'     => 'tables.php',
221
                        'urlvars' => [
222
                            'action' => 'confinsertrow',
223
                            'table'  => Decorator::field('relname'),
224
                        ],
225
                    ],
226
                ],
227
            ],
228
            'empty'        => [
229
                'multiaction' => 'confirm_empty',
230
                'content'     => $lang['strempty'],
231
                'attr'        => [
232
                    'href' => [
233
                        'url'     => 'tables.php',
234
                        'urlvars' => [
235
                            'action' => 'confirm_empty',
236
                            'table'  => Decorator::field('relname'),
237
                        ],
238
                    ],
239
                ],
240
            ],
241
            'alter'        => [
242
                'content' => $lang['stralter'],
243
                'attr'    => [
244
                    'href' => [
245
                        'url'     => 'tblproperties.php',
246
                        'urlvars' => [
247
                            'action' => 'confirm_alter',
248
                            'table'  => Decorator::field('relname'),
249
                        ],
250
                    ],
251
                ],
252
            ],
253
            'drop'         => [
254
                'multiaction' => 'confirm_drop',
255
                'content'     => $lang['strdrop'],
256
                'attr'        => [
257
                    'href' => [
258
                        'url'     => 'tables.php',
259
                        'urlvars' => [
260
                            'action' => 'confirm_drop',
261
                            'table'  => Decorator::field('relname'),
262
                        ],
263
                    ],
264
                ],
265
            ],
266
            'vacuum'       => [
267
                'multiaction' => 'confirm_vacuum',
268
                'content'     => $lang['strvacuum'],
269
                'attr'        => [
270
                    'href' => [
271
                        'url'     => 'tables.php',
272
                        'urlvars' => [
273
                            'action' => 'confirm_vacuum',
274
                            'table'  => Decorator::field('relname'),
275
                        ],
276
                    ],
277
                ],
278
            ],
279
            'analyze'      => [
280
                'multiaction' => 'confirm_analyze',
281
                'content'     => $lang['stranalyze'],
282
                'attr'        => [
283
                    'href' => [
284
                        'url'     => 'tables.php',
285
                        'urlvars' => [
286
                            'action' => 'confirm_analyze',
287
                            'table'  => Decorator::field('relname'),
288
                        ],
289
                    ],
290
                ],
291
            ],
292
            'reindex'      => [
293
                'multiaction' => 'confirm_reindex',
294
                'content'     => $lang['strreindex'],
295
                'attr'        => [
296
                    'href' => [
297
                        'url'     => 'tables.php',
298
                        'urlvars' => [
299
                            'action' => 'confirm_reindex',
300
                            'table'  => Decorator::field('relname'),
301
                        ],
302
                    ],
303
                ],
304
            ],
305
            //'cluster' TODO ?
306
        ];
307
308
        if (!$data->hasTablespaces()) {
309
            unset($columns['tablespace']);
310
        }
311
312
        //\Kint::dump($tables);
313
314
        echo $this->printTable($tables, $columns, $actions, $this->table_place, $lang['strnotables']);
315
316
        $navlinks = [
317
            'create' => [
318
                'attr'    => [
319
                    'href' => [
320
                        'url'     => 'tables.php',
321
                        'urlvars' => [
322
                            'action'   => 'create',
323
                            'server'   => $_REQUEST['server'],
324
                            'database' => $_REQUEST['database'],
325
                            'schema'   => $_REQUEST['schema'],
326
                        ],
327
                    ],
328
                ],
329
                'content' => $lang['strcreatetable'],
330
            ],
331
        ];
332
333
        if (($tables->recordCount() > 0) && $data->hasCreateTableLike()) {
334
            $navlinks['createlike'] = [
335
                'attr'    => [
336
                    'href' => [
337
                        'url'     => 'tables.php',
338
                        'urlvars' => [
339
                            'action'   => 'createlike',
340
                            'server'   => $_REQUEST['server'],
341
                            'database' => $_REQUEST['database'],
342
                            'schema'   => $_REQUEST['schema'],
343
                        ],
344
                    ],
345
                ],
346
                'content' => $lang['strcreatetablelike'],
347
            ];
348
        }
349
        $this->printNavLinks($navlinks, 'tables-tables', get_defined_vars());
350
    }
351
352
    /**
353
     * Generate XML for the browser tree.
354
     */
355
    public function doTree()
356
    {
357
        $lang = $this->lang;
358
        $data = $this->misc->getDatabaseAccessor();
359
360
        //\PC::debug($this->misc->getDatabase(), 'getDatabase');
361
362
        $tables = $data->getTables();
363
364
        $reqvars = $this->misc->getRequestVars('table');
365
366
        $attrs = [
367
            'text'       => Decorator::field('relname'),
368
            'icon'       => 'Table',
369
            'iconAction' => Decorator::url('display.php', $reqvars, ['table' => Decorator::field('relname')]),
370
            'toolTip'    => Decorator::field('relcomment'),
371
            'action'     => Decorator::redirecturl('redirect.php', $reqvars, ['table' => Decorator::field('relname')]),
372
            'branch'     => Decorator::url('tables.php', $reqvars, ['action' => 'subtree', 'table' => Decorator::field('relname')]),
373
        ];
374
375
        return $this->printTree($tables, $attrs, 'tables');
376
    }
377
378
    public function doSubTree()
379
    {
380
        $lang = $this->lang;
381
        $data = $this->misc->getDatabaseAccessor();
382
383
        $tabs    = $this->misc->getNavTabs('table');
384
        $items   = $this->adjustTabsForTree($tabs);
385
        $reqvars = $this->misc->getRequestVars('table');
386
387
        $attrs = [
388
            'text'   => Decorator::field('title'),
389
            'icon'   => Decorator::field('icon'),
390
            'action' => Decorator::actionurl(
391
                Decorator::field('url'),
392
                $reqvars,
393
                Decorator::field('urlvars'),
394
                ['table' => $_REQUEST['table']]
395
            ),
396
            'branch' => Decorator::ifempty(
397
                Decorator::field('branch'),
398
                '',
399
                Decorator::url(
400
                    Decorator::field('url'),
401
                    $reqvars,
402
                    [
403
                        'action' => 'tree',
404
                        'table'  => $_REQUEST['table'],
405
                    ]
406
                )
407
            ),
408
        ];
409
410
        return $this->printTree($items, $attrs, 'table');
411
    }
412
413
    /**
414
     * Displays a screen where they can enter a new table.
415
     *
416
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
417
     */
418
    public function doCreate($msg = '')
419
    {
420
        $lang = $this->lang;
421
        $data = $this->misc->getDatabaseAccessor();
422
423
        if (!isset($_REQUEST['stage'])) {
424
            $_REQUEST['stage'] = 1;
425
            $default_with_oids = $data->getDefaultWithOid();
426
            if ('off' == $default_with_oids) {
427
                $_REQUEST['withoutoids'] = 'on';
428
            }
429
        }
430
431
        if (!isset($_REQUEST['name'])) {
432
            $_REQUEST['name'] = '';
433
        }
434
435
        if (!isset($_REQUEST['fields'])) {
436
            $_REQUEST['fields'] = '';
437
        }
438
439
        if (!isset($_REQUEST['tblcomment'])) {
440
            $_REQUEST['tblcomment'] = '';
441
        }
442
443
        if (!isset($_REQUEST['spcname'])) {
444
            $_REQUEST['spcname'] = '';
445
        }
446
447
        switch ($_REQUEST['stage']) {
448
            case 1:
449
                // You are presented with a form in which you describe the table, pick the tablespace and state how many fields it will have
450
                // Fetch all tablespaces from the database
451
                if ($data->hasTablespaces()) {
452
                    $tablespaces = $data->getTablespaces();
453
                }
454
455
                $this->printTrail('schema');
456
                $this->printTitle($lang['strcreatetable'], 'pg.table.create');
457
                $this->printMsg($msg);
458
459
                echo '<form action="' . \SUBFOLDER . '/src/views/' . $this->script . '" method="post">';
460
                echo "\n";
461
                echo "<table>\n";
462
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
463
                echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
464
                htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n";
465
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strnumcols']}</th>\n";
466
                echo "\t\t<td class=\"data\"><input name=\"fields\" size=\"5\" maxlength=\"{$data->_maxNameLen}\" value=\"",
467
                htmlspecialchars($_REQUEST['fields']), "\" /></td>\n\t</tr>\n";
468
                echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stroptions']}</th>\n";
469
                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";
470
471
                // Tablespace (if there are any)
472
                if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) {
473
                    echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
474
                    echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"spcname\">\n";
475
                    // Always offer the default (empty) option
476
                    echo "\t\t\t\t<option value=\"\"",
477
                    ('' == $_REQUEST['spcname']) ? ' selected="selected"' : '', "></option>\n";
478
                    // Display all other tablespaces
479
                    while (!$tablespaces->EOF) {
480
                        $spcname = htmlspecialchars($tablespaces->fields['spcname']);
481
                        echo "\t\t\t\t<option value=\"{$spcname}\"",
482
                        ($tablespaces->fields['spcname'] == $_REQUEST['spcname']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
483
                        $tablespaces->moveNext();
484
                    }
485
                    echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
486
                }
487
488
                echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
489
                echo "\t\t<td><textarea name=\"tblcomment\" rows=\"3\" cols=\"32\">",
490
                htmlspecialchars($_REQUEST['tblcomment']), "</textarea></td>\n\t</tr>\n";
491
492
                echo "</table>\n";
493
                echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n";
494
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
495
                echo $this->misc->form;
496
                echo "<input type=\"submit\" value=\"{$lang['strnext']}\" />\n";
497
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
498
                echo "</form>\n";
499
500
                break;
501
            case 2:
502
503
                // Check inputs
504
                $fields = trim($_REQUEST['fields']);
505
                if ('' == trim($_REQUEST['name'])) {
506
                    $_REQUEST['stage'] = 1;
507
                    $this->doCreate($lang['strtableneedsname']);
508
509
                    return;
510
                }
511
                if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields < 1) {
512
                    $_REQUEST['stage'] = 1;
513
                    $this->doCreate($lang['strtableneedscols']);
514
515
                    return;
516
                }
517
518
                $types        = $data->getTypes(true, false, true);
519
                $types_for_js = [];
520
521
                $this->printTrail('schema');
522
                $this->printTitle($lang['strcreatetable'], 'pg.table.create');
523
                $this->printMsg($msg);
524
525
                echo '<script src="' . \SUBFOLDER . '/js/tables.js" type="text/javascript"></script>';
526
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
527
528
                // Output table header
529
                echo "<table>\n";
530
                echo "\t<tr><th colspan=\"2\" class=\"data required\">{$lang['strcolumn']}</th><th colspan=\"2\" class=\"data required\">{$lang['strtype']}</th>";
531
                echo "<th class=\"data\">{$lang['strlength']}</th><th class=\"data\">{$lang['strnotnull']}</th>";
532
                echo "<th class=\"data\">{$lang['struniquekey']}</th><th class=\"data\">{$lang['strprimarykey']}</th>";
533
                echo "<th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>\n";
534
535
                for ($i = 0; $i < $_REQUEST['fields']; ++$i) {
536
                    if (!isset($_REQUEST['field'][$i])) {
537
                        $_REQUEST['field'][$i] = '';
538
                    }
539
540
                    if (!isset($_REQUEST['length'][$i])) {
541
                        $_REQUEST['length'][$i] = '';
542
                    }
543
544
                    if (!isset($_REQUEST['default'][$i])) {
545
                        $_REQUEST['default'][$i] = '';
546
                    }
547
548
                    if (!isset($_REQUEST['colcomment'][$i])) {
549
                        $_REQUEST['colcomment'][$i] = '';
550
                    }
551
552
                    echo "\t<tr>\n\t\t<td>", $i + 1, ".&nbsp;</td>\n";
553
                    echo "\t\t<td><input name=\"field[{$i}]\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
554
                    htmlspecialchars($_REQUEST['field'][$i]), "\" /></td>\n";
555
                    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";
556
                    // Output any "magic" types
557
                    foreach ($data->extraTypes as $v) {
558
                        $types_for_js[strtolower($v)] = 1;
559
                        echo "\t\t\t\t<option value=\"", htmlspecialchars($v), '"',
560
                        (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] == $v) ? ' selected="selected"' : '', '>',
561
                        $this->misc->printVal($v), "</option>\n";
562
                    }
563
                    $types->moveFirst();
564
                    while (!$types->EOF) {
565
                        $typname                = $types->fields['typname'];
566
                        $types_for_js[$typname] = 1;
567
                        echo "\t\t\t\t<option value=\"", htmlspecialchars($typname), '"',
568
                        (isset($_REQUEST['type'][$i]) && $_REQUEST['type'][$i] == $typname) ? ' selected="selected"' : '', '>',
569
                        $this->misc->printVal($typname), "</option>\n";
570
                        $types->moveNext();
571
                    }
572
                    echo "\t\t\t</select>\n\t\t\n";
573
                    if (0 == $i) {
574
                        // only define js types array once
575
                        $predefined_size_types = array_intersect($data->predefined_size_types, array_keys($types_for_js));
576
                        $escaped_predef_types  = []; // the JS escaped array elements
577
                        foreach ($predefined_size_types as $value) {
578
                            $escaped_predef_types[] = "'{$value}'";
579
                        }
580
                        echo '<script type="text/javascript">predefined_lengths = new Array(' . implode(',', $escaped_predef_types) . ");</script>\n\t</td>";
581
                    }
582
583
                    // Output array type selector
584
                    echo "\t\t<td>\n\t\t\t<select name=\"array[{$i}]\">\n";
585
                    echo "\t\t\t\t<option value=\"\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '') ? ' selected="selected"' : '', "></option>\n";
586
                    echo "\t\t\t\t<option value=\"[]\"", (isset($_REQUEST['array'][$i]) && $_REQUEST['array'][$i] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
587
                    echo "\t\t\t</select>\n\t\t</td>\n";
588
589
                    echo "\t\t<td><input name=\"length[{$i}]\" id=\"lengths{$i}\" size=\"10\" value=\"",
590
                    htmlspecialchars($_REQUEST['length'][$i]), "\" /></td>\n";
591
                    echo "\t\t<td><input type=\"checkbox\" name=\"notnull[{$i}]\"", (isset($_REQUEST['notnull'][$i])) ? ' checked="checked"' : '', " /></td>\n";
592
                    echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"uniquekey[{$i}]\""
593
                        . (isset($_REQUEST['uniquekey'][$i]) ? ' checked="checked"' : '') . " /></td>\n";
594
                    echo "\t\t<td style=\"text-align: center\"><input type=\"checkbox\" name=\"primarykey[{$i}]\" "
595
                        . (isset($_REQUEST['primarykey'][$i]) ? ' checked="checked"' : '')
596
                        . " /></td>\n";
597
                    echo "\t\t<td><input name=\"default[{$i}]\" size=\"20\" value=\"",
598
                    htmlspecialchars($_REQUEST['default'][$i]), "\" /></td>\n";
599
                    echo "\t\t<td><input name=\"colcomment[{$i}]\" size=\"40\" value=\"",
600
                    htmlspecialchars($_REQUEST['colcomment'][$i]), "\" />
601
						<script type=\"text/javascript\">checkLengths(document.getElementById('types{$i}').value,{$i});</script>
602
						</td>\n\t</tr>\n";
603
                }
604
                echo "</table>\n";
605
                echo "<p><input type=\"hidden\" name=\"action\" value=\"create\" />\n";
606
                echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n";
607
                echo $this->misc->form;
608
                echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n";
609
                echo '<input type="hidden" name="fields" value="', htmlspecialchars($_REQUEST['fields']), "\" />\n";
610
                if (isset($_REQUEST['withoutoids'])) {
611
                    echo "<input type=\"hidden\" name=\"withoutoids\" value=\"true\" />\n";
612
                }
613
                echo '<input type="hidden" name="tblcomment" value="', htmlspecialchars($_REQUEST['tblcomment']), "\" />\n";
614
                if (isset($_REQUEST['spcname'])) {
615
                    echo '<input type="hidden" name="spcname" value="', htmlspecialchars($_REQUEST['spcname']), "\" />\n";
616
                }
617
                echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
618
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
619
                echo "</form>\n";
620
621
                break;
622
            case 3:
623
624
                if (!isset($_REQUEST['notnull'])) {
625
                    $_REQUEST['notnull'] = [];
626
                }
627
628
                if (!isset($_REQUEST['uniquekey'])) {
629
                    $_REQUEST['uniquekey'] = [];
630
                }
631
632
                if (!isset($_REQUEST['primarykey'])) {
633
                    $_REQUEST['primarykey'] = [];
634
                }
635
636
                if (!isset($_REQUEST['length'])) {
637
                    $_REQUEST['length'] = [];
638
                }
639
640
                // Default tablespace to null if it isn't set
641
                if (!isset($_REQUEST['spcname'])) {
642
                    $_REQUEST['spcname'] = null;
643
                }
644
645
                // Check inputs
646
                $fields = trim($_REQUEST['fields']);
647
                if ('' == trim($_REQUEST['name'])) {
648
                    $_REQUEST['stage'] = 1;
649
                    $this->doCreate($lang['strtableneedsname']);
650
651
                    return;
652
                }
653
                if ('' == $fields || !is_numeric($fields) || $fields != (int) $fields || $fields <= 0) {
654
                    $_REQUEST['stage'] = 1;
655
                    $this->doCreate($lang['strtableneedscols']);
656
657
                    return;
658
                }
659
660
                $status = $data->createTable(
661
                    $_REQUEST['name'],
662
                    $_REQUEST['fields'],
663
                    $_REQUEST['field'],
664
                    $_REQUEST['type'],
665
                    $_REQUEST['array'],
666
                    $_REQUEST['length'],
667
                    $_REQUEST['notnull'],
668
                    $_REQUEST['default'],
669
                    isset($_REQUEST['withoutoids']),
670
                    $_REQUEST['colcomment'],
671
                    $_REQUEST['tblcomment'],
672
                    $_REQUEST['spcname'],
673
                    $_REQUEST['uniquekey'],
674
                    $_REQUEST['primarykey']
675
                );
676
677
                if (0 == $status) {
678
                    $this->misc->setReloadBrowser(true);
679
680
                    return $this->doDefault($lang['strtablecreated']);
681
                }
682
                if ($status == -1) {
683
                    $_REQUEST['stage'] = 2;
684
                    $this->doCreate($lang['strtableneedsfield']);
685
686
                    return;
687
                }
688
                $_REQUEST['stage'] = 2;
689
                $this->doCreate($lang['strtablecreatedbad']);
690
691
                return;
692
                break;
1 ignored issue
show
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...
693
            default:
694
                echo "<p>{$lang['strinvalidparam']}</p>\n";
695
        }
696
    }
697
698
    /**
699
     * Dsiplay a screen where user can create a table from an existing one.
700
     * We don't have to check if pg supports schema cause create table like
701
     * is available under pg 7.4+ which has schema.
702
     *
703
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
704
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
705
     */
706
    public function doCreateLike($confirm, $msg = '')
707
    {
708
        $lang = $this->lang;
709
        $data = $this->misc->getDatabaseAccessor();
710
711
        if (!$confirm) {
712
            if (!isset($_REQUEST['name'])) {
713
                $_REQUEST['name'] = '';
714
            }
715
716
            if (!isset($_REQUEST['like'])) {
717
                $_REQUEST['like'] = '';
718
            }
719
720
            if (!isset($_REQUEST['tablespace'])) {
721
                $_REQUEST['tablespace'] = '';
722
            }
723
724
            $this->printTrail('schema');
725
            $this->printTitle($lang['strcreatetable'], 'pg.table.create');
726
            $this->printMsg($msg);
727
728
            $tbltmp = $data->getTables(true);
729
            $tbltmp = $tbltmp->getArray();
730
731
            $tables = [];
732
            $tblsel = '';
733
            foreach ($tbltmp as $a) {
734
                $data->fieldClean($a['nspname']);
735
                $data->fieldClean($a['relname']);
736
                $tables["\"{$a['nspname']}\".\"{$a['relname']}\""] = serialize(['schema' => $a['nspname'], 'table' => $a['relname']]);
737
                if ($_REQUEST['like'] == $tables["\"{$a['nspname']}\".\"{$a['relname']}\""]) {
738
                    $tblsel = htmlspecialchars($tables["\"{$a['nspname']}\".\"{$a['relname']}\""]);
739
                }
740
            }
741
742
            unset($tbltmp);
743
744
            echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
745
            echo "<table>\n\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
746
            echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n";
747
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strcreatetablelikeparent']}</th>\n";
748
            echo "\t\t<td class=\"data\">";
749
            echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tables, 'like', true, $tblsel, false);
750
            echo "</td>\n\t</tr>\n";
751
            if ($data->hasTablespaces()) {
752
                $tblsp_ = $data->getTablespaces();
753
                if ($tblsp_->recordCount() > 0) {
754
                    $tblsp_ = $tblsp_->getArray();
755
                    $tblsp  = [];
756
                    foreach ($tblsp_ as $a) {
757
                        $tblsp[$a['spcname']] = $a['spcname'];
758
                    }
759
760
                    echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
761
                    echo "\t\t<td class=\"data\">";
762
                    echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tblsp, 'tablespace', true, $_REQUEST['tablespace'], false);
763
                    echo "</td>\n\t</tr>\n";
764
                }
765
            }
766
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['stroptions']}</th>\n\t\t<td class=\"data\">";
767
            echo '<label for="withdefaults"><input type="checkbox" id="withdefaults" name="withdefaults"',
768
            isset($_REQUEST['withdefaults']) ? ' checked="checked"' : '',
769
                "/>{$lang['strcreatelikewithdefaults']}</label>";
770
            if ($data->hasCreateTableLikeWithConstraints()) {
771
                echo '<br /><label for="withconstraints"><input type="checkbox" id="withconstraints" name="withconstraints"',
772
                isset($_REQUEST['withconstraints']) ? ' checked="checked"' : '',
773
                    "/>{$lang['strcreatelikewithconstraints']}</label>";
774
            }
775
            if ($data->hasCreateTableLikeWithIndexes()) {
776
                echo '<br /><label for="withindexes"><input type="checkbox" id="withindexes" name="withindexes"',
777
                isset($_REQUEST['withindexes']) ? ' checked="checked"' : '',
778
                    "/>{$lang['strcreatelikewithindexes']}</label>";
779
            }
780
            echo "</td>\n\t</tr>\n";
781
            echo '</table>';
782
783
            echo "<input type=\"hidden\" name=\"action\" value=\"confcreatelike\" />\n";
784
            echo $this->misc->form;
785
            echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
786
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
787
            echo "</form>\n";
788
        } else {
789
            if ('' == trim($_REQUEST['name'])) {
790
                $this->doCreateLike(false, $lang['strtableneedsname']);
791
792
                return;
793
            }
794
            if ('' == trim($_REQUEST['like'])) {
795
                $this->doCreateLike(false, $lang['strtablelikeneedslike']);
796
797
                return;
798
            }
799
800
            if (!isset($_REQUEST['tablespace'])) {
801
                $_REQUEST['tablespace'] = '';
802
            }
803
804
            $status = $data->createTableLike(
805
                $_REQUEST['name'],
806
                unserialize($_REQUEST['like']),
807
                isset($_REQUEST['withdefaults']),
808
                isset($_REQUEST['withconstraints']),
809
                isset($_REQUEST['withindexes']),
810
                $_REQUEST['tablespace']
811
            );
812
813
            if (0 == $status) {
814
                $this->misc->setReloadBrowser(true);
815
816
                return $this->doDefault($lang['strtablecreated']);
817
            }
818
            $this->doCreateLike(false, $lang['strtablecreatedbad']);
819
820
            return;
821
        }
822
    }
823
824
    /**
825
     * Ask for select parameters and perform select.
826
     *
827
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
828
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
829
     */
830
    public function doSelectRows($confirm, $msg = '')
831
    {
832
        $lang = $this->lang;
833
        $data = $this->misc->getDatabaseAccessor();
834
835
        if ($confirm) {
836
            $this->printTrail('table');
837
            $this->printTabs('table', 'select');
838
            $this->printMsg($msg);
839
840
            $attrs = $data->getTableAttributes($_REQUEST['table']);
841
842
            echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\" id=\"selectform\">\n";
843
            if ($attrs->recordCount() > 0) {
844
                // JavaScript for select all feature
845
                echo "<script type=\"text/javascript\">\n";
846
                echo "//<![CDATA[\n";
847
                echo "	function selectAll() {\n";
848
                echo "		for (var i=0; i<document.getElementById('selectform').elements.length; i++) {\n";
849
                echo "			var e = document.getElementById('selectform').elements[i];\n";
850
                echo "			if (e.name.indexOf('show') == 0) e.checked = document.getElementById('selectform').selectall.checked;\n";
851
                echo "		}\n";
852
                echo "	}\n";
853
                echo "//]]>\n";
854
                echo "</script>\n";
855
856
                echo "<table>\n";
857
858
                // Output table header
859
                echo "<tr><th class=\"data\">{$lang['strshow']}</th><th class=\"data\">{$lang['strcolumn']}</th>";
860
                echo "<th class=\"data\">{$lang['strtype']}</th><th class=\"data\">{$lang['stroperator']}</th>";
861
                echo "<th class=\"data\">{$lang['strvalue']}</th></tr>";
862
863
                $i = 0;
864
                while (!$attrs->EOF) {
865
                    $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']);
866
                    // Set up default value if there isn't one already
867
                    if (!isset($_REQUEST['values'][$attrs->fields['attname']])) {
868
                        $_REQUEST['values'][$attrs->fields['attname']] = null;
869
                    }
870
871
                    if (!isset($_REQUEST['ops'][$attrs->fields['attname']])) {
872
                        $_REQUEST['ops'][$attrs->fields['attname']] = null;
873
                    }
874
875
                    // Continue drawing row
876
                    $id = (0 == ($i % 2) ? '1' : '2');
877
                    echo "<tr class=\"data{$id}\">\n";
878
                    echo '<td style="white-space:nowrap;">';
879
                    echo '<input type="checkbox" name="show[', htmlspecialchars($attrs->fields['attname']), ']"',
880
                    isset($_REQUEST['show'][$attrs->fields['attname']]) ? ' checked="checked"' : '', ' /></td>';
881
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
882
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod'])), '</td>';
883
                    echo '<td style="white-space:nowrap;">';
884
                    echo "<select name=\"ops[{$attrs->fields['attname']}]\">\n";
885
                    foreach (array_keys($data->selectOps) as $v) {
886
                        echo '<option value="', htmlspecialchars($v), '"', ($_REQUEST['ops'][$attrs->fields['attname']] == $v) ? ' selected="selected"' : '',
887
                        '>', htmlspecialchars($v), "</option>\n";
888
                    }
889
                    echo "</select>\n</td>\n";
890
                    echo '<td style="white-space:nowrap;">', $data->printField(
891
                        "values[{$attrs->fields['attname']}]",
892
                        $_REQUEST['values'][$attrs->fields['attname']],
893
                        $attrs->fields['type']
894
                    ), '</td>';
895
                    echo "</tr>\n";
896
                    ++$i;
897
                    $attrs->moveNext();
898
                }
899
                // Select all checkbox
900
                echo "<tr><td colspan=\"5\"><input type=\"checkbox\" id=\"selectall\" name=\"selectall\" accesskey=\"a\" onclick=\"javascript:selectAll()\" /><label for=\"selectall\">{$lang['strselectallfields']}</label></td>";
901
                echo "</tr></table>\n";
902
            } else {
903
                echo "<p>{$lang['strinvalidparam']}</p>\n";
904
            }
905
906
            echo "<p><input type=\"hidden\" name=\"action\" value=\"selectrows\" />\n";
907
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
908
            echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
909
            echo $this->misc->form;
910
            echo "<input type=\"submit\" name=\"select\" accesskey=\"r\" value=\"{$lang['strselect']}\" />\n";
911
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
912
            echo "</form>\n";
913
914
            return;
915
        }
916
        if (!isset($_POST['show'])) {
917
            $_POST['show'] = [];
918
        }
919
920
        if (!isset($_POST['values'])) {
921
            $_POST['values'] = [];
922
        }
923
924
        if (!isset($_POST['nulls'])) {
925
            $_POST['nulls'] = [];
926
        }
927
928
        // Verify that they haven't supplied a value for unary operators
929
        foreach ($_POST['ops'] as $k => $v) {
930
            if ('p' == $data->selectOps[$v] && $_POST['values'][$k] != '') {
931
                $this->doSelectRows(true, $lang['strselectunary']);
932
933
                return;
934
            }
935
        }
936
937
        if (0 == sizeof($_POST['show'])) {
938
            $this->doSelectRows(true, $lang['strselectneedscol']);
939
        } else {
940
            // Generate query SQL
941
            $query = $data->getSelectSQL(
942
                $_REQUEST['table'],
943
                array_keys($_POST['show']),
944
                $_POST['values'],
945
                $_POST['ops']
946
            );
947
            $_REQUEST['query']  = $query;
948
            $_REQUEST['return'] = 'selectrows';
949
950
            $this->setNoOutput(true);
951
952
            $display_controller = new DisplayController($this->getContainer());
953
954
            return $display_controller->render();
955
        }
956
    }
957
958
    /**
959
     * Ask for insert parameters and then actually insert row.
960
     *
961
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
962
     * @param mixed $msg
1 ignored issue
show
Missing parameter comment
Loading history...
963
     */
964
    public function doInsertRow($confirm, $msg = '')
965
    {
966
        $lang = $this->lang;
967
        $data = $this->misc->getDatabaseAccessor();
968
969
        if ($confirm) {
970
            $this->printTrail('table');
971
            $this->printTabs('table', 'insert');
972
973
            $this->printMsg($msg);
974
975
            $attrs = $data->getTableAttributes($_REQUEST['table']);
976
977
            if (('disable' != $conf['autocomplete'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $conf does not exist. Did you maybe mean $confirm?
Loading history...
978
                $fksprops = $this->misc->getAutocompleteFKProperties($_REQUEST['table']);
979
                if (false !== $fksprops) {
980
                    echo $fksprops['code'];
981
                }
982
            } else {
983
                $fksprops = false;
984
            }
985
986
            echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\" id=\"ac_form\">\n";
987
            if ($attrs->recordCount() > 0) {
988
                echo "<table>\n";
989
990
                // Output table header
991
                echo "<tr><th class=\"data\">{$lang['strcolumn']}</th><th class=\"data\">{$lang['strtype']}</th>";
992
                echo "<th class=\"data\">{$lang['strformat']}</th>";
993
                echo "<th class=\"data\">{$lang['strnull']}</th><th class=\"data\">{$lang['strvalue']}</th></tr>";
994
995
                $i      = 0;
996
                $fields = [];
997
                while (!$attrs->EOF) {
998
                    $fields[$attrs->fields['attnum']] = $attrs->fields['attname'];
999
                    $attrs->fields['attnotnull']      = $data->phpBool($attrs->fields['attnotnull']);
1000
                    // Set up default value if there isn't one already
1001
                    if (!isset($_REQUEST['values'][$attrs->fields['attnum']])) {
1002
                        $_REQUEST['values'][$attrs->fields['attnum']] = $attrs->fields['adsrc'];
1003
                    }
1004
1005
                    // Default format to 'VALUE' if there is no default,
1006
                    // otherwise default to 'EXPRESSION'
1007
                    if (!isset($_REQUEST['format'][$attrs->fields['attnum']])) {
1008
                        $_REQUEST['format'][$attrs->fields['attnum']] = (null === $attrs->fields['adsrc']) ? 'VALUE' : 'EXPRESSION';
1009
                    }
1010
1011
                    // Continue drawing row
1012
                    $id = (0 == ($i % 2) ? '1' : '2');
1013
                    echo "<tr class=\"data{$id}\">\n";
1014
                    echo '<td style="white-space:nowrap;">', $this->misc->printVal($attrs->fields['attname']), '</td>';
1015
                    echo "<td style=\"white-space:nowrap;\">\n";
1016
                    echo $this->misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod']));
1017
                    echo "<input type=\"hidden\" name=\"types[{$attrs->fields['attnum']}]\" value=\"",
1018
                    htmlspecialchars($attrs->fields['type']), '" /></td>';
1019
                    echo "<td style=\"white-space:nowrap;\">\n";
1020
                    echo "<select name=\"format[{$attrs->fields['attnum']}]\">\n";
1021
                    echo '<option value="VALUE"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'VALUE') ? ' selected="selected"' : '', ">{$lang['strvalue']}</option>\n";
1022
                    echo '<option value="EXPRESSION"', ($_REQUEST['format'][$attrs->fields['attnum']] == 'EXPRESSION') ? ' selected="selected"' : '', ">{$lang['strexpression']}</option>\n";
1023
                    echo "</select>\n</td>\n";
1024
                    echo '<td style="white-space:nowrap;">';
1025
                    // Output null box if the column allows nulls (doesn't look at CHECKs or ASSERTIONS)
1026
                    if (!$attrs->fields['attnotnull']) {
1027
                        echo "<label><span><input type=\"checkbox\" name=\"nulls[{$attrs->fields['attnum']}]\"",
1028
                        isset($_REQUEST['nulls'][$attrs->fields['attnum']]) ? ' checked="checked"' : '', ' /></span></label></td>';
1029
                    } else {
1030
                        echo '&nbsp;</td>';
1031
                    }
1032
                    echo "<td id=\"row_att_{$attrs->fields['attnum']}\" style=\"white-space:nowrap;\">";
1033
                    if ((false !== $fksprops) && isset($fksprops['byfield'][$attrs->fields['attnum']])) {
1034
                        echo $data->printField(
1035
                            "values[{$attrs->fields['attnum']}]",
1036
                            $_REQUEST['values'][$attrs->fields['attnum']],
1037
                            'fktype' /*force FK*/,
1038
                            [
1039
                                'id'           => "attr_{$attrs->fields['attnum']}",
1040
                                'autocomplete' => 'off',
1041
                            ]
1042
                        );
1043
                    } else {
1044
                        echo $data->printField("values[{$attrs->fields['attnum']}]", $_REQUEST['values'][$attrs->fields['attnum']], $attrs->fields['type']);
1045
                    }
1046
                    echo "</td>\n";
1047
                    echo "</tr>\n";
1048
                    ++$i;
1049
                    $attrs->moveNext();
1050
                }
1051
                echo "</table>\n";
1052
1053
                if (!isset($_SESSION['counter'])) {
1054
                    $_SESSION['counter'] = 0;
1055
                }
1056
1057
                echo "<input type=\"hidden\" name=\"action\" value=\"insertrow\" />\n";
1058
                echo '<input type="hidden" name="fields" value="', htmlentities(serialize($fields), ENT_QUOTES, 'UTF-8'), "\" />\n";
1059
                echo '<input type="hidden" name="protection_counter" value="' . $_SESSION['counter'] . "\" />\n";
1060
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
1061
                echo "<p><input type=\"submit\" name=\"insert\" value=\"{$lang['strinsert']}\" />\n";
1062
                echo "<input type=\"submit\" name=\"insertandrepeat\" accesskey=\"r\" value=\"{$lang['strinsertandrepeat']}\" />\n";
1063
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1064
1065
                if (false !== $fksprops) {
1066
                    if ('default off' != $conf['autocomplete']) {
1067
                        echo "<input type=\"checkbox\" id=\"no_ac\" value=\"1\" checked=\"checked\" /><label for=\"no_ac\">{$lang['strac']}</label>\n";
1068
                    } else {
1069
                        echo "<input type=\"checkbox\" id=\"no_ac\" value=\"0\" /><label for=\"no_ac\">{$lang['strac']}</label>\n";
1070
                    }
1071
                }
1072
                echo "</p>\n";
1073
            } else {
1074
                echo "<p>{$lang['strnofieldsforinsert']}</p>\n";
1075
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1076
            }
1077
            echo $this->misc->form;
1078
            echo "</form>\n";
1079
        } else {
1080
            if (!isset($_POST['values'])) {
1081
                $_POST['values'] = [];
1082
            }
1083
1084
            if (!isset($_POST['nulls'])) {
1085
                $_POST['nulls'] = [];
1086
            }
1087
1088
            $_POST['fields'] = unserialize(htmlspecialchars_decode($_POST['fields'], ENT_QUOTES));
1089
1090
            if ($_SESSION['counter']++ == $_POST['protection_counter']) {
1091
                $status = $data->insertRow($_POST['table'], $_POST['fields'], $_POST['values'], $_POST['nulls'], $_POST['format'], $_POST['types']);
1092
                if (0 == $status) {
1093
                    if (isset($_POST['insert'])) {
1094
                        return $this->doDefault($lang['strrowinserted']);
1095
                    }
1096
                    $_REQUEST['values'] = [];
1097
                    $_REQUEST['nulls']  = [];
1098
                    $this->doInsertRow(true, $lang['strrowinserted']);
1099
                } else {
1100
                    $this->doInsertRow(true, $lang['strrowinsertedbad']);
1101
                }
1102
            } else {
1103
                $this->doInsertRow(true, $lang['strrowduplicate']);
1104
            }
1105
        }
1106
    }
1107
1108
    /**
1109
     * Show confirmation of empty and perform actual empty.
1110
     *
1111
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
1112
     */
1113
    public function doEmpty($confirm)
1114
    {
1115
        $lang = $this->lang;
1116
        $data = $this->misc->getDatabaseAccessor();
1117
1118
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1119
            return $this->doDefault($lang['strspecifytabletoempty']);
1120
        }
1121
1122
        if ($confirm) {
1123
            if (isset($_REQUEST['ma'])) {
1124
                $this->printTrail('schema');
1125
                $this->printTitle($lang['strempty'], 'pg.table.empty');
1126
1127
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
1128
                foreach ($_REQUEST['ma'] as $v) {
1129
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
1130
                    echo '<p>', sprintf($lang['strconfemptytable'], $this->misc->printVal($a['table'])), "</p>\n";
1131
                    printf('<input type="hidden" name="table[]" value="%s" />', htmlspecialchars($a['table']));
1132
                }
1133
            } // END mutli empty
1134
            else {
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
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
1142
            } // END not mutli empty
1143
1144
            echo "<input type=\"hidden\" name=\"action\" value=\"empty\" />\n";
1145
            echo $this->misc->form;
1146
            echo "<input type=\"submit\" name=\"empty\" value=\"{$lang['strempty']}\" /> <input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1147
            echo "</form>\n";
1148
        } // END if confirm
1149
        else {
1150
            // Do Empty
1151
            if (is_array($_REQUEST['table'])) {
1152
                $msg = '';
1153
                foreach ($_REQUEST['table'] as $t) {
1154
                    $status = $data->emptyTable($t);
1155
                    if (0 == $status) {
1156
                        $msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtableemptied']);
1157
                    } else {
1158
                        $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtableemptiedbad']));
1159
1160
                        return;
1161
                    }
1162
                }
1163
                $this->doDefault($msg);
1164
            } // END mutli empty
1165
            else {
1166
                $status = $data->emptyTable($_POST['table']);
1167
                if (0 == $status) {
1168
                    return $this->doDefault($lang['strtableemptied']);
1169
                }
1170
1171
                return $this->doDefault($lang['strtableemptiedbad']);
1172
            } // END not mutli empty
1173
        } // END do Empty
1174
    }
1175
1176
    /**
1177
     * Show confirmation of drop and perform actual drop.
1178
     *
1179
     * @param mixed $confirm
1 ignored issue
show
Missing parameter comment
Loading history...
1180
     */
1181
    public function doDrop($confirm)
1182
    {
1183
        $lang = $this->lang;
1184
        $data = $this->misc->getDatabaseAccessor();
1185
1186
        if (empty($_REQUEST['table']) && empty($_REQUEST['ma'])) {
1187
            return $this->doDefault($lang['strspecifytabletodrop']);
1188
        }
1189
1190
        if ($confirm) {
1191
            //If multi drop
1192
            if (isset($_REQUEST['ma'])) {
1193
                $this->printTrail('schema');
1194
                $this->printTitle($lang['strdrop'], 'pg.table.drop');
1195
1196
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
1197
                foreach ($_REQUEST['ma'] as $v) {
1198
                    $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES));
1199
                    echo '<p>', sprintf($lang['strconfdroptable'], $this->misc->printVal($a['table'])), "</p>\n";
1200
                    printf('<input type="hidden" name="table[]" value="%s" />', htmlspecialchars($a['table']));
1201
                }
1202
            } else {
1203
                $this->printTrail('table');
1204
                $this->printTitle($lang['strdrop'], 'pg.table.drop');
1205
1206
                echo '<p>', sprintf($lang['strconfdroptable'], $this->misc->printVal($_REQUEST['table'])), "</p>\n";
1207
1208
                echo '<form action="' . \SUBFOLDER . "/src/views/tables.php\" method=\"post\">\n";
1209
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
1210
            } // END if multi drop
1211
1212
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
1213
            echo $this->misc->form;
1214
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
1215
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
1216
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
1217
            echo "</form>\n";
1218
        } // END confirm
1219
        else {
1220
            //If multi drop
1221
            if (is_array($_REQUEST['table'])) {
1222
                $msg    = '';
1223
                $status = $data->beginTransaction();
1224
                if (0 == $status) {
1225
                    foreach ($_REQUEST['table'] as $t) {
1226
                        $status = $data->dropTable($t, isset($_POST['cascade']));
1227
                        if (0 == $status) {
1228
                            $msg .= sprintf('%s: %s<br />', htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtabledropped']);
1229
                        } else {
1230
                            $data->endTransaction();
1231
1232
                            return $this->doDefault(sprintf('%s%s: %s<br />', $msg, htmlentities($t, ENT_QUOTES, 'UTF-8'), $lang['strtabledroppedbad']));
1233
                        }
1234
                    }
1235
                }
1236
                if (0 == $data->endTransaction()) {
1237
                    // Everything went fine, back to the Default page....
1238
                    $this->misc->setReloadBrowser(true);
1239
1240
                    return $this->doDefault($msg);
1241
                }
1242
1243
                return $this->doDefault($lang['strtabledroppedbad']);
1244
            }
1245
            $status = $data->dropTable($_POST['table'], isset($_POST['cascade']));
1246
            if (0 == $status) {
1247
                $this->misc->setReloadBrowser(true);
1248
1249
                return $this->doDefault($lang['strtabledropped']);
1250
            }
1251
1252
            return $this->doDefault($lang['strtabledroppedbad']);
1253
        } // END DROP
1254
    }
1255
1256
    // END Function
1257
}
1258