Passed
Push — master ( 5397c5...a7725b )
by Felipe
05:40
created

MaterializedviewpropertiesController::doDefinition()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 26
nc 3
nop 1
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.46
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 *
12
 * @package PHPPgAdmin
13
 */
14
class MaterializedviewpropertiesController extends BaseController
15
{
16
    use \PHPPgAdmin\Traits\ExportTrait;
17
    use \PHPPgAdmin\Traits\ViewsAndMaterializedViewsTrait;
18
    public $controller_title = 'strviews';
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 (isset($_POST['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 (isset($_POST['cancel'])) {
60
                    $this->doDefault();
61
                } else {
62
                    $this->doProperties();
63
                }
64
65
                break;
66
            case 'alter':
67
                if (isset($_POST['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 (isset($_POST['drop'])) {
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()
103
    {
104
        $data = $this->misc->getDatabaseAccessor();
105
106
        $status = $data->setView($_POST[$this->subject], $_POST['formDefinition'], $_POST['formComment']);
107
        if (0 == $status) {
108
            $this->doDefinition($this->lang['strviewupdated']);
109
        } else {
110
            $this->doEdit($this->lang['strviewupdatedbad']);
111
        }
112
    }
113
114
    /**
115
     * Function to refresh a matview.
116
     */
117
    public function doRefresh()
118
    {
119
        $data = $this->misc->getDatabaseAccessor();
120
        $sql  = 'REFRESH MATERIALIZED VIEW '.$_REQUEST[$this->subject];
121
        $this->prtrace($sql);
122
        $status = $data->execute($sql);
123
124
        if (0 == $status) {
125
            return $this->doDefault($this->lang['strviewupdated']);
126
        }
127
128
        return $this->doDefault($this->lang['strviewupdatedbad']);
129
    }
130
131
    /**
132
     * Function to allow editing of a matview.
133
     *
134
     * @param mixed $msg
135
     */
136
    public function doEdit($msg = '')
137
    {
138
        $data = $this->misc->getDatabaseAccessor();
139
140
        $this->printTrail($this->subject);
141
        $this->printTitle($this->lang['stredit'], 'pg.matview.alter');
142
        $this->printMsg($msg);
143
144
        $viewdata = $data->getView($_REQUEST[$this->subject]);
145
146
        if ($viewdata->recordCount() > 0) {
147
            if (!isset($_POST['formDefinition'])) {
148
                $_POST['formDefinition'] = $viewdata->fields['vwdefinition'];
149
                $_POST['formComment']    = $viewdata->fields['relcomment'];
150
            }
151
152
            echo '<form action="'.\SUBFOLDER."/src/views/materializedviewproperties\" method=\"post\">\n";
153
            echo "<table style=\"width: 100%\">\n";
154
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>\n";
155
            echo "\t\t<td class=\"data1\"><textarea style=\"width: 100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
156
            htmlspecialchars($_POST['formDefinition']), "</textarea></td>\n\t</tr>\n";
157
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
158
            echo "\t\t<td class=\"data1\"><textarea rows=\"3\" cols=\"32\" name=\"formComment\">",
159
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
160
            echo "</table>\n";
161
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
162
            echo '<input type="hidden" name="matview" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
163
            echo $this->misc->form;
164
            echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
165
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
166
            echo "</form>\n";
167
        } else {
168
            echo "<p>{$this->lang['strnodata']}</p>\n";
169
        }
170
    }
171
172
    /**
173
     * Displays a screen where they can alter a column in a matview.
174
     *
175
     * @param mixed $msg
176
     */
177
    public function doProperties($msg = '')
178
    {
179
        $data = $this->misc->getDatabaseAccessor();
180
181
        $this->coalesceArr($_REQUEST, 'stage', 1);
182
183
        switch ($_REQUEST['stage']) {
184
            case 1:
185
186
                $this->printTrail('column');
187
                $this->printTitle($this->lang['stralter'], 'pg.column.alter');
188
                $this->printMsg($msg);
189
190
                echo '<form action="'.\SUBFOLDER."/src/views/materializedviewproperties\" method=\"post\">\n";
191
192
                // Output matview header
193
                echo "<table>\n";
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>\n";
215
                echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
216
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
217
                echo $this->misc->form;
218
                echo '<input type="hidden" name="matview" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
219
                echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n";
220
                echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
221
                echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
222
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
223
                echo "</form>\n";
224
225
                break;
226
            case 2:
227
228
                // Check inputs
229
                if ('' == trim($_REQUEST['field'])) {
230
                    $_REQUEST['stage'] = 1;
231
                    $this->doProperties($this->lang['strcolneedsname']);
232
233
                    return;
234
                }
235
236
                // Alter the matview column
237
                list($status, $sql) = $data->alterColumn(
238
                    $_REQUEST[$this->subject],
239
                    $_REQUEST['column'],
240
                    $_REQUEST['field'],
241
                    false,
242
                    false,
243
                    $_REQUEST['default'],
244
                    $_REQUEST['olddefault'],
245
                    '',
246
                    '',
247
                    '',
248
                    '',
249
                    $_REQUEST['comment']
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
            default:
262
                echo "<p>{$this->lang['strinvalidparam']}</p>\n";
263
        }
264
    }
265
266
    public function doAlter($confirm = false, $msg = '')
267
    {
268
        $data = $this->misc->getDatabaseAccessor();
269
270
        if ($confirm) {
271
            $this->printTrail($this->subject);
272
            $this->printTitle($this->lang['stralter'], 'pg.matview.alter');
273
            $this->printMsg($msg);
274
275
            // Fetch matview info
276
            $matview = $data->getView($_REQUEST[$this->subject]);
277
278
            if ($matview->recordCount() > 0) {
279
                $this->coalesceArr($_POST, 'name', $matview->fields['relname']);
280
281
                $this->coalesceArr($_POST, 'owner', $matview->fields['relowner']);
282
283
                $this->coalesceArr($_POST, 'newschema', $matview->fields['nspname']);
284
285
                $this->coalesceArr($_POST, 'comment', $matview->fields['relcomment']);
286
287
                echo '<form action="'.\SUBFOLDER."/src/views/materializedviewproperties\" method=\"post\">\n";
288
                echo "<table>\n";
289
                echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n";
290
                echo '<td class="data1">';
291
                echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
292
                htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
293
294
                if ($data->isSuperUser()) {
295
                    // Fetch all users
296
                    $users = $data->getUsers();
297
298
                    echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>\n";
299
                    echo '<td class="data1"><select name="owner">';
300
                    while (!$users->EOF) {
301
                        $uname = $users->fields['usename'];
302
                        echo '<option value="', htmlspecialchars($uname), '"',
303
                        ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
304
                        $users->moveNext();
305
                    }
306
                    echo "</select></td></tr>\n";
307
                }
308
309
                if ($data->hasAlterTableSchema()) {
310
                    $schemas = $data->getSchemas();
311
                    echo "<tr><th class=\"data left required\">{$this->lang['strschema']}</th>\n";
312
                    echo '<td class="data1"><select name="newschema">';
313
                    while (!$schemas->EOF) {
314
                        $schema = $schemas->fields['nspname'];
315
                        echo '<option value="', htmlspecialchars($schema), '"',
316
                        ($schema == $_POST['newschema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
317
                        $schemas->moveNext();
318
                    }
319
                    echo "</select></td></tr>\n";
320
                }
321
322
                echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>\n";
323
                echo '<td class="data1">';
324
                echo '<textarea rows="3" cols="32" name="comment">';
325
                echo htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
326
                echo "</table>\n";
327
                echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
328
                echo '<input type="hidden" name="matview" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
329
                echo $this->misc->form;
330
                echo "<p><input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n";
331
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
332
                echo "</form>\n";
333
            } else {
334
                echo "<p>{$this->lang['strnodata']}</p>\n";
335
            }
336
        } else {
337
            // For databases that don't allow owner change
338
            $this->coalesceArr($_POST, 'owner', '');
339
340
            $this->coalesceArr($_POST, 'newschema', null);
341
342
            $status = $data->alterView($_POST[$this->subject], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']);
343
            if (0 == $status) {
344
                // If matview has been renamed, need to change to the new name and
345
                // reload the browser frame.
346
                if ($_POST[$this->subject] != $_POST['name']) {
347
                    // Jump them to the new matview name
348
                    $_REQUEST[$this->subject] = $_POST['name'];
349
                    // Force a browser reload
350
                    $this->misc->setReloadBrowser(true);
351
                }
352
                // If schema has changed, need to change to the new schema and reload the browser
353
                if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
354
                    // Jump them to the new sequence schema
355
                    $this->misc->setCurrentSchema($_POST['newschema']);
356
                    $this->misc->setReloadBrowser(true);
357
                }
358
                $this->doDefault($this->lang['strviewaltered']);
359
            } else {
360
                $this->doAlter(true, $this->lang['strviewalteredbad']);
361
            }
362
        }
363
    }
364
}
365