Passed
Pull Request — develop (#92)
by Felipe
06:19
created

DomainsController::render()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 86
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 60
nc 18
nop 0
dl 0
loc 86
rs 4.7996
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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