Passed
Pull Request — develop (#92)
by Felipe
04:25
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
        $data   = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
23
        $action = $this->action;
24
25
        $this->printHeader($lang['strtablespaces']);
26
        $this->printBody();
27
28
        switch ($action) {
29
            case 'save_create':
30
                if (isset($_REQUEST['cancel'])) {
31
                    $this->doDefault();
32
                } else {
33
                    $this->doSaveCreate();
34
                }
35
36
                break;
37
            case 'create':
38
                $this->doCreate();
39
40
                break;
41
            case 'drop':
42
                if (isset($_REQUEST['cancel'])) {
43
                    $this->doDefault();
44
                } else {
45
                    $this->doDrop(false);
46
                }
47
48
                break;
49
            case 'confirm_drop':
50
                $this->doDrop(true);
51
52
                break;
53
            case 'save_edit':
54
                if (isset($_REQUEST['cancel'])) {
55
                    $this->doDefault();
56
                } else {
57
                    $this->doSaveAlter();
58
                }
59
60
                break;
61
            case 'edit':
62
                $this->doAlter();
63
64
                break;
65
            default:
66
                $this->doDefault();
67
68
                break;
69
        }
70
71
        $this->printFooter();
72
    }
73
74
    /**
75
     * Show default list of tablespaces in the cluster.
76
     *
77
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     */
79
    public function doDefault($msg = '')
80
    {
81
        $lang = $this->lang;
82
        $data = $this->misc->getDatabaseAccessor();
83
84
        $this->printTrail('server');
85
        $this->printTabs('server', 'tablespaces');
86
        $this->printMsg($msg);
87
88
        $tablespaces = $data->getTablespaces();
89
90
        $columns = [
91
            'database' => [
92
                'title' => $lang['strname'],
93
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
94
            ],
95
            'owner'    => [
96
                'title' => $lang['strowner'],
97
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spcowner'),
98
            ],
99
            'location' => [
100
                'title' => $lang['strlocation'],
101
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spclocation'),
102
            ],
103
            'actions'  => [
104
                'title' => $lang['stractions'],
105
            ],
106
        ];
107
108
        if ($data->hasSharedComments()) {
109
            $columns['comment'] = [
110
                'title' => $lang['strcomment'],
111
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spccomment'),
112
            ];
113
        }
114
115
        $actions = [
116
            'alter'      => [
117
                'content' => $lang['stralter'],
118
                'attr'    => [
119
                    'href' => [
120
                        'url'     => 'tablespaces.php',
121
                        'urlvars' => [
122
                            'action'     => 'edit',
123
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
124
                        ],
125
                    ],
126
                ],
127
            ],
128
            'drop'       => [
129
                'content' => $lang['strdrop'],
130
                'attr'    => [
131
                    'href' => [
132
                        'url'     => 'tablespaces.php',
133
                        'urlvars' => [
134
                            'action'     => 'confirm_drop',
135
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
136
                        ],
137
                    ],
138
                ],
139
            ],
140
            'privileges' => [
141
                'content' => $lang['strprivileges'],
142
                'attr'    => [
143
                    'href' => [
144
                        'url'     => 'privileges.php',
145
                        'urlvars' => [
146
                            'subject'    => 'tablespace',
147
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
148
                        ],
149
                    ],
150
                ],
151
            ],
152
        ];
153
154
        echo $this->printTable($tablespaces, $columns, $actions, 'tablespaces-tablespaces', $lang['strnotablespaces']);
155
156
        $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...
157
            'attr'    => [
158
                'href' => [
159
                    'url'     => 'tablespaces.php',
160
                    'urlvars' => [
161
                        'action' => 'create',
162
                        'server' => $_REQUEST['server'],
163
                    ],
164
                ],
165
            ],
166
            'content' => $lang['strcreatetablespace'],
167
        ]], '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...
168
    }
169
170
    /**
171
     * Function to allow altering of a tablespace.
172
     *
173
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
174
     */
175
    public function doAlter($msg = '')
176
    {
177
        $lang = $this->lang;
178
        $data = $this->misc->getDatabaseAccessor();
179
180
        $this->printTrail('tablespace');
181
        $this->printTitle($lang['stralter'], 'pg.tablespace.alter');
182
        $this->printMsg($msg);
183
184
        // Fetch tablespace info
185
        $tablespace = $data->getTablespace($_REQUEST['tablespace']);
186
        // Fetch all users
187
        $users = $data->getUsers();
188
189
        if ($tablespace->recordCount() > 0) {
190
            if (!isset($_POST['name'])) {
191
                $_POST['name'] = $tablespace->fields['spcname'];
192
            }
193
194
            if (!isset($_POST['owner'])) {
195
                $_POST['owner'] = $tablespace->fields['spcowner'];
196
            }
197
198
            if (!isset($_POST['comment'])) {
199
                $_POST['comment'] = ($data->hasSharedComments()) ? $tablespace->fields['spccomment'] : '';
200
            }
201
202
            echo '<form action="' . \SUBFOLDER . "/src/views/tablespaces.php\" method=\"post\">\n";
203
            echo $this->misc->form;
204
            echo "<table>\n";
205
            echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
206
            echo '<td class="data1">';
207
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
208
            htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
209
            echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
210
            echo '<td class="data1"><select name="owner">';
211
            while (!$users->EOF) {
212
                $uname = $users->fields['usename'];
213
                echo '<option value="', htmlspecialchars($uname), '"',
214
                ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
215
                $users->moveNext();
216
            }
217
            echo "</select></td></tr>\n";
218
            if ($data->hasSharedComments()) {
219
                echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
220
                echo '<td class="data1">';
221
                echo '<textarea rows="3" cols="32" name="comment">',
222
                htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
223
            }
224
            echo "</table>\n";
225
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
226
            echo '<input type="hidden" name="tablespace" value="', htmlspecialchars($_REQUEST['tablespace']), "\" />\n";
227
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
228
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
229
            echo "</form>\n";
230
        } else {
231
            echo "<p>{$lang['strnodata']}</p>\n";
232
        }
233
    }
234
235
    /**
236
     * Function to save after altering a tablespace.
237
     */
238
    public function doSaveAlter()
239
    {
240
        $lang = $this->lang;
241
        $data = $this->misc->getDatabaseAccessor();
242
243
        // Check data
244
        if ('' == trim($_POST['name'])) {
245
            $this->doAlter($lang['strtablespaceneedsname']);
246
        } else {
247
            $status = $data->alterTablespace($_POST['tablespace'], $_POST['name'], $_POST['owner'], $_POST['comment']);
248
            if (0 == $status) {
249
                // If tablespace has been renamed, need to change to the new name
250
                if ($_POST['tablespace'] != $_POST['name']) {
251
                    // Jump them to the new table name
252
                    $_REQUEST['tablespace'] = $_POST['name'];
253
                }
254
                $this->doDefault($lang['strtablespacealtered']);
255
            } else {
256
                $this->doAlter($lang['strtablespacealteredbad']);
257
            }
258
        }
259
    }
260
261
    /**
262
     * Show confirmation of drop and perform actual drop.
263
     *
264
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
265
     */
266
    public function doDrop($confirm)
267
    {
268
        $lang = $this->lang;
269
        $data = $this->misc->getDatabaseAccessor();
270
271
        if ($confirm) {
272
            $this->printTrail('tablespace');
273
            $this->printTitle($lang['strdrop'], 'pg.tablespace.drop');
274
275
            echo '<p>', sprintf($lang['strconfdroptablespace'], $this->misc->printVal($_REQUEST['tablespace'])), "</p>\n";
276
277
            echo '<form action="' . \SUBFOLDER . "/src/views/tablespaces.php\" method=\"post\">\n";
278
            echo $this->misc->form;
279
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
280
            echo '<input type="hidden" name="tablespace" value="', htmlspecialchars($_REQUEST['tablespace']), "\" />\n";
281
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
282
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n";
283
            echo "</form>\n";
284
        } else {
285
            $status = $data->droptablespace($_REQUEST['tablespace']);
286
            if (0 == $status) {
287
                $this->doDefault($lang['strtablespacedropped']);
288
            } else {
289
                $this->doDefault($lang['strtablespacedroppedbad']);
290
            }
291
        }
292
    }
293
294
    /**
295
     * Displays a screen where they can enter a new tablespace.
296
     *
297
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
298
     */
299
    public function doCreate($msg = '')
300
    {
301
        $lang = $this->lang;
302
        $data = $this->misc->getDatabaseAccessor();
303
304
        $server_info = $this->misc->getServerInfo();
305
306
        if (!isset($_POST['formSpcname'])) {
307
            $_POST['formSpcname'] = '';
308
        }
309
310
        if (!isset($_POST['formOwner'])) {
311
            $_POST['formOwner'] = $server_info['username'];
312
        }
313
314
        if (!isset($_POST['formLoc'])) {
315
            $_POST['formLoc'] = '';
316
        }
317
318
        if (!isset($_POST['formComment'])) {
319
            $_POST['formComment'] = '';
320
        }
321
322
        // Fetch all users
323
        $users = $data->getUsers();
324
325
        $this->printTrail('server');
326
        $this->printTitle($lang['strcreatetablespace'], 'pg.tablespace.create');
327
        $this->printMsg($msg);
328
329
        echo '<form action="' . \SUBFOLDER . "/src/views/tablespaces.php\" method=\"post\">\n";
330
        echo $this->misc->form;
331
        echo "<table>\n";
332
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
333
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formSpcname\" maxlength=\"{$data->_maxNameLen}\" value=\"", htmlspecialchars($_POST['formSpcname']), "\" /></td>\n\t</tr>\n";
334
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strowner']}</th>\n";
335
        echo "\t\t<td class=\"data1\"><select name=\"formOwner\">\n";
336
        while (!$users->EOF) {
337
            $uname = $users->fields['usename'];
338
            echo "\t\t\t<option value=\"", htmlspecialchars($uname), '"',
339
            ($uname == $_POST['formOwner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
340
            $users->moveNext();
341
        }
342
        echo "\t\t</select></td>\n\t</tr>\n";
343
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strlocation']}</th>\n";
344
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formLoc\" value=\"", htmlspecialchars($_POST['formLoc']), "\" /></td>\n\t</tr>\n";
345
        // Comments (if available)
346
        if ($data->hasSharedComments()) {
347
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
348
            echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
349
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
350
        }
351
        echo "</table>\n";
352
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
353
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
354
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
355
        echo "</form>\n";
356
    }
357
358
    /**
359
     * Actually creates the new tablespace in the cluster.
360
     */
361
    public function doSaveCreate()
362
    {
363
        $lang = $this->lang;
364
        $data = $this->misc->getDatabaseAccessor();
365
366
        // Check data
367
        if ('' == trim($_POST['formSpcname'])) {
368
            $this->doCreate($lang['strtablespaceneedsname']);
369
        } elseif ('' == trim($_POST['formLoc'])) {
370
            $this->doCreate($lang['strtablespaceneedsloc']);
371
        } else {
372
            // Default comment to blank if it isn't set
373
            if (!isset($_POST['formComment'])) {
374
                $_POST['formComment'] = null;
375
            }
376
377
            $status = $data->createTablespace($_POST['formSpcname'], $_POST['formOwner'], $_POST['formLoc'], $_POST['formComment']);
378
            if (0 == $status) {
379
                $this->doDefault($lang['strtablespacecreated']);
380
            } else {
381
                $this->doCreate($lang['strtablespacecreatedbad']);
382
            }
383
        }
384
    }
385
}
386