Passed
Pull Request — develop (#92)
by Felipe
04:47
created

ViewpropertiesController   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 653
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 653
rs 6.715
c 0
b 0
f 0
wmc 53

9 Methods

Rating   Name   Duplication   Size   Complexity  
C render() 0 74 15
B doDefinition() 0 43 3
A doExport() 0 55 1
C doProperties() 0 91 7
B doEdit() 0 36 3
A doSaveEdit() 0 12 2
B doTree() 0 40 1
D doAlter() 0 110 19
B doDefault() 0 134 2

How to fix   Complexity   

Complex Class

Complex classes like ViewpropertiesController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ViewpropertiesController, and based on these observations, apply Extract Interface, too.

1
<?php
2
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class ViewpropertiesController extends BaseController
15
{
16
    public $controller_name = 'ViewpropertiesController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
24
25
        $lang = $this->lang;
26
27
        $action = $this->action;
28
        if ('tree' == $action) {
29
            return $this->doTree();
30
        }
31
32
        $this->printHeader($lang['strviews'].' - '.$_REQUEST['view']);
33
        $this->printBody();
34
35
        switch ($action) {
36
            case 'save_edit':
37
                if (isset($_POST['cancel'])) {
38
                    $this->doDefinition();
39
                } else {
40
                    $this->doSaveEdit();
41
                }
42
43
                break;
44
            case 'edit':
45
                $this->doEdit();
46
47
                break;
48
            case 'export':
49
                $this->doExport();
50
51
                break;
52
            case 'definition':
53
                $this->doDefinition();
54
55
                break;
56
            case 'properties':
57
                if (isset($_POST['cancel'])) {
58
                    $this->doDefault();
59
                } else {
60
                    $this->doProperties();
61
                }
62
63
                break;
64
            case 'alter':
65
                if (isset($_POST['alter'])) {
66
                    $this->doAlter(false);
67
                } else {
68
                    $this->doDefault();
69
                }
70
71
                break;
72
            case 'confirm_alter':
73
                doAlter(true);
0 ignored issues
show
Bug introduced by
The function doAlter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
                /** @scrutinizer ignore-call */ 
74
                doAlter(true);
Loading history...
74
75
                break;
76
            case 'drop':
77
                if (isset($_POST['drop'])) {
78
                    $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

78
                    $this->/** @scrutinizer ignore-call */ 
79
                           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...
79
                } else {
80
                    $this->doDefault();
81
                }
82
83
                break;
84
            case 'confirm_drop':
85
                $this->doDrop(true);
86
87
                break;
88
            default:
89
                $this->doDefault();
90
91
                break;
92
        }
93
94
        $this->printFooter();
95
    }
96
97
    /**
98
     * Show view definition and virtual columns.
99
     *
100
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
101
     */
102
    public function doDefault($msg = '')
103
    {
104
        $conf = $this->conf;
105
106
        $lang = $this->lang;
107
        $data = $this->misc->getDatabaseAccessor();
108
109
        $attPre = function (&$rowdata) use ($data) {
110
            $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']);
111
        };
112
113
        $this->printTrail('view');
114
        $this->printTabs('view', 'columns');
115
        $this->printMsg($msg);
116
117
        // Get view
118
        $vdata = $data->getView($_REQUEST['view']);
119
        // Get columns (using same method for getting a view)
120
        $attrs = $data->getTableAttributes($_REQUEST['view']);
121
122
        // Show comment if any
123
        if (null !== $vdata->fields['relcomment']) {
124
            echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n";
125
        }
126
127
        $columns = [
128
            'column' => [
129
                'title' => $lang['strcolumn'],
130
                'field' => Decorator::field('attname'),
131
                'url'   => "colproperties.php?subject=column&amp;{$this->misc->href}&amp;view=".urlencode($_REQUEST['view']).'&amp;',
132
                'vars'  => ['column' => 'attname'],
133
            ],
134
            'type' => [
135
                'title' => $lang['strtype'],
136
                'field' => Decorator::field('+type'),
137
            ],
138
            'default' => [
139
                'title' => $lang['strdefault'],
140
                'field' => Decorator::field('adsrc'),
141
            ],
142
            'actions' => [
143
                'title' => $lang['stractions'],
144
            ],
145
            'comment' => [
146
                'title' => $lang['strcomment'],
147
                'field' => Decorator::field('comment'),
148
            ],
149
        ];
150
151
        $actions = [
152
            'alter' => [
153
                'content' => $lang['stralter'],
154
                'attr'    => [
155
                    'href' => [
156
                        'url'     => 'viewproperties.php',
157
                        'urlvars' => [
158
                            'action' => 'properties',
159
                            'view'   => $_REQUEST['view'],
160
                            'column' => Decorator::field('attname'),
161
                        ],
162
                    ],
163
                ],
164
            ],
165
        ];
166
167
        echo $this->printTable($attrs, $columns, $actions, 'viewproperties-viewproperties', null, $attPre);
168
169
        echo "<br />\n";
170
171
        $navlinks = [
172
            'browse' => [
173
                'attr' => [
174
                    'href' => [
175
                        'url'     => 'display.php',
176
                        'urlvars' => [
177
                            'server'   => $_REQUEST['server'],
178
                            'database' => $_REQUEST['database'],
179
                            'schema'   => $_REQUEST['schema'],
180
                            'view'     => $_REQUEST['view'],
181
                            'subject'  => 'view',
182
                            'return'   => 'view',
183
                        ],
184
                    ],
185
                ],
186
                'content' => $lang['strbrowse'],
187
            ],
188
            'select' => [
189
                'attr' => [
190
                    'href' => [
191
                        'url'     => 'views.php',
192
                        'urlvars' => [
193
                            'action'   => 'confselectrows',
194
                            'server'   => $_REQUEST['server'],
195
                            'database' => $_REQUEST['database'],
196
                            'schema'   => $_REQUEST['schema'],
197
                            'view'     => $_REQUEST['view'],
198
                        ],
199
                    ],
200
                ],
201
                'content' => $lang['strselect'],
202
            ],
203
            'drop' => [
204
                'attr' => [
205
                    'href' => [
206
                        'url'     => 'views.php',
207
                        'urlvars' => [
208
                            'action'   => 'confirm_drop',
209
                            'server'   => $_REQUEST['server'],
210
                            'database' => $_REQUEST['database'],
211
                            'schema'   => $_REQUEST['schema'],
212
                            'view'     => $_REQUEST['view'],
213
                        ],
214
                    ],
215
                ],
216
                'content' => $lang['strdrop'],
217
            ],
218
            'alter' => [
219
                'attr' => [
220
                    'href' => [
221
                        'url'     => 'viewproperties.php',
222
                        'urlvars' => [
223
                            'action'   => 'confirm_alter',
224
                            'server'   => $_REQUEST['server'],
225
                            'database' => $_REQUEST['database'],
226
                            'schema'   => $_REQUEST['schema'],
227
                            'view'     => $_REQUEST['view'],
228
                        ],
229
                    ],
230
                ],
231
                'content' => $lang['stralter'],
232
            ],
233
        ];
234
235
        $this->printNavLinks($navlinks, 'viewproperties-viewproperties', get_defined_vars());
236
    }
237
238
    public function doTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
239
    {
240
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
241
242
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
243
        $data = $this->misc->getDatabaseAccessor();
244
245
        $reqvars = $this->misc->getRequestVars('column');
246
        $columns = $data->getTableAttributes($_REQUEST['view']);
247
248
        $attrs = [
249
            'text'   => Decorator::field('attname'),
250
            'action' => Decorator::actionurl(
251
                'colproperties.php',
252
                $reqvars,
253
                [
254
                    'view'   => $_REQUEST['view'],
255
                    'column' => Decorator::field('attname'),
256
                ]
257
            ),
258
            'icon'       => 'Column',
259
            'iconAction' => Decorator::url(
260
                'display.php',
261
                $reqvars,
262
                [
263
                    'view'   => $_REQUEST['view'],
264
                    'column' => Decorator::field('attname'),
265
                    'query'  => Decorator::replace(
266
                        'SELECT "%column%", count(*) AS "count" FROM %view% GROUP BY "%column%" ORDER BY "%column%"',
267
                        [
268
                            '%column%' => Decorator::field('attname'),
269
                            '%view%'   => $_REQUEST['view'],
270
                        ]
271
                    ),
272
                ]
273
            ),
274
            'toolTip' => Decorator::field('comment'),
275
        ];
276
277
        return $this->printTree($columns, $attrs, 'viewcolumns');
278
    }
279
280
    /**
281
     * Function to save after editing a view.
282
     */
283
    public function doSaveEdit()
284
    {
285
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
286
287
        $lang = $this->lang;
288
        $data = $this->misc->getDatabaseAccessor();
289
290
        $status = $data->setView($_POST['view'], $_POST['formDefinition'], $_POST['formComment']);
291
        if (0 == $status) {
292
            $this->doDefinition($lang['strviewupdated']);
293
        } else {
294
            $this->doEdit($lang['strviewupdatedbad']);
295
        }
296
    }
297
298
    /**
299
     * Function to allow editing of a view.
300
     *
301
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
302
     */
303
    public function doEdit($msg = '')
304
    {
305
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
306
307
        $lang = $this->lang;
308
        $data = $this->misc->getDatabaseAccessor();
309
310
        $this->printTrail('view');
311
        $this->printTitle($lang['stredit'], 'pg.view.alter');
312
        $this->printMsg($msg);
313
314
        $viewdata = $data->getView($_REQUEST['view']);
315
316
        if ($viewdata->recordCount() > 0) {
317
            if (!isset($_POST['formDefinition'])) {
318
                $_POST['formDefinition'] = $viewdata->fields['vwdefinition'];
319
                $_POST['formComment']    = $viewdata->fields['relcomment'];
320
            }
321
322
            echo '<form action="'.\SUBFOLDER."/src/views/viewproperties.php\" method=\"post\">\n";
323
            echo "<table style=\"width: 100%\">\n";
324
            echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strdefinition']}</th>\n";
325
            echo "\t\t<td class=\"data1\"><textarea style=\"width: 100%;\" rows=\"20\" cols=\"50\" name=\"formDefinition\">",
326
            htmlspecialchars($_POST['formDefinition']), "</textarea></td>\n\t</tr>\n";
327
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
328
            echo "\t\t<td class=\"data1\"><textarea rows=\"3\" cols=\"32\" name=\"formComment\">",
329
            htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n";
330
            echo "</table>\n";
331
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_edit\" />\n";
332
            echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n";
333
            echo $this->misc->form;
334
            echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
335
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
336
            echo "</form>\n";
337
        } else {
338
            echo "<p>{$lang['strnodata']}</p>\n";
339
        }
340
    }
341
342
    /**
343
     * Allow the dumping of the data "in" a view
344
     * NOTE:: PostgreSQL doesn't currently support dumping the data in a view
345
     *        so I have disabled the data related parts for now. In the future
346
     *        we should allow it conditionally if it becomes supported.  This is
347
     *        a SMOP since it is based on pg_dump version not backend version.
348
     *
349
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
350
     */
351
    public function doExport($msg = '')
352
    {
353
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
354
355
        $lang = $this->lang;
356
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
357
358
        $this->printTrail('view');
359
        $this->printTabs('view', 'export');
360
        $this->printMsg($msg);
361
362
        echo '<form action="'.\SUBFOLDER."/src/views/dataexport.php\" method=\"post\">\n";
363
        echo "<table>\n";
364
        echo "<tr><th class=\"data\">{$lang['strformat']}</th><th class=\"data\" colspan=\"2\">{$lang['stroptions']}</th></tr>\n";
365
        // Data only
366
        echo "<!--\n";
367
        echo '<tr><th class="data left">';
368
        echo "<input type=\"radio\" id=\"what1\" name=\"what\" value=\"dataonly\" /><label for=\"what1\">{$lang['strdataonly']}</label></th>\n";
369
        echo "<td>{$lang['strformat']}</td>\n";
370
        echo "<td><select name=\"d_format\" >\n";
371
        echo "<option value=\"copy\">COPY</option>\n";
372
        echo "<option value=\"sql\">SQL</option>\n";
373
        echo "<option value=\"csv\">CSV</option>\n";
374
        echo "<option value=\"tab\">{$lang['strtabbed']}</option>\n";
375
        echo "<option value=\"html\">XHTML</option>\n";
376
        echo "<option value=\"xml\">XML</option>\n";
377
        echo "</select>\n</td>\n</tr>\n";
378
        echo "-->\n";
379
380
        // Structure only
381
        echo "<tr><th class=\"data left\"><input type=\"radio\" id=\"what2\" name=\"what\" value=\"structureonly\" checked=\"checked\" /><label for=\"what2\">{$lang['strstructureonly']}</label></th>\n";
382
        echo "<td><label for=\"s_clean\">{$lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"s_clean\" name=\"s_clean\" /></td>\n</tr>\n";
383
        // Structure and data
384
        echo "<!--\n";
385
        echo '<tr><th class="data left" rowspan="2">';
386
        echo "<input type=\"radio\" id=\"what3\" name=\"what\" value=\"structureanddata\" /><label for=\"what3\">{$lang['strstructureanddata']}</label></th>\n";
387
        echo "<td>{$lang['strformat']}</td>\n";
388
        echo "<td><select name=\"sd_format\">\n";
389
        echo "<option value=\"copy\">COPY</option>\n";
390
        echo "<option value=\"sql\">SQL</option>\n";
391
        echo "</select>\n</td>\n</tr>\n";
392
        echo "<td><label for=\"sd_clean\">{$lang['strdrop']}</label></td><td><input type=\"checkbox\" id=\"sd_clean\" name=\"sd_clean\" /></td>\n</tr>\n";
393
        echo "-->\n";
394
        echo "</table>\n";
395
396
        echo "<h3>{$lang['stroptions']}</h3>\n";
397
        echo "<p><input type=\"radio\" id=\"output1\" name=\"output\" value=\"show\" checked=\"checked\" /><label for=\"output1\">{$lang['strshow']}</label>\n";
398
        echo "<br/><input type=\"radio\" id=\"output2\" name=\"output\" value=\"download\" /><label for=\"output2\">{$lang['strdownload']}</label></p>\n";
399
400
        echo "<p><input type=\"hidden\" name=\"action\" value=\"export\" />\n";
401
        echo $this->misc->form;
402
        echo "<input type=\"hidden\" name=\"subject\" value=\"view\" />\n";
403
        echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n";
404
        echo "<input type=\"submit\" value=\"{$lang['strexport']}\" /></p>\n";
405
        echo "</form>\n";
406
    }
407
408
    /**
409
     * Show definition for a view.
410
     *
411
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
412
     */
413
    public function doDefinition($msg = '')
414
    {
415
        $conf = $this->conf;
416
417
        $lang = $this->lang;
418
        $data = $this->misc->getDatabaseAccessor();
419
420
        // Get view
421
        $vdata = $data->getView($_REQUEST['view']);
422
423
        $this->printTrail('view');
424
        $this->printTabs('view', 'definition');
425
        $this->printMsg($msg);
426
427
        if ($vdata->recordCount() > 0) {
428
            // Show comment if any
429
            if (null !== $vdata->fields['relcomment']) {
430
                echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n";
431
            }
432
433
            echo "<table style=\"width: 100%\">\n";
434
            echo "<tr><th class=\"data\">{$lang['strdefinition']}</th></tr>\n";
435
            echo '<tr><td class="data1">', $this->misc->printVal($vdata->fields['vwdefinition']), "</td></tr>\n";
436
            echo "</table>\n";
437
        } else {
438
            echo "<p>{$lang['strnodata']}</p>\n";
439
        }
440
441
        $this->printNavLinks(['alter' => [
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
442
            'attr' => [
443
                'href' => [
444
                    'url'     => 'viewproperties.php',
445
                    'urlvars' => [
446
                        'action'   => 'edit',
447
                        'server'   => $_REQUEST['server'],
448
                        'database' => $_REQUEST['database'],
449
                        'schema'   => $_REQUEST['schema'],
450
                        'view'     => $_REQUEST['view'],
451
                    ],
452
                ],
453
            ],
454
            'content' => $lang['stralter'],
455
        ]], 'viewproperties-definition', get_defined_vars());
1 ignored issue
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
456
    }
457
458
    /**
459
     * Displays a screen where they can alter a column in a view.
460
     *
461
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
462
     */
463
    public function doProperties($msg = '')
464
    {
465
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
466
467
        $lang = $this->lang;
468
        $data = $this->misc->getDatabaseAccessor();
469
470
        if (!isset($_REQUEST['stage'])) {
471
            $_REQUEST['stage'] = 1;
472
        }
473
474
        switch ($_REQUEST['stage']) {
475
            case 1:
476
477
                $this->printTrail('column');
478
                $this->printTitle($lang['stralter'], 'pg.column.alter');
479
                $this->printMsg($msg);
480
481
                echo '<form action="'.\SUBFOLDER."/src/views/viewproperties.php\" method=\"post\">\n";
482
483
                // Output view header
484
                echo "<table>\n";
485
                echo "<tr><th class=\"data required\">{$lang['strname']}</th><th class=\"data required\">{$lang['strtype']}</th>";
486
                echo "<th class=\"data\">{$lang['strdefault']}</th><th class=\"data\">{$lang['strcomment']}</th></tr>";
487
488
                $column = $data->getTableAttributes($_REQUEST['view'], $_REQUEST['column']);
489
490
                if (!isset($_REQUEST['default'])) {
491
                    $_REQUEST['field']   = $column->fields['attname'];
492
                    $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc'];
493
                    $_REQUEST['comment'] = $column->fields['comment'];
494
                }
495
496
                echo '<tr><td><input name="field" size="32" value="',
497
                htmlspecialchars($_REQUEST['field']), '" /></td>';
498
499
                echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), '</td>';
500
                echo '<td><input name="default" size="20" value="',
501
                htmlspecialchars($_REQUEST['default']), '" /></td>';
502
                echo '<td><input name="comment" size="32" value="',
503
                htmlspecialchars($_REQUEST['comment']), '" /></td>';
504
505
                echo "</table>\n";
506
                echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n";
507
                echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n";
508
                echo $this->misc->form;
509
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n";
510
                echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n";
511
                echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n";
512
                echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n";
513
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
514
                echo "</form>\n";
515
516
                break;
517
            case 2:
518
519
                // Check inputs
520
                if ('' == trim($_REQUEST['field'])) {
521
                    $_REQUEST['stage'] = 1;
522
                    $this->doProperties($lang['strcolneedsname']);
523
524
                    return;
525
                }
526
527
                // Alter the view column
528
                list($status, $sql) = $data->alterColumn(
529
                    $_REQUEST['view'],
530
                    $_REQUEST['column'],
531
                    $_REQUEST['field'],
532
                    false,
533
                    false,
534
                    $_REQUEST['default'],
535
                    $_REQUEST['olddefault'],
536
                    '',
537
                    '',
538
                    '',
539
                    '',
540
                    $_REQUEST['comment']
541
                );
542
                if (0 == $status) {
543
                    $this->doDefault($lang['strcolumnaltered']);
544
                } else {
545
                    $_REQUEST['stage'] = 1;
546
                    $this->doProperties($lang['strcolumnalteredbad']);
547
548
                    return;
549
                }
550
551
                break;
552
            default:
553
                echo "<p>{$lang['strinvalidparam']}</p>\n";
554
        }
555
    }
556
557
    public function doAlter($confirm = false, $msg = '')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
558
    {
559
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
560
561
        $lang = $this->lang;
562
        $data = $this->misc->getDatabaseAccessor();
563
564
        if ($confirm) {
565
            $this->printTrail('view');
566
            $this->printTitle($lang['stralter'], 'pg.view.alter');
567
            $this->printMsg($msg);
568
569
            // Fetch view info
570
            $view = $data->getView($_REQUEST['view']);
571
572
            if ($view->recordCount() > 0) {
573
                if (!isset($_POST['name'])) {
574
                    $_POST['name'] = $view->fields['relname'];
575
                }
576
577
                if (!isset($_POST['owner'])) {
578
                    $_POST['owner'] = $view->fields['relowner'];
579
                }
580
581
                if (!isset($_POST['newschema'])) {
582
                    $_POST['newschema'] = $view->fields['nspname'];
583
                }
584
585
                if (!isset($_POST['comment'])) {
586
                    $_POST['comment'] = $view->fields['relcomment'];
587
                }
588
589
                echo '<form action="'.\SUBFOLDER."/src/views/viewproperties.php\" method=\"post\">\n";
590
                echo "<table>\n";
591
                echo "<tr><th class=\"data left required\">{$lang['strname']}</th>\n";
592
                echo '<td class="data1">';
593
                echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
594
                htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
595
596
                if ($data->isSuperUser()) {
597
                    // Fetch all users
598
                    $users = $data->getUsers();
599
600
                    echo "<tr><th class=\"data left required\">{$lang['strowner']}</th>\n";
601
                    echo '<td class="data1"><select name="owner">';
602
                    while (!$users->EOF) {
603
                        $uname = $users->fields['usename'];
604
                        echo '<option value="', htmlspecialchars($uname), '"',
605
                        ($uname == $_POST['owner']) ? ' selected="selected"' : '', '>', htmlspecialchars($uname), "</option>\n";
606
                        $users->moveNext();
607
                    }
608
                    echo "</select></td></tr>\n";
609
                }
610
611
                if ($data->hasAlterTableSchema()) {
612
                    $schemas = $data->getSchemas();
613
                    echo "<tr><th class=\"data left required\">{$lang['strschema']}</th>\n";
614
                    echo '<td class="data1"><select name="newschema">';
615
                    while (!$schemas->EOF) {
616
                        $schema = $schemas->fields['nspname'];
617
                        echo '<option value="', htmlspecialchars($schema), '"',
618
                        ($schema == $_POST['newschema']) ? ' selected="selected"' : '', '>', htmlspecialchars($schema), "</option>\n";
619
                        $schemas->moveNext();
620
                    }
621
                    echo "</select></td></tr>\n";
622
                }
623
624
                echo "<tr><th class=\"data left\">{$lang['strcomment']}</th>\n";
625
                echo '<td class="data1">';
626
                echo '<textarea rows="3" cols="32" name="comment">',
627
                htmlspecialchars($_POST['comment']), "</textarea></td></tr>\n";
628
                echo "</table>\n";
629
                echo "<input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
630
                echo '<input type="hidden" name="view" value="', htmlspecialchars($_REQUEST['view']), "\" />\n";
631
                echo $this->misc->form;
632
                echo "<p><input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
633
                echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
634
                echo "</form>\n";
635
            } else {
636
                echo "<p>{$lang['strnodata']}</p>\n";
637
            }
638
        } else {
639
            // For databases that don't allow owner change
640
            if (!isset($_POST['owner'])) {
641
                $_POST['owner'] = '';
642
            }
643
644
            if (!isset($_POST['newschema'])) {
645
                $_POST['newschema'] = null;
646
            }
647
648
            $status = $data->alterView($_POST['view'], $_POST['name'], $_POST['owner'], $_POST['newschema'], $_POST['comment']);
649
            if (0 == $status) {
650
                // If view has been renamed, need to change to the new name and
651
                // reload the browser frame.
652
                if ($_POST['view'] != $_POST['name']) {
653
                    // Jump them to the new view name
654
                    $_REQUEST['view'] = $_POST['name'];
655
                    // Force a browser reload
656
                    $this->misc->setReloadBrowser(true);
657
                }
658
                // If schema has changed, need to change to the new schema and reload the browser
659
                if (!empty($_POST['newschema']) && ($_POST['newschema'] != $data->_schema)) {
660
                    // Jump them to the new sequence schema
661
                    $this->misc->setCurrentSchema($_POST['newschema']);
662
                    $this->misc->setReloadBrowser(true);
663
                }
664
                $this->doDefault($lang['strviewaltered']);
665
            } else {
666
                $this->doAlter(true, $lang['strviewalteredbad']);
667
            }
668
        }
669
    }
670
}
671