Passed
Push — develop ( c6c0b1...90e9a5 )
by Felipe
10:30 queued 02:43
created

TablespacesController::render()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 5.5471
c 0
b 0
f 0
cc 10
eloc 34
nc 10
nop 0

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