ViewpropertiesController::render()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 79
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 47
c 2
b 0
f 0
nc 12
nop 0
dl 0
loc 79
rs 6.9666

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