Test Failed
Pull Request — develop (#380)
by Felipe
04:40
created

ViewpropertiesController   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 422
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 280
dl 0
loc 422
rs 9.52
c 2
b 0
f 0
wmc 36

5 Methods

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