DomainsController   C
last analyzed

Complexity

Total Complexity 56

Size/Duplication

Total Lines 669
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 410
c 1
b 0
f 0
dl 0
loc 669
rs 5.5199
wmc 56

11 Methods

Rating   Name   Duplication   Size   Complexity  
A doSaveCreate() 0 24 3
B doDefault() 0 94 2
B doAlter() 0 57 7
B doProperties() 0 126 6
A addCheck() 0 45 4
A doSaveAlter() 0 15 2
D render() 0 82 18
A doDrop() 0 24 3
A doDropConstraint() 0 30 3
A doTree() 0 23 1
B doCreate() 0 67 7

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class DomainsController extends BaseController
15
{
16
    public $controller_title = 'strdomains';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        if ('tree' === $this->action) {
24
            return $this->doTree();
25
        }
26
27
        $this->printHeader();
28
        $this->printBody();
29
30
        switch ($this->action) {
31
            case 'add_check':
32
                $this->addCheck(true);
33
34
                break;
35
            case 'save_add_check':
36
                if (null !== $this->getPostParam('cancel')) {
37
                    $this->doProperties();
38
                } else {
39
                    $this->addCheck(false);
40
                }
41
42
                break;
43
            case 'drop_con':
44
                if (null !== $this->getPostParam('drop')) {
45
                    $this->doDropConstraint(false);
46
                } else {
47
                    $this->doProperties();
48
                }
49
50
                break;
51
            case 'confirm_drop_con':
52
                $this->doDropConstraint(true);
53
54
                break;
55
            case 'save_create':
56
                if (null !== $this->getPostParam('cancel')) {
57
                    $this->doDefault();
58
                } else {
59
                    $this->doSaveCreate();
60
                }
61
62
                break;
63
            case 'create':
64
                $this->doCreate();
65
66
                break;
67
            case 'drop':
68
                if (null !== $this->getPostParam('drop')) {
69
                    $this->doDrop(false);
70
                } else {
71
                    $this->doDefault();
72
                }
73
74
                break;
75
            case 'confirm_drop':
76
                $this->doDrop(true);
77
78
                break;
79
            case 'save_alter':
80
                if (null !== $this->getPostParam('alter')) {
81
                    $this->doSaveAlter();
82
                } else {
83
                    $this->doProperties();
84
                }
85
86
                break;
87
            case 'alter':
88
                $this->doAlter();
89
90
                break;
91
            case 'properties':
92
                $this->doProperties();
93
94
                break;
95
96
            default:
97
                $this->doDefault();
98
99
                break;
100
        }
101
102
        return $this->printFooter();
103
    }
104
105
    /**
106
     * Show default list of domains in the database.
107
     *
108
     * @param mixed $msg
109
     */
110
    public function doDefault($msg = ''): void
111
    {
112
        $data = $this->misc->getDatabaseAccessor();
113
114
        $this->printTrail('schema');
115
        $this->printTabs('schema', 'domains');
116
        $this->printMsg($msg);
117
118
        $domains = $data->getDomains();
119
120
        $columns = [
121
            'domain' => [
122
                'title' => $this->lang['strdomain'],
123
                'field' => Decorator::field('domname'),
124
                'url' => "domains?action=properties&amp;{$this->misc->href}&amp;",
125
                'vars' => ['domain' => 'domname'],
126
            ],
127
            'type' => [
128
                'title' => $this->lang['strtype'],
129
                'field' => Decorator::field('domtype'),
130
            ],
131
            'notnull' => [
132
                'title' => $this->lang['strnotnull'],
133
                'field' => Decorator::field('domnotnull'),
134
                'type' => 'bool',
135
                'params' => ['true' => 'NOT NULL', 'false' => ''],
136
            ],
137
            'default' => [
138
                'title' => $this->lang['strdefault'],
139
                'field' => Decorator::field('domdef'),
140
            ],
141
            'owner' => [
142
                'title' => $this->lang['strowner'],
143
                'field' => Decorator::field('domowner'),
144
            ],
145
            'actions' => [
146
                'title' => $this->lang['stractions'],
147
            ],
148
            'comment' => [
149
                'title' => $this->lang['strcomment'],
150
                'field' => Decorator::field('domcomment'),
151
            ],
152
        ];
153
154
        $actions = [
155
            'alter' => [
156
                'content' => $this->lang['stralter'],
157
                'attr' => [
158
                    'href' => [
159
                        'url' => 'domains',
160
                        'urlvars' => [
161
                            'action' => 'alter',
162
                            'domain' => Decorator::field('domname'),
163
                        ],
164
                    ],
165
                ],
166
            ],
167
            'drop' => [
168
                'content' => $this->lang['strdrop'],
169
                'attr' => [
170
                    'href' => [
171
                        'url' => 'domains',
172
                        'urlvars' => [
173
                            'action' => 'confirm_drop',
174
                            'domain' => Decorator::field('domname'),
175
                        ],
176
                    ],
177
                ],
178
            ],
179
        ];
180
181
        if (!$data->hasAlterDomains()) {
182
            unset($actions['alter']);
183
        }
184
185
        echo $this->printTable($domains, $columns, $actions, 'domains-domains', $this->lang['strnodomains']);
0 ignored issues
show
Bug introduced by
It seems like $domains can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

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

185
        echo $this->printTable(/** @scrutinizer ignore-type */ $domains, $columns, $actions, 'domains-domains', $this->lang['strnodomains']);
Loading history...
186
187
        $navlinks = [
188
            'create' => [
189
                'attr' => [
190
                    'href' => [
191
                        'url' => 'domains',
192
                        'urlvars' => [
193
                            'action' => 'create',
194
                            'server' => $_REQUEST['server'],
195
                            'database' => $_REQUEST['database'],
196
                            'schema' => $_REQUEST['schema'],
197
                        ],
198
                    ],
199
                ],
200
                'content' => $this->lang['strcreatedomain'],
201
            ],
202
        ];
203
        $this->printNavLinks($navlinks, 'domains-domains', \get_defined_vars());
204
    }
205
206
    /**
207
     * Generate XML for the browser tree.
208
     */
209
    public function doTree()
210
    {
211
        $data = $this->misc->getDatabaseAccessor();
212
213
        $domains = $data->getDomains();
214
215
        $reqvars = $this->misc->getRequestVars('domain');
216
217
        $attrs = [
218
            'text' => Decorator::field('domname'),
219
            'icon' => 'Domain',
220
            'toolTip' => Decorator::field('domcomment'),
221
            'action' => Decorator::actionurl(
222
                'domains',
223
                $reqvars,
224
                [
225
                    'action' => 'properties',
226
                    'domain' => Decorator::field('domname'),
227
                ]
228
            ),
229
        ];
230
231
        return $this->printTree($domains, $attrs, 'domains');
0 ignored issues
show
Bug introduced by
It seems like $domains can also be of type integer; however, parameter $_treedata of PHPPgAdmin\Controller\BaseController::printTree() does only seem to accept PHPPgAdmin\ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

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

231
        return $this->printTree(/** @scrutinizer ignore-type */ $domains, $attrs, 'domains');
Loading history...
232
    }
233
234
    /**
235
     * Function to save after altering a domain.
236
     */
237
    public function doSaveAlter(): void
238
    {
239
        $data = $this->misc->getDatabaseAccessor();
240
241
        $status = $data->alterDomain(
242
            $_POST['domain'],
243
            $_POST['domdefault'],
244
            isset($_POST['domnotnull']),
245
            $_POST['domowner']
246
        );
247
248
        if (0 === $status) {
249
            $this->doProperties($this->lang['strdomainaltered']);
250
        } else {
251
            $this->doAlter($this->lang['strdomainalteredbad']);
252
        }
253
    }
254
255
    /**
256
     * Allow altering a domain.
257
     *
258
     * @param mixed $msg
259
     */
260
    public function doAlter($msg = ''): void
261
    {
262
        $data = $this->misc->getDatabaseAccessor();
263
264
        $this->printTrail('domain');
265
        $this->printTitle($this->lang['stralter'], 'pg.domain.alter');
266
        $this->printMsg($msg);
267
268
        // Fetch domain info
269
        $domaindata = $data->getDomain($_REQUEST['domain']);
270
        // Fetch all users
271
        $users = $data->getUsers();
272
273
        if (0 < $domaindata->recordCount()) {
274
            if (!isset($_POST['domname'])) {
275
                $_POST['domtype'] = $domaindata->fields['domtype'];
276
                $_POST['domdefault'] = $domaindata->fields['domdef'];
277
                $domaindata->fields['domnotnull'] = $data->phpBool($domaindata->fields['domnotnull']);
278
279
                if ($domaindata->fields['domnotnull']) {
280
                    $_POST['domnotnull'] = 'on';
281
                }
282
283
                $_POST['domowner'] = $domaindata->fields['domowner'];
284
            }
285
286
            // Display domain info
287
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/domains" method="post">' . \PHP_EOL;
288
            echo '<table>' . \PHP_EOL;
289
            echo "<tr><th class=\"data left required\" style=\"width: 70px\">{$this->lang['strname']}</th>" . \PHP_EOL;
290
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domname']), '</td></tr>' . \PHP_EOL;
291
            echo "<tr><th class=\"data left required\">{$this->lang['strtype']}</th>" . \PHP_EOL;
292
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domtype']), '</td></tr>' . \PHP_EOL;
293
            echo "<tr><th class=\"data left\"><label for=\"domnotnull\">{$this->lang['strnotnull']}</label></th>" . \PHP_EOL;
294
            echo '<td class="data1"><input type="checkbox" id="domnotnull" name="domnotnull"', (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), ' /></td></tr>' . \PHP_EOL;
295
            echo "<tr><th class=\"data left\">{$this->lang['strdefault']}</th>" . \PHP_EOL;
296
            echo '<td class="data1"><input name="domdefault" size="32" value="',
297
            \htmlspecialchars($_POST['domdefault']), '" /></td></tr>' . \PHP_EOL;
298
            echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>" . \PHP_EOL;
299
            echo '<td class="data1"><select name="domowner">';
300
301
            while (!$users->EOF) {
302
                $uname = $users->fields['usename'];
303
                echo '<option value="', \htmlspecialchars($uname), '"',
304
                ($uname === $_POST['domowner']) ? ' selected="selected"' : '', '>', \htmlspecialchars($uname), '</option>' . \PHP_EOL;
305
                $users->moveNext();
306
            }
307
            echo '</select></td></tr>' . \PHP_EOL;
308
            echo '</table>' . \PHP_EOL;
309
            echo '<p><input type="hidden" name="action" value="save_alter" />' . \PHP_EOL;
310
            echo '<input type="hidden" name="domain" value="', \htmlspecialchars($_REQUEST['domain']), '" />' . \PHP_EOL;
311
            echo $this->view->form;
312
            echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />" . \PHP_EOL;
313
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
314
            echo '</form>' . \PHP_EOL;
315
        } else {
316
            echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
317
        }
318
    }
319
320
    /**
321
     * Confirm and then actually add a CHECK constraint.
322
     *
323
     * @param mixed $confirm
324
     * @param mixed $msg
325
     */
326
    public function addCheck($confirm, $msg = ''): void
327
    {
328
        $data = $this->misc->getDatabaseAccessor();
329
330
        $this->coalesceArr($_POST, 'name', '');
331
332
        $this->coalesceArr($_POST, 'definition', '');
333
334
        if ($confirm) {
335
            $this->printTrail('domain');
336
            $this->printTitle($this->lang['straddcheck'], 'pg.constraint.check');
337
            $this->printMsg($msg);
338
339
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/domains" method="post">' . \PHP_EOL;
340
            echo '<table>' . \PHP_EOL;
341
            echo "<tr><th class=\"data\">{$this->lang['strname']}</th>" . \PHP_EOL;
342
            echo "<th class=\"data required\">{$this->lang['strdefinition']}</th></tr>" . \PHP_EOL;
343
344
            echo "<tr><td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
345
            \htmlspecialchars($_POST['name']), '" /></td>' . \PHP_EOL;
346
347
            echo '<td class="data1">(<input name="definition" size="32" value="',
348
            \htmlspecialchars($_POST['definition']), '" />)</td></tr>' . \PHP_EOL;
349
            echo '</table>' . \PHP_EOL;
350
351
            echo '<p><input type="hidden" name="action" value="save_add_check" />' . \PHP_EOL;
352
            echo '<input type="hidden" name="domain" value="', \htmlspecialchars($_REQUEST['domain']), '" />' . \PHP_EOL;
353
            echo $this->view->form;
354
            echo "<input type=\"submit\" name=\"add\" value=\"{$this->lang['stradd']}\" />" . \PHP_EOL;
355
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
356
            echo '</form>' . \PHP_EOL;
357
        } else {
358
            if ('' === \trim($_POST['definition'])) {
359
                $this->addCheck(true, $this->lang['strcheckneedsdefinition']);
360
            } else {
361
                $status = $data->addDomainCheckConstraint(
362
                    $_POST['domain'],
363
                    $_POST['definition'],
364
                    $_POST['name']
365
                );
366
367
                if (0 === $status) {
368
                    $this->doProperties($this->lang['strcheckadded']);
369
                } else {
370
                    $this->addCheck(true, $this->lang['strcheckaddedbad']);
371
                }
372
            }
373
        }
374
    }
375
376
    /**
377
     * Show confirmation of drop constraint and perform actual drop.
378
     *
379
     * @param mixed $confirm
380
     * @param mixed $msg
381
     */
382
    public function doDropConstraint($confirm, $msg = ''): void
383
    {
384
        $data = $this->misc->getDatabaseAccessor();
385
386
        if ($confirm) {
387
            $this->printTrail('domain');
388
            $this->printTitle($this->lang['strdrop'], 'pg.constraint.drop');
389
            $this->printMsg($msg);
390
391
            echo '<p>', \sprintf(
392
                $this->lang['strconfdropconstraint'],
393
                $this->misc->printVal($_REQUEST['constraint']),
394
                $this->misc->printVal($_REQUEST['domain'])
395
            ), '</p>' . \PHP_EOL;
396
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/domains" method="post">' . \PHP_EOL;
397
            echo '<input type="hidden" name="action" value="drop_con" />' . \PHP_EOL;
398
            echo '<input type="hidden" name="domain" value="', \htmlspecialchars($_REQUEST['domain']), '" />' . \PHP_EOL;
399
            echo '<input type="hidden" name="constraint" value="', \htmlspecialchars($_REQUEST['constraint']), '" />' . \PHP_EOL;
400
            echo $this->view->form;
401
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>" . \PHP_EOL;
402
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL;
403
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
404
            echo '</form>' . \PHP_EOL;
405
        } else {
406
            $status = $data->dropDomainConstraint($_POST['domain'], $_POST['constraint'], isset($_POST['cascade']));
407
408
            if (0 === $status) {
409
                $this->doProperties($this->lang['strconstraintdropped']);
410
            } else {
411
                $this->doDropConstraint(true, $this->lang['strconstraintdroppedbad']);
412
            }
413
        }
414
    }
415
416
    /**
417
     * Show properties for a domain.  Allow manipulating constraints as well.
418
     *
419
     * @param mixed $msg
420
     */
421
    public function doProperties($msg = ''): void
422
    {
423
        $data = $this->misc->getDatabaseAccessor();
424
425
        $this->printTrail('domain');
426
        $this->printTitle($this->lang['strproperties'], 'pg.domain');
427
        $this->printMsg($msg);
428
429
        $domaindata = $data->getDomain($_REQUEST['domain']);
430
431
        if (0 < $domaindata->recordCount()) {
432
            // Show comment if any
433
            if (null !== $domaindata->fields['domcomment']) {
434
                echo '<p class="comment">', $this->misc->printVal($domaindata->fields['domcomment']), '</p>' . \PHP_EOL;
435
            }
436
437
            // Display domain info
438
            $domaindata->fields['domnotnull'] = $data->phpBool($domaindata->fields['domnotnull']);
439
            echo '<table>' . \PHP_EOL;
440
            echo "<tr><th class=\"data left\" style=\"width: 70px\">{$this->lang['strname']}</th>" . \PHP_EOL;
441
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domname']), '</td></tr>' . \PHP_EOL;
442
            echo "<tr><th class=\"data left\">{$this->lang['strtype']}</th>" . \PHP_EOL;
443
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domtype']), '</td></tr>' . \PHP_EOL;
444
            echo "<tr><th class=\"data left\">{$this->lang['strnotnull']}</th>" . \PHP_EOL;
445
            echo '<td class="data1">', ($domaindata->fields['domnotnull'] ? 'NOT NULL' : ''), '</td></tr>' . \PHP_EOL;
446
            echo "<tr><th class=\"data left\">{$this->lang['strdefault']}</th>" . \PHP_EOL;
447
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domdef']), '</td></tr>' . \PHP_EOL;
448
            echo "<tr><th class=\"data left\">{$this->lang['strowner']}</th>" . \PHP_EOL;
449
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domowner']), '</td></tr>' . \PHP_EOL;
450
            echo '</table>' . \PHP_EOL;
451
452
            // Display domain constraints
453
            echo "<h3>{$this->lang['strconstraints']}</h3>" . \PHP_EOL;
454
455
            if ($data->hasDomainConstraints()) {
456
                $domaincons = $data->getDomainConstraints($_REQUEST['domain']);
457
458
                $columns = [
459
                    'name' => [
460
                        'title' => $this->lang['strname'],
461
                        'field' => Decorator::field('conname'),
462
                    ],
463
                    'definition' => [
464
                        'title' => $this->lang['strdefinition'],
465
                        'field' => Decorator::field('consrc'),
466
                    ],
467
                    'actions' => [
468
                        'title' => $this->lang['stractions'],
469
                    ],
470
                ];
471
472
                $actions = [
473
                    'drop' => [
474
                        'content' => $this->lang['strdrop'],
475
                        'attr' => [
476
                            'href' => [
477
                                'url' => 'domains',
478
                                'urlvars' => [
479
                                    'action' => 'confirm_drop_con',
480
                                    'domain' => $_REQUEST['domain'],
481
                                    'constraint' => Decorator::field('conname'),
482
                                    'type' => Decorator::field('contype'),
483
                                ],
484
                            ],
485
                        ],
486
                    ],
487
                ];
488
489
                echo $this->printTable($domaincons, $columns, $actions, 'domains-properties', $this->lang['strnodata']);
0 ignored issues
show
Bug introduced by
It seems like $domaincons can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

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

489
                echo $this->printTable(/** @scrutinizer ignore-type */ $domaincons, $columns, $actions, 'domains-properties', $this->lang['strnodata']);
Loading history...
490
            }
491
        } else {
492
            echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
493
        }
494
495
        $navlinks = [
496
            'drop' => [
497
                'attr' => [
498
                    'href' => [
499
                        'url' => 'domains',
500
                        'urlvars' => [
501
                            'action' => 'confirm_drop',
502
                            'server' => $_REQUEST['server'],
503
                            'database' => $_REQUEST['database'],
504
                            'schema' => $_REQUEST['schema'],
505
                            'domain' => $_REQUEST['domain'],
506
                        ],
507
                    ],
508
                ],
509
                'content' => $this->lang['strdrop'],
510
            ],
511
        ];
512
513
        if ($data->hasAlterDomains()) {
514
            $navlinks['addcheck'] = [
515
                'attr' => [
516
                    'href' => [
517
                        'url' => 'domains',
518
                        'urlvars' => [
519
                            'action' => 'add_check',
520
                            'server' => $_REQUEST['server'],
521
                            'database' => $_REQUEST['database'],
522
                            'schema' => $_REQUEST['schema'],
523
                            'domain' => $_REQUEST['domain'],
524
                        ],
525
                    ],
526
                ],
527
                'content' => $this->lang['straddcheck'],
528
            ];
529
            $navlinks['alter'] = [
530
                'attr' => [
531
                    'href' => [
532
                        'url' => 'domains',
533
                        'urlvars' => [
534
                            'action' => 'alter',
535
                            'server' => $_REQUEST['server'],
536
                            'database' => $_REQUEST['database'],
537
                            'schema' => $_REQUEST['schema'],
538
                            'domain' => $_REQUEST['domain'],
539
                        ],
540
                    ],
541
                ],
542
                'content' => $this->lang['stralter'],
543
            ];
544
        }
545
546
        $this->printNavLinks($navlinks, 'domains-properties', \get_defined_vars());
547
    }
548
549
    /**
550
     * Show confirmation of drop and perform actual drop.
551
     *
552
     * @param mixed $confirm
553
     */
554
    public function doDrop($confirm): void
555
    {
556
        $data = $this->misc->getDatabaseAccessor();
557
558
        if ($confirm) {
559
            $this->printTrail('domain');
560
            $this->printTitle($this->lang['strdrop'], 'pg.domain.drop');
561
562
            echo '<p>', \sprintf($this->lang['strconfdropdomain'], $this->misc->printVal($_REQUEST['domain'])), '</p>' . \PHP_EOL;
563
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/domains" method="post">' . \PHP_EOL;
564
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /><label for=\"cascade\">{$this->lang['strcascade']}</label></p>" . \PHP_EOL;
565
            echo '<p><input type="hidden" name="action" value="drop" />' . \PHP_EOL;
566
            echo '<input type="hidden" name="domain" value="', \htmlspecialchars($_REQUEST['domain']), '" />' . \PHP_EOL;
567
            echo $this->view->form;
568
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL;
569
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
570
            echo '</form>' . \PHP_EOL;
571
        } else {
572
            $status = $data->dropDomain($_POST['domain'], isset($_POST['cascade']));
573
574
            if (0 === $status) {
575
                $this->doDefault($this->lang['strdomaindropped']);
576
            } else {
577
                $this->doDefault($this->lang['strdomaindroppedbad']);
578
            }
579
        }
580
    }
581
582
    /**
583
     * Displays a screen where they can enter a new domain.
584
     *
585
     * @param mixed $msg
586
     */
587
    public function doCreate($msg = ''): void
588
    {
589
        $data = $this->misc->getDatabaseAccessor();
590
591
        $this->coalesceArr($_POST, 'domname', '');
592
593
        $this->coalesceArr($_POST, 'domtype', '');
594
595
        $this->coalesceArr($_POST, 'domlength', '');
596
597
        $this->coalesceArr($_POST, 'domarray', '');
598
599
        $this->coalesceArr($_POST, 'domdefault', '');
600
601
        $this->coalesceArr($_POST, 'domcheck', '');
602
603
        $types = $data->getTypes(true);
604
605
        $this->printTrail('schema');
606
        $this->printTitle($this->lang['strcreatedomain'], 'pg.domain.create');
607
        $this->printMsg($msg);
608
609
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/domains" method="post">' . \PHP_EOL;
610
        echo '<table>' . \PHP_EOL;
611
        echo "<tr><th class=\"data left required\" style=\"width: 70px\">{$this->lang['strname']}</th>" . \PHP_EOL;
612
        echo "<td class=\"data1\"><input name=\"domname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
613
        \htmlspecialchars($_POST['domname']), '" /></td></tr>' . \PHP_EOL;
614
        echo "<tr><th class=\"data left required\">{$this->lang['strtype']}</th>" . \PHP_EOL;
615
        echo '<td class="data1">' . \PHP_EOL;
616
        // Output return type list
617
        echo '<select name="domtype">' . \PHP_EOL;
618
619
        while (!$types->EOF) {
620
            echo '<option value="', \htmlspecialchars($types->fields['typname']), '"',
621
            ($types->fields['typname'] === $_POST['domtype']) ? ' selected="selected"' : '', '>',
622
            $this->misc->printVal($types->fields['typname']), '</option>' . \PHP_EOL;
623
            $types->moveNext();
624
        }
625
        echo '</select>' . \PHP_EOL;
626
627
        // Type length
628
        echo '<input type="text" size="4" name="domlength" value="', \htmlspecialchars($_POST['domlength']), '" />';
629
630
        // Output array type selector
631
        echo '<select name="domarray">' . \PHP_EOL;
632
        echo '<option value=""', ('' === $_POST['domarray']) ? ' selected="selected"' : '', '></option>' . \PHP_EOL;
633
        echo '<option value="[]"', ('[]' === $_POST['domarray']) ? ' selected="selected"' : '', '>[ ]</option>' . \PHP_EOL;
634
        echo '</select></td></tr>' . \PHP_EOL;
635
636
        echo "<tr><th class=\"data left\"><label for=\"domnotnull\">{$this->lang['strnotnull']}</label></th>" . \PHP_EOL;
637
        echo '<td class="data1"><input type="checkbox" id="domnotnull" name="domnotnull"',
638
        (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), ' /></td></tr>' . \PHP_EOL;
639
        echo "<tr><th class=\"data left\">{$this->lang['strdefault']}</th>" . \PHP_EOL;
640
        echo '<td class="data1"><input name="domdefault" size="32" value="',
641
        \htmlspecialchars($_POST['domdefault']), '" /></td></tr>' . \PHP_EOL;
642
643
        if ($data->hasDomainConstraints()) {
644
            echo "<tr><th class=\"data left\">{$this->lang['strconstraints']}</th>" . \PHP_EOL;
645
            echo '<td class="data1">CHECK (<input name="domcheck" size="32" value="',
646
            \htmlspecialchars($_POST['domcheck']), '" />)</td></tr>' . \PHP_EOL;
647
        }
648
        echo '</table>' . \PHP_EOL;
649
        echo '<p><input type="hidden" name="action" value="save_create" />' . \PHP_EOL;
650
        echo $this->view->form;
651
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL;
652
        echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
653
        echo '</form>' . \PHP_EOL;
654
    }
655
656
    /**
657
     * Actually creates the new domain in the database.
658
     */
659
    public function doSaveCreate(): void
660
    {
661
        $data = $this->misc->getDatabaseAccessor();
662
663
        $this->coalesceArr($_POST, 'domcheck', '');
664
665
        // Check that they've given a name and a definition
666
        if ('' === $_POST['domname']) {
667
            $this->doCreate($this->lang['strdomainneedsname']);
668
        } else {
669
            $status = $data->createDomain(
670
                $_POST['domname'],
671
                $_POST['domtype'],
672
                $_POST['domlength'],
673
                '' !== $_POST['domarray'],
674
                isset($_POST['domnotnull']),
675
                $_POST['domdefault'],
676
                $_POST['domcheck']
677
            );
678
679
            if (0 === $status) {
680
                $this->doDefault($this->lang['strdomaincreated']);
681
            } else {
682
                $this->doCreate($this->lang['strdomaincreatedbad']);
683
            }
684
        }
685
    }
686
}
687