Passed
Push — develop ( b546ed...ec145b )
by Felipe
08:20
created

AggregatesController::doTree()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.48
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class AggregatesController extends BaseController
17
{
18
    public $table_place      = 'aggregates-aggregates';
19
    public $controller_title = 'straggregates';
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
30
        ob_start();
31
        switch ($this->action) {
32
            case 'create':
33
                $this->doCreate();
34
35
                break;
36
            case 'save_create':
37
                if (isset($_POST['cancel'])) {
38
                    $this->doDefault();
39
                } else {
40
                    $this->doSaveCreate();
41
                }
42
43
                break;
44
            case 'alter':
45
                $this->doAlter();
46
47
                break;
48
            case 'save_alter':
49
                if (isset($_POST['alter'])) {
50
                    $this->doSaveAlter();
51
                } else {
52
                    $this->doProperties();
53
                }
54
55
                break;
56
            case 'drop':
57
                if (isset($_POST['drop'])) {
58
                    $this->doDrop(false);
59
                } else {
60
                    $this->doDefault();
61
                }
62
63
                break;
64
            case 'confirm_drop':
65
                $this->doDrop(true);
66
67
                break;
68
            default:
69
                $this->doDefault();
70
71
                break;
72
            case 'properties':
73
                $this->doProperties();
74
75
                break;
76
        }
77
78
        $output = ob_get_clean();
79
80
        $this->printHeader($this->headerTitle(), null, true, $header_template);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $header_template seems to be never defined.
Loading history...
81
        $this->printBody();
82
        echo $output;
83
84
        return $this->printFooter();
85
    }
86
87
    /**
88
     * Show default list of aggregate functions in the database.
89
     *
90
     * @param mixed $msg
91
     */
92
    public function doDefault($msg = '')
93
    {
94
        $this->printTrail('schema');
95
        $this->printTabs('schema', 'aggregates');
96
        $this->printMsg($msg);
97
98
        $aggregates = $this->data->getAggregates();
99
        $columns    = [
100
            'aggrname'    => [
101
                'title' => $this->lang['strname'],
102
                'field' => Decorator::field('proname'),
103
                'url'   => "redirect.php?subject=aggregate&amp;action=properties&amp;{$misc->href}&amp;",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $misc seems to be never defined.
Loading history...
104
                'vars'  => ['aggrname' => 'proname', 'aggrtype' => 'proargtypes'],
105
            ],
106
            'aggrtype'    => [
107
                'title' => $this->lang['strtype'],
108
                'field' => Decorator::field('proargtypes'),
109
            ],
110
            'aggrtransfn' => [
111
                'title' => $this->lang['straggrsfunc'],
112
                'field' => Decorator::field('aggtransfn'),
113
            ],
114
            'owner'       => [
115
                'title' => $this->lang['strowner'],
116
                'field' => Decorator::field('usename'),
117
            ],
118
            'actions'     => [
119
                'title' => $this->lang['stractions'],
120
            ],
121
            'comment'     => [
122
                'title' => $this->lang['strcomment'],
123
                'field' => Decorator::field('aggrcomment'),
124
            ],
125
        ];
126
127
        $actions = [
128
            'alter' => [
129
                'content' => $this->lang['stralter'],
130
                'attr'    => [
131
                    'href' => [
132
                        'url'     => 'aggregates.php',
133
                        'urlvars' => [
134
                            'action'   => 'alter',
135
                            'aggrname' => Decorator::field('proname'),
136
                            'aggrtype' => Decorator::field('proargtypes'),
137
                        ],
138
                    ],
139
                ],
140
            ],
141
            'drop'  => [
142
                'content' => $this->lang['strdrop'],
143
                'attr'    => [
144
                    'href' => [
145
                        'url'     => 'aggregates.php',
146
                        'urlvars' => [
147
                            'action'   => 'confirm_drop',
148
                            'aggrname' => Decorator::field('proname'),
149
                            'aggrtype' => Decorator::field('proargtypes'),
150
                        ],
151
                    ],
152
                ],
153
            ],
154
        ];
155
        if (!$this->data->hasAlterAggregate()) {
156
            unset($actions['alter']);
157
        }
158
159
        $echo->printTable($aggregates, $columns, $actions, $this->table_place, $this->lang['strnoaggregates']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $echo seems to be never defined.
Loading history...
160
161
        $navlinks = [
162
            'create' => [
163
                'attr'    => [
164
                    'href' => [
165
                        'url'     => 'aggregates.php',
166
                        'urlvars' => [
167
                            'action'   => 'create',
168
                            'server'   => $_REQUEST['server'],
169
                            'database' => $_REQUEST['database'],
170
                            'schema'   => $_REQUEST['schema'],
171
                        ],
172
                    ],
173
                ],
174
                'content' => $this->lang['strcreateaggregate'],
175
            ],
176
        ];
177
        $this->printNavLinks($navlinks, $this->table_place, get_defined_vars());
178
    }
179
180
    public function doTree()
181
    {
182
        $this->data = $this->misc->getDatabaseAccessor();
183
184
        $aggregates = $this->data->getAggregates();
185
        $proto      = Decorator::concat(Decorator::field('proname'), ' (', Decorator::field('proargtypes'), ')');
186
        $reqvars    = $this->misc->getRequestVars('aggregate');
187
188
        $attrs = [
189
            'text'    => $proto,
190
            'icon'    => 'Aggregate',
191
            'toolTip' => Decorator::field('aggcomment'),
192
            'action'  => Decorator::redirecturl(
193
                'redirect',
194
                $reqvars,
195
                [
196
                    'action'   => 'properties',
197
                    'aggrname' => Decorator::field('proname'),
198
                    'aggrtype' => Decorator::field('proargtypes'),
199
                ]
200
            ),
201
        ];
202
203
        return $this->printTree($aggregates, $attrs, 'aggregates');
204
    }
205
206
    /**
207
     * Actually creates the new aggregate in the database.
208
     */
209
    public function doSaveCreate()
210
    {
211
        $this->data = $this->misc->getDatabaseAccessor();
212
        // Check inputs
213
        if ('' == trim($_REQUEST['name'])) {
214
            return $this->doCreate($this->lang['straggrneedsname']);
215
        }
216
        if ('' == trim($_REQUEST['basetype'])) {
217
            return $this->doCreate($this->lang['straggrneedsbasetype']);
218
        }
219
        if ('' == trim($_REQUEST['sfunc'])) {
220
            return $this->doCreate($this->lang['straggrneedssfunc']);
221
        }
222
        if ('' == trim($_REQUEST['stype'])) {
223
            return $this->doCreate($this->lang['straggrneedsstype']);
224
        }
225
226
        $status = $this->data->createAggregate(
227
            $_REQUEST['name'],
228
            $_REQUEST['basetype'],
229
            $_REQUEST['sfunc'],
230
            $_REQUEST['stype'],
231
            $_REQUEST['ffunc'],
232
            $_REQUEST['initcond'],
233
            $_REQUEST['sortop'],
234
            $_REQUEST['aggrcomment']
235
        );
236
237
        if (0 == $status) {
238
            $this->misc->setReloadBrowser(true);
239
            $this->doDefault($this->lang['straggrcreated']);
240
        } else {
241
            $this->doCreate($this->lang['straggrcreatedbad']);
242
        }
243
    }
244
245
    /**
246
     * Displays a screen for create a new aggregate function.
247
     *
248
     * @param mixed $msg
249
     */
250
    public function doCreate($msg = '')
251
    {
252
        $this->data = $this->misc->getDatabaseAccessor();
253
254
        $this->coalesceArr($_REQUEST, 'name', '');
255
256
        $this->coalesceArr($_REQUEST, 'basetype', '');
257
258
        $this->coalesceArr($_REQUEST, 'sfunc', '');
259
260
        $this->coalesceArr($_REQUEST, 'stype', '');
261
262
        $this->coalesceArr($_REQUEST, 'ffunc', '');
263
264
        $this->coalesceArr($_REQUEST, 'initcond', '');
265
266
        $this->coalesceArr($_REQUEST, 'sortop', '');
267
268
        $this->coalesceArr($_REQUEST, 'aggrcomment', '');
269
270
        $this->printTrail('schema');
271
        $this->printTitle($this->lang['strcreateaggregate'], 'pg.aggregate.create');
272
        $this->printMsg($msg);
273
274
        echo '<form action="'.\SUBFOLDER."/src/views/aggregates\" method=\"post\">\n";
275
        echo "<table>\n";
276
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['strname']}</th>\n";
277
        echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$this->data->_maxNameLen}\" value=\"",
278
        htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n";
279
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['straggrbasetype']}</th>\n";
280
        echo "\t\t<td class=\"data\"><input name=\"basetype\" size=\"32\" maxlength=\"{$this->data->_maxNameLen}\" value=\"",
281
        htmlspecialchars($_REQUEST['basetype']), "\" /></td>\n\t</tr>\n";
282
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['straggrsfunc']}</th>\n";
283
        echo "\t\t<td class=\"data\"><input name=\"sfunc\" size=\"32\" maxlength=\"{$this->data->_maxNameLen}\" value=\"",
284
        htmlspecialchars($_REQUEST['sfunc']), "\" /></td>\n\t</tr>\n";
285
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$this->lang['straggrstype']}</th>\n";
286
        echo "\t\t<td class=\"data\"><input name=\"stype\" size=\"32\" maxlength=\"{$this->data->_maxNameLen}\" value=\"",
287
        htmlspecialchars($_REQUEST['stype']), "\" /></td>\n\t</tr>\n";
288
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['straggrffunc']}</th>\n";
289
        echo "\t\t<td class=\"data\"><input name=\"ffunc\" size=\"32\" maxlength=\"{$this->data->_maxNameLen}\" value=\"",
290
        htmlspecialchars($_REQUEST['ffunc']), "\" /></td>\n\t</tr>\n";
291
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['straggrinitcond']}</th>\n";
292
        echo "\t\t<td class=\"data\"><input name=\"initcond\" size=\"32\" maxlength=\"{$this->data->_maxNameLen}\" value=\"",
293
        htmlspecialchars($_REQUEST['initcond']), "\" /></td>\n\t</tr>\n";
294
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['straggrsortop']}</th>\n";
295
        echo "\t\t<td class=\"data\"><input name=\"sortop\" size=\"32\" maxlength=\"{$this->data->_maxNameLen}\" value=\"",
296
        htmlspecialchars($_REQUEST['sortop']), "\" /></td>\n\t</tr>\n";
297
        echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
298
        echo "\t\t<td><textarea name=\"aggrcomment\" rows=\"3\" cols=\"32\">",
299
        htmlspecialchars($_REQUEST['aggrcomment']), "</textarea></td>\n\t</tr>\n";
300
301
        echo "</table>\n";
302
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
303
        echo $this->misc->form;
304
        echo "<input type=\"submit\" value=\"{$this->lang['strcreate']}\" />\n";
305
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
306
        echo "</form>\n";
307
    }
308
309
    /**
310
     * Function to save after altering an aggregate.
311
     */
312
    public function doSaveAlter()
313
    {
314
        $this->data = $this->misc->getDatabaseAccessor();
315
316
        // Check inputs
317
        if ('' == trim($_REQUEST['aggrname'])) {
318
            $this->doAlter($this->lang['straggrneedsname']);
319
320
            return;
321
        }
322
323
        $status = $this->data->alterAggregate(
324
            $_REQUEST['aggrname'],
325
            $_REQUEST['aggrtype'],
326
            $_REQUEST['aggrowner'],
327
            $_REQUEST['aggrschema'],
328
            $_REQUEST['aggrcomment'],
329
            $_REQUEST['newaggrname'],
330
            $_REQUEST['newaggrowner'],
331
            $_REQUEST['newaggrschema'],
332
            $_REQUEST['newaggrcomment']
333
        );
334
        if (0 == $status) {
335
            $this->doDefault($this->lang['straggraltered']);
336
        } else {
337
            $this->doAlter($this->lang['straggralteredbad']);
338
339
            return;
340
        }
341
    }
342
343
    /**
344
     * Function to allow editing an aggregate function.
345
     *
346
     * @param mixed $msg
347
     */
348
    public function doAlter($msg = '')
349
    {
350
        $this->data = $this->misc->getDatabaseAccessor();
351
352
        $this->printTrail('aggregate');
353
        $this->printTitle($this->lang['stralter'], 'pg.aggregate.alter');
354
        $this->printMsg($msg);
355
356
        echo '<form action="'.\SUBFOLDER."/src/views/aggregates\" method=\"post\">\n";
357
        $aggrdata = $this->data->getAggregate($_REQUEST['aggrname'], $_REQUEST['aggrtype']);
358
        if ($aggrdata->recordCount() > 0) {
359
            // Output table header
360
            echo "<table>\n";
361
            echo "\t<tr>\n\t\t<th class=\"data required\">{$this->lang['strname']}</th>";
362
            echo "<th class=\"data required\">{$this->lang['strowner']}</th>";
363
            echo "<th class=\"data required\">{$this->lang['strschema']}</th>\n\t</tr>\n";
364
365
            // Display aggregate's name, owner and schema
366
            echo "\t<tr>\n\t\t<td><input name=\"newaggrname\" size=\"32\" maxlength=\"32\" value=\"", htmlspecialchars($_REQUEST['aggrname']), '" /></td>';
367
            echo '<td><input name="newaggrowner" size="32" maxlength="32" value="', htmlspecialchars($aggrdata->fields['usename']), '" /></td>';
368
            echo '<td><input name="newaggrschema" size="32" maxlength="32" value="', htmlspecialchars($_REQUEST['schema']), "\" /></td>\n\t</tr>\n";
369
            echo "\t<tr>\n\t\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
370
            echo "\t\t<td><textarea name=\"newaggrcomment\" rows=\"3\" cols=\"32\">",
371
            htmlspecialchars($aggrdata->fields['aggrcomment']), "</textarea></td>\n\t</tr>\n";
372
            echo "</table>\n";
373
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_alter\" />\n";
374
            echo $this->misc->form;
375
            echo '<input type="hidden" name="aggrname" value="', htmlspecialchars($_REQUEST['aggrname']), "\" />\n";
376
            echo '<input type="hidden" name="aggrtype" value="', htmlspecialchars($_REQUEST['aggrtype']), "\" />\n";
377
            echo '<input type="hidden" name="aggrowner" value="', htmlspecialchars($aggrdata->fields['usename']), "\" />\n";
378
            echo '<input type="hidden" name="aggrschema" value="', htmlspecialchars($_REQUEST['schema']), "\" />\n";
379
            echo '<input type="hidden" name="aggrcomment" value="', htmlspecialchars($aggrdata->fields['aggrcomment']), "\" />\n";
380
            echo "<input type=\"submit\" name=\"alter\" value=\"{$this->lang['stralter']}\" />\n";
381
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
382
        } else {
383
            echo "<p>{$this->lang['strnodata']}</p>\n";
384
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strback']}\" /></p>\n";
385
        }
386
        echo "</form>\n";
387
    }
388
389
    /**
390
     * Show confirmation of drop and perform actual drop of the aggregate function selected.
391
     *
392
     * @param mixed $confirm
393
     */
394
    public function doDrop($confirm)
395
    {
396
        $this->data = $this->misc->getDatabaseAccessor();
397
398
        if ($confirm) {
399
            $this->printTrail('aggregate');
400
            $this->printTitle($this->lang['strdrop'], 'pg.aggregate.drop');
401
402
            echo '<p>', sprintf($this->lang['strconfdropaggregate'], htmlspecialchars($_REQUEST['aggrname'])), "</p>\n";
403
404
            echo '<form action="'.\SUBFOLDER."/src/views/aggregates\" method=\"post\">\n";
405
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>\n";
406
            echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
407
            echo '<input type="hidden" name="aggrname" value="', htmlspecialchars($_REQUEST['aggrname']), "\" />\n";
408
            echo '<input type="hidden" name="aggrtype" value="', htmlspecialchars($_REQUEST['aggrtype']), "\" />\n";
409
            echo $this->misc->form;
410
            echo "<input type=\"submit\" name=\"drop\" value=\"{$this->lang['strdrop']}\" />\n";
411
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
412
            echo "</form>\n";
413
        } else {
414
            $status = $this->data->dropAggregate($_POST['aggrname'], $_POST['aggrtype'], isset($_POST['cascade']));
415
            if (0 == $status) {
416
                $this->misc->setReloadBrowser(true);
417
                $this->doDefault($this->lang['straggregatedropped']);
418
            } else {
419
                $this->doDefault($this->lang['straggregatedroppedbad']);
420
            }
421
        }
422
    }
423
424
    /**
425
     * Show the properties of an aggregate.
426
     *
427
     * @param mixed $msg
428
     */
429
    public function doProperties($msg = '')
430
    {
431
        $this->data = $this->misc->getDatabaseAccessor();
432
433
        $this->printTrail('aggregate');
434
        $this->printTitle($this->lang['strproperties'], 'pg.aggregate');
435
        $this->printMsg($msg);
436
437
        $aggrdata = $this->data->getAggregate($_REQUEST['aggrname'], $_REQUEST['aggrtype']);
438
439
        if ($aggrdata->recordCount() > 0) {
440
            // Display aggregate's info
441
            echo "<table>\n";
442
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['strname']}</th>\n";
443
            echo "\t<td class=\"data1\">", htmlspecialchars($_REQUEST['aggrname']), "</td>\n</tr>\n";
444
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['straggrbasetype']}</th>\n";
445
            echo "\t<td class=\"data1\">", htmlspecialchars($_REQUEST['aggrtype']), "</td>\n</tr>\n";
446
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['straggrsfunc']}</th>\n";
447
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggtransfn']), "</td>\n</tr>\n";
448
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['straggrstype']}</th>\n";
449
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggstype']), "</td>\n</tr>\n";
450
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['straggrffunc']}</th>\n";
451
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggfinalfn']), "</td>\n</tr>\n";
452
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['straggrinitcond']}</th>\n";
453
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['agginitval']), "</td>\n</tr>\n";
454
            if ($this->data->hasAggregateSortOp()) {
455
                echo "<tr>\n\t<th class=\"data left\">{$this->lang['straggrsortop']}</th>\n";
456
                echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggsortop']), "</td>\n</tr>\n";
457
            }
458
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['strowner']}</th>\n";
459
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['usename']), "</td>\n</tr>\n";
460
            echo "<tr>\n\t<th class=\"data left\">{$this->lang['strcomment']}</th>\n";
461
            echo "\t<td class=\"data1\">", $this->misc->printVal($aggrdata->fields['aggrcomment']), "</td>\n</tr>\n";
462
            echo "</table>\n";
463
        } else {
464
            echo "<p>{$this->lang['strnodata']}</p>\n";
465
        }
466
467
        $navlinks = [
468
            'showall' => [
469
                'attr'    => [
470
                    'href' => [
471
                        'url'     => 'aggregates',
472
                        'urlvars' => [
473
                            'server'   => $_REQUEST['server'],
474
                            'database' => $_REQUEST['database'],
475
                            'schema'   => $_REQUEST['schema'],
476
                        ],
477
                    ],
478
                ],
479
                'content' => $this->lang['straggrshowall'],
480
            ],
481
        ];
482
483
        if ($this->data->hasAlterAggregate()) {
484
            $navlinks['alter'] = [
485
                'attr'    => [
486
                    'href' => [
487
                        'url'     => 'aggregates',
488
                        'urlvars' => [
489
                            'action'   => 'alter',
490
                            'server'   => $_REQUEST['server'],
491
                            'database' => $_REQUEST['database'],
492
                            'schema'   => $_REQUEST['schema'],
493
                            'aggrname' => $_REQUEST['aggrname'],
494
                            'aggrtype' => $_REQUEST['aggrtype'],
495
                        ],
496
                    ],
497
                ],
498
                'content' => $this->lang['stralter'],
499
            ];
500
        }
501
502
        $navlinks['drop'] = [
503
            'attr'    => [
504
                'href' => [
505
                    'url'     => 'aggregates',
506
                    'urlvars' => [
507
                        'action'   => 'confirm_drop',
508
                        'server'   => $_REQUEST['server'],
509
                        'database' => $_REQUEST['database'],
510
                        'schema'   => $_REQUEST['schema'],
511
                        'aggrname' => $_REQUEST['aggrname'],
512
                        'aggrtype' => $_REQUEST['aggrtype'],
513
                    ],
514
                ],
515
            ],
516
            'content' => $this->lang['strdrop'],
517
        ];
518
519
        $this->printNavLinks($navlinks, 'aggregates-properties', get_defined_vars());
520
    }
521
}
522