Cancelled
Push — develop ( 8620b3...1fbc32 )
by Felipe
04:39
created

TablesController::formInsertRow()   D

Complexity

Conditions 20
Paths 21

Size

Total Lines 123
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 123
rs 4.7294
c 0
b 0
f 0
cc 20
eloc 86
nc 21
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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