Test Failed
Branch develop (db5506)
by Felipe
03:46
created

DomainsController::render()   C

Complexity

Conditions 18
Paths 18

Size

Total Lines 79
Code Lines 61

Duplication

Lines 63
Ratio 79.75 %

Importance

Changes 0
Metric Value
cc 18
eloc 61
nc 18
nop 0
dl 63
loc 79
rs 5.0757
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
3
namespace PHPPgAdmin\Controller;
4
5
use \PHPPgAdmin\Decorators\Decorator;
6
7
/**
8
 * Base controller class
9
 */
10
class DomainsController extends BaseController
11
{
12
    public $_name = 'DomainsController';
13
14
    public function render()
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
    {
16
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
17
        $misc = $this->misc;
0 ignored issues
show
Unused Code introduced by
The assignment to $misc is dead and can be removed.
Loading history...
18
        $lang = $this->lang;
19
20
        $action = $this->action;
21
        if ($action == 'tree') {
22
            return $this->doTree();
23
        }
24
25
        $this->printHeader($lang['strdomains']);
26
        $this->printBody();
27
28 View Code Duplication
        switch ($action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
            case 'add_check':
30
                $this->addCheck(true);
31
                break;
32
            case 'save_add_check':
33
                if (isset($_POST['cancel'])) {
34
                    $this->doProperties();
35
                } else {
36
                    $this->addCheck(false);
37
                }
38
39
                break;
40
            case 'drop_con':
41
                if (isset($_POST['drop'])) {
42
                    $this->doDropConstraint(false);
43
                } else {
44
                    $this->doProperties();
45
                }
46
47
                break;
48
            case 'confirm_drop_con':
49
                $this->doDropConstraint(true);
50
                break;
51
            case 'save_create':
52
                if (isset($_POST['cancel'])) {
53
                    $this->doDefault();
54
                } else {
55
                    $this->doSaveCreate();
56
                }
57
58
                break;
59
            case 'create':
60
                $this->doCreate();
61
                break;
62
            case 'drop':
63
                if (isset($_POST['drop'])) {
64
                    $this->doDrop(false);
65
                } else {
66
                    $this->doDefault();
67
                }
68
69
                break;
70
            case 'confirm_drop':
71
                $this->doDrop(true);
72
                break;
73
            case 'save_alter':
74
                if (isset($_POST['alter'])) {
75
                    $this->doSaveAlter();
76
                } else {
77
                    $this->doProperties();
78
                }
79
80
                break;
81
            case 'alter':
82
                $this->doAlter();
83
                break;
84
            case 'properties':
85
                $this->doProperties();
86
                break;
87
            default:
88
                $this->doDefault();
89
                break;
90
        }
91
92
        return $this->printFooter();
93
    }
94
95
    /**
96
     * Show default list of domains in the database
97
     */
98
    public function doDefault($msg = '')
0 ignored issues
show
Coding Style introduced by
doDefault uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
99
    {
100
        $conf = $this->conf;
101
        $misc = $this->misc;
102
        $lang = $this->lang;
103
        $data = $misc->getDatabaseAccessor();
104
105
        $this->printTrail('schema');
106
        $this->printTabs('schema', 'domains');
107
        $this->printMsg($msg);
108
109
        $domains = $data->getDomains();
110
111
        $columns = [
112
            'domain'  => [
113
                'title' => $lang['strdomain'],
114
                'field' => Decorator::field('domname'),
115
                'url'   => "domains.php?action=properties&amp;{$misc->href}&amp;",
116
                'vars'  => ['domain' => 'domname'],
117
            ],
118
            'type'    => [
119
                'title' => $lang['strtype'],
120
                'field' => Decorator::field('domtype'),
121
            ],
122
            'notnull' => [
123
                'title'  => $lang['strnotnull'],
124
                'field'  => Decorator::field('domnotnull'),
125
                'type'   => 'bool',
126
                'params' => ['true' => 'NOT NULL', 'false' => ''],
127
            ],
128
            'default' => [
129
                'title' => $lang['strdefault'],
130
                'field' => Decorator::field('domdef'),
131
            ],
132
            'owner'   => [
133
                'title' => $lang['strowner'],
134
                'field' => Decorator::field('domowner'),
135
            ],
136
            'actions' => [
137
                'title' => $lang['stractions'],
138
            ],
139
            'comment' => [
140
                'title' => $lang['strcomment'],
141
                'field' => Decorator::field('domcomment'),
142
            ],
143
        ];
144
145
        $actions = [
146
            'alter' => [
147
                'content' => $lang['stralter'],
148
                'attr'    => [
149
                    'href' => [
150
                        'url'     => 'domains.php',
151
                        'urlvars' => [
152
                            'action' => 'alter',
153
                            'domain' => Decorator::field('domname'),
154
                        ],
155
                    ],
156
                ],
157
            ],
158
            'drop'  => [
159
                'content' => $lang['strdrop'],
160
                'attr'    => [
161
                    'href' => [
162
                        'url'     => 'domains.php',
163
                        'urlvars' => [
164
                            'action' => 'confirm_drop',
165
                            'domain' => Decorator::field('domname'),
166
                        ],
167
                    ],
168
                ],
169
            ],
170
        ];
171
172
        if (!$data->hasAlterDomains()) {
173
            unset($actions['alter']);
174
        }
175
176
        echo $this->printTable($domains, $columns, $actions, 'domains-domains', $lang['strnodomains']);
177
178
        $navlinks = [
179
            'create' => [
180
                'attr'    => [
181
                    'href' => [
182
                        'url'     => 'domains.php',
183
                        'urlvars' => [
184
                            'action'   => 'create',
185
                            'server'   => $_REQUEST['server'],
186
                            'database' => $_REQUEST['database'],
187
                            'schema'   => $_REQUEST['schema'],
188
                        ],
189
                    ],
190
                ],
191
                'content' => $lang['strcreatedomain'],
192
            ],
193
        ];
194
        $this->printNavLinks($navlinks, 'domains-domains', get_defined_vars());
195
    }
196
197
    /**
198
     * Generate XML for the browser tree.
199
     */
200 View Code Duplication
    public function doTree()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
    {
202
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
203
        $misc = $this->misc;
204
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
205
        $data = $misc->getDatabaseAccessor();
206
207
        $domains = $data->getDomains();
208
209
        $reqvars = $misc->getRequestVars('domain');
210
211
        $attrs = [
212
            'text'    => Decorator::field('domname'),
213
            'icon'    => 'Domain',
214
            'toolTip' => Decorator::field('domcomment'),
215
            'action'  => Decorator::actionurl('domains.php',
216
                $reqvars,
217
                [
218
                    'action' => 'properties',
219
                    'domain' => Decorator::field('domname'),
220
                ]
221
            ),
222
        ];
223
224
        return $this->printTree($domains, $attrs, 'domains');
225
    }
226
227
    /**
228
     * Function to save after altering a domain
229
     */
230
    public function doSaveAlter()
0 ignored issues
show
Coding Style introduced by
doSaveAlter uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
231
    {
232
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
233
        $misc = $this->misc;
234
        $lang = $this->lang;
235
        $data = $misc->getDatabaseAccessor();
236
237
        $status = $data->alterDomain($_POST['domain'], $_POST['domdefault'],
238
            isset($_POST['domnotnull']), $_POST['domowner']);
239
        if ($status == 0) {
240
            $this->doProperties($lang['strdomainaltered']);
241
        } else {
242
            $this->doAlter($lang['strdomainalteredbad']);
243
        }
244
    }
245
246
    /**
247
     * Allow altering a domain
248
     */
249
    public function doAlter($msg = '')
0 ignored issues
show
Coding Style introduced by
doAlter uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doAlter uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
250
    {
251
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
252
        $misc = $this->misc;
253
        $lang = $this->lang;
254
        $data = $misc->getDatabaseAccessor();
255
256
        $this->printTrail('domain');
257
        $this->printTitle($lang['stralter'], 'pg.domain.alter');
258
        $this->printMsg($msg);
259
260
        // Fetch domain info
261
        $domaindata = $data->getDomain($_REQUEST['domain']);
262
        // Fetch all users
263
        $users = $data->getUsers();
264
265
        if ($domaindata->recordCount() > 0) {
266
            if (!isset($_POST['domname'])) {
267
                $_POST['domtype']                 = $domaindata->fields['domtype'];
268
                $_POST['domdefault']              = $domaindata->fields['domdef'];
269
                $domaindata->fields['domnotnull'] = $data->phpBool($domaindata->fields['domnotnull']);
270
                if ($domaindata->fields['domnotnull']) {
271
                    $_POST['domnotnull'] = 'on';
272
                }
273
274
                $_POST['domowner'] = $domaindata->fields['domowner'];
275
            }
276
277
            // Display domain info
278
            echo '<form action="' . SUBFOLDER . "/src/views/domains.php\" method=\"post\">\n";
279
            echo "<table>\n";
280
            echo "<tr><th class=\"data left required\" style=\"width: 70px\">{$lang['strname']}</th>\n";
281
            echo '<td class="data1">', $misc->printVal($domaindata->fields['domname']), "</td></tr>\n";
282
            echo "<tr><th class=\"data left required\">{$lang['strtype']}</th>\n";
283
            echo '<td class="data1">', $misc->printVal($domaindata->fields['domtype']), "</td></tr>\n";
284
            echo "<tr><th class=\"data left\"><label for=\"domnotnull\">{$lang['strnotnull']}</label></th>\n";
285
            echo '<td class="data1"><input type="checkbox" id="domnotnull" name="domnotnull"', (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), " /></td></tr>\n";
286
            echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
287
            echo '<td class="data1"><input name="domdefault" size="32" value="',
288
            htmlspecialchars($_POST['domdefault']), "\" /></td></tr>\n";
289
            echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
290
            echo '<td class="data1"><select name="domowner">';
291
            while (!$users->EOF) {
292
                $uname = $users->fields['usename'];
293
                echo '<option value="', htmlspecialchars($uname), '"',
294
                ($uname == $_POST['domowner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
295
                $users->moveNext();
296
            }
297
            echo "</select></td></tr>\n";
298
            echo "</table>\n";
299
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_alter\" />\n";
300
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
301
            echo $misc->form;
302
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
303
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
304
            echo "</form>\n";
305
        } else {
306
            echo "<p>{$lang['strnodata']}</p>\n";
307
        }
308
    }
309
310
    /**
311
     * Confirm and then actually add a CHECK constraint
312
     */
313 View Code Duplication
    public function addCheck($confirm, $msg = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
addCheck uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
addCheck uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
314
    {
315
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
316
        $misc = $this->misc;
317
        $lang = $this->lang;
318
        $data = $misc->getDatabaseAccessor();
319
320
        if (!isset($_POST['name'])) {
321
            $_POST['name'] = '';
322
        }
323
324
        if (!isset($_POST['definition'])) {
325
            $_POST['definition'] = '';
326
        }
327
328
        if ($confirm) {
329
            $this->printTrail('domain');
330
            $this->printTitle($lang['straddcheck'], 'pg.constraint.check');
331
            $this->printMsg($msg);
332
333
            echo '<form action="' . SUBFOLDER . "/src/views/domains.php\" method=\"post\">\n";
334
            echo "<table>\n";
335
            echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
336
            echo "<th class=\"data required\">{$lang['strdefinition']}</th></tr>\n";
337
338
            echo "<tr><td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
339
            htmlspecialchars($_POST['name']), "\" /></td>\n";
340
341
            echo '<td class="data1">(<input name="definition" size="32" value="',
342
            htmlspecialchars($_POST['definition']), "\" />)</td></tr>\n";
343
            echo "</table>\n";
344
345
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_check\" />\n";
346
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
347
            echo $misc->form;
348
            echo "<input type=\"submit\" name=\"add\" value=\"{$lang['stradd']}\" />\n";
349
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
350
            echo "</form>\n";
351
        } else {
352
            if (trim($_POST['definition']) == '') {
353
                $this->addCheck(true, $lang['strcheckneedsdefinition']);
354
            } else {
355
                $status = $data->addDomainCheckConstraint($_POST['domain'],
356
                    $_POST['definition'], $_POST['name']);
357
                if ($status == 0) {
358
                    $this->doProperties($lang['strcheckadded']);
359
                } else {
360
                    $this->addCheck(true, $lang['strcheckaddedbad']);
361
                }
362
            }
363
        }
364
    }
365
366
    /**
367
     * Show confirmation of drop constraint and perform actual drop
368
     */
369 View Code Duplication
    public function doDropConstraint($confirm, $msg = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
doDropConstraint uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doDropConstraint uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
370
    {
371
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
372
        $misc = $this->misc;
373
        $lang = $this->lang;
374
        $data = $misc->getDatabaseAccessor();
375
376
        if ($confirm) {
377
            $this->printTrail('domain');
378
            $this->printTitle($lang['strdrop'], 'pg.constraint.drop');
379
            $this->printMsg($msg);
380
381
            echo '<p>', sprintf($lang['strconfdropconstraint'], $misc->printVal($_REQUEST['constraint']),
382
                $misc->printVal($_REQUEST['domain'])), "</p>\n";
383
            echo '<form action="' . SUBFOLDER . "/src/views/domains.php\" method=\"post\">\n";
384
            echo "<input type=\"hidden\" name=\"action\" value=\"drop_con\" />\n";
385
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
386
            echo '<input type="hidden" name="constraint" value="', htmlspecialchars($_REQUEST['constraint']), "\" />\n";
387
            echo $misc->form;
388
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
389
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
390
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
391
            echo "</form>\n";
392
        } else {
393
            $status = $data->dropDomainConstraint($_POST['domain'], $_POST['constraint'], isset($_POST['cascade']));
394
            if ($status == 0) {
395
                $this->doProperties($lang['strconstraintdropped']);
396
            } else {
397
                $this->doDropConstraint(true, $lang['strconstraintdroppedbad']);
398
            }
399
        }
400
    }
401
402
    /**
403
     * Show properties for a domain.  Allow manipulating constraints as well.
404
     */
405
    public function doProperties($msg = '')
0 ignored issues
show
Coding Style introduced by
doProperties uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
406
    {
407
        $conf = $this->conf;
408
        $misc = $this->misc;
409
        $lang = $this->lang;
410
        $data = $misc->getDatabaseAccessor();
411
412
        $this->printTrail('domain');
413
        $this->printTitle($lang['strproperties'], 'pg.domain');
414
        $this->printMsg($msg);
415
416
        $domaindata = $data->getDomain($_REQUEST['domain']);
417
418
        if ($domaindata->recordCount() > 0) {
419
            // Show comment if any
420
            if ($domaindata->fields['domcomment'] !== null) {
421
                echo '<p class="comment">', $misc->printVal($domaindata->fields['domcomment']), "</p>\n";
422
            }
423
424
            // Display domain info
425
            $domaindata->fields['domnotnull'] = $data->phpBool($domaindata->fields['domnotnull']);
426
            echo "<table>\n";
427
            echo "<tr><th class=\"data left\" style=\"width: 70px\">{$lang['strname']}</th>\n";
428
            echo '<td class="data1">', $misc->printVal($domaindata->fields['domname']), "</td></tr>\n";
429
            echo "<tr><th class=\"data left\">{$lang['strtype']}</th>\n";
430
            echo '<td class="data1">', $misc->printVal($domaindata->fields['domtype']), "</td></tr>\n";
431
            echo "<tr><th class=\"data left\">{$lang['strnotnull']}</th>\n";
432
            echo '<td class="data1">', ($domaindata->fields['domnotnull'] ? 'NOT NULL' : ''), "</td></tr>\n";
433
            echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
434
            echo '<td class="data1">', $misc->printVal($domaindata->fields['domdef']), "</td></tr>\n";
435
            echo "<tr><th class=\"data left\">{$lang['strowner']}</th>\n";
436
            echo '<td class="data1">', $misc->printVal($domaindata->fields['domowner']), "</td></tr>\n";
437
            echo "</table>\n";
438
439
            // Display domain constraints
440
            echo "<h3>{$lang['strconstraints']}</h3>\n";
441
            if ($data->hasDomainConstraints()) {
442
                $domaincons = $data->getDomainConstraints($_REQUEST['domain']);
443
444
                $columns = [
445
                    'name'       => [
446
                        'title' => $lang['strname'],
447
                        'field' => Decorator::field('conname'),
448
                    ],
449
                    'definition' => [
450
                        'title' => $lang['strdefinition'],
451
                        'field' => Decorator::field('consrc'),
452
                    ],
453
                    'actions'    => [
454
                        'title' => $lang['stractions'],
455
                    ],
456
                ];
457
458
                $actions = [
459
                    'drop' => [
460
                        'content' => $lang['strdrop'],
461
                        'attr'    => [
462
                            'href' => [
463
                                'url'     => 'domains.php',
464
                                'urlvars' => [
465
                                    'action'     => 'confirm_drop_con',
466
                                    'domain'     => $_REQUEST['domain'],
467
                                    'constraint' => Decorator::field('conname'),
468
                                    'type'       => Decorator::field('contype'),
469
                                ],
470
                            ],
471
                        ],
472
                    ],
473
                ];
474
475
                echo $this->printTable($domaincons, $columns, $actions, 'domains-properties', $lang['strnodata']);
476
            }
477
        } else {
478
            echo "<p>{$lang['strnodata']}</p>\n";
479
        }
480
481
        $navlinks = [
482
            'drop' => [
483
                'attr'    => [
484
                    'href' => [
485
                        'url'     => 'domains.php',
486
                        'urlvars' => [
487
                            'action'   => 'confirm_drop',
488
                            'server'   => $_REQUEST['server'],
489
                            'database' => $_REQUEST['database'],
490
                            'schema'   => $_REQUEST['schema'],
491
                            'domain'   => $_REQUEST['domain'],
492
                        ],
493
                    ],
494
                ],
495
                'content' => $lang['strdrop'],
496
            ],
497
        ];
498
        if ($data->hasAlterDomains()) {
499
            $navlinks['addcheck'] = [
500
                'attr'    => [
501
                    'href' => [
502
                        'url'     => 'domains.php',
503
                        'urlvars' => [
504
                            'action'   => 'add_check',
505
                            'server'   => $_REQUEST['server'],
506
                            'database' => $_REQUEST['database'],
507
                            'schema'   => $_REQUEST['schema'],
508
                            'domain'   => $_REQUEST['domain'],
509
                        ],
510
                    ],
511
                ],
512
                'content' => $lang['straddcheck'],
513
            ];
514
            $navlinks['alter'] = [
515
                'attr'    => [
516
                    'href' => [
517
                        'url'     => 'domains.php',
518
                        'urlvars' => [
519
                            'action'   => 'alter',
520
                            'server'   => $_REQUEST['server'],
521
                            'database' => $_REQUEST['database'],
522
                            'schema'   => $_REQUEST['schema'],
523
                            'domain'   => $_REQUEST['domain'],
524
                        ],
525
                    ],
526
                ],
527
                'content' => $lang['stralter'],
528
            ];
529
        }
530
531
        $this->printNavLinks($navlinks, 'domains-properties', get_defined_vars());
532
    }
533
534
    /**
535
     * Show confirmation of drop and perform actual drop
536
     */
537 View Code Duplication
    public function doDrop($confirm)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
doDrop uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
doDrop uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
538
    {
539
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
540
        $misc = $this->misc;
541
        $lang = $this->lang;
542
        $data = $misc->getDatabaseAccessor();
543
544
        if ($confirm) {
545
            $this->printTrail('domain');
546
            $this->printTitle($lang['strdrop'], 'pg.domain.drop');
547
548
            echo '<p>', sprintf($lang['strconfdropdomain'], $misc->printVal($_REQUEST['domain'])), "</p>\n";
549
            echo '<form action="' . SUBFOLDER . "/src/views/domains.php\" method=\"post\">\n";
550
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /><label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
551
            echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
552
            echo '<input type="hidden" name="domain" value="', htmlspecialchars($_REQUEST['domain']), "\" />\n";
553
            echo $misc->form;
554
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
555
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
556
            echo "</form>\n";
557
        } else {
558
            $status = $data->dropDomain($_POST['domain'], isset($_POST['cascade']));
559
            if ($status == 0) {
560
                $this->doDefault($lang['strdomaindropped']);
561
            } else {
562
                $this->doDefault($lang['strdomaindroppedbad']);
563
            }
564
        }
565
    }
566
567
    /**
568
     * Displays a screen where they can enter a new domain
569
     */
570
    public function doCreate($msg = '')
0 ignored issues
show
Coding Style introduced by
doCreate uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
571
    {
572
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
573
        $misc = $this->misc;
574
        $lang = $this->lang;
575
        $data = $misc->getDatabaseAccessor();
576
577
        if (!isset($_POST['domname'])) {
578
            $_POST['domname'] = '';
579
        }
580
581
        if (!isset($_POST['domtype'])) {
582
            $_POST['domtype'] = '';
583
        }
584
585
        if (!isset($_POST['domlength'])) {
586
            $_POST['domlength'] = '';
587
        }
588
589
        if (!isset($_POST['domarray'])) {
590
            $_POST['domarray'] = '';
591
        }
592
593
        if (!isset($_POST['domdefault'])) {
594
            $_POST['domdefault'] = '';
595
        }
596
597
        if (!isset($_POST['domcheck'])) {
598
            $_POST['domcheck'] = '';
599
        }
600
601
        $types = $data->getTypes(true);
602
603
        $this->printTrail('schema');
604
        $this->printTitle($lang['strcreatedomain'], 'pg.domain.create');
605
        $this->printMsg($msg);
606
607
        echo '<form action="' . SUBFOLDER . "/src/views/domains.php\" method=\"post\">\n";
608
        echo "<table>\n";
609
        echo "<tr><th class=\"data left required\" style=\"width: 70px\">{$lang['strname']}</th>\n";
610
        echo "<td class=\"data1\"><input name=\"domname\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
611
        htmlspecialchars($_POST['domname']), "\" /></td></tr>\n";
612
        echo "<tr><th class=\"data left required\">{$lang['strtype']}</th>\n";
613
        echo "<td class=\"data1\">\n";
614
        // Output return type list
615
        echo "<select name=\"domtype\">\n";
616
        while (!$types->EOF) {
617
            echo '<option value="', htmlspecialchars($types->fields['typname']), '"',
618
            ($types->fields['typname'] == $_POST['domtype']) ? ' selected="selected"' : '', '>',
619
            $misc->printVal($types->fields['typname']), "</option>\n";
620
            $types->moveNext();
621
        }
622
        echo "</select>\n";
623
624
        // Type length
625
        echo '<input type="text" size="4" name="domlength" value="', htmlspecialchars($_POST['domlength']), '" />';
626
627
        // Output array type selector
628
        echo "<select name=\"domarray\">\n";
629
        echo '<option value=""', ($_POST['domarray'] == '') ? ' selected="selected"' : '', "></option>\n";
630
        echo '<option value="[]"', ($_POST['domarray'] == '[]') ? ' selected="selected"' : '', ">[ ]</option>\n";
631
        echo "</select></td></tr>\n";
632
633
        echo "<tr><th class=\"data left\"><label for=\"domnotnull\">{$lang['strnotnull']}</label></th>\n";
634
        echo '<td class="data1"><input type="checkbox" id="domnotnull" name="domnotnull"',
635
        (isset($_POST['domnotnull']) ? ' checked="checked"' : ''), " /></td></tr>\n";
636
        echo "<tr><th class=\"data left\">{$lang['strdefault']}</th>\n";
637
        echo '<td class="data1"><input name="domdefault" size="32" value="',
638
        htmlspecialchars($_POST['domdefault']), "\" /></td></tr>\n";
639
        if ($data->hasDomainConstraints()) {
640
            echo "<tr><th class=\"data left\">{$lang['strconstraints']}</th>\n";
641
            echo '<td class="data1">CHECK (<input name="domcheck" size="32" value="',
642
            htmlspecialchars($_POST['domcheck']), "\" />)</td></tr>\n";
643
        }
644
        echo "</table>\n";
645
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
646
        echo $misc->form;
647
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
648
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
649
        echo "</form>\n";
650
    }
651
652
    /**
653
     * Actually creates the new domain in the database
654
     */
655
    public function doSaveCreate()
0 ignored issues
show
Coding Style introduced by
doSaveCreate uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
656
    {
657
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
658
        $misc = $this->misc;
659
        $lang = $this->lang;
660
        $data = $misc->getDatabaseAccessor();
661
662
        if (!isset($_POST['domcheck'])) {
663
            $_POST['domcheck'] = '';
664
        }
665
666
        // Check that they've given a name and a definition
667
        if ($_POST['domname'] == '') {
668
            $this->doCreate($lang['strdomainneedsname']);
669
        } else {
670
            $status = $data->createDomain($_POST['domname'], $_POST['domtype'], $_POST['domlength'], $_POST['domarray'] != '',
671
                isset($_POST['domnotnull']), $_POST['domdefault'], $_POST['domcheck']);
672
            if ($status == 0) {
673
                $this->doDefault($lang['strdomaincreated']);
674
            } else {
675
                $this->doCreate($lang['strdomaincreatedbad']);
676
            }
677
        }
678
    }
679
}
680