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

ConstraintsController   F

Complexity

Total Complexity 103

Size/Duplication

Total Lines 747
Duplicated Lines 0 %

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
D render() 0 104 25
B doDefault() 0 123 3
D addPrimaryOrUniqueKey() 0 139 26
B doDrop() 0 33 3
F addForeignKey() 0 245 40
B addCheck() 0 51 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
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
24
25
        $lang = $this->lang;
26
27
        $action = $this->action;
28
        if ('tree' == $action) {
29
            return $this->/* @scrutinizer ignore-call */doTree();
30
        }
31
32
        $this->printHeader(
33
            $lang['strtables'].' - '.$_REQUEST['table'].' - '.$lang['strconstraints'],
34
            '<script src="'.\SUBFOLDER.'/js/indexes.js" type="text/javascript"></script>',
35
            true,
36
            'header_select2.twig'
37
        );
38
39
        if ('add_unique_key' == $action || 'save_add_unique_key' == $action
40
            || 'add_primary_key' == $action || 'save_add_primary_key' == $action
41
            || '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...
42
            echo '<body onload="init();">';
43
        } else {
44
            $this->printBody();
45
        }
46
47
        switch ($action) {
48
            case 'add_foreign_key':
49
                $this->addForeignKey(1);
50
51
                break;
52
            case 'save_add_foreign_key':
53
                if (isset($_POST['cancel'])) {
54
                    $this->doDefault();
55
                } else {
56
                    $this->addForeignKey($_REQUEST['stage']);
57
                }
58
59
                break;
60
            case 'add_unique_key':
61
                $this->addPrimaryOrUniqueKey('unique', true);
62
63
                break;
64
            case 'save_add_unique_key':
65
                if (isset($_POST['cancel'])) {
66
                    $this->doDefault();
67
                } else {
68
                    $this->addPrimaryOrUniqueKey('unique', false);
69
                }
70
71
                break;
72
            case 'add_primary_key':
73
                $this->addPrimaryOrUniqueKey('primary', true);
74
75
                break;
76
            case 'save_add_primary_key':
77
                if (isset($_POST['cancel'])) {
78
                    $this->doDefault();
79
                } else {
80
                    $this->addPrimaryOrUniqueKey('primary', false);
81
                }
82
83
                break;
84
            case 'add_check':
85
                $this->addCheck(true);
86
87
                break;
88
            case 'save_add_check':
89
                if (isset($_POST['cancel'])) {
90
                    $this->doDefault();
91
                } else {
92
                    $this->addCheck(false);
93
                }
94
95
                break;
96
            case 'save_create':
97
                $this->// @scrutinizer ignore-call
98
                doSaveCreate();
0 ignored issues
show
Bug introduced by
The method doSaveCreate() does not exist on PHPPgAdmin\Controller\ConstraintsController. ( Ignorable by Annotation )

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

98
                /** @scrutinizer ignore-call */ 
99
                doSaveCreate();

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...
99
100
                break;
101
            case 'create':
102
                $this->// @scrutinizer ignore-call
103
                doCreate();
0 ignored issues
show
Bug introduced by
The method doCreate() does not exist on PHPPgAdmin\Controller\ConstraintsController. ( Ignorable by Annotation )

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

103
                /** @scrutinizer ignore-call */ 
104
                doCreate();

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