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

TblpropertiesController::render()   C

Complexity

Conditions 14
Paths 14

Size

Total Lines 80
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 56
nc 14
nop 0
dl 0
loc 80
rs 5.0545
c 0
b 0
f 0

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
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class TblpropertiesController extends BaseController
15
{
16
    public $controller_name = 'TblpropertiesController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
24
        $misc = $this->misc;
25
        $lang = $this->lang;
26
27
        $action = $this->action;
28
        if ('tree' == $action) {
29
            return $this->doTree();
30
        }
31
        $data = $misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
32
33
        $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...
34
        $header_template = 'header.twig';
35
36
        ob_start();
37
        switch ($action) {
38
            case 'alter':
39
                if (isset($_POST['alter'])) {
40
                    $this->doSaveAlter();
41
                } else {
42
                    $this->doDefault();
43
                }
44
45
                break;
46
            case 'confirm_alter':
47
                $this->doAlter();
48
49
                break;
50
            case 'import':
51
                $this->doImport();
52
53
                break;
54
            case 'export':
55
                $this->doExport();
56
57
                break;
58
            case 'add_column':
59
                if (isset($_POST['cancel'])) {
60
                    $this->doDefault();
61
                } else {
62
                    $header_template = 'header_select2.twig';
63
                    $this->doAddColumn();
64
                }
65
66
                break;
67
            case 'properties':
68
                if (isset($_POST['cancel'])) {
69
                    $this->doDefault();
70
                } else {
71
                    $this->doProperties();
0 ignored issues
show
Bug introduced by
The method doProperties() does not exist on PHPPgAdmin\Controller\TblpropertiesController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                    $this->/** @scrutinizer ignore-call */ 
72
                           doProperties();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
                }
73
74
                break;
75
            case 'drop':
76
                if (isset($_POST['drop'])) {
77
                    $this->doDrop(false);
78
                } else {
79
                    $this->doDefault();
80
                }
81
82
                break;
83
            case 'confirm_drop':
84
                $this->doDrop(true);
85
86
                break;
87
            default:
88
                $this->doDefault();
89
90
                break;
91
        }
92
93
        $output = ob_get_clean();
94
95
        $this->printHeader($lang['strtables'].' - '.$_REQUEST['table'], null, true, $header_template);
96
        $this->printBody();
97
98
        echo $output;
99
100
        return $this->printFooter();
101
    }
102
103
    /**
104
     * Show default list of columns in the table.
105
     *
106
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     */
108
    public function doDefault($msg = '')
109
    {
110
        $conf = $this->conf;
111
        $misc = $this->misc;
112
        $lang = $this->lang;
113
        $data = $misc->getDatabaseAccessor();
114
115
        $attPre = function (&$rowdata, $actions) use ($data) {
116
            $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']);
117
            $attname                  = $rowdata->fields['attname'];
118
            $table                    = $_REQUEST['table'];
119
            $data->fieldClean($attname);
120
            $data->fieldClean($table);
121
122
            $actions['browse']['attr']['href']['urlvars']['query'] = "SELECT \"{$attname}\", count(*) AS \"count\"
123
                FROM \"{$table}\" GROUP BY \"{$attname}\" ORDER BY \"{$attname}\"";
124
125
            return $actions;
126
        };
127
128
        $cstrRender = function ($s, $p) use ($misc, $data) {
129
            $str = '';
130
            foreach ($p['keys'] as $k => $c) {
131
                if (is_null($p['keys'][$k]['consrc'])) {
132
                    $atts        = $data->getAttributeNames($_REQUEST['table'], explode(' ', $p['keys'][$k]['indkey']));
133
                    $c['consrc'] = ('u' == $c['contype'] ? 'UNIQUE (' : 'PRIMARY KEY (').join(',', $atts).')';
134
                }
135
136
                if ($c['p_field'] == $s) {
137
                    switch ($c['contype']) {
138
                        case 'p':
139
                            $str .= '<a href="constraints.php?'.$misc->href.'&amp;table='.urlencode($c['p_table']).'&amp;schema='.urlencode($c['p_schema']).'"><img src="'.
140
                            $misc->icon('PrimaryKey').'" alt="[pk]" title="'.htmlentities($c['consrc'], ENT_QUOTES, 'UTF-8').'" /></a>';
141
142
                            break;
143
                        case 'f':
144
                            $str .= '<a href="tblproperties.php?'.$misc->href.'&amp;table='.urlencode($c['f_table']).'&amp;schema='.urlencode($c['f_schema']).'"><img src="'.
145
                            $misc->icon('ForeignKey').'" alt="[fk]" title="'.htmlentities($c['consrc'], ENT_QUOTES, 'UTF-8').'" /></a>';
146
147
                            break;
148
                        case 'u':
149
                            $str .= '<a href="constraints.php?'.$misc->href.'&amp;table='.urlencode($c['p_table']).'&amp;schema='.urlencode($c['p_schema']).'"><img src="'.
150
                            $misc->icon('UniqueConstraint').'" alt="[uniq]" title="'.htmlentities($c['consrc'], ENT_QUOTES, 'UTF-8').'" /></a>';
151
152
                            break;
153
                        case 'c':
154
                            $str .= '<a href="constraints.php?'.$misc->href.'&amp;table='.urlencode($c['p_table']).'&amp;schema='.urlencode($c['p_schema']).'"><img src="'.
155
                            $misc->icon('CheckConstraint').'" alt="[check]" title="'.htmlentities($c['consrc'], ENT_QUOTES, 'UTF-8').'" /></a>';
156
                    }
157
                }
158
            }
159
160
            return $str;
161
        };
162
163
        $this->printTrail('table');
164
        $this->printTabs('table', 'columns');
165
        $this->printMsg($msg);
166
167
        // Get table
168
        $tdata = $data->getTable($_REQUEST['table']);
169
        // Get columns
170
        $attrs = $data->getTableAttributes($_REQUEST['table']);
171
        // Get constraints keys
172
        $ck = $data->getConstraintsWithFields($_REQUEST['table']);
173
174
        // Show comment if any
175
        if (null !== $tdata->fields['relcomment']) {
176
            echo '<p class="comment">', $misc->printVal($tdata->fields['relcomment']), "</p>\n";
177
        }
178
179
        $columns = [
180
            'column' => [
181
                'title' => $lang['strcolumn'],
182
                'field' => Decorator::field('attname'),
183
                'url'   => "colproperties.php?subject=column&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table']).'&amp;',
184
                'vars'  => ['column' => 'attname'],
185
            ],
186
            'type' => [
187
                'title' => $lang['strtype'],
188
                'field' => Decorator::field('+type'),
189
            ],
190
            'notnull' => [
191
                'title'  => $lang['strnotnull'],
192
                'field'  => Decorator::field('attnotnull'),
193
                'type'   => 'bool',
194
                'params' => ['true' => 'NOT NULL', 'false' => ''],
195
            ],
196
            'default' => [
197
                'title' => $lang['strdefault'],
198
                'field' => Decorator::field('adsrc'),
199
            ],
200
            'keyprop' => [
201
                'title'  => $lang['strconstraints'],
202
                'class'  => 'constraint_cell',
203
                'field'  => Decorator::field('attname'),
204
                'type'   => 'callback',
205
                'params' => [
206
                    'function' => $cstrRender,
207
                    'keys'     => $ck->getArray(),
208
                ],
209
            ],
210
            'actions' => [
211
                'title' => $lang['stractions'],
212
            ],
213
            'comment' => [
214
                'title' => $lang['strcomment'],
215
                'field' => Decorator::field('comment'),
216
            ],
217
        ];
218
219
        $actions = [
0 ignored issues
show
Unused Code introduced by
The assignment to $actions is dead and can be removed.
Loading history...
220
            'browse' => [
221
                'title' => $lang['strbrowse'],
222
                'url'   => "display.php?{$misc->href}&amp;subject=column&amp;return=table&amp;table=".urlencode($_REQUEST['table']).'&amp;',
223
                'vars'  => ['column' => 'attname'],
224
            ],
225
            'alter' => [
226
                'title' => $lang['stralter'],
227
                'url'   => "colproperties.php?action=properties&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table']).'&amp;',
228
                'vars'  => ['column' => 'attname'],
229
            ],
230
            'privileges' => [
231
                'title' => $lang['strprivileges'],
232
                'url'   => "privileges.php?subject=column&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table']).'&amp;',
233
                'vars'  => ['column' => 'attname'],
234
            ],
235
            'drop' => [
236
                'title' => $lang['strdrop'],
237
                'url'   => "tblproperties.php?action=confirm_drop&amp;{$misc->href}&amp;table=".urlencode($_REQUEST['table']).'&amp;',
238
                'vars'  => ['column' => 'attname'],
239
            ],
240
        ];
241
242
        $actions = [
243
            'browse' => [
244
                'content' => $lang['strbrowse'],
245
                'attr'    => [
246
                    'href' => [
247
                        'url'     => 'display.php',
248
                        'urlvars' => [
249
                            'table'   => $_REQUEST['table'],
250
                            'subject' => 'column',
251
                            'return'  => 'table',
252
                            'column'  => Decorator::field('attname'),
253
                        ],
254
                    ],
255
                ],
256
            ],
257
            'alter' => [
258
                'content' => $lang['stralter'],
259
                'attr'    => [
260
                    'href' => [
261
                        'url'     => 'colproperties.php',
262
                        'urlvars' => [
263
                            'subject' => 'column',
264
                            'action'  => 'properties',
265
                            'table'   => $_REQUEST['table'],
266
                            'column'  => Decorator::field('attname'),
267
                        ],
268
                    ],
269
                ],
270
            ],
271
            'privileges' => [
272
                'content' => $lang['strprivileges'],
273
                'attr'    => [
274
                    'href' => [
275
                        'url'     => 'privileges.php',
276
                        'urlvars' => [
277
                            'subject' => 'column',
278
                            'table'   => $_REQUEST['table'],
279
                            'column'  => Decorator::field('attname'),
280
                        ],
281
                    ],
282
                ],
283
            ],
284
            'drop' => [
285
                'content' => $lang['strdrop'],
286
                'attr'    => [
287
                    'href' => [
288
                        'url'     => 'tblproperties.php',
289
                        'urlvars' => [
290
                            'subject' => 'column',
291
                            'action'  => 'confirm_drop',
292
                            'table'   => $_REQUEST['table'],
293
                            'column'  => Decorator::field('attname'),
294
                        ],
295
                    ],
296
                ],
297
            ],
298
        ];
299
300
        echo $this->printTable($attrs, $columns, $actions, 'tblproperties-tblproperties', null, $attPre);
301
302
        $navlinks = [
303
            'browse' => [
304
                'attr' => [
305
                    'href' => [
306
                        'url'     => 'display.php',
307
                        'urlvars' => [
308
                            'server'   => $_REQUEST['server'],
309
                            'database' => $_REQUEST['database'],
310
                            'schema'   => $_REQUEST['schema'],
311
                            'table'    => $_REQUEST['table'],
312
                            'subject'  => 'table',
313
                            'return'   => 'table',
314
                        ],
315
                    ],
316
                ],
317
                'content' => $lang['strbrowse'],
318
            ],
319
            'select' => [
320
                'attr' => [
321
                    'href' => [
322
                        'url'     => 'tables.php',
323
                        'urlvars' => [
324
                            'action'   => 'confselectrows',
325
                            'server'   => $_REQUEST['server'],
326
                            'database' => $_REQUEST['database'],
327
                            'schema'   => $_REQUEST['schema'],
328
                            'table'    => $_REQUEST['table'],
329
                        ],
330
                    ],
331
                ],
332
                'content' => $lang['strselect'],
333
            ],
334
            'insert' => [
335
                'attr' => [
336
                    'href' => [
337
                        'url'     => 'tables.php',
338
                        'urlvars' => [
339
                            'action'   => 'confinsertrow',
340
                            'server'   => $_REQUEST['server'],
341
                            'database' => $_REQUEST['database'],
342
                            'schema'   => $_REQUEST['schema'],
343
                            'table'    => $_REQUEST['table'],
344
                        ],
345
                    ],
346
                ],
347
                'content' => $lang['strinsert'],
348
            ],
349
            'empty' => [
350
                'attr' => [
351
                    'href' => [
352
                        'url'     => 'tables.php',
353
                        'urlvars' => [
354
                            'action'   => 'confirm_empty',
355
                            'server'   => $_REQUEST['server'],
356
                            'database' => $_REQUEST['database'],
357
                            'schema'   => $_REQUEST['schema'],
358
                            'table'    => $_REQUEST['table'],
359
                        ],
360
                    ],
361
                ],
362
                'content' => $lang['strempty'],
363
            ],
364
            'drop' => [
365
                'attr' => [
366
                    'href' => [
367
                        'url'     => 'tables.php',
368
                        'urlvars' => [
369
                            'action'   => 'confirm_drop',
370
                            'server'   => $_REQUEST['server'],
371
                            'database' => $_REQUEST['database'],
372
                            'schema'   => $_REQUEST['schema'],
373
                            'table'    => $_REQUEST['table'],
374
                        ],
375
                    ],
376
                ],
377
                'content' => $lang['strdrop'],
378
            ],
379
            'addcolumn' => [
380
                'attr' => [
381
                    'href' => [
382
                        'url'     => 'tblproperties.php',
383
                        'urlvars' => [
384
                            'action'   => 'add_column',
385
                            'server'   => $_REQUEST['server'],
386
                            'database' => $_REQUEST['database'],
387
                            'schema'   => $_REQUEST['schema'],
388
                            'table'    => $_REQUEST['table'],
389
                        ],
390
                    ],
391
                ],
392
                'content' => $lang['straddcolumn'],
393
            ],
394
            'alter' => [
395
                'attr' => [
396
                    'href' => [
397
                        'url'     => 'tblproperties.php',
398
                        'urlvars' => [
399
                            'action'   => 'confirm_alter',
400
                            'server'   => $_REQUEST['server'],
401
                            'database' => $_REQUEST['database'],
402
                            'schema'   => $_REQUEST['schema'],
403
                            'table'    => $_REQUEST['table'],
404
                        ],
405
                    ],
406
                ],
407
                'content' => $lang['stralter'],
408
            ],
409
        ];
410
        $this->printNavLinks($navlinks, 'tblproperties-tblproperties', get_defined_vars());
411
    }
412
413
    public function doTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
414
    {
415
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
416
        $misc = $this->misc;
417
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
418
        $data = $misc->getDatabaseAccessor();
419
420
        $columns = $data->getTableAttributes($_REQUEST['table']);
421
        $reqvars = $misc->getRequestVars('column');
422
423
        $attrs = [
424
            'text'   => Decorator::field('attname'),
425
            'action' => Decorator::actionurl(
426
                'colproperties.php',
427
                $reqvars,
428
                [
429
                    'table'  => $_REQUEST['table'],
430
                    'column' => Decorator::field('attname'),
431
                ]
432
            ),
433
            'icon'       => 'Column',
434
            'iconAction' => Decorator::url(
435
                'display.php',
436
                $reqvars,
437
                [
438
                    'table'  => $_REQUEST['table'],
439
                    'column' => Decorator::field('attname'),
440
                    'query'  => Decorator::replace(
441
                        'SELECT "%column%", count(*) AS "count" FROM "%table%" GROUP BY "%column%" ORDER BY "%column%"',
442
                        [
443
                            '%column%' => Decorator::field('attname'),
444
                            '%table%'  => $_REQUEST['table'],
445
                        ]
446
                    ),
447
                ]
448
            ),
449
            'toolTip' => Decorator::field('comment'),
450
        ];
451
452
        return $this->printTree($columns, $attrs, 'tblcolumns');
453
    }
454
455
    public function doSaveAlter()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
456
    {
457
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
458
        $misc = $this->misc;
459
        $lang = $this->lang;
460
        $data = $misc->getDatabaseAccessor();
461
462
        // For databases that don't allow owner change
463
        if (!isset($_POST['owner'])) {
464
            $_POST['owner'] = '';
465
        }
466
467
        // Default tablespace to null if it isn't set
468
        if (!isset($_POST['tablespace'])) {
469
            $_POST['tablespace'] = null;
470
        }
471
472
        if (!isset($_POST['newschema'])) {
473
            $_POST['newschema'] = null;
474
        }
475
476
        $status = $data->alterTable($_POST['table'], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment'], $_POST['tablespace']);
477
        if (0 == $status) {
478
            // If table has been renamed, need to change to the new name and
479
            // reload the browser frame.
480
            if ($_POST['table'] != $_POST['name']) {
481
                // Jump them to the new table name
482
                $_REQUEST['table'] = $_POST['name'];
483
                // Force a browser reload
484
                $misc->setReloadBrowser(true);
485
            }
486
            // If schema has changed, need to change to the new schema and reload the browser
487
            if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
488
                // Jump them to the new sequence schema
489
                $misc->setCurrentSchema($_POST['newschema']);
490
                $misc->setReloadBrowser(true);
491
            }
492
            $this->doDefault($lang['strtablealtered']);
493
        } else {
494
            $this->doAlter($lang['strtablealteredbad']);
495
        }
496
    }
497
498
    /**
499
     * Function to allow altering of a table.
500
     *
501
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
502
     */
503
    public function doAlter($msg = '')
504
    {
505
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
506
        $misc = $this->misc;
507
        $lang = $this->lang;
508
        $data = $misc->getDatabaseAccessor();
509
510
        $this->printTrail('table');
511
        $this->printTitle($lang['stralter'], 'pg.table.alter');
512
        $this->printMsg($msg);
513
514
        // Fetch table info
515
        $table = $data->getTable($_REQUEST['table']);
516
        // Fetch all users
517
        $users = $data->getUsers();
518
        // Fetch all tablespaces from the database
519
        if ($data->hasTablespaces()) {
520
            $tablespaces = $data->getTablespaces(true);
521
        }
522
523
        if ($table->recordCount() > 0) {
524
            if (!isset($_POST['name'])) {
525
                $_POST['name'] = $table->fields['relname'];
526
            }
527
528
            if (!isset($_POST['owner'])) {
529
                $_POST['owner'] = $table->fields['relowner'];
530
            }
531
532
            if (!isset($_POST['newschema'])) {
533
                $_POST['newschema'] = $table->fields['nspname'];
534
            }
535
536
            if (!isset($_POST['comment'])) {
537
                $_POST['comment'] = $table->fields['relcomment'];
538
            }
539
540
            if ($data->hasTablespaces() && !isset($_POST['tablespace'])) {
541
                $_POST['tablespace'] = $table->fields['tablespace'];
542
            }
543
544
            echo '<form action="'.\SUBFOLDER."/src/views/tblproperties.php\" method=\"post\">\n";
545
            echo "<table>\n";
546
            echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
547
            echo '<td class="data1">';
548
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
549
            htmlspecialchars($_POST['name'], ENT_QUOTES), "\" /></td></tr>\n";
550
551
            if ($data->isSuperUser()) {
552
                echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
553
                echo '<td class="data1"><select name="owner">';
554
                while (!$users->EOF) {
555
                    $uname = $users->fields['usename'];
556
                    echo '<option value="', htmlspecialchars($uname), '"',
557
                    ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
558
                    $users->moveNext();
559
                }
560
                echo "</select></td></tr>\n";
561
            }
562
563
            if ($data->hasAlterTableSchema()) {
564
                $schemas = $data->getSchemas();
565
                echo "<tr><th class=\"data left required\">{$lang['strschema']}</th>\n";
566
                echo '<td class="data1"><select name="newschema">';
567
                while (!$schemas->EOF) {
568
                    $schema = $schemas->fields['nspname'];
569
                    echo '<option value="', htmlspecialchars($schema), '"',
570
                    ($schema == $_POST['newschema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
571
                    $schemas->moveNext();
572
                }
573
                echo "</select></td></tr>\n";
574
            }
575
576
            // Tablespace (if there are any)
577
            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...
578
                echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n";
579
                echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"tablespace\">\n";
580
                // Always offer the default (empty) option
581
                echo "\t\t\t\t<option value=\"\"",
582
                ('' == $_POST['tablespace']) ? ' selected="selected"' : '', "></option>\n";
583
                // Display all other tablespaces
584
                while (!$tablespaces->EOF) {
585
                    $spcname = htmlspecialchars($tablespaces->fields['spcname']);
586
                    echo "\t\t\t\t<option value=\"{$spcname}\"",
587
                    ($spcname == $_POST['tablespace']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
588
                    $tablespaces->moveNext();
589
                }
590
                echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n";
591
            }
592
593
            echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
594
            echo '<td class="data1">';
595
            echo '<textarea rows="3" cols="32" name="comment">',
596
            htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
597
            echo "</table>\n";
598
            echo "<p><input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
599
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
600
            echo $misc->form;
601
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
602
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
603
            echo "</form>\n";
604
        } else {
605
            echo "<p>{$lang['strnodata']}</p>\n";
606
        }
607
    }
608
609
    public function doExport($msg = '')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
610
    {
611
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
612
        $misc = $this->misc;
613
        $lang = $this->lang;
614
        $data = $misc->getDatabaseAccessor();
615
616
        // Determine whether or not the table has an object ID
617
        $hasID = $data->hasObjectID($_REQUEST['table']);
618
619
        $this->printTrail('table');
620
        $this->printTabs('table', 'export');
621
        $this->printMsg($msg);
622
623
        echo '<form action="'.\SUBFOLDER."/src/views/dataexport.php\" method=\"post\">\n";
624
        echo "<table>\n";
625
        echo "<tr><th class=\"data\">{$lang['strformat']}</th><th class=\"data\" colspan=\"2\">{$lang['stroptions']}</th></tr>\n";
626
        // Data only
627
        echo '<tr><th class="data left" rowspan="', ($hasID) ? 2 : 1, '">';
628
        echo "<input type=\"radio\" id=\"what1\" name=\"what\" value=\"dataonly\" checked=\"checked\" /><label for=\"what1\">{$lang['strdataonly']}</label></th>\n";
629
        echo "<td>{$lang['strformat']}</td>\n";
630
        echo "<td><select name=\"d_format\">\n";
631
        echo "<option value=\"copy\">COPY</option>\n";
632
        echo "<option value=\"sql\">SQL</option>\n";
633
        echo "<option value=\"csv\">CSV</option>\n";
634
        echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n";
635
        echo "<option value=\"html\">XHTML</option>\n";
636
        echo "<option value=\"xml\">XML</option>\n";
637
        echo "</select>\n</td>\n</tr>\n";
638
        if ($hasID) {
639
            echo "<tr><td><label for=\"d_oids\">{$lang['stroids']}</td><td><input type=\"checkbox\" id=\"d_oids\" name=\"d_oids\" /></td>\n</tr>\n";
640
        }
641
        // Structure only
642
        echo "<tr><th class=\"data left\"><input type=\"radio\" id=\"what2\" name=\"what\" value=\"structureonly\" /><label for=\"what2\">{$lang['strstructureonly']}</label></th>\n";
643
        echo "<td><label for=\"s_clean\">{$lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"s_clean\" name=\"s_clean\" /></td>\n</tr>\n";
644
        // Structure and data
645
        echo '<tr><th class="data left" rowspan="', ($hasID) ? 3 : 2, '">';
646
        echo "<input type=\"radio\" id=\"what3\" name=\"what\" value=\"structureanddata\" /><label for=\"what3\">{$lang['strstructureanddata']}</label></th>\n";
647
        echo "<td>{$lang['strformat']}</td>\n";
648
        echo "<td><select name=\"sd_format\">\n";
649
        echo "<option value=\"copy\">COPY</option>\n";
650
        echo "<option value=\"sql\">SQL</option>\n";
651
        echo "</select>\n</td>\n</tr>\n";
652
        echo "<tr><td><label for=\"sd_clean\">{$lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"sd_clean\" name=\"sd_clean\" /></td>\n</tr>\n";
653
        if ($hasID) {
654
            echo "<tr><td><label for=\"sd_oids\">{$lang['stroids']}</label></td><td><input type=\"checkbox\" id=\"sd_oids\" name=\"sd_oids\" /></td>\n</tr>\n";
655
        }
656
        echo "</table>\n";
657
658
        echo "<h3>{$lang['stroptions']}</h3>\n";
659
        echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$lang['strshow']}</label>\n";
660
        echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$lang['strdownload']}</label></p>\n";
661
662
        echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n";
663
        echo $misc->form;
664
        echo "<input type=\"hidden\" name=\"subject\" value=\"table\" />\n";
665
        echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
666
        echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n";
667
        echo "</form>\n";
668
    }
669
670
    public function doImport($msg = '')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
671
    {
672
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
673
        $misc = $this->misc;
674
        $lang = $this->lang;
675
        $data = $misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
676
677
        $this->printTrail('table');
678
        $this->printTabs('table', 'import');
679
        $this->printMsg($msg);
680
681
        // Check that file uploads are enabled
682
        if (ini_get('file_uploads')) {
683
            // Don't show upload option if max size of uploads is zero
684
            $max_size = $misc->inisizeToBytes(ini_get('upload_max_filesize'));
685
            if (is_double($max_size) && $max_size > 0) {
686
                echo '<form action="'.\SUBFOLDER."/src/views/dataimport.php\" method=\"post\" enctype=\"multipart/form-data\">\n";
687
                echo "<table>\n";
688
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strformat']}</th>\n";
689
                echo "\t\t<td><select name=\"format\">\n";
690
                echo "\t\t\t<option value=\"auto\">{$lang['strauto']}</option>\n";
691
                echo "\t\t\t<option value=\"csv\">CSV</option>\n";
692
                echo "\t\t\t<option value=\"tab\">{$lang['strtabbed']}</option>\n";
693
                if (function_exists('xml_parser_create')) {
694
                    echo "\t\t\t<option value=\"xml\">XML</option>\n";
695
                }
696
                echo "\t\t</select></td>\n\t</tr>\n";
697
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strallowednulls']}</th>\n";
698
                echo "\t\t<td><label><input type=\"checkbox\" name=\"allowednulls[0]\" value=\"\\N\" checked=\"checked\" />{$lang['strbackslashn']}</label><br />\n";
699
                echo "\t\t<label><input type=\"checkbox\" name=\"allowednulls[1]\" value=\"NULL\" />NULL</label><br />\n";
700
                echo "\t\t<label><input type=\"checkbox\" name=\"allowednulls[2]\" value=\"\" />{$lang['stremptystring']}</label></td>\n\t</tr>\n";
701
                echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strfile']}</th>\n";
702
                echo "\t\t<td><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"{$max_size}\" />";
703
                echo "<input type=\"file\" name=\"source\" /></td>\n\t</tr>\n";
704
                echo "</table>\n";
705
                echo "<p><input type=\"hidden\" name=\"action\" value=\"import\" />\n";
706
                echo $misc->form;
707
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
708
                echo "<input type=\"submit\" value=\"{$lang['strimport']}\" /></p>\n";
709
                echo "</form>\n";
710
            }
711
        } else {
712
            echo "<p>{$lang['strnouploads']}</p>\n";
713
        }
714
    }
715
716
    /**
717
     * Displays a screen where they can add a column.
718
     *
719
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
720
     */
721
    public function doAddColumn($msg = '')
722
    {
723
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
724
        $misc = $this->misc;
725
        $lang = $this->lang;
726
        $data = $misc->getDatabaseAccessor();
727
728
        if (!isset($_REQUEST['stage'])) {
729
            $_REQUEST['stage'] = 1;
730
        }
731
732
        switch ($_REQUEST['stage']) {
733
            case 1:
734
                // Set variable defaults
735
                if (!isset($_POST['field'])) {
736
                    $_POST['field'] = '';
737
                }
738
739
                if (!isset($_POST['type'])) {
740
                    $_POST['type'] = '';
741
                }
742
743
                if (!isset($_POST['array'])) {
744
                    $_POST['array'] = '';
745
                }
746
747
                if (!isset($_POST['length'])) {
748
                    $_POST['length'] = '';
749
                }
750
751
                if (!isset($_POST['default'])) {
752
                    $_POST['default'] = '';
753
                }
754
755
                if (!isset($_POST['comment'])) {
756
                    $_POST['comment'] = '';
757
                }
758
759
                // Fetch all available types
760
                $types        = $data->getTypes(true, false, true);
761
                $types_for_js = [];
762
763
                $this->printTrail('table');
764
                $this->printTitle($lang['straddcolumn'], 'pg.column.add');
765
                $this->printMsg($msg);
766
767
                echo '<script src="'.\SUBFOLDER.'/js/tables.js" type="text/javascript"></script>';
768
                echo '<form action="'.\SUBFOLDER."/src/views/tblproperties.php\" method=\"post\">\n";
769
770
                // Output table header
771
                echo "<table>\n";
772
                echo "<tr><th class=\"data required\">{$lang['strname']}</th>\n<th colspan=\"2\" class=\"data required\">{$lang['strtype']}</th>\n";
773
                echo "<th class=\"data\">{$lang['strlength']}</th>\n";
774
                if ($data->hasCreateFieldWithConstraints()) {
775
                    echo "<th class=\"data\">{$lang['strnotnull']}</th>\n<th class=\"data\">{$lang['strdefault']}</th>\n";
776
                }
777
778
                echo "<th class=\"data\">{$lang['strcomment']}</th></tr>\n";
779
780
                echo "<tr><td><input name=\"field\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
781
                htmlspecialchars($_POST['field']), "\" /></td>\n";
782
                echo "<td><select  class=\"select2\" name=\"type\" id=\"type\" onchange=\"checkLengths(document.getElementById('type').value,'');\">\n";
783
                // Output any "magic" types.  This came in with the alter column type so we'll check that
784
                if ($data->hasMagicTypes()) {
785
                    foreach ($data->extraTypes as $v) {
786
                        $types_for_js[] = strtolower($v);
787
                        echo "\t<option value=\"", htmlspecialchars($v), '"',
788
                        ($v == $_POST['type']) ? ' selected="selected"' : '', '>',
789
                        $misc->printVal($v), "</option>\n";
790
                    }
791
                }
792
                while (!$types->EOF) {
793
                    $typname        = $types->fields['typname'];
794
                    $types_for_js[] = $typname;
795
                    echo "\t<option value=\"", htmlspecialchars($typname), '"', ($typname == $_POST['type']) ? ' selected="selected"' : '', '>',
796
                    $misc->printVal($typname), "</option>\n";
797
                    $types->moveNext();
798
                }
799
                echo "</select></td>\n";
800
801
                // Output array type selector
802
                echo "<td><select name=\"array\">\n";
803
                echo "\t<option value=\"\"", ('' == $_POST['array']) ? ' selected="selected"' : '', "></option>\n";
804
                echo "\t<option value=\"[]\"", ('[]' == $_POST['array']) ? ' selected="selected"' : '', ">[ ]</option>\n";
805
                echo "</select></td>\n";
806
                $predefined_size_types = array_intersect($data->predefined_size_types, $types_for_js);
807
                $escaped_predef_types  = []; // the JS escaped array elements
808
                foreach ($predefined_size_types as $value) {
809
                    $escaped_predef_types[] = "'{$value}'";
810
                }
811
812
                echo '<td><input name="length" id="lengths" size="8" value="',
813
                htmlspecialchars($_POST['length']), "\" /></td>\n";
814
                // Support for adding column with not null and default
815
                if ($data->hasCreateFieldWithConstraints()) {
816
                    echo '<td><input type="checkbox" name="notnull"',
817
                    (isset($_REQUEST['notnull'])) ? ' checked="checked"' : '', " /></td>\n";
818
                    echo '<td><input name="default" size="20" value="',
819
                    htmlspecialchars($_POST['default']), "\" /></td>\n";
820
                }
821
                echo '<td><input name="comment" size="40" value="',
822
                htmlspecialchars($_POST['comment']), "\" /></td></tr>\n";
823
                echo "</table>\n";
824
                echo "<p><input type=\"hidden\" name=\"action\" value=\"add_column\" />\n";
825
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
826
                echo $misc->form;
827
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
828
                if (!$data->hasCreateFieldWithConstraints()) {
829
                    echo "<input type=\"hidden\" name=\"default\" value=\"\" />\n";
830
                }
831
                echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
832
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
833
                echo "</form>\n";
834
                echo '<script type="text/javascript">predefined_lengths = new Array('.implode(',', $escaped_predef_types).");checkLengths(document.getElementById('type').value,'');</script>\n";
835
836
                break;
837
            case 2:
838
                // Check inputs
839
                if ('' == trim($_POST['field'])) {
840
                    $_REQUEST['stage'] = 1;
841
                    $this->doAddColumn($lang['strcolneedsname']);
842
843
                    return;
844
                }
845
                if (!isset($_POST['length'])) {
846
                    $_POST['length'] = '';
847
                }
848
849
                $status = $data->addColumn(
850
                    $_POST['table'],
851
                    $_POST['field'],
852
                    $_POST['type'],
853
                    '' != $_POST['array'],
854
                    $_POST['length'],
855
                    isset($_POST['notnull']),
856
                    $_POST['default'],
857
                    $_POST['comment']
858
                );
859
                if (0 == $status) {
860
                    $misc->setReloadBrowser(true);
861
                    $this->doDefault($lang['strcolumnadded']);
862
                } else {
863
                    $_REQUEST['stage'] = 1;
864
                    $this->doAddColumn($lang['strcolumnaddedbad']);
865
866
                    return;
867
                }
868
869
                break;
870
            default:
871
                echo "<p>{$lang['strinvalidparam']}</p>\n";
872
        }
873
    }
874
875
    /**
876
     * Show confirmation of drop column and perform actual drop.
877
     *
878
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
879
     */
880
    public function doDrop($confirm)
881
    {
882
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
883
        $misc = $this->misc;
884
        $lang = $this->lang;
885
        $data = $misc->getDatabaseAccessor();
886
887
        if ($confirm) {
888
            $this->printTrail('column');
889
            $this->printTitle($lang['strdrop'], 'pg.column.drop');
890
891
            echo '<p>', sprintf(
892
                $lang['strconfdropcolumn'],
893
                $misc->printVal($_REQUEST['column']),
894
                $misc->printVal($_REQUEST['table'])
895
            ), "</p>\n";
896
897
            echo '<form action="'.\SUBFOLDER."/src/views/tblproperties.php\" method=\"post\">\n";
898
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
899
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
900
            echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n";
901
            echo $misc->form;
902
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\"> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
903
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
904
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
905
            echo "</form>\n";
906
        } else {
907
            $status = $data->dropColumn($_POST['table'], $_POST['column'], isset($_POST['cascade']));
908
            if (0 == $status) {
909
                $misc->setReloadBrowser(true);
910
                $this->doDefault($lang['strcolumndropped']);
911
            } else {
912
                $this->doDefault($lang['strcolumndroppedbad']);
913
            }
914
        }
915
    }
916
}
917