Passed
Push — develop ( ca4520...e7398d )
by Felipe
04:50
created

TablesController   F

Complexity

Total Complexity 150

Size/Duplication

Total Lines 1237
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 150
dl 0
loc 1237
rs 0.6314
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
D render() 0 111 22
B doSubTree() 0 30 1
A doTree() 0 20 1
C doDrop() 0 71 12
B doInsertRow() 0 26 4
A doDefault() 0 52 3
D doCreateLike() 0 106 15
B _getColumns() 0 36 1
C doEmpty() 0 63 10
A displayJson() 0 13 1
D doSelectRows() 0 118 14
F doCreate() 0 259 47
B _getActions() 0 126 1
D formInsertRow() 0 117 18

How to fix   Complexity   

Complex Class

Complex classes like TablesController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TablesController, and based on these observations, apply Extract Interface, too.

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