Issues (217)

MaterializedviewpropertiesController.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 */
12
class MaterializedviewpropertiesController extends BaseController
13
{
14
    use \PHPPgAdmin\Traits\ExportTrait;
0 ignored issues
show
The trait PHPPgAdmin\Traits\ExportTrait requires the property $subFolder which is not provided by PHPPgAdmin\Controller\Ma...iewpropertiesController.
Loading history...
15
    use \PHPPgAdmin\Traits\ViewsMatViewsPropertiesTrait;
16
17
    public $controller_title = 'strviews';
18
19
    public $subject = 'matview';
20
21
    /**
22
     * Default method to render the controller according to the action parameter.
23
     */
24
    public function render()
25
    {
26
        if ('tree' === $this->action) {
27
            return $this->doTree();
28
        }
29
30
        $this->printHeader($this->headerTitle('', '', $_REQUEST[$this->subject]));
31
        $this->printBody();
32
33
        switch ($this->action) {
34
            case 'save_edit':
35
                if (null !== $this->getPostParam('cancel')) {
36
                    $this->doDefinition();
37
                } else {
38
                    $this->doSaveEdit();
39
                }
40
41
                break;
42
            case 'edit':
43
                $this->doEdit();
44
45
                break;
46
            case 'export':
47
                $this->doExport();
48
49
                break;
50
            case 'refresh':
51
                $this->doRefresh();
52
53
                break;
54
            case 'definition':
55
                $this->doDefinition();
56
57
                break;
58
            case 'properties':
59
                if (null !== $this->getPostParam('cancel')) {
60
                    $this->doDefault();
61
                } else {
62
                    $this->doProperties();
63
                }
64
65
                break;
66
            case 'alter':
67
                if (null !== $this->getPostParam('alter')) {
68
                    $this->doAlter(false);
69
                } else {
70
                    $this->doDefault();
71
                }
72
73
                break;
74
            case 'confirm_alter':
75
                $this->doAlter(true);
76
77
                break;
78
            /*case 'drop':
79
            if($this->getPostParam('drop')!==null){
80
            $this->doDrop(false);
81
            } else {
82
            $this->doDefault();
83
            }
84
85
            break;
86
            case 'confirm_drop':
87
            $this->doDrop(true);
88
89
            break;*/
90
            default:
91
                $this->doDefault();
92
93
                break;
94
        }
95
96
        $this->printFooter();
97
    }
98
99
    /**
100
     * Function to save after editing a matview.
101
     */
102
    public function doSaveEdit(): void
103
    {
104
        $data = $this->misc->getDatabaseAccessor();
105
106
        $status = $data->setView($_POST[$this->subject], $_POST['formDefinition'], $_POST['formComment'], true);
107
108
        if (0 === $status) {
109
            $this->doDefinition($this->lang['strviewupdated']);
110
        } else {
111
            $this->doEdit($this->lang['strviewupdatedbad']);
112
        }
113
    }
114
115
    /**
116
     * Function to refresh a matview.
117
     */
118
    public function doRefresh()
119
    {
120
        $data = $this->misc->getDatabaseAccessor();
121
        $sql = 'REFRESH MATERIALIZED VIEW ' . $_REQUEST[$this->subject];
122
        //$this->prtrace($sql);
123
        $status = $data->execute($sql);
124
125
        if (0 === $status) {
126
            return $this->doDefault($this->lang['strviewupdated']);
127
        }
128
129
        return $this->doDefault($this->lang['strviewupdatedbad']);
130
    }
131
132
    /**
133
     * Function to allow editing of a matview.
134
     *
135
     * @param mixed $msg
136
     */
137
    public function doEdit($msg = ''): void
138
    {
139
        $data = $this->misc->getDatabaseAccessor();
140
141
        $this->printTrail($this->subject);
142
        $this->printTitle($this->lang['stredit'], 'pg.matview.alter');
143
        $this->printMsg($msg);
144
145
        $viewdata = $data->getView($_REQUEST[$this->subject]);
146
147
        if (0 < $viewdata->recordCount()) {
148
            if (!isset($_POST['formDefinition'])) {
149
                $_POST['formDefinition'] = $viewdata->fields['vwdefinition'];
150
                $_POST['formComment'] = $viewdata->fields['relcomment'];
151
            }
152
153
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/materializedviewproperties" method="post">' . \PHP_EOL;
154
            echo '<table style="width: 100%">' . \PHP_EOL;
155
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>" . \PHP_EOL;
156
            echo "\t\t<td class=\"data1\"><textarea style=\"width: 100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
157
            \htmlspecialchars($_POST['formDefinition']), "</textarea></td>\n\t</tr>" . \PHP_EOL;
158
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL;
159
            echo "\t\t<td class=\"data1\"><textarea rows=\"3\" cols=\"32\" name=\"formComment\">",
160
            \htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>" . \PHP_EOL;
161
            echo '</table>' . \PHP_EOL;
162
            echo '<p><input type="hidden" name="action" value="save_edit" />' . \PHP_EOL;
163
            echo '<input type="hidden" name="matview" value="', \htmlspecialchars($_REQUEST[$this->subject]), '" />' . \PHP_EOL;
164
            echo $this->view->form;
165
            echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />" . \PHP_EOL;
166
            echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
167
            echo '</form>' . \PHP_EOL;
168
        } else {
169
            echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
170
        }
171
    }
172
173
    /**
174
     * Displays a screen where they can alter a column in a matview.
175
     *
176
     * @param mixed $msg
177
     */
178
    public function doProperties($msg = ''): void
179
    {
180
        $data = $this->misc->getDatabaseAccessor();
181
182
        $this->coalesceArr($_REQUEST, 'stage', 1);
183
184
        switch ($_REQUEST['stage']) {
185
            case 1:
186
                $this->printTrail('column');
187
                $this->printTitle($this->lang['stralter'], 'pg.column.alter');
188
                $this->printMsg($msg);
189
190
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/materializedviewproperties" method="post">' . \PHP_EOL;
191
192
                // Output matview header
193
                echo '<table>' . \PHP_EOL;
194
                echo "<tr><th class=\"data required\">{$this->lang['strname']}</th><th class=\"data required\">{$this->lang['strtype']}</th>";
195
                echo "<th class=\"data\">{$this->lang['strdefault']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>";
196
197
                $column = $data->getTableAttributes($_REQUEST[$this->subject], $_REQUEST['column']);
198
199
                if (!isset($_REQUEST['default'])) {
200
                    $_REQUEST['field'] = $column->fields['attname'];
201
                    $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
202
                    $_REQUEST['comment'] = $column->fields['comment'];
203
                }
204
205
                echo '<tr><td><input name="field" size="32" value="',
206
                \htmlspecialchars($_REQUEST['field']), '" /></td>';
207
208
                echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), '</td>';
209
                echo '<td><input name="default" size="20" value="',
210
                \htmlspecialchars($_REQUEST['default']), '" /></td>';
211
                echo '<td><input name="comment" size="32" value="',
212
                \htmlspecialchars($_REQUEST['comment']), '" /></td>';
213
214
                echo '</table>' . \PHP_EOL;
215
                echo '<p><input type="hidden" name="action" value="properties" />' . \PHP_EOL;
216
                echo '<input type="hidden" name="stage" value="2" />' . \PHP_EOL;
217
                echo $this->view->form;
218
                echo '<input type="hidden" name="matview" value="', \htmlspecialchars($_REQUEST[$this->subject]), '" />' . \PHP_EOL;
219
                echo '<input type="hidden" name="column" value="', \htmlspecialchars($_REQUEST['column']), '" />' . \PHP_EOL;
220
                echo '<input type="hidden" name="olddefault" value="', \htmlspecialchars($_REQUEST['olddefault']), '" />' . \PHP_EOL;
221
                echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />" . \PHP_EOL;
222
                echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
223
                echo '</form>' . \PHP_EOL;
224
225
                break;
226
            case 2:
227
                // Check inputs
228
                if ('' === \trim($_REQUEST['field'])) {
229
                    $_REQUEST['stage'] = 1;
230
                    $this->doProperties($this->lang['strcolneedsname']);
231
232
                    return;
233
                }
234
235
                // Alter the matview column
236
                [$status, $sql] = $data->alterColumn(
237
                    $_REQUEST[$this->subject],
238
                    $_REQUEST['column'],
239
                    $_REQUEST['field'],
240
                    false,
241
                    false,
242
                    $_REQUEST['default'],
243
                    $_REQUEST['olddefault'],
244
                    '',
245
                    '',
246
                    '',
247
                    '',
248
                    $_REQUEST['comment']
249
                );
250
251
                if (0 === $status) {
252
                    $this->doDefault($this->lang['strcolumnaltered']);
253
                } else {
254
                    $_REQUEST['stage'] = 1;
255
                    $this->doProperties($this->lang['strcolumnalteredbad']);
256
257
                    return;
258
                }
259
260
                break;
261
262
            default:
263
                echo "<p>{$this->lang['strinvalidparam']}</p>" . \PHP_EOL;
264
        }
265
    }
266
267
    public function doAlter(bool $confirm = false, $msg = ''): void
268
    {
269
        $data = $this->misc->getDatabaseAccessor();
270
271
        if ($confirm) {
272
            $this->printTrail($this->subject);
273
            $this->printTitle($this->lang['stralter'], 'pg.matview.alter');
274
            $this->printMsg($msg);
275
276
            // Fetch matview info
277
            $matview = $data->getView($_REQUEST[$this->subject]);
278
279
            if (0 < $matview->recordCount()) {
280
                $this->coalesceArr($_POST, 'name', $matview->fields['relname']);
281
282
                $this->coalesceArr($_POST, 'owner', $matview->fields['relowner']);
283
284
                $this->coalesceArr($_POST, 'newschema', $matview->fields['nspname']);
285
286
                $this->coalesceArr($_POST, 'comment', $matview->fields['relcomment']);
287
288
                echo '<form action="' . \containerInstance()->subFolder . '/src/views/materializedviewproperties" method="post">' . \PHP_EOL;
289
                echo '<table>' . \PHP_EOL;
290
                echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>" . \PHP_EOL;
291
                echo '<td class="data1">';
292
                echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
293
                \htmlspecialchars($_POST['name']), '" /></td></tr>' . \PHP_EOL;
294
295
                if ($data->isSuperUser()) {
296
                    // Fetch all users
297
                    $users = $data->getUsers();
298
299
                    echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>" . \PHP_EOL;
300
                    echo '<td class="data1"><select name="owner">';
301
302
                    while (!$users->EOF) {
303
                        $uname = $users->fields['usename'];
304
                        echo '<option value="', \htmlspecialchars($uname), '"',
305
                        ($uname === $_POST['owner']) ? ' selected="selected"' : '', '>', \htmlspecialchars($uname), '</option>' . \PHP_EOL;
306
                        $users->moveNext();
307
                    }
308
                    echo '</select></td></tr>' . \PHP_EOL;
309
                }
310
311
                if ($data->hasAlterTableSchema()) {
312
                    $schemas = $data->getSchemas();
313
                    echo "<tr><th class=\"data left required\">{$this->lang['strschema']}</th>" . \PHP_EOL;
314
                    echo '<td class="data1"><select name="newschema">';
315
316
                    while (!$schemas->EOF) {
317
                        $schema = $schemas->fields['nspname'];
318
                        echo '<option value="', \htmlspecialchars($schema), '"',
319
                        ($schema === $_POST['newschema']) ? ' selected="selected"' : '', '>', \htmlspecialchars($schema), '</option>' . \PHP_EOL;
320
                        $schemas->moveNext();
321
                    }
322
                    echo '</select></td></tr>' . \PHP_EOL;
323
                }
324
325
                echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>" . \PHP_EOL;
326
                echo '<td class="data1">';
327
                echo '<textarea rows="3" cols="32" name="comment">';
328
                echo \htmlspecialchars($_POST['comment']), '</textarea></td></tr>' . \PHP_EOL;
329
                echo '</table>' . \PHP_EOL;
330
                echo '<input type="hidden" name="action" value="alter" />' . \PHP_EOL;
331
                echo '<input type="hidden" name="matview" value="', \htmlspecialchars($_REQUEST[$this->subject]), '" />' . \PHP_EOL;
332
                echo $this->view->form;
333
                echo "<p><input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />" . \PHP_EOL;
334
                echo \sprintf('<input type="submit" name="cancel" value="%s"  /></p>%s', $this->lang['strcancel'], \PHP_EOL);
335
                echo '</form>' . \PHP_EOL;
336
            } else {
337
                echo "<p>{$this->lang['strnodata']}</p>" . \PHP_EOL;
338
            }
339
        } else {
340
            // For databases that don't allow owner change
341
            $this->coalesceArr($_POST, 'owner', '');
342
343
            $this->coalesceArr($_POST, 'newschema', null);
344
345
            $status = $data->alterMatView($_POST[$this->subject], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']);
346
347
            if (0 === $status) {
348
                // If matview has been renamed, need to change to the new name and
349
                // reload the browser frame.
350
                if ($_POST[$this->subject] !== $_POST['name']) {
351
                    // Jump them to the new matview name
352
                    $_REQUEST[$this->subject] = $_POST['name'];
353
                    // Force a browser reload
354
                    $this->view->setReloadBrowser(true);
355
                }
356
                // If schema has changed, need to change to the new schema and reload the browser
357
                if (!empty($_POST['newschema']) && ($_POST['newschema'] !== $data->_schema)) {
358
                    // Jump them to the new sequence schema
359
                    $this->misc->setCurrentSchema($_POST['newschema']);
360
                    $this->view->setReloadBrowser(true);
361
                }
362
                $this->doDefault($this->lang['strviewaltered']);
363
            } else {
364
                $this->doAlter(true, $this->lang['strviewalteredbad']);
365
            }
366
        }
367
    }
368
}
369