Passed
Push — develop ( e54c4a...a6767a )
by Felipe
11:32 queued 05:47
created

ViewpropertiesController::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.45
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 *
12
 * @package PHPPgAdmin
13
 */
14
class ViewpropertiesController extends BaseController
15
{
16
    use \PHPPgAdmin\Traits\ExportTrait;
17
    use \PHPPgAdmin\Traits\ViewsAndMaterializedViewsTrait;
18
19
    public $controller_title = 'strviews';
20
    public $subject          = 'view';
21
22
    /**
23
     * Default method to render the controller according to the action parameter.
24
     */
25
    public function render()
26
    {
27
        if ('tree' == $this->action) {
28
            return $this->doTree();
29
        }
30
31
        $this->printHeader($this->headerTitle('', '', $_REQUEST[$this->subject]));
32
        $this->printBody();
33
34
        switch ($this->action) {
35
            case 'save_edit':
36
                if (isset($_POST['cancel'])) {
37
                    $this->doDefinition();
38
                } else {
39
                    $this->doSaveEdit();
40
                }
41
42
                break;
43
            case 'edit':
44
                $this->doEdit();
45
46
                break;
47
            case 'export':
48
                $this->doExport();
49
50
                break;
51
            case 'definition':
52
                $this->doDefinition();
53
54
                break;
55
            case 'properties':
56
                if (isset($_POST['cancel'])) {
57
                    $this->doDefault();
58
                } else {
59
                    $this->doProperties();
60
                }
61
62
                break;
63
            case 'alter':
64
                if (isset($_POST['alter'])) {
65
                    $this->doAlter(false);
66
                } else {
67
                    $this->doDefault();
68
                }
69
70
                break;
71
            case 'confirm_alter':
72
                $this->doAlter(true);
73
74
                break;
75
            case 'drop':
76
                if (isset($_POST['drop'])) {
77
                    $this->doDrop(false);
0 ignored issues
show
Bug introduced by
The method doDrop() does not exist on PHPPgAdmin\Controller\ViewpropertiesController. ( Ignorable by Annotation )

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

77
                    $this->/** @scrutinizer ignore-call */ 
78
                           doDrop(false);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
                } else {
79
                    $this->doDefault();
80
                }
81
82
                break;
83
            case 'confirm_drop':
84
                $this->doDrop(true);
85
86
                break;
87
            default:
88
                $this->doDefault();
89
90
                break;
91
        }
92
93
        $this->printFooter();
94
    }
95
96
    /**
97
     * Function to save after editing a view.
98
     */
99
    public function doSaveEdit()
100
    {
101
        $data = $this->misc->getDatabaseAccessor();
102
103
        $status = $data->setView($_POST[$this->subject], $_POST['formDefinition'], $_POST['formComment']);
104
        if (0 == $status) {
105
            $this->doDefinition($this->lang['strviewupdated']);
106
        } else {
107
            $this->doEdit($this->lang['strviewupdatedbad']);
108
        }
109
    }
110
111
    /**
112
     * Function to allow editing of a view.
113
     *
114
     * @param mixed $msg
115
     */
116
    public function doEdit($msg = '')
117
    {
118
        $data = $this->misc->getDatabaseAccessor();
119
120
        $this->printTrail($this->subject);
121
        $this->printTitle($this->lang['stredit'], 'pg.view.alter');
122
        $this->printMsg($msg);
123
124
        $viewdata = $data->getView($_REQUEST[$this->subject]);
125
126
        if ($viewdata->recordCount() > 0) {
127
            if (!isset($_POST['formDefinition'])) {
128
                $_POST['formDefinition'] = $viewdata->fields['vwdefinition'];
129
                $_POST['formComment']    = $viewdata->fields['relcomment'];
130
            }
131
132
            echo '<form action="' . \SUBFOLDER . "/src/views/viewproperties\" method=\"post\">\n";
133
            echo "<table style=\"width: 100%\">\n";
134
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strdefinition']}</th>\n";
135
            echo "\t\t<td class=\"data1\"><textarea style=\"width: 100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
136
            htmlspecialchars($_POST['formDefinition']), "</textarea></td>\n\t</tr>\n";
137
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
138
            echo "\t\t<td class=\"data1\"><textarea rows=\"3\" cols=\"32\" name=\"formComment\">",
139
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
140
            echo "</table>\n";
141
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
142
            echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
143
            echo $this->misc->form;
144
            echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
145
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
146
            echo "</form>\n";
147
        } else {
148
            echo "<p>{$this->lang['strnodata']}</p>\n";
149
        }
150
    }
151
152
    /**
153
     * Displays a screen where they can alter a column in a view.
154
     *
155
     * @param mixed $msg
156
     */
157
    public function doProperties($msg = '')
158
    {
159
        $data = $this->misc->getDatabaseAccessor();
160
161
        if (!isset($_REQUEST['stage'])) {
162
            $_REQUEST['stage'] = 1;
163
        }
164
165
        switch ($_REQUEST['stage']) {
166
            case 1:
167
168
                $this->printTrail('column');
169
                $this->printTitle($this->lang['stralter'], 'pg.column.alter');
170
                $this->printMsg($msg);
171
172
                echo '<form action="' . \SUBFOLDER . "/src/views/viewproperties\" method=\"post\">\n";
173
174
                // Output view header
175
                echo "<table>\n";
176
                echo "<tr><th class=\"data required\">{$this->lang['strname']}</th><th class=\"data required\">{$this->lang['strtype']}</th>";
177
                echo "<th class=\"data\">{$this->lang['strdefault']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>";
178
179
                $column = $data->getTableAttributes($_REQUEST[$this->subject], $_REQUEST['column']);
180
181
                if (!isset($_REQUEST['default'])) {
182
                    $_REQUEST['field']   = $column->fields['attname'];
183
                    $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
184
                    $_REQUEST['comment'] = $column->fields['comment'];
185
                }
186
187
                echo '<tr><td><input name="field" size="32" value="',
188
                htmlspecialchars($_REQUEST['field']), '" /></td>';
189
190
                echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), '</td>';
191
                echo '<td><input name="default" size="20" value="',
192
                htmlspecialchars($_REQUEST['default']), '" /></td>';
193
                echo '<td><input name="comment" size="32" value="',
194
                htmlspecialchars($_REQUEST['comment']), '" /></td>';
195
196
                echo "</table>\n";
197
                echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
198
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
199
                echo $this->misc->form;
200
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
201
                echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n";
202
                echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
203
                echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
204
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
205
                echo "</form>\n";
206
207
                break;
208
            case 2:
209
210
                // Check inputs
211
                if ('' == trim($_REQUEST['field'])) {
212
                    $_REQUEST['stage'] = 1;
213
                    $this->doProperties($this->lang['strcolneedsname']);
214
215
                    return;
216
                }
217
218
                // Alter the view column
219
                list($status, $sql) = $data->alterColumn(
220
                    $_REQUEST[$this->subject],
221
                    $_REQUEST['column'],
222
                    $_REQUEST['field'],
223
                    false,
224
                    false,
225
                    $_REQUEST['default'],
226
                    $_REQUEST['olddefault'],
227
                    '',
228
                    '',
229
                    '',
230
                    '',
231
                    $_REQUEST['comment']
232
                );
233
                if (0 == $status) {
234
                    $this->doDefault($this->lang['strcolumnaltered']);
235
                } else {
236
                    $_REQUEST['stage'] = 1;
237
                    $this->doProperties($this->lang['strcolumnalteredbad']);
238
239
                    return;
240
                }
241
242
                break;
243
            default:
244
                echo "<p>{$this->lang['strinvalidparam']}</p>\n";
245
        }
246
    }
247
248
    public function doAlter($confirm = false, $msg = '')
249
    {
250
        $data = $this->misc->getDatabaseAccessor();
251
252
        if ($confirm) {
253
            $this->printTrail($this->subject);
254
            $this->printTitle($this->lang['stralter'], 'pg.view.alter');
255
            $this->printMsg($msg);
256
257
            // Fetch view info
258
            $view = $data->getView($_REQUEST[$this->subject]);
259
260
            if ($view->recordCount() > 0) {
261
                if (!isset($_POST['name'])) {
262
                    $_POST['name'] = $view->fields['relname'];
263
                }
264
265
                if (!isset($_POST['owner'])) {
266
                    $_POST['owner'] = $view->fields['relowner'];
267
                }
268
269
                if (!isset($_POST['newschema'])) {
270
                    $_POST['newschema'] = $view->fields['nspname'];
271
                }
272
273
                if (!isset($_POST['comment'])) {
274
                    $_POST['comment'] = $view->fields['relcomment'];
275
                }
276
277
                echo '<form action="' . \SUBFOLDER . "/src/views/viewproperties\" method=\"post\">\n";
278
                echo "<table>\n";
279
                echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n";
280
                echo '<td class="data1">';
281
                echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
282
                htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
283
284
                if ($data->isSuperUser()) {
285
                    // Fetch all users
286
                    $users = $data->getUsers();
287
288
                    echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>\n";
289
                    echo '<td class="data1"><select name="owner">';
290
                    while (!$users->EOF) {
291
                        $uname = $users->fields['usename'];
292
                        echo '<option value="', htmlspecialchars($uname), '"',
293
                        ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
294
                        $users->moveNext();
295
                    }
296
                    echo "</select></td></tr>\n";
297
                }
298
299
                if ($data->hasAlterTableSchema()) {
300
                    $schemas = $data->getSchemas();
301
                    echo "<tr><th class=\"data left required\">{$this->lang['strschema']}</th>\n";
302
                    echo '<td class="data1"><select name="newschema">';
303
                    while (!$schemas->EOF) {
304
                        $schema = $schemas->fields['nspname'];
305
                        echo '<option value="', htmlspecialchars($schema), '"',
306
                        ($schema == $_POST['newschema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
307
                        $schemas->moveNext();
308
                    }
309
                    echo "</select></td></tr>\n";
310
                }
311
312
                echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>\n";
313
                echo '<td class="data1">';
314
                echo '<textarea rows="3" cols="32" name="comment">',
315
                htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
316
                echo "</table>\n";
317
                echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
318
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
319
                echo $this->misc->form;
320
                echo "<p><input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n";
321
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
322
                echo "</form>\n";
323
            } else {
324
                echo "<p>{$this->lang['strnodata']}</p>\n";
325
            }
326
        } else {
327
            // For databases that don't allow owner change
328
            if (!isset($_POST['owner'])) {
329
                $_POST['owner'] = '';
330
            }
331
332
            if (!isset($_POST['newschema'])) {
333
                $_POST['newschema'] = null;
334
            }
335
336
            $status = $data->alterView($_POST[$this->subject], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']);
337
            if (0 == $status) {
338
                // If view has been renamed, need to change to the new name and
339
                // reload the browser frame.
340
                if ($_POST[$this->subject] != $_POST['name']) {
341
                    // Jump them to the new view name
342
                    $_REQUEST[$this->subject] = $_POST['name'];
343
                    // Force a browser reload
344
                    $this->misc->setReloadBrowser(true);
345
                }
346
                // If schema has changed, need to change to the new schema and reload the browser
347
                if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
348
                    // Jump them to the new sequence schema
349
                    $this->misc->setCurrentSchema($_POST['newschema']);
350
                    $this->misc->setReloadBrowser(true);
351
                }
352
                $this->doDefault($this->lang['strviewaltered']);
353
            } else {
354
                $this->doAlter(true, $this->lang['strviewalteredbad']);
355
            }
356
        }
357
    }
358
}
359