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

DomainsController::doDefault()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 97
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 64
nc 2
nop 1
dl 0
loc 97
rs 8.3604
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class DomainsController extends BaseController
15
{
16
    public $controller_name = 'DomainsController';
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->doTree();
30
        }
31
32
        $this->printHeader($lang['strdomains']);
33
        $this->printBody();
34
35
        switch ($action) {
36
            case 'add_check':
37
                $this->addCheck(true);
38
39
                break;
40
            case 'save_add_check':
41
                if (isset($_POST['cancel'])) {
42
                    $this->doProperties();
43
                } else {
44
                    $this->addCheck(false);
45
                }
46
47
                break;
48
            case 'drop_con':
49
                if (isset($_POST['drop'])) {
50
                    $this->doDropConstraint(false);
51
                } else {
52
                    $this->doProperties();
53
                }
54
55
                break;
56
            case 'confirm_drop_con':
57
                $this->doDropConstraint(true);
58
59
                break;
60
            case 'save_create':
61
                if (isset($_POST['cancel'])) {
62
                    $this->doDefault();
63
                } else {
64
                    $this->doSaveCreate();
65
                }
66
67
                break;
68
            case 'create':
69
                $this->doCreate();
70
71
                break;
72
            case 'drop':
73
                if (isset($_POST['drop'])) {
74
                    $this->doDrop(false);
75
                } else {
76
                    $this->doDefault();
77
                }
78
79
                break;
80
            case 'confirm_drop':
81
                $this->doDrop(true);
82
83
                break;
84
            case 'save_alter':
85
                if (isset($_POST['alter'])) {
86
                    $this->doSaveAlter();
87
                } else {
88
                    $this->doProperties();
89
                }
90
91
                break;
92
            case 'alter':
93
                $this->doAlter();
94
95
                break;
96
            case 'properties':
97
                $this->doProperties();
98
99
                break;
100
            default:
101
                $this->doDefault();
102
103
                break;
104
        }
105
106
        return $this->printFooter();
107
    }
108
109
    /**
110
     * Show default list of domains in the database.
111
     *
112
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
113
     */
114
    public function doDefault($msg = '')
115
    {
116
        $conf = $this->conf;
117
118
        $lang = $this->lang;
119
        $data = $this->misc->getDatabaseAccessor();
120
121
        $this->printTrail('schema');
122
        $this->printTabs('schema', 'domains');
123
        $this->printMsg($msg);
124
125
        $domains = $data->getDomains();
126
127
        $columns = [
128
            'domain' => [
129
                'title' => $lang['strdomain'],
130
                'field' => Decorator::field('domname'),
131
                'url'   => "domains.php?action=properties&amp;{$this->misc->href}&amp;",
132
                'vars'  => ['domain' => 'domname'],
133
            ],
134
            'type' => [
135
                'title' => $lang['strtype'],
136
                'field' => Decorator::field('domtype'),
137
            ],
138
            'notnull' => [
139
                'title'  => $lang['strnotnull'],
140
                'field'  => Decorator::field('domnotnull'),
141
                'type'   => 'bool',
142
                'params' => ['true' => 'NOT NULL', 'false' => ''],
143
            ],
144
            'default' => [
145
                'title' => $lang['strdefault'],
146
                'field' => Decorator::field('domdef'),
147
            ],
148
            'owner' => [
149
                'title' => $lang['strowner'],
150
                'field' => Decorator::field('domowner'),
151
            ],
152
            'actions' => [
153
                'title' => $lang['stractions'],
154
            ],
155
            'comment' => [
156
                'title' => $lang['strcomment'],
157
                'field' => Decorator::field('domcomment'),
158
            ],
159
        ];
160
161
        $actions = [
162
            'alter' => [
163
                'content' => $lang['stralter'],
164
                'attr'    => [
165
                    'href' => [
166
                        'url'     => 'domains.php',
167
                        'urlvars' => [
168
                            'action' => 'alter',
169
                            'domain' => Decorator::field('domname'),
170
                        ],
171
                    ],
172
                ],
173
            ],
174
            'drop' => [
175
                'content' => $lang['strdrop'],
176
                'attr'    => [
177
                    'href' => [
178
                        'url'     => 'domains.php',
179
                        'urlvars' => [
180
                            'action' => 'confirm_drop',
181
                            'domain' => Decorator::field('domname'),
182
                        ],
183
                    ],
184
                ],
185
            ],
186
        ];
187
188
        if (!$data->hasAlterDomains()) {
189
            unset($actions['alter']);
190
        }
191
192
        echo $this->printTable($domains, $columns, $actions, 'domains-domains', $lang['strnodomains']);
193
194
        $navlinks = [
195
            'create' => [
196
                'attr' => [
197
                    'href' => [
198
                        'url'     => 'domains.php',
199
                        'urlvars' => [
200
                            'action'   => 'create',
201
                            'server'   => $_REQUEST['server'],
202
                            'database' => $_REQUEST['database'],
203
                            'schema'   => $_REQUEST['schema'],
204
                        ],
205
                    ],
206
                ],
207
                'content' => $lang['strcreatedomain'],
208
            ],
209
        ];
210
        $this->printNavLinks($navlinks, 'domains-domains', get_defined_vars());
211
    }
212
213
    /**
214
     * Generate XML for the browser tree.
215
     */
216
    public function doTree()
217
    {
218
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
219
220
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
221
        $data = $this->misc->getDatabaseAccessor();
222
223
        $domains = $data->getDomains();
224
225
        $reqvars = $this->misc->getRequestVars('domain');
226
227
        $attrs = [
228
            'text'    => Decorator::field('domname'),
229
            'icon'    => 'Domain',
230
            'toolTip' => Decorator::field('domcomment'),
231
            'action'  => Decorator::actionurl(
232
                'domains.php',
233
                $reqvars,
234
                [
235
                    'action' => 'properties',
236
                    'domain' => Decorator::field('domname'),
237
                ]
238
            ),
239
        ];
240
241
        return $this->printTree($domains, $attrs, 'domains');
242
    }
243
244
    /**
245
     * Function to save after altering a domain.
246
     */
247
    public function doSaveAlter()
248
    {
249
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
250
251
        $lang = $this->lang;
252
        $data = $this->misc->getDatabaseAccessor();
253
254
        $status = $data->alterDomain(
255
            $_POST['domain'],
256
            $_POST['domdefault'],
257
            isset($_POST['domnotnull']),
258
            $_POST['domowner']
259
        );
260
        if (0 == $status) {
261
            $this->doProperties($lang['strdomainaltered']);
262
        } else {
263
            $this->doAlter($lang['strdomainalteredbad']);
264
        }
265
    }
266
267
    /**
268
     * Allow altering a domain.
269
     *
270
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
271
     */
272
    public function doAlter($msg = '')
273
    {
274
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
275
276
        $lang = $this->lang;
277
        $data = $this->misc->getDatabaseAccessor();
278
279
        $this->printTrail('domain');
280
        $this->printTitle($lang['stralter'], 'pg.domain.alter');
281
        $this->printMsg($msg);
282
283
        // Fetch domain info
284
        $domaindata = $data->getDomain($_REQUEST['domain']);
285
        // Fetch all users
286
        $users = $data->getUsers();
287
288
        if ($domaindata->recordCount() > 0) {
289
            if (!isset($_POST['domname'])) {
290
                $_POST['domtype']                 = $domaindata->fields['domtype'];
291
                $_POST['domdefault']              = $domaindata->fields['domdef'];
292
                $domaindata->fields['domnotnull'] = $data->phpBool($domaindata->fields['domnotnull']);
293
                if ($domaindata->fields['domnotnull']) {
294
                    $_POST['domnotnull'] = 'on';
295
                }
296
297
                $_POST['domowner'] = $domaindata->fields['domowner'];
298
            }
299
300
            // Display domain info
301
            echo '<form action="'.\SUBFOLDER."/src/views/domains.php\" method=\"post\">\n";
302
            echo "<table>\n";
303
            echo "<tr><th class=\"data left required\" style=\"width: 70px\">{$lang['strname']}</th>\n";
304
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domname']), "</td></tr>\n";
305
            echo "<tr><th class=\"data left required\">{$lang['strtype']}</th>\n";
306
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domtype']), "</td></tr>\n";
307
            echo "<tr><th class=\"data left\"><label for=\"domnotnull\">{$lang['strnotnull']}</label></th>\n";
308
            echo '<td class="data1"><input type="checkbox" id="domnotnull" name="domnotnull"', (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), " /></td></tr>\n";
309
            echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
310
            echo '<td class="data1"><input name="domdefault" size="32" value="',
311
            htmlspecialchars($_POST['domdefault']), "\" /></td></tr>\n";
312
            echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
313
            echo '<td class="data1"><select name="domowner">';
314
            while (!$users->EOF) {
315
                $uname = $users->fields['usename'];
316
                echo '<option value="', htmlspecialchars($uname), '"',
317
                ($uname == $_POST['domowner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
318
                $users->moveNext();
319
            }
320
            echo "</select></td></tr>\n";
321
            echo "</table>\n";
322
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_alter\" />\n";
323
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
324
            echo $this->misc->form;
325
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
326
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
327
            echo "</form>\n";
328
        } else {
329
            echo "<p>{$lang['strnodata']}</p>\n";
330
        }
331
    }
332
333
    /**
334
     * Confirm and then actually add a CHECK constraint.
335
     *
336
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
337
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
338
     */
339
    public function addCheck($confirm, $msg = '')
340
    {
341
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
342
343
        $lang = $this->lang;
344
        $data = $this->misc->getDatabaseAccessor();
345
346
        if (!isset($_POST['name'])) {
347
            $_POST['name'] = '';
348
        }
349
350
        if (!isset($_POST['definition'])) {
351
            $_POST['definition'] = '';
352
        }
353
354
        if ($confirm) {
355
            $this->printTrail('domain');
356
            $this->printTitle($lang['straddcheck'], 'pg.constraint.check');
357
            $this->printMsg($msg);
358
359
            echo '<form action="'.\SUBFOLDER."/src/views/domains.php\" method=\"post\">\n";
360
            echo "<table>\n";
361
            echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
362
            echo "<th class=\"data required\">{$lang['strdefinition']}</th></tr>\n";
363
364
            echo "<tr><td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
365
            htmlspecialchars($_POST['name']), "\" /></td>\n";
366
367
            echo '<td class="data1">(<input name="definition" size="32" value="',
368
            htmlspecialchars($_POST['definition']), "\" />)</td></tr>\n";
369
            echo "</table>\n";
370
371
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_check\" />\n";
372
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
373
            echo $this->misc->form;
374
            echo "<input type=\"submit\" name=\"add\" value=\"{$lang['stradd']}\" />\n";
375
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
376
            echo "</form>\n";
377
        } else {
378
            if ('' == trim($_POST['definition'])) {
379
                $this->addCheck(true, $lang['strcheckneedsdefinition']);
380
            } else {
381
                $status = $data->addDomainCheckConstraint(
382
                    $_POST['domain'],
383
                    $_POST['definition'],
384
                    $_POST['name']
385
                );
386
                if (0 == $status) {
387
                    $this->doProperties($lang['strcheckadded']);
388
                } else {
389
                    $this->addCheck(true, $lang['strcheckaddedbad']);
390
                }
391
            }
392
        }
393
    }
394
395
    /**
396
     * Show confirmation of drop constraint and perform actual drop.
397
     *
398
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
399
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
400
     */
401
    public function doDropConstraint($confirm, $msg = '')
402
    {
403
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
404
405
        $lang = $this->lang;
406
        $data = $this->misc->getDatabaseAccessor();
407
408
        if ($confirm) {
409
            $this->printTrail('domain');
410
            $this->printTitle($lang['strdrop'], 'pg.constraint.drop');
411
            $this->printMsg($msg);
412
413
            echo '<p>', sprintf(
414
                $lang['strconfdropconstraint'],
415
                $this->misc->printVal($_REQUEST['constraint']),
416
                $this->misc->printVal($_REQUEST['domain'])
417
            ), "</p>\n";
418
            echo '<form action="'.\SUBFOLDER."/src/views/domains.php\" method=\"post\">\n";
419
            echo "<input type=\"hidden\" name=\"action\" value=\"drop_con\" />\n";
420
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
421
            echo '<input type="hidden" name="constraint" value="', htmlspecialchars($_REQUEST['constraint']), "\" />\n";
422
            echo $this->misc->form;
423
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
424
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
425
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
426
            echo "</form>\n";
427
        } else {
428
            $status = $data->dropDomainConstraint($_POST['domain'], $_POST['constraint'], isset($_POST['cascade']));
429
            if (0 == $status) {
430
                $this->doProperties($lang['strconstraintdropped']);
431
            } else {
432
                $this->doDropConstraint(true, $lang['strconstraintdroppedbad']);
433
            }
434
        }
435
    }
436
437
    /**
438
     * Show properties for a domain.  Allow manipulating constraints as well.
439
     *
440
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
441
     */
442
    public function doProperties($msg = '')
443
    {
444
        $conf = $this->conf;
445
446
        $lang = $this->lang;
447
        $data = $this->misc->getDatabaseAccessor();
448
449
        $this->printTrail('domain');
450
        $this->printTitle($lang['strproperties'], 'pg.domain');
451
        $this->printMsg($msg);
452
453
        $domaindata = $data->getDomain($_REQUEST['domain']);
454
455
        if ($domaindata->recordCount() > 0) {
456
            // Show comment if any
457
            if (null !== $domaindata->fields['domcomment']) {
458
                echo '<p class="comment">', $this->misc->printVal($domaindata->fields['domcomment']), "</p>\n";
459
            }
460
461
            // Display domain info
462
            $domaindata->fields['domnotnull'] = $data->phpBool($domaindata->fields['domnotnull']);
463
            echo "<table>\n";
464
            echo "<tr><th class=\"data left\" style=\"width: 70px\">{$lang['strname']}</th>\n";
465
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domname']), "</td></tr>\n";
466
            echo "<tr><th class=\"data left\">{$lang['strtype']}</th>\n";
467
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domtype']), "</td></tr>\n";
468
            echo "<tr><th class=\"data left\">{$lang['strnotnull']}</th>\n";
469
            echo '<td class="data1">', ($domaindata->fields['domnotnull'] ? 'NOT NULL' : ''), "</td></tr>\n";
470
            echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
471
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domdef']), "</td></tr>\n";
472
            echo "<tr><th class=\"data left\">{$lang['strowner']}</th>\n";
473
            echo '<td class="data1">', $this->misc->printVal($domaindata->fields['domowner']), "</td></tr>\n";
474
            echo "</table>\n";
475
476
            // Display domain constraints
477
            echo "<h3>{$lang['strconstraints']}</h3>\n";
478
            if ($data->hasDomainConstraints()) {
479
                $domaincons = $data->getDomainConstraints($_REQUEST['domain']);
480
481
                $columns = [
482
                    'name' => [
483
                        'title' => $lang['strname'],
484
                        'field' => Decorator::field('conname'),
485
                    ],
486
                    'definition' => [
487
                        'title' => $lang['strdefinition'],
488
                        'field' => Decorator::field('consrc'),
489
                    ],
490
                    'actions' => [
491
                        'title' => $lang['stractions'],
492
                    ],
493
                ];
494
495
                $actions = [
496
                    'drop' => [
497
                        'content' => $lang['strdrop'],
498
                        'attr'    => [
499
                            'href' => [
500
                                'url'     => 'domains.php',
501
                                'urlvars' => [
502
                                    'action'     => 'confirm_drop_con',
503
                                    'domain'     => $_REQUEST['domain'],
504
                                    'constraint' => Decorator::field('conname'),
505
                                    'type'       => Decorator::field('contype'),
506
                                ],
507
                            ],
508
                        ],
509
                    ],
510
                ];
511
512
                echo $this->printTable($domaincons, $columns, $actions, 'domains-properties', $lang['strnodata']);
513
            }
514
        } else {
515
            echo "<p>{$lang['strnodata']}</p>\n";
516
        }
517
518
        $navlinks = [
519
            'drop' => [
520
                'attr' => [
521
                    'href' => [
522
                        'url'     => 'domains.php',
523
                        'urlvars' => [
524
                            'action'   => 'confirm_drop',
525
                            'server'   => $_REQUEST['server'],
526
                            'database' => $_REQUEST['database'],
527
                            'schema'   => $_REQUEST['schema'],
528
                            'domain'   => $_REQUEST['domain'],
529
                        ],
530
                    ],
531
                ],
532
                'content' => $lang['strdrop'],
533
            ],
534
        ];
535
        if ($data->hasAlterDomains()) {
536
            $navlinks['addcheck'] = [
537
                'attr' => [
538
                    'href' => [
539
                        'url'     => 'domains.php',
540
                        'urlvars' => [
541
                            'action'   => 'add_check',
542
                            'server'   => $_REQUEST['server'],
543
                            'database' => $_REQUEST['database'],
544
                            'schema'   => $_REQUEST['schema'],
545
                            'domain'   => $_REQUEST['domain'],
546
                        ],
547
                    ],
548
                ],
549
                'content' => $lang['straddcheck'],
550
            ];
551
            $navlinks['alter'] = [
552
                'attr' => [
553
                    'href' => [
554
                        'url'     => 'domains.php',
555
                        'urlvars' => [
556
                            'action'   => 'alter',
557
                            'server'   => $_REQUEST['server'],
558
                            'database' => $_REQUEST['database'],
559
                            'schema'   => $_REQUEST['schema'],
560
                            'domain'   => $_REQUEST['domain'],
561
                        ],
562
                    ],
563
                ],
564
                'content' => $lang['stralter'],
565
            ];
566
        }
567
568
        $this->printNavLinks($navlinks, 'domains-properties', get_defined_vars());
569
    }
570
571
    /**
572
     * Show confirmation of drop and perform actual drop.
573
     *
574
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
575
     */
576
    public function doDrop($confirm)
577
    {
578
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
579
580
        $lang = $this->lang;
581
        $data = $this->misc->getDatabaseAccessor();
582
583
        if ($confirm) {
584
            $this->printTrail('domain');
585
            $this->printTitle($lang['strdrop'], 'pg.domain.drop');
586
587
            echo '<p>', sprintf($lang['strconfdropdomain'], $this->misc->printVal($_REQUEST['domain'])), "</p>\n";
588
            echo '<form action="'.\SUBFOLDER."/src/views/domains.php\" method=\"post\">\n";
589
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /><label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
590
            echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
591
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
592
            echo $this->misc->form;
593
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
594
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
595
            echo "</form>\n";
596
        } else {
597
            $status = $data->dropDomain($_POST['domain'], isset($_POST['cascade']));
598
            if (0 == $status) {
599
                $this->doDefault($lang['strdomaindropped']);
600
            } else {
601
                $this->doDefault($lang['strdomaindroppedbad']);
602
            }
603
        }
604
    }
605
606
    /**
607
     * Displays a screen where they can enter a new domain.
608
     *
609
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
610
     */
611
    public function doCreate($msg = '')
612
    {
613
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
614
615
        $lang = $this->lang;
616
        $data = $this->misc->getDatabaseAccessor();
617
618
        if (!isset($_POST['domname'])) {
619
            $_POST['domname'] = '';
620
        }
621
622
        if (!isset($_POST['domtype'])) {
623
            $_POST['domtype'] = '';
624
        }
625
626
        if (!isset($_POST['domlength'])) {
627
            $_POST['domlength'] = '';
628
        }
629
630
        if (!isset($_POST['domarray'])) {
631
            $_POST['domarray'] = '';
632
        }
633
634
        if (!isset($_POST['domdefault'])) {
635
            $_POST['domdefault'] = '';
636
        }
637
638
        if (!isset($_POST['domcheck'])) {
639
            $_POST['domcheck'] = '';
640
        }
641
642
        $types = $data->getTypes(true);
643
644
        $this->printTrail('schema');
645
        $this->printTitle($lang['strcreatedomain'], 'pg.domain.create');
646
        $this->printMsg($msg);
647
648
        echo '<form action="'.\SUBFOLDER."/src/views/domains.php\" method=\"post\">\n";
649
        echo "<table>\n";
650
        echo "<tr><th class=\"data left required\" style=\"width: 70px\">{$lang['strname']}</th>\n";
651
        echo "<td class=\"data1\"><input name=\"domname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
652
        htmlspecialchars($_POST['domname']), "\" /></td></tr>\n";
653
        echo "<tr><th class=\"data left required\">{$lang['strtype']}</th>\n";
654
        echo "<td class=\"data1\">\n";
655
        // Output return type list
656
        echo "<select name=\"domtype\">\n";
657
        while (!$types->EOF) {
658
            echo '<option value="', htmlspecialchars($types->fields['typname']), '"',
659
            ($types->fields['typname'] == $_POST['domtype']) ? ' selected="selected"' : '', '>',
660
            $this->misc->printVal($types->fields['typname']), "</option>\n";
661
            $types->moveNext();
662
        }
663
        echo "</select>\n";
664
665
        // Type length
666
        echo '<input type="text" size="4" name="domlength" value="', htmlspecialchars($_POST['domlength']), '" />';
667
668
        // Output array type selector
669
        echo "<select name=\"domarray\">\n";
670
        echo '<option value=""', ('' == $_POST['domarray']) ? ' selected="selected"' : '', "></option>\n";
671
        echo '<option value="[]"', ('[]' == $_POST['domarray']) ? ' selected="selected"' : '', ">[ ]</option>\n";
672
        echo "</select></td></tr>\n";
673
674
        echo "<tr><th class=\"data left\"><label for=\"domnotnull\">{$lang['strnotnull']}</label></th>\n";
675
        echo '<td class="data1"><input type="checkbox" id="domnotnull" name="domnotnull"',
676
        (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), " /></td></tr>\n";
677
        echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
678
        echo '<td class="data1"><input name="domdefault" size="32" value="',
679
        htmlspecialchars($_POST['domdefault']), "\" /></td></tr>\n";
680
        if ($data->hasDomainConstraints()) {
681
            echo "<tr><th class=\"data left\">{$lang['strconstraints']}</th>\n";
682
            echo '<td class="data1">CHECK (<input name="domcheck" size="32" value="',
683
            htmlspecialchars($_POST['domcheck']), "\" />)</td></tr>\n";
684
        }
685
        echo "</table>\n";
686
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
687
        echo $this->misc->form;
688
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
689
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
690
        echo "</form>\n";
691
    }
692
693
    /**
694
     * Actually creates the new domain in the database.
695
     */
696
    public function doSaveCreate()
697
    {
698
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
699
700
        $lang = $this->lang;
701
        $data = $this->misc->getDatabaseAccessor();
702
703
        if (!isset($_POST['domcheck'])) {
704
            $_POST['domcheck'] = '';
705
        }
706
707
        // Check that they've given a name and a definition
708
        if ('' == $_POST['domname']) {
709
            $this->doCreate($lang['strdomainneedsname']);
710
        } else {
711
            $status = $data->createDomain(
712
                $_POST['domname'],
713
                $_POST['domtype'],
714
                $_POST['domlength'],
715
                '' != $_POST['domarray'],
716
                isset($_POST['domnotnull']),
717
                $_POST['domdefault'],
718
                $_POST['domcheck']
719
            );
720
            if (0 == $status) {
721
                $this->doDefault($lang['strdomaincreated']);
722
            } else {
723
                $this->doCreate($lang['strdomaincreatedbad']);
724
            }
725
        }
726
    }
727
}
728