TablespacesController::doCreate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 34
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 49
rs 9.376
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 */
12
class TablespacesController extends BaseController
13
{
14
    public $controller_title = 'strtablespaces';
15
16
    /**
17
     * Default method to render the controller according to the action parameter.
18
     */
19
    public function render(): void
20
    {
21
        $this->printHeader();
22
        $this->printBody();
23
24
        switch ($this->action) {
25
            case 'save_create':
26
                if (isset($_REQUEST['cancel'])) {
27
                    $this->doDefault();
28
                } else {
29
                    $this->doSaveCreate();
30
                }
31
32
                break;
33
            case 'create':
34
                $this->doCreate();
35
36
                break;
37
            case 'drop':
38
                if (isset($_REQUEST['cancel'])) {
39
                    $this->doDefault();
40
                } else {
41
                    $this->doDrop(false);
42
                }
43
44
                break;
45
            case 'confirm_drop':
46
                $this->doDrop(true);
47
48
                break;
49
            case 'save_edit':
50
                if (isset($_REQUEST['cancel'])) {
51
                    $this->doDefault();
52
                } else {
53
                    $this->doSaveAlter();
54
                }
55
56
                break;
57
            case 'edit':
58
                $this->doAlter();
59
60
                break;
61
62
            default:
63
                $this->doDefault();
64
65
                break;
66
        }
67
68
        $this->printFooter();
69
    }
70
71
    /**
72
     * Show default list of tablespaces in the cluster.
73
     *
74
     * @param mixed $msg
75
     */
76
    public function doDefault($msg = ''): void
77
    {
78
        $data = $this->misc->getDatabaseAccessor();
79
80
        $this->printTrail('server');
81
        $this->printTabs('server', 'tablespaces');
82
        $this->printMsg($msg);
83
84
        $tablespaces = $data->getTablespaces();
85
86
        $columns = [
87
            'database' => [
88
                'title' => $this->lang['strname'],
89
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
90
            ],
91
            'owner' => [
92
                'title' => $this->lang['strowner'],
93
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spcowner'),
94
            ],
95
            'location' => [
96
                'title' => $this->lang['strlocation'],
97
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spclocation'),
98
            ],
99
            'actions' => [
100
                'title' => $this->lang['stractions'],
101
            ],
102
        ];
103
104
        if ($data->hasSharedComments()) {
105
            $columns['comment'] = [
106
                'title' => $this->lang['strcomment'],
107
                'field' => \PHPPgAdmin\Decorators\Decorator::field('spccomment'),
108
            ];
109
        }
110
111
        $actions = [
112
            'alter' => [
113
                'content' => $this->lang['stralter'],
114
                'attr' => [
115
                    'href' => [
116
                        'url' => 'tablespaces',
117
                        'urlvars' => [
118
                            'action' => 'edit',
119
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
120
                        ],
121
                    ],
122
                ],
123
            ],
124
            'drop' => [
125
                'content' => $this->lang['strdrop'],
126
                'attr' => [
127
                    'href' => [
128
                        'url' => 'tablespaces',
129
                        'urlvars' => [
130
                            'action' => 'confirm_drop',
131
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
132
                        ],
133
                    ],
134
                ],
135
            ],
136
            'privileges' => [
137
                'content' => $this->lang['strprivileges'],
138
                'attr' => [
139
                    'href' => [
140
                        'url' => 'privileges',
141
                        'urlvars' => [
142
                            'subject' => 'tablespace',
143
                            'tablespace' => \PHPPgAdmin\Decorators\Decorator::field('spcname'),
144
                        ],
145
                    ],
146
                ],
147
            ],
148
        ];
149
150
        echo $this->printTable($tablespaces, $columns, $actions, 'tablespaces-tablespaces', $this->lang['strnotablespaces']);
0 ignored issues
show
Bug introduced by
It seems like $tablespaces can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
        echo $this->printTable(/** @scrutinizer ignore-type */ $tablespaces, $columns, $actions, 'tablespaces-tablespaces', $this->lang['strnotablespaces']);
Loading history...
151
152
        $this->printNavLinks(['create' => [
153
            'attr' => [
154
                'href' => [
155
                    'url' => 'tablespaces',
156
                    'urlvars' => [
157
                        'action' => 'create',
158
                        'server' => $_REQUEST['server'],
159
                    ],
160
                ],
161
            ],
162
            'content' => $this->lang['strcreatetablespace'],
163
        ]], 'tablespaces-tablespaces', \get_defined_vars());
164
    }
165
166
    /**
167
     * Function to allow altering of a tablespace.
168
     *
169
     * @param mixed $msg
170
     */
171
    public function doAlter($msg = ''): void
172
    {
173
        $data = $this->misc->getDatabaseAccessor();
174
175
        $this->printTrail('tablespace');
176
        $this->printTitle($this->lang['stralter'], 'pg.tablespace.alter');
177
        $this->printMsg($msg);
178
179
        // Fetch tablespace info
180
        $tablespace = $data->getTablespace($_REQUEST['tablespace']);
181
        // Fetch all users
182
        $users = $data->getUsers();
183
184
        if (0 < $tablespace->recordCount()) {
185
            $this->coalesceArr($_POST, 'name', $tablespace->fields['spcname']);
186
187
            $this->coalesceArr($_POST, 'owner', $tablespace->fields['spcowner']);
188
189
            $this->coalesceArr($_POST, 'comment', ($data->hasSharedComments()) ? $tablespace->fields['spccomment'] : '');
190
191
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/tablespaces" method="post">' . \PHP_EOL;
192
            echo $this->view->form;
193
            echo '<table>' . \PHP_EOL;
194
            echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
195
            echo '<td class="data1">';
196
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
197
            \htmlspecialchars($_POST['name']), '" /></td></tr>' . \PHP_EOL;
198
            echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>" . \PHP_EOL;
199
            echo '<td class="data1"><select name="owner">';
200
201
            while (!$users->EOF) {
202
                $uname = $users->fields['usename'];
203
                echo '<option value="', \htmlspecialchars($uname), '"',
204
                ($uname === $_POST['owner']) ? ' selected="selected"' : '', '>', \htmlspecialchars($uname), '</option>' . \PHP_EOL;
205
                $users->moveNext();
206
            }
207
            echo '</select></td></tr>' . \PHP_EOL;
208
209
            if ($data->hasSharedComments()) {
210
                echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL;
211
                echo '<td class="data1">';
212
                echo '<textarea rows="3" cols="32" name="comment">',
213
                \htmlspecialchars($_POST['comment']), '</textarea></td></tr>' . \PHP_EOL;
214
            }
215
            echo '</table>' . \PHP_EOL;
216
            echo '<p><input type="hidden" name="action" value="save_edit" />' . \PHP_EOL;
217
            echo '<input type="hidden" name="tablespace" value="', \htmlspecialchars($_REQUEST['tablespace']), '" />' . \PHP_EOL;
218
            echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />" . \PHP_EOL;
219
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
220
            echo '</form>' . \PHP_EOL;
221
        } else {
222
            echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
223
        }
224
    }
225
226
    /**
227
     * Function to save after altering a tablespace.
228
     */
229
    public function doSaveAlter(): void
230
    {
231
        $data = $this->misc->getDatabaseAccessor();
232
233
        // Check data
234
        if ('' === \trim($_POST['name'])) {
235
            $this->doAlter($this->lang['strtablespaceneedsname']);
236
        } else {
237
            $status = $data->alterTablespace($_POST['tablespace'], $_POST['name'], $_POST['owner'], $_POST['comment']);
238
239
            if (0 === $status) {
240
                // If tablespace has been renamed, need to change to the new name
241
                if ($_POST['tablespace'] !== $_POST['name']) {
242
                    // Jump them to the new table name
243
                    $_REQUEST['tablespace'] = $_POST['name'];
244
                }
245
                $this->doDefault($this->lang['strtablespacealtered']);
246
            } else {
247
                $this->doAlter($this->lang['strtablespacealteredbad']);
248
            }
249
        }
250
    }
251
252
    /**
253
     * Show confirmation of drop and perform actual drop.
254
     *
255
     * @param mixed $confirm
256
     */
257
    public function doDrop($confirm): void
258
    {
259
        $data = $this->misc->getDatabaseAccessor();
260
261
        if ($confirm) {
262
            $this->printTrail('tablespace');
263
            $this->printTitle($this->lang['strdrop'], 'pg.tablespace.drop');
264
265
            echo '<p>', \sprintf($this->lang['strconfdroptablespace'], $this->misc->printVal($_REQUEST['tablespace'])), '</p>' . \PHP_EOL;
266
267
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/tablespaces" method="post">' . \PHP_EOL;
268
            echo $this->view->form;
269
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
270
            echo '<input type="hidden" name="tablespace" value="', \htmlspecialchars($_REQUEST['tablespace']), '" />' . \PHP_EOL;
271
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />" . \PHP_EOL;
272
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" />" . \PHP_EOL;
273
            echo '</form>' . \PHP_EOL;
274
        } else {
275
            $status = $data->droptablespace($_REQUEST['tablespace']);
276
277
            if (0 === $status) {
278
                $this->doDefault($this->lang['strtablespacedropped']);
279
            } else {
280
                $this->doDefault($this->lang['strtablespacedroppedbad']);
281
            }
282
        }
283
    }
284
285
    /**
286
     * Displays a screen where they can enter a new tablespace.
287
     *
288
     * @param mixed $msg
289
     */
290
    public function doCreate($msg = ''): void
291
    {
292
        $data = $this->misc->getDatabaseAccessor();
293
294
        $server_info = $this->misc->getServerInfo();
295
296
        $this->coalesceArr($_POST, 'formSpcname', '');
297
298
        $this->coalesceArr($_POST, 'formOwner', $server_info['username']);
299
300
        $this->coalesceArr($_POST, 'formLoc', '');
301
302
        $this->coalesceArr($_POST, 'formComment', '');
303
304
        // Fetch all users
305
        $users = $data->getUsers();
306
307
        $this->printTrail('server');
308
        $this->printTitle($this->lang['strcreatetablespace'], 'pg.tablespace.create');
309
        $this->printMsg($msg);
310
311
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/tablespaces" method="post">' . \PHP_EOL;
312
        echo $this->view->form;
313
        echo '<table>' . \PHP_EOL;
314
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
315
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formSpcname\" maxlength=\"{$data->_maxNameLen}\" value=\"", \htmlspecialchars($_POST['formSpcname']), "\" /></td>\n\t</tr>" . \PHP_EOL;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 190 characters; contains 199 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
316
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strowner']}</th>" . \PHP_EOL;
317
        echo "\t\t<td class=\"data1\"><select name=\"formOwner\">" . \PHP_EOL;
318
319
        while (!$users->EOF) {
320
            $uname = $users->fields['usename'];
321
            echo "\t\t\t<option value=\"", \htmlspecialchars($uname), '"',
322
            ($uname === $_POST['formOwner']) ? ' selected="selected"' : '', '>', \htmlspecialchars($uname), '</option>' . \PHP_EOL;
323
            $users->moveNext();
324
        }
325
        echo "\t\t</select></td>\n\t</tr>" . \PHP_EOL;
326
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strlocation']}</th>" . \PHP_EOL;
327
        echo "\t\t<td class=\"data1\"><input size=\"32\" name=\"formLoc\" value=\"", \htmlspecialchars($_POST['formLoc']), "\" /></td>\n\t</tr>" . \PHP_EOL;
328
        // Comments (if available)
329
        if ($data->hasSharedComments()) {
330
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL;
331
            echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">",
332
            \htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>" . \PHP_EOL;
333
        }
334
        echo '</table>' . \PHP_EOL;
335
        echo '<p><input type="hidden" name="action" value="save_create" />' . \PHP_EOL;
336
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />" . \PHP_EOL;
337
        echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
338
        echo '</form>' . \PHP_EOL;
339
    }
340
341
    /**
342
     * Actually creates the new tablespace in the cluster.
343
     */
344
    public function doSaveCreate(): void
345
    {
346
        $data = $this->misc->getDatabaseAccessor();
347
348
        // Check data
349
        if ('' === \trim($_POST['formSpcname'])) {
350
            $this->doCreate($this->lang['strtablespaceneedsname']);
351
        } elseif ('' === \trim($_POST['formLoc'])) {
352
            $this->doCreate($this->lang['strtablespaceneedsloc']);
353
        } else {
354
            // Default comment to blank if it isn't set
355
            $this->coalesceArr($_POST, 'formComment', null);
356
357
            $status = $data->createTablespace($_POST['formSpcname'], $_POST['formOwner'], $_POST['formLoc'], $_POST['formComment']);
358
359
            if (0 === $status) {
360
                $this->doDefault($this->lang['strtablespacecreated']);
361
            } else {
362
                $this->doCreate($this->lang['strtablespacecreatedbad']);
363
            }
364
        }
365
    }
366
}
367