Passed
Push — develop ( c2019a...2cab77 )
by Felipe
04:50
created

TablesController   F

Complexity

Total Complexity 150

Size/Duplication

Total Lines 1238
Duplicated Lines 0 %

Importance

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