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

ViewpropertiesController::doAlter()   D

Complexity

Conditions 13
Paths 10

Size

Total Lines 95
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 4.9922
c 0
b 0
f 0
cc 13
eloc 64
nc 10
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 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);
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
        $this->coalesceArr($_REQUEST, 'stage', 1);
162
163
        switch ($_REQUEST['stage']) {
164
            case 1:
165
166
                $this->printTrail('column');
167
                $this->printTitle($this->lang['stralter'], 'pg.column.alter');
168
                $this->printMsg($msg);
169
170
                echo '<form action="'.\SUBFOLDER."/src/views/viewproperties\" method=\"post\">\n";
171
172
                // Output view header
173
                echo "<table>\n";
174
                echo "<tr><th class=\"data required\">{$this->lang['strname']}</th><th class=\"data required\">{$this->lang['strtype']}</th>";
175
                echo "<th class=\"data\">{$this->lang['strdefault']}</th><th class=\"data\">{$this->lang['strcomment']}</th></tr>";
176
177
                $column = $data->getTableAttributes($_REQUEST[$this->subject], $_REQUEST['column']);
178
179
                if (!isset($_REQUEST['default'])) {
180
                    $_REQUEST['field']   = $column->fields['attname'];
181
                    $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
182
                    $_REQUEST['comment'] = $column->fields['comment'];
183
                }
184
185
                echo '<tr><td><input name="field" size="32" value="',
186
                htmlspecialchars($_REQUEST['field']), '" /></td>';
187
188
                echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), '</td>';
189
                echo '<td><input name="default" size="20" value="',
190
                htmlspecialchars($_REQUEST['default']), '" /></td>';
191
                echo '<td><input name="comment" size="32" value="',
192
                htmlspecialchars($_REQUEST['comment']), '" /></td>';
193
194
                echo "</table>\n";
195
                echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
196
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
197
                echo $this->misc->form;
198
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
199
                echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n";
200
                echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
201
                echo "<input type=\"submit\" value=\"{$this->lang['stralter']}\" />\n";
202
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
203
                echo "</form>\n";
204
205
                break;
206
            case 2:
207
208
                // Check inputs
209
                if ('' == trim($_REQUEST['field'])) {
210
                    $_REQUEST['stage'] = 1;
211
                    $this->doProperties($this->lang['strcolneedsname']);
212
213
                    return;
214
                }
215
216
                // Alter the view column
217
                list($status, $sql) = $data->alterColumn(
218
                    $_REQUEST[$this->subject],
219
                    $_REQUEST['column'],
220
                    $_REQUEST['field'],
221
                    false,
222
                    false,
223
                    $_REQUEST['default'],
224
                    $_REQUEST['olddefault'],
225
                    '',
226
                    '',
227
                    '',
228
                    '',
229
                    $_REQUEST['comment']
230
                );
231
                if (0 == $status) {
232
                    $this->doDefault($this->lang['strcolumnaltered']);
233
                } else {
234
                    $_REQUEST['stage'] = 1;
235
                    $this->doProperties($this->lang['strcolumnalteredbad']);
236
237
                    return;
238
                }
239
240
                break;
241
            default:
242
                echo "<p>{$this->lang['strinvalidparam']}</p>\n";
243
        }
244
    }
245
246
    public function doAlter($confirm = false, $msg = '')
247
    {
248
        $data = $this->misc->getDatabaseAccessor();
249
250
        if ($confirm) {
251
            $this->printTrail($this->subject);
252
            $this->printTitle($this->lang['stralter'], 'pg.view.alter');
253
            $this->printMsg($msg);
254
255
            // Fetch view info
256
            $view = $data->getView($_REQUEST[$this->subject]);
257
258
            if ($view->recordCount() > 0) {
259
                $this->coalesceArr($_POST, 'name', $view->fields['relname']);
260
261
                $this->coalesceArr($_POST, 'owner', $view->fields['relowner']);
262
263
                $this->coalesceArr($_POST, 'newschema', $view->fields['nspname']);
264
265
                $this->coalesceArr($_POST, 'comment', $view->fields['relcomment']);
266
267
                echo '<form action="'.\SUBFOLDER."/src/views/viewproperties\" method=\"post\">\n";
268
                echo "<table>\n";
269
                echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n";
270
                echo '<td class="data1">';
271
                echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
272
                htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
273
274
                if ($data->isSuperUser()) {
275
                    // Fetch all users
276
                    $users = $data->getUsers();
277
278
                    echo "<tr><th class=\"data left required\">{$this->lang['strowner']}</th>\n";
279
                    echo '<td class="data1"><select name="owner">';
280
                    while (!$users->EOF) {
281
                        $uname = $users->fields['usename'];
282
                        echo '<option value="', htmlspecialchars($uname), '"',
283
                        ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
284
                        $users->moveNext();
285
                    }
286
                    echo "</select></td></tr>\n";
287
                }
288
289
                if ($data->hasAlterTableSchema()) {
290
                    $schemas = $data->getSchemas();
291
                    echo "<tr><th class=\"data left required\">{$this->lang['strschema']}</th>\n";
292
                    echo '<td class="data1"><select name="newschema">';
293
                    while (!$schemas->EOF) {
294
                        $schema = $schemas->fields['nspname'];
295
                        echo '<option value="', htmlspecialchars($schema), '"',
296
                        ($schema == $_POST['newschema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
297
                        $schemas->moveNext();
298
                    }
299
                    echo "</select></td></tr>\n";
300
                }
301
302
                echo "<tr><th class=\"data left\">{$this->lang['strcomment']}</th>\n";
303
                echo '<td class="data1">';
304
                echo '<textarea rows="3" cols="32" name="comment">',
305
                htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
306
                echo "</table>\n";
307
                echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
308
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST[$this->subject]), "\" />\n";
309
                echo $this->misc->form;
310
                echo "<p><input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n";
311
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
312
                echo "</form>\n";
313
            } else {
314
                echo "<p>{$this->lang['strnodata']}</p>\n";
315
            }
316
        } else {
317
            // For databases that don't allow owner change
318
            $this->coalesceArr($_POST, 'owner', '');
319
320
            $this->coalesceArr($_POST, 'newschema', null);
321
322
            $status = $data->alterView($_POST[$this->subject], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']);
323
            if (0 == $status) {
324
                // If view has been renamed, need to change to the new name and
325
                // reload the browser frame.
326
                if ($_POST[$this->subject] != $_POST['name']) {
327
                    // Jump them to the new view name
328
                    $_REQUEST[$this->subject] = $_POST['name'];
329
                    // Force a browser reload
330
                    $this->misc->setReloadBrowser(true);
331
                }
332
                // If schema has changed, need to change to the new schema and reload the browser
333
                if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
334
                    // Jump them to the new sequence schema
335
                    $this->misc->setCurrentSchema($_POST['newschema']);
336
                    $this->misc->setReloadBrowser(true);
337
                }
338
                $this->doDefault($this->lang['strviewaltered']);
339
            } else {
340
                $this->doAlter(true, $this->lang['strviewalteredbad']);
341
            }
342
        }
343
    }
344
}
345