Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

TablespacesController::doAlter()   B

Complexity

Conditions 9
Paths 49

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 42
nc 49
nop 1
dl 0
loc 57
rs 7.0745
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
/**
10
 * Base controller class.
11
 */
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...
12
class TablespacesController extends BaseController
13
{
14
    public $controller_name = 'TablespacesController';
15
16
    /**
17
     * Default method to render the controller according to the action parameter.
18
     */
19
    public function render()
20
    {
21
        $lang   = $this->lang;
22
        $action = $this->action;
23
24
        $this->printHeader($lang['strtablespaces']);
25
        $this->printBody();
26
27
        switch ($action) {
28
            case 'save_create':
29
                if (isset($_REQUEST['cancel'])) {
30
                    $this->doDefault();
31
                } else {
32
                    $this->doSaveCreate();
33
                }
34
35
                break;
36
            case 'create':
37
                $this->doCreate();
38
39
                break;
40
            case 'drop':
41
                if (isset($_REQUEST['cancel'])) {
42
                    $this->doDefault();
43
                } else {
44
                    $this->doDrop(false);
45
                }
46
47
                break;
48
            case 'confirm_drop':
49
                $this->doDrop(true);
50
51
                break;
52
            case 'save_edit':
53
                if (isset($_REQUEST['cancel'])) {
54
                    $this->doDefault();
55
                } else {
56
                    $this->doSaveAlter();
57
                }
58
59
                break;
60
            case 'edit':
61
                $this->doAlter();
62
63
                break;
64
            default:
65
                $this->doDefault();
66
67
                break;
68
        }
69
70
        $this->printFooter();
71
    }
72
73
    /**
74
     * Show default list of tablespaces in the cluster.
75
     *
76
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     */
78
    public function doDefault($msg = '')
79
    {
80
        $lang = $this->lang;
81
        $data = $this->misc->getDatabaseAccessor();
82
83
        $this->printTrail('server');
84
        $this->printTabs('server', 'tablespaces');
85
        $this->printMsg($msg);
86
87
        $tablespaces = $data->getTablespaces();
88
89
        $columns = [
90
            'database' => [
91
                'title' => $lang['strname'],
92
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
93
            ],
94
            'owner'    => [
95
                'title' => $lang['strowner'],
96
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spcowner'),
97
            ],
98
            'location' => [
99
                'title' => $lang['strlocation'],
100
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spclocation'),
101
            ],
102
            'actions'  => [
103
                'title' => $lang['stractions'],
104
            ],
105
        ];
106
107
        if ($data->hasSharedComments()) {
108
            $columns['comment'] = [
109
                'title' => $lang['strcomment'],
110
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spccomment'),
111
            ];
112
        }
113
114
        $actions = [
115
            'alter'      => [
116
                'content' => $lang['stralter'],
117
                'attr'    => [
118
                    'href' => [
119
                        'url'     => 'tablespaces.php',
120
                        'urlvars' => [
121
                            'action'     => 'edit',
122
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
123
                        ],
124
                    ],
125
                ],
126
            ],
127
            'drop'       => [
128
                'content' => $lang['strdrop'],
129
                'attr'    => [
130
                    'href' => [
131
                        'url'     => 'tablespaces.php',
132
                        'urlvars' => [
133
                            'action'     => 'confirm_drop',
134
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
135
                        ],
136
                    ],
137
                ],
138
            ],
139
            'privileges' => [
140
                'content' => $lang['strprivileges'],
141
                'attr'    => [
142
                    'href' => [
143
                        'url'     => 'privileges.php',
144
                        'urlvars' => [
145
                            'subject'    => 'tablespace',
146
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
147
                        ],
148
                    ],
149
                ],
150
            ],
151
        ];
152
153
        echo $this->printTable($tablespaces, $columns, $actions, 'tablespaces-tablespaces', $lang['strnotablespaces']);
154
155
        $this->printNavLinks(['create' => [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
156
            'attr'    => [
157
                'href' => [
158
                    'url'     => 'tablespaces.php',
159
                    'urlvars' => [
160
                        'action' => 'create',
161
                        'server' => $_REQUEST['server'],
162
                    ],
163
                ],
164
            ],
165
            'content' => $lang['strcreatetablespace'],
166
        ]], 'tablespaces-tablespaces', get_defined_vars());
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
167
    }
168
169
    /**
170
     * Function to allow altering of a tablespace.
171
     *
172
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
173
     */
174
    public function doAlter($msg = '')
175
    {
176
        $lang = $this->lang;
177
        $data = $this->misc->getDatabaseAccessor();
178
179
        $this->printTrail('tablespace');
180
        $this->printTitle($lang['stralter'], 'pg.tablespace.alter');
181
        $this->printMsg($msg);
182
183
        // Fetch tablespace info
184
        $tablespace = $data->getTablespace($_REQUEST['tablespace']);
185
        // Fetch all users
186
        $users = $data->getUsers();
187
188
        if ($tablespace->recordCount() > 0) {
189
            if (!isset($_POST['name'])) {
190
                $_POST['name'] = $tablespace->fields['spcname'];
191
            }
192
193
            if (!isset($_POST['owner'])) {
194
                $_POST['owner'] = $tablespace->fields['spcowner'];
195
            }
196
197
            if (!isset($_POST['comment'])) {
198
                $_POST['comment'] = ($data->hasSharedComments()) ? $tablespace->fields['spccomment'] : '';
199
            }
200
201
            echo '<form action="' . \SUBFOLDER . "/src/views/tablespaces.php\" method=\"post\">\n";
202
            echo $this->misc->form;
203
            echo "<table>\n";
204
            echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
205
            echo '<td class="data1">';
206
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
207
            htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
208
            echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
209
            echo '<td class="data1"><select name="owner">';
210
            while (!$users->EOF) {
211
                $uname = $users->fields['usename'];
212
                echo '<option value="', htmlspecialchars($uname), '"',
213
                ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
214
                $users->moveNext();
215
            }
216
            echo "</select></td></tr>\n";
217
            if ($data->hasSharedComments()) {
218
                echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
219
                echo '<td class="data1">';
220
                echo '<textarea rows="3" cols="32" name="comment">',
221
                htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
222
            }
223
            echo "</table>\n";
224
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
225
            echo '<input type="hidden" name="tablespace" value="', htmlspecialchars($_REQUEST['tablespace']), "\" />\n";
226
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
227
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
228
            echo "</form>\n";
229
        } else {
230
            echo "<p>{$lang['strnodata']}</p>\n";
231
        }
232
    }
233
234
    /**
235
     * Function to save after altering a tablespace.
236
     */
237
    public function doSaveAlter()
238
    {
239
        $lang = $this->lang;
240
        $data = $this->misc->getDatabaseAccessor();
241
242
        // Check data
243
        if ('' == trim($_POST['name'])) {
244
            $this->doAlter($lang['strtablespaceneedsname']);
245
        } else {
246
            $status = $data->alterTablespace($_POST['tablespace'], $_POST['name'], $_POST['owner'], $_POST['comment']);
247
            if (0 == $status) {
248
                // If tablespace has been renamed, need to change to the new name
249
                if ($_POST['tablespace'] != $_POST['name']) {
250
                    // Jump them to the new table name
251
                    $_REQUEST['tablespace'] = $_POST['name'];
252
                }
253
                $this->doDefault($lang['strtablespacealtered']);
254
            } else {
255
                $this->doAlter($lang['strtablespacealteredbad']);
256
            }
257
        }
258
    }
259
260
    /**
261
     * Show confirmation of drop and perform actual drop.
262
     *
263
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
264
     */
265
    public function doDrop($confirm)
266
    {
267
        $lang = $this->lang;
268
        $data = $this->misc->getDatabaseAccessor();
269
270
        if ($confirm) {
271
            $this->printTrail('tablespace');
272
            $this->printTitle($lang['strdrop'], 'pg.tablespace.drop');
273
274
            echo '<p>', sprintf($lang['strconfdroptablespace'], $this->misc->printVal($_REQUEST['tablespace'])), "</p>\n";
275
276
            echo '<form action="' . \SUBFOLDER . "/src/views/tablespaces.php\" method=\"post\">\n";
277
            echo $this->misc->form;
278
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
279
            echo '<input type="hidden" name="tablespace" value="', htmlspecialchars($_REQUEST['tablespace']), "\" />\n";
280
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
281
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
282
            echo "</form>\n";
283
        } else {
284
            $status = $data->droptablespace($_REQUEST['tablespace']);
285
            if (0 == $status) {
286
                $this->doDefault($lang['strtablespacedropped']);
287
            } else {
288
                $this->doDefault($lang['strtablespacedroppedbad']);
289
            }
290
        }
291
    }
292
293
    /**
294
     * Displays a screen where they can enter a new tablespace.
295
     *
296
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
297
     */
298
    public function doCreate($msg = '')
299
    {
300
        $lang = $this->lang;
301
        $data = $this->misc->getDatabaseAccessor();
302
303
        $server_info = $this->misc->getServerInfo();
304
305
        if (!isset($_POST['formSpcname'])) {
306
            $_POST['formSpcname'] = '';
307
        }
308
309
        if (!isset($_POST['formOwner'])) {
310
            $_POST['formOwner'] = $server_info['username'];
311
        }
312
313
        if (!isset($_POST['formLoc'])) {
314
            $_POST['formLoc'] = '';
315
        }
316
317
        if (!isset($_POST['formComment'])) {
318
            $_POST['formComment'] = '';
319
        }
320
321
        // Fetch all users
322
        $users = $data->getUsers();
323
324
        $this->printTrail('server');
325
        $this->printTitle($lang['strcreatetablespace'], 'pg.tablespace.create');
326
        $this->printMsg($msg);
327
328
        echo '<form action="' . \SUBFOLDER . "/src/views/tablespaces.php\" method=\"post\">\n";
329
        echo $this->misc->form;
330
        echo "<table>\n";
331
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
332
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formSpcname\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_POST['formSpcname']), "\" /></td>\n\t</tr>\n";
333
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strowner']}</th>\n";
334
        echo "\t\t<td class=\"data1\"><select name=\"formOwner\">\n";
335
        while (!$users->EOF) {
336
            $uname = $users->fields['usename'];
337
            echo "\t\t\t<option value=\"", htmlspecialchars($uname), '"',
338
            ($uname == $_POST['formOwner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
339
            $users->moveNext();
340
        }
341
        echo "\t\t</select></td>\n\t</tr>\n";
342
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strlocation']}</th>\n";
343
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formLoc\" value=\"", htmlspecialchars($_POST['formLoc']), "\" /></td>\n\t</tr>\n";
344
        // Comments (if available)
345
        if ($data->hasSharedComments()) {
346
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
347
            echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
348
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
349
        }
350
        echo "</table>\n";
351
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
352
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
353
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
354
        echo "</form>\n";
355
    }
356
357
    /**
358
     * Actually creates the new tablespace in the cluster.
359
     */
360
    public function doSaveCreate()
361
    {
362
        $lang = $this->lang;
363
        $data = $this->misc->getDatabaseAccessor();
364
365
        // Check data
366
        if ('' == trim($_POST['formSpcname'])) {
367
            $this->doCreate($lang['strtablespaceneedsname']);
368
        } elseif ('' == trim($_POST['formLoc'])) {
369
            $this->doCreate($lang['strtablespaceneedsloc']);
370
        } else {
371
            // Default comment to blank if it isn't set
372
            if (!isset($_POST['formComment'])) {
373
                $_POST['formComment'] = null;
374
            }
375
376
            $status = $data->createTablespace($_POST['formSpcname'], $_POST['formOwner'], $_POST['formLoc'], $_POST['formComment']);
377
            if (0 == $status) {
378
                $this->doDefault($lang['strtablespacecreated']);
379
            } else {
380
                $this->doCreate($lang['strtablespacecreatedbad']);
381
            }
382
        }
383
    }
384
}
385