Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

ConstraintsController   F

Complexity

Total Complexity 103

Size/Duplication

Total Lines 735
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 735
rs 1.263
c 0
b 0
f 0
wmc 103

6 Methods

Rating   Name   Duplication   Size   Complexity  
B doDefault() 0 121 3
D render() 0 102 25
D addPrimaryOrUniqueKey() 0 137 26
B doDrop() 0 31 3
F addForeignKey() 0 243 40
B addCheck() 0 49 6

How to fix   Complexity   

Complex Class

Complex classes like ConstraintsController 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 ConstraintsController, and based on these observations, apply Extract Interface, too.

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 ConstraintsController extends BaseController
15
{
16
    public $controller_name = 'ConstraintsController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $lang = $this->lang;
24
25
        $action = $this->action;
26
        if ('tree' == $action) {
27
            return $this->/* @scrutinizer ignore-call */doTree();
28
        }
29
30
        $this->printHeader(
31
            $lang['strtables'] . ' - ' . $_REQUEST['table'] . ' - ' . $lang['strconstraints'],
32
            '<script src="' . \SUBFOLDER . '/js/indexes.js" type="text/javascript"></script>',
33
            true,
34
            'header_select2.twig'
35
        );
36
37
        if ('add_unique_key' == $action || 'save_add_unique_key' == $action
38
            || 'add_primary_key' == $action || 'save_add_primary_key' == $action
39
            || 'add_foreign_key' == $action || 'save_add_foreign_key' == $action) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
40
            echo '<body onload="init();">';
41
        } else {
42
            $this->printBody();
43
        }
44
45
        switch ($action) {
46
            case 'add_foreign_key':
47
                $this->addForeignKey(1);
48
49
                break;
50
            case 'save_add_foreign_key':
51
                if (isset($_POST['cancel'])) {
52
                    $this->doDefault();
53
                } else {
54
                    $this->addForeignKey($_REQUEST['stage']);
55
                }
56
57
                break;
58
            case 'add_unique_key':
59
                $this->addPrimaryOrUniqueKey('unique', true);
60
61
                break;
62
            case 'save_add_unique_key':
63
                if (isset($_POST['cancel'])) {
64
                    $this->doDefault();
65
                } else {
66
                    $this->addPrimaryOrUniqueKey('unique', false);
67
                }
68
69
                break;
70
            case 'add_primary_key':
71
                $this->addPrimaryOrUniqueKey('primary', true);
72
73
                break;
74
            case 'save_add_primary_key':
75
                if (isset($_POST['cancel'])) {
76
                    $this->doDefault();
77
                } else {
78
                    $this->addPrimaryOrUniqueKey('primary', false);
79
                }
80
81
                break;
82
            case 'add_check':
83
                $this->addCheck(true);
84
85
                break;
86
            case 'save_add_check':
87
                if (isset($_POST['cancel'])) {
88
                    $this->doDefault();
89
                } else {
90
                    $this->addCheck(false);
91
                }
92
93
                break;
94
            case 'save_create':
95
                $this->/** @scrutinizer ignore-call */
1 ignored issue
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
96
                doSaveCreate();
97
98
                break;
99
            case 'create':
100
                $this->/** @scrutinizer ignore-call */
1 ignored issue
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
101
                doCreate();
102
103
                break;
104
            case 'drop':
105
                if (isset($_POST['drop'])) {
106
                    $this->doDrop(false);
107
                } else {
108
                    $this->doDefault();
109
                }
110
111
                break;
112
            case 'confirm_drop':
113
                $this->doDrop(true);
114
115
                break;
116
            default:
117
                $this->doDefault();
118
119
                break;
120
        }
121
122
        $this->printFooter();
123
    }
124
125
    /**
126
     * List all the constraints on the table.
127
     *
128
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     */
130
    public function doDefault($msg = '')
131
    {
132
        $lang = $this->lang;
133
        $data = $this->misc->getDatabaseAccessor();
134
135
        $cnPre = function (&$rowdata) use ($data) {
136
            if (is_null($rowdata->fields['consrc'])) {
137
                $atts                           = $data->getAttributeNames($_REQUEST['table'], explode(' ', $rowdata->fields['indkey']));
138
                $rowdata->fields['+definition'] = ('u' == $rowdata->fields['contype'] ? 'UNIQUE (' : 'PRIMARY KEY (') . join(',', $atts) . ')';
139
            } else {
140
                $rowdata->fields['+definition'] = $rowdata->fields['consrc'];
141
            }
142
        };
143
144
        $this->printTrail('table');
145
        $this->printTabs('table', 'constraints');
146
        $this->printMsg($msg);
147
148
        $constraints = $data->getConstraints($_REQUEST['table']);
149
150
        $columns = [
151
            'constraint' => [
152
                'title' => $lang['strname'],
153
                'field' => Decorator::field('conname'),
154
            ],
155
            'definition' => [
156
                'title' => $lang['strdefinition'],
157
                'field' => Decorator::field('+definition'),
158
                'type'  => 'pre',
159
            ],
160
            'actions'    => [
161
                'title' => $lang['stractions'],
162
            ],
163
            'comment'    => [
164
                'title' => $lang['strcomment'],
165
                'field' => Decorator::field('constcomment'),
166
            ],
167
        ];
168
169
        $actions = [
170
            'drop' => [
171
                'content' => $lang['strdrop'],
172
                'attr'    => [
173
                    'href' => [
174
                        'url'     => 'constraints.php',
175
                        'urlvars' => [
176
                            'action'     => 'confirm_drop',
177
                            'table'      => $_REQUEST['table'],
178
                            'constraint' => Decorator::field('conname'),
179
                            'type'       => Decorator::field('contype'),
180
                        ],
181
                    ],
182
                ],
183
            ],
184
        ];
185
186
        echo $this->printTable($constraints, $columns, $actions, 'constraints-constraints', $lang['strnoconstraints'], $cnPre);
187
188
        $navlinks = [
189
            'addcheck' => [
190
                'attr'    => [
191
                    'href' => [
192
                        'url'     => 'constraints.php',
193
                        'urlvars' => [
194
                            'action'   => 'add_check',
195
                            'server'   => $_REQUEST['server'],
196
                            'database' => $_REQUEST['database'],
197
                            'schema'   => $_REQUEST['schema'],
198
                            'table'    => $_REQUEST['table'],
199
                        ],
200
                    ],
201
                ],
202
                'content' => $lang['straddcheck'],
203
            ],
204
            'adduniq'  => [
205
                'attr'    => [
206
                    'href' => [
207
                        'url'     => 'constraints.php',
208
                        'urlvars' => [
209
                            'action'   => 'add_unique_key',
210
                            'server'   => $_REQUEST['server'],
211
                            'database' => $_REQUEST['database'],
212
                            'schema'   => $_REQUEST['schema'],
213
                            'table'    => $_REQUEST['table'],
214
                        ],
215
                    ],
216
                ],
217
                'content' => $lang['stradduniq'],
218
            ],
219
            'addpk'    => [
220
                'attr'    => [
221
                    'href' => [
222
                        'url'     => 'constraints.php',
223
                        'urlvars' => [
224
                            'action'   => 'add_primary_key',
225
                            'server'   => $_REQUEST['server'],
226
                            'database' => $_REQUEST['database'],
227
                            'schema'   => $_REQUEST['schema'],
228
                            'table'    => $_REQUEST['table'],
229
                        ],
230
                    ],
231
                ],
232
                'content' => $lang['straddpk'],
233
            ],
234
            'addfk'    => [
235
                'attr'    => [
236
                    'href' => [
237
                        'url'     => 'constraints.php',
238
                        'urlvars' => [
239
                            'action'   => 'add_foreign_key',
240
                            'server'   => $_REQUEST['server'],
241
                            'database' => $_REQUEST['database'],
242
                            'schema'   => $_REQUEST['schema'],
243
                            'table'    => $_REQUEST['table'],
244
                        ],
245
                    ],
246
                ],
247
                'content' => $lang['straddfk'],
248
            ],
249
        ];
250
        $this->printNavLinks($navlinks, 'constraints-constraints', get_defined_vars());
251
    }
252
253
    /**
254
     * Confirm and then actually add a FOREIGN KEY constraint.
255
     *
256
     * @param mixed $stage
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
257
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
258
     */
259
    public function addForeignKey($stage, $msg = '')
260
    {
261
        $lang = $this->lang;
262
        $data = $this->misc->getDatabaseAccessor();
263
264
        if (!isset($_POST['name'])) {
265
            $_POST['name'] = '';
266
        }
267
268
        if (!isset($_POST['target'])) {
269
            $_POST['target'] = '';
270
        }
271
272
        switch ($stage) {
273
            case 2:
274
                // Check that they've given at least one source column
275
                if (!isset($_REQUEST['SourceColumnList']) && (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || 0 == sizeof($_POST['IndexColumnList']))) {
276
                    $this->addForeignKey(1, $lang['strfkneedscols']);
277
                } else {
278
                    // Copy the IndexColumnList variable from stage 1
279
                    if (isset($_REQUEST['IndexColumnList']) && !isset($_REQUEST['SourceColumnList'])) {
280
                        $_REQUEST['SourceColumnList'] = serialize($_REQUEST['IndexColumnList']);
281
                    }
282
283
                    // Initialise variables
284
                    if (!isset($_POST['upd_action'])) {
285
                        $_POST['upd_action'] = null;
286
                    }
287
288
                    if (!isset($_POST['del_action'])) {
289
                        $_POST['del_action'] = null;
290
                    }
291
292
                    if (!isset($_POST['match'])) {
293
                        $_POST['match'] = null;
294
                    }
295
296
                    if (!isset($_POST['deferrable'])) {
297
                        $_POST['deferrable'] = null;
298
                    }
299
300
                    if (!isset($_POST['initially'])) {
301
                        $_POST['initially'] = null;
302
                    }
303
304
                    $_REQUEST['target'] = unserialize($_REQUEST['target']);
305
306
                    $this->printTrail('table');
307
                    $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key');
308
                    $this->printMsg($msg);
309
310
                    // Unserialize target and fetch appropriate table. This is a bit messy
311
                    // because the table could be in another schema.
312
                    $data->setSchema($_REQUEST['target']['schemaname']);
313
                    $attrs = $data->getTableAttributes($_REQUEST['target']['tablename']);
314
                    $data->setSchema($_REQUEST['schema']);
315
316
                    $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10);
317
                    $selColumns->set_style('width: 15em;');
318
319
                    if ($attrs->recordCount() > 0) {
320
                        while (!$attrs->EOF) {
321
                            $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname']));
322
                            $attrs->moveNext();
323
                        }
324
                    }
325
326
                    $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10);
327
                    $selIndex->set_style('width: 15em;');
328
                    $selIndex->set_attribute('id', 'IndexColumnList');
329
                    $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>');
330
                    $buttonAdd->set_attribute('onclick', 'buttonPressed(this);');
331
                    $buttonAdd->set_attribute('type', 'button');
332
333
                    $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<');
334
                    $buttonRemove->set_attribute('onclick', 'buttonPressed(this);');
335
                    $buttonRemove->set_attribute('type', 'button');
336
337
                    echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n";
338
339
                    echo "<table>\n";
340
                    echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>";
341
                    echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\">&nbsp;</th><th class=data>{$lang['strfkcolumnlist']}</th></tr>\n";
342
                    echo '<tr><td class="data1">' . $selColumns->fetch() . "</td>\n";
343
                    echo '<td class="data1" style="text-align: center">' . $buttonRemove->fetch() . $buttonAdd->fetch() . '</td>';
344
                    echo '<td class="data1">' . $selIndex->fetch() . "</td></tr>\n";
345
                    echo "<tr><th class=\"data\" colspan=\"3\">{$lang['stractions']}</th></tr>";
346
                    echo '<tr>';
347
                    echo "<td class=\"data1\" colspan=\"3\">\n";
348
                    // ON SELECT actions
349
                    echo "{$lang['stronupdate']} <select name=\"upd_action\">";
350
                    foreach ($data->fkactions as $v) {
351
                        echo "<option value=\"{$v}\"", ($_POST['upd_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
352
                    }
353
354
                    echo "</select><br />\n";
355
356
                    // ON DELETE actions
357
                    echo "{$lang['strondelete']} <select name=\"del_action\">";
358
                    foreach ($data->fkactions as $v) {
359
                        echo "<option value=\"{$v}\"", ($_POST['del_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
360
                    }
361
362
                    echo "</select><br />\n";
363
364
                    // MATCH options
365
                    echo '<select name="match">';
366
                    foreach ($data->fkmatches as $v) {
367
                        echo "<option value=\"{$v}\"", ($_POST['match'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
368
                    }
369
370
                    echo "</select><br />\n";
371
372
                    // DEFERRABLE options
373
                    echo '<select name="deferrable">';
374
                    foreach ($data->fkdeferrable as $v) {
375
                        echo "<option value=\"{$v}\"", ($_POST['deferrable'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
376
                    }
377
378
                    echo "</select><br />\n";
379
380
                    // INITIALLY options
381
                    echo '<select name="initially">';
382
                    foreach ($data->fkinitial as $v) {
383
                        echo "<option value=\"{$v}\"", ($_POST['initially'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n";
384
                    }
385
386
                    echo "</select>\n";
387
                    echo "</td></tr>\n";
388
                    echo "</table>\n";
389
390
                    echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n";
391
                    echo $this->misc->form;
392
                    echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
393
                    echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n";
394
                    echo '<input type="hidden" name="target" value="', htmlspecialchars(serialize($_REQUEST['target'])), "\" />\n";
395
                    echo '<input type="hidden" name="SourceColumnList" value="', htmlspecialchars($_REQUEST['SourceColumnList']), "\" />\n";
396
                    echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n";
397
                    echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
398
                    echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
399
                    echo "</form>\n";
400
                }
401
402
                break;
403
            case 3:
404
                // Unserialize target
405
                $_POST['target'] = unserialize($_POST['target']);
406
407
                // Check that they've given at least one column
408
                if (isset($_POST['SourceColumnList'])) {
409
                    $temp = unserialize($_POST['SourceColumnList']);
410
                }
411
412
                if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList'])
413
                    || 0 == sizeof($_POST['IndexColumnList']) || !isset($temp)
414
                    || !is_array($temp) || 0 == sizeof($temp)) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
415
                    $this->addForeignKey(2, $lang['strfkneedscols']);
416
                } else {
417
                    $status = $data->addForeignKey(
418
                        $_POST['table'],
419
                        $_POST['target']['schemaname'],
420
                        $_POST['target']['tablename'],
421
                        unserialize($_POST['SourceColumnList']),
422
                        $_POST['IndexColumnList'],
423
                        $_POST['upd_action'],
424
                        $_POST['del_action'],
425
                        $_POST['match'],
426
                        $_POST['deferrable'],
427
                        $_POST['initially'],
428
                        $_POST['name']
429
                    );
430
                    if (0 == $status) {
431
                        $this->doDefault($lang['strfkadded']);
432
                    } else {
433
                        $this->addForeignKey(2, $lang['strfkaddedbad']);
434
                    }
435
                }
436
437
                break;
438
            default:
439
                $this->printTrail('table');
440
                $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key');
441
                $this->printMsg($msg);
442
443
                $attrs  = $data->getTableAttributes($_REQUEST['table']);
444
                $tables = $data->getTables(true);
445
446
                $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10);
447
                $selColumns->set_style('width: 15em;');
448
449
                if ($attrs->recordCount() > 0) {
450
                    while (!$attrs->EOF) {
451
                        $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname']));
452
                        $attrs->moveNext();
453
                    }
454
                }
455
456
                $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10);
457
                $selIndex->set_style('width: 15em;');
458
                $selIndex->set_attribute('id', 'IndexColumnList');
459
                $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>');
460
                $buttonAdd->set_attribute('onclick', 'buttonPressed(this);');
461
                $buttonAdd->set_attribute('type', 'button');
462
463
                $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<');
464
                $buttonRemove->set_attribute('onclick', 'buttonPressed(this);');
465
                $buttonRemove->set_attribute('type', 'button');
466
467
                echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n";
468
469
                echo "<table>\n";
470
                echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strname']}</th></tr>\n";
471
                echo "<tr><td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" /></td></tr>\n";
472
                echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\">&nbsp;</th><th class=\"data required\">{$lang['strfkcolumnlist']}</th></tr>\n";
473
                echo '<tr><td class="data1">' . $selColumns->fetch() . "</td>\n";
474
                echo '<td class="data1" style="text-align: center">' . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>\n";
475
                echo '<td class=data1>' . $selIndex->fetch() . "</td></tr>\n";
476
                echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>";
477
                echo '<tr>';
478
                echo '<td class="data1" colspan="3"><select class="select2" name="target">';
479
                while (!$tables->EOF) {
480
                    $key = ['schemaname' => $tables->fields['nspname'], 'tablename' => $tables->fields['relname']];
481
                    $key = serialize($key);
482
                    echo '<option value="', htmlspecialchars($key), '">';
483
                    if ($tables->fields['nspname'] != $_REQUEST['schema']) {
484
                        echo htmlspecialchars($tables->fields['nspname']), '.';
485
                    }
486
                    echo htmlspecialchars($tables->fields['relname']), "</option>\n";
487
                    $tables->moveNext();
488
                }
489
                echo "</select>\n";
490
                echo '</td></tr>';
491
                echo "</table>\n";
492
493
                echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n";
494
                echo $this->misc->form;
495
                echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
496
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
497
                echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
498
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
499
                echo "</form>\n";
500
                //echo "<script>jQuery('select[name=\"target\"]').select2()</script>";
501
                break;
502
        }
503
    }
504
505
    /**
506
     * Confirm and then actually add a PRIMARY KEY or UNIQUE constraint.
507
     *
508
     * @param mixed $type
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
509
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
510
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
511
     */
512
    public function addPrimaryOrUniqueKey($type, $confirm, $msg = '')
513
    {
514
        $lang = $this->lang;
515
        $data = $this->misc->getDatabaseAccessor();
516
517
        if (!isset($_POST['name'])) {
518
            $_POST['name'] = '';
519
        }
520
521
        if ($confirm) {
522
            if (!isset($_POST['name'])) {
523
                $_POST['name'] = '';
524
            }
525
526
            if (!isset($_POST['tablespace'])) {
527
                $_POST['tablespace'] = '';
528
            }
529
530
            $this->printTrail('table');
531
532
            switch ($type) {
533
                case 'primary':
534
                    $this->printTitle($lang['straddpk'], 'pg.constraint.primary_key');
535
536
                    break;
537
                case 'unique':
538
                    $this->printTitle($lang['stradduniq'], 'pg.constraint.unique_key');
539
540
                    break;
541
                default:
542
                    $this->doDefault($lang['strinvalidparam']);
543
544
                    return;
545
            }
546
547
            $this->printMsg($msg);
548
549
            $attrs = $data->getTableAttributes($_REQUEST['table']);
550
            // Fetch all tablespaces from the database
551
            if ($data->hasTablespaces()) {
552
                $tablespaces = $data->getTablespaces();
553
            }
554
555
            $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10);
556
            $selColumns->set_style('width: 15em;');
557
558
            if ($attrs->recordCount() > 0) {
559
                while (!$attrs->EOF) {
560
                    $new_option = new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname']);
561
                    $selColumns->add($new_option);
562
                    $attrs->moveNext();
563
                }
564
            }
565
566
            $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10);
567
            $selIndex->set_style('width: 15em;');
568
            $selIndex->set_attribute('id', 'IndexColumnList');
569
            $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>');
570
            $buttonAdd->set_attribute('onclick', 'buttonPressed(this);');
571
            $buttonAdd->set_attribute('type', 'button');
572
573
            $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<');
574
            $buttonRemove->set_attribute('onclick', 'buttonPressed(this);');
575
            $buttonRemove->set_attribute('type', 'button');
576
577
            echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n";
578
579
            echo "<table>\n";
580
            echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strname']}</th></tr>";
581
            echo '<tr>';
582
            echo '<td class="data1" colspan="3"><input type="text" name="name" value="', htmlspecialchars($_POST['name']),
583
                "\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" /></td></tr>";
584
            echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\">&nbsp;</th><th class=\"data required\">{$lang['strindexcolumnlist']}</th></tr>\n";
585
            echo '<tr><td class="data1">' . $selColumns->fetch() . "</td>\n";
586
            echo '<td class="data1" style="text-align: center">' . $buttonRemove->fetch() . $buttonAdd->fetch() . '</td>';
587
            echo '<td class=data1>' . $selIndex->fetch() . "</td></tr>\n";
588
589
            // Tablespace (if there are any)
590
            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...
591
                echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strtablespace']}</th></tr>";
592
                echo "<tr><td class=\"data1\" colspan=\"3\"><select name=\"tablespace\">\n";
593
                // Always offer the default (empty) option
594
                echo "\t\t\t\t<option value=\"\"",
595
                ('' == $_POST['tablespace']) ? ' selected="selected"' : '', "></option>\n";
596
                // Display all other tablespaces
597
                while (!$tablespaces->EOF) {
598
                    $spcname = htmlspecialchars($tablespaces->fields['spcname']);
599
                    echo "\t\t\t\t<option value=\"{$spcname}\"",
600
                    ($spcname == $_POST['tablespace']) ? ' selected="selected"' : '', ">{$spcname}</option>\n";
601
                    $tablespaces->moveNext();
602
                }
603
                echo "</select></td></tr>\n";
604
            }
605
606
            echo "</table>\n";
607
608
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_primary_key\" />\n";
609
            echo $this->misc->form;
610
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
611
            echo '<input type="hidden" name="type" value="', htmlspecialchars($type), "\" />\n";
612
            echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n";
613
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
614
            echo "</form>\n";
615
        } else {
616
            // Default tablespace to empty if it isn't set
617
            if (!isset($_POST['tablespace'])) {
618
                $_POST['tablespace'] = '';
619
            }
620
621
            if ('primary' == $_POST['type']) {
622
                // Check that they've given at least one column
623
                if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList'])
624
                    || 0 == sizeof($_POST['IndexColumnList'])) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
625
                    $this->addPrimaryOrUniqueKey($_POST['type'], true, $lang['strpkneedscols']);
626
                } else {
627
                    $status = $data->addPrimaryKey($_POST['table'], $_POST['IndexColumnList'], $_POST['name'], $_POST['tablespace']);
628
                    if (0 == $status) {
629
                        $this->doDefault($lang['strpkadded']);
630
                    } else {
631
                        $this->addPrimaryOrUniqueKey($_POST['type'], true, $lang['strpkaddedbad']);
632
                    }
633
                }
634
            } elseif ('unique' == $_POST['type']) {
635
                // Check that they've given at least one column
636
                if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList'])
637
                    || 0 == sizeof($_POST['IndexColumnList'])) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
638
                    $this->addPrimaryOrUniqueKey($_POST['type'], true, $lang['struniqneedscols']);
639
                } else {
640
                    $status = $data->addUniqueKey($_POST['table'], $_POST['IndexColumnList'], $_POST['name'], $_POST['tablespace']);
641
                    if (0 == $status) {
642
                        $this->doDefault($lang['struniqadded']);
643
                    } else {
644
                        $this->addPrimaryOrUniqueKey($_POST['type'], true, $lang['struniqaddedbad']);
645
                    }
646
                }
647
            } else {
648
                $this->doDefault($lang['strinvalidparam']);
649
            }
650
        }
651
    }
652
653
    /**
654
     * Confirm and then actually add a CHECK constraint.
655
     *
656
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
657
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
658
     */
659
    public function addCheck($confirm, $msg = '')
660
    {
661
        $lang = $this->lang;
662
        $data = $this->misc->getDatabaseAccessor();
663
664
        if (!isset($_POST['name'])) {
665
            $_POST['name'] = '';
666
        }
667
668
        if (!isset($_POST['definition'])) {
669
            $_POST['definition'] = '';
670
        }
671
672
        if ($confirm) {
673
            $this->printTrail('table');
674
            $this->printTitle($lang['straddcheck'], 'pg.constraint.check');
675
            $this->printMsg($msg);
676
677
            echo '<form action="' . \SUBFOLDER . "/src/views/constraints.php\" method=\"post\">\n";
678
            echo "<table>\n";
679
            echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
680
            echo "<th class=\"data required\">{$lang['strdefinition']}</th></tr>\n";
681
682
            echo "<tr><td class=\"data1\"><input name=\"name\" size=\"24\" maxlength=\"{$data->_maxNameLen}\" value=\"",
683
            htmlspecialchars($_POST['name']), "\" /></td>\n";
684
685
            echo '<td class="data1">(<input name="definition" size="64" value="',
686
            htmlspecialchars($_POST['definition']), "\" />)</td></tr>\n";
687
            echo "</table>\n";
688
689
            echo "<input type=\"hidden\" name=\"action\" value=\"save_add_check\" />\n";
690
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
691
            echo $this->misc->form;
692
            echo "<p><input type=\"submit\" name=\"ok\" value=\"{$lang['stradd']}\" />\n";
693
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
694
            echo "</form>\n";
695
        } else {
696
            if ('' == trim($_POST['definition'])) {
697
                $this->addCheck(true, $lang['strcheckneedsdefinition']);
698
            } else {
699
                $status = $data->addCheckConstraint(
700
                    $_POST['table'],
701
                    $_POST['definition'],
702
                    $_POST['name']
703
                );
704
                if (0 == $status) {
705
                    $this->doDefault($lang['strcheckadded']);
706
                } else {
707
                    $this->addCheck(true, $lang['strcheckaddedbad']);
708
                }
709
            }
710
        }
711
    }
712
713
    /**
714
     * Show confirmation of drop and perform actual drop.
715
     *
716
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
717
     */
718
    public function doDrop($confirm)
719
    {
720
        $lang = $this->lang;
721
        $data = $this->misc->getDatabaseAccessor();
722
723
        if ($confirm) {
724
            $this->printTrail('constraint');
725
            $this->printTitle($lang['strdrop'], 'pg.constraint.drop');
726
727
            echo '<p>', sprintf(
728
                $lang['strconfdropconstraint'],
729
                $this->misc->printVal($_REQUEST['constraint']),
730
                $this->misc->printVal($_REQUEST['table'])
731
            ), "</p>\n";
732
733
            echo '<form action="' . \SUBFOLDER . "/src/views/constraints.php\" method=\"post\">\n";
734
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
735
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
736
            echo '<input type="hidden" name="constraint" value="', htmlspecialchars($_REQUEST['constraint']), "\" />\n";
737
            echo '<input type="hidden" name="type" value="', htmlspecialchars($_REQUEST['type']), "\" />\n";
738
            echo $this->misc->form;
739
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
740
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
741
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
742
            echo "</form>\n";
743
        } else {
744
            $status = $data->dropConstraint($_POST['constraint'], $_POST['table'], $_POST['type'], isset($_POST['cascade']));
745
            if (0 == $status) {
746
                $this->doDefault($lang['strconstraintdropped']);
747
            } else {
748
                $this->doDefault($lang['strconstraintdroppedbad']);
749
            }
750
        }
751
    }
752
}
753