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

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