Passed
Pull Request — develop (#92)
by Felipe
04:47
created

src/controllers/TablespacesController.php (5 issues)

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