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

AggregatesController::doDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
/**
10
 * Base controller class.
11
 */
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...
12
class AggregatesController extends BaseController
13
{
14
    public $controller_name = 'AggregatesController';
15
16
    /**
17
     * Default method to render the controller according to the action parameter.
18
     */
19
    public function render()
20
    {
21
    }
22
23
    /**
24
     * Show default list of aggregate functions in the database.
25
     *
26
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
27
     */
28
    public function doDefault($msg = '')
1 ignored issue
show
Unused Code introduced by
The parameter $msg is not used and could be removed. ( Ignorable by Annotation )

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

28
    public function doDefault(/** @scrutinizer ignore-unused */ $msg = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
    {
30
    }
31
32
    public function doTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
33
    {
34
    }
35
36
    /**
37
     * Actually creates the new aggregate in the database.
38
     */
39
    public function doSaveCreate()
40
    {
41
        $lang = $this->lang;
42
        $data = $this->misc->getDatabaseAccessor();
43
        // Check inputs
44
        if ('' == trim($_REQUEST['name'])) {
45
            $this->doCreate($lang['straggrneedsname']);
46
47
            return;
48
        }
49
        if ('' == trim($_REQUEST['basetype'])) {
50
            $this->doCreate($lang['straggrneedsbasetype']);
51
52
            return;
53
        }
54
        if ('' == trim($_REQUEST['sfunc'])) {
55
            $this->doCreate($lang['straggrneedssfunc']);
56
57
            return;
58
        }
59
        if ('' == trim($_REQUEST['stype'])) {
60
            $this->doCreate($lang['straggrneedsstype']);
61
62
            return;
63
        }
64
65
        $status = $data->createAggregate(
66
            $_REQUEST['name'],
67
            $_REQUEST['basetype'],
68
            $_REQUEST['sfunc'],
69
            $_REQUEST['stype'],
70
            $_REQUEST['ffunc'],
71
            $_REQUEST['initcond'],
72
            $_REQUEST['sortop'],
73
            $_REQUEST['aggrcomment']
74
        );
75
76
        if (0 == $status) {
77
            $this->misc->setReloadBrowser(true);
78
            $this->doDefault($lang['straggrcreated']);
79
        } else {
80
            $this->doCreate($lang['straggrcreatedbad']);
81
        }
82
    }
83
84
    /**
85
     * Displays a screen for create a new aggregate function.
86
     *
87
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
88
     */
89
    public function doCreate($msg = '')
90
    {
91
        $lang = $this->lang;
92
        $data = $this->misc->getDatabaseAccessor();
93
94
        if (!isset($_REQUEST['name'])) {
95
            $_REQUEST['name'] = '';
96
        }
97
98
        if (!isset($_REQUEST['basetype'])) {
99
            $_REQUEST['basetype'] = '';
100
        }
101
102
        if (!isset($_REQUEST['sfunc'])) {
103
            $_REQUEST['sfunc'] = '';
104
        }
105
106
        if (!isset($_REQUEST['stype'])) {
107
            $_REQUEST['stype'] = '';
108
        }
109
110
        if (!isset($_REQUEST['ffunc'])) {
111
            $_REQUEST['ffunc'] = '';
112
        }
113
114
        if (!isset($_REQUEST['initcond'])) {
115
            $_REQUEST['initcond'] = '';
116
        }
117
118
        if (!isset($_REQUEST['sortop'])) {
119
            $_REQUEST['sortop'] = '';
120
        }
121
122
        if (!isset($_REQUEST['aggrcomment'])) {
123
            $_REQUEST['aggrcomment'] = '';
124
        }
125
126
        $this->printTrail('schema');
127
        $this->printTitle($lang['strcreateaggregate'], 'pg.aggregate.create');
128
        $this->printMsg($msg);
129
130
        echo '<form action="' . \SUBFOLDER . "/src/views/aggregates.php\" method=\"post\">\n";
131
        echo "<table>\n";
132
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
133
        echo "\t\t<td class=\"data\"><input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
134
        htmlspecialchars($_REQUEST['name']), "\" /></td>\n\t</tr>\n";
135
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['straggrbasetype']}</th>\n";
136
        echo "\t\t<td class=\"data\"><input name=\"basetype\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
137
        htmlspecialchars($_REQUEST['basetype']), "\" /></td>\n\t</tr>\n";
138
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['straggrsfunc']}</th>\n";
139
        echo "\t\t<td class=\"data\"><input name=\"sfunc\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
140
        htmlspecialchars($_REQUEST['sfunc']), "\" /></td>\n\t</tr>\n";
141
        echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['straggrstype']}</th>\n";
142
        echo "\t\t<td class=\"data\"><input name=\"stype\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
143
        htmlspecialchars($_REQUEST['stype']), "\" /></td>\n\t</tr>\n";
144
        echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['straggrffunc']}</th>\n";
145
        echo "\t\t<td class=\"data\"><input name=\"ffunc\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
146
        htmlspecialchars($_REQUEST['ffunc']), "\" /></td>\n\t</tr>\n";
147
        echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['straggrinitcond']}</th>\n";
148
        echo "\t\t<td class=\"data\"><input name=\"initcond\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
149
        htmlspecialchars($_REQUEST['initcond']), "\" /></td>\n\t</tr>\n";
150
        echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['straggrsortop']}</th>\n";
151
        echo "\t\t<td class=\"data\"><input name=\"sortop\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
152
        htmlspecialchars($_REQUEST['sortop']), "\" /></td>\n\t</tr>\n";
153
        echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
154
        echo "\t\t<td><textarea name=\"aggrcomment\" rows=\"3\" cols=\"32\">",
155
        htmlspecialchars($_REQUEST['aggrcomment']), "</textarea></td>\n\t</tr>\n";
156
157
        echo "</table>\n";
158
        echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
159
        echo $this->misc->form;
160
        echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
161
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
162
        echo "</form>\n";
163
    }
164
165
    /**
166
     * Function to save after altering an aggregate.
167
     */
168
    public function doSaveAlter()
169
    {
170
        $lang = $this->lang;
171
        $data = $this->misc->getDatabaseAccessor();
172
173
        // Check inputs
174
        if ('' == trim($_REQUEST['aggrname'])) {
175
            $this->doAlter($lang['straggrneedsname']);
176
177
            return;
178
        }
179
180
        $status = $data->alterAggregate(
181
            $_REQUEST['aggrname'],
182
            $_REQUEST['aggrtype'],
183
            $_REQUEST['aggrowner'],
184
            $_REQUEST['aggrschema'],
185
            $_REQUEST['aggrcomment'],
186
            $_REQUEST['newaggrname'],
187
            $_REQUEST['newaggrowner'],
188
            $_REQUEST['newaggrschema'],
189
            $_REQUEST['newaggrcomment']
190
        );
191
        if (0 == $status) {
192
            $this->doDefault($lang['straggraltered']);
193
        } else {
194
            $this->doAlter($lang['straggralteredbad']);
195
196
            return;
197
        }
198
    }
199
200
    /**
201
     * Function to allow editing an aggregate function.
202
     *
203
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
204
     */
205
    public function doAlter($msg = '')
206
    {
207
        $lang = $this->lang;
208
        $data = $this->misc->getDatabaseAccessor();
209
210
        $this->printTrail('aggregate');
211
        $this->printTitle($lang['stralter'], 'pg.aggregate.alter');
212
        $this->printMsg($msg);
213
214
        echo '<form action="' . \SUBFOLDER . "/src/views/aggregates.php\" method=\"post\">\n";
215
        $aggrdata = $data->getAggregate($_REQUEST['aggrname'], $_REQUEST['aggrtype']);
216
        if ($aggrdata->recordCount() > 0) {
217
            // Output table header
218
            echo "<table>\n";
219
            echo "\t<tr>\n\t\t<th class=\"data required\">{$lang['strname']}</th>";
220
            echo "<th class=\"data required\">{$lang['strowner']}</th>";
221
            echo "<th class=\"data required\">{$lang['strschema']}</th>\n\t</tr>\n";
222
223
            // Display aggregate's name, owner and schema
224
            echo "\t<tr>\n\t\t<td><input name=\"newaggrname\" size=\"32\" maxlength=\"32\" value=\"", htmlspecialchars($_REQUEST['aggrname']), '" /></td>';
225
            echo '<td><input name="newaggrowner" size="32" maxlength="32" value="', htmlspecialchars($aggrdata->fields['usename']), '" /></td>';
226
            echo '<td><input name="newaggrschema" size="32" maxlength="32" value="', htmlspecialchars($_REQUEST['schema']), "\" /></td>\n\t</tr>\n";
227
            echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
228
            echo "\t\t<td><textarea name=\"newaggrcomment\" rows=\"3\" cols=\"32\">",
229
            htmlspecialchars($aggrdata->fields['aggrcomment']), "</textarea></td>\n\t</tr>\n";
230
            echo "</table>\n";
231
            echo "<p><input type=\"hidden\" name=\"action\" value=\"save_alter\" />\n";
232
            echo $this->misc->form;
233
            echo '<input type="hidden" name="aggrname" value="', htmlspecialchars($_REQUEST['aggrname']), "\" />\n";
234
            echo '<input type="hidden" name="aggrtype" value="', htmlspecialchars($_REQUEST['aggrtype']), "\" />\n";
235
            echo '<input type="hidden" name="aggrowner" value="', htmlspecialchars($aggrdata->fields['usename']), "\" />\n";
236
            echo '<input type="hidden" name="aggrschema" value="', htmlspecialchars($_REQUEST['schema']), "\" />\n";
237
            echo '<input type="hidden" name="aggrcomment" value="', htmlspecialchars($aggrdata->fields['aggrcomment']), "\" />\n";
238
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n";
239
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
240
        } else {
241
            echo "<p>{$lang['strnodata']}</p>\n";
242
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strback']}\" /></p>\n";
243
        }
244
        echo "</form>\n";
245
    }
246
247
    /**
248
     * Show confirmation of drop and perform actual drop of the aggregate function selected.
249
     *
250
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
251
     */
252
    public function doDrop($confirm)
253
    {
254
        $lang = $this->lang;
255
        $data = $this->misc->getDatabaseAccessor();
256
257
        if ($confirm) {
258
            $this->printTrail('aggregate');
259
            $this->printTitle($lang['strdrop'], 'pg.aggregate.drop');
260
261
            echo '<p>', sprintf($lang['strconfdropaggregate'], htmlspecialchars($_REQUEST['aggrname'])), "</p>\n";
262
263
            echo '<form action="' . \SUBFOLDER . "/src/views/aggregates.php\" method=\"post\">\n";
264
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
265
            echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
266
            echo '<input type="hidden" name="aggrname" value="', htmlspecialchars($_REQUEST['aggrname']), "\" />\n";
267
            echo '<input type="hidden" name="aggrtype" value="', htmlspecialchars($_REQUEST['aggrtype']), "\" />\n";
268
            echo $this->misc->form;
269
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
270
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
271
            echo "</form>\n";
272
        } else {
273
            $status = $data->dropAggregate($_POST['aggrname'], $_POST['aggrtype'], isset($_POST['cascade']));
274
            if (0 == $status) {
275
                $this->misc->setReloadBrowser(true);
276
                $this->doDefault($lang['straggregatedropped']);
277
            } else {
278
                $this->doDefault($lang['straggregatedroppedbad']);
279
            }
280
        }
281
    }
282
283
    /**
284
     * Show the properties of an aggregate.
285
     *
286
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
287
     */
288
    public function doProperties($msg = '')
289
    {
290
        $lang = $this->lang;
291
        $data = $this->misc->getDatabaseAccessor();
292
293
        $this->printTrail('aggregate');
294
        $this->printTitle($lang['strproperties'], 'pg.aggregate');
295
        $this->printMsg($msg);
296
297
        $aggrdata = $data->getAggregate($_REQUEST['aggrname'], $_REQUEST['aggrtype']);
298
299
        if ($aggrdata->recordCount() > 0) {
300
            // Display aggregate's info
301
            echo "<table>\n";
302
            echo "<tr>\n\t<th class=\"data left\">{$lang['strname']}</th>\n";
303
            echo "\t<td class=\"data1\">", htmlspecialchars($_REQUEST['aggrname']), "</td>\n</tr>\n";
304
            echo "<tr>\n\t<th class=\"data left\">{$lang['straggrbasetype']}</th>\n";
305
            echo "\t<td class=\"data1\">", htmlspecialchars($_REQUEST['aggrtype']), "</td>\n</tr>\n";
306
            echo "<tr>\n\t<th class=\"data left\">{$lang['straggrsfunc']}</th>\n";
307
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggtransfn']), "</td>\n</tr>\n";
308
            echo "<tr>\n\t<th class=\"data left\">{$lang['straggrstype']}</th>\n";
309
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggstype']), "</td>\n</tr>\n";
310
            echo "<tr>\n\t<th class=\"data left\">{$lang['straggrffunc']}</th>\n";
311
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggfinalfn']), "</td>\n</tr>\n";
312
            echo "<tr>\n\t<th class=\"data left\">{$lang['straggrinitcond']}</th>\n";
313
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['agginitval']), "</td>\n</tr>\n";
314
            if ($data->hasAggregateSortOp()) {
315
                echo "<tr>\n\t<th class=\"data left\">{$lang['straggrsortop']}</th>\n";
316
                echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['aggsortop']), "</td>\n</tr>\n";
317
            }
318
            echo "<tr>\n\t<th class=\"data left\">{$lang['strowner']}</th>\n";
319
            echo "\t<td class=\"data1\">", htmlspecialchars($aggrdata->fields['usename']), "</td>\n</tr>\n";
320
            echo "<tr>\n\t<th class=\"data left\">{$lang['strcomment']}</th>\n";
321
            echo "\t<td class=\"data1\">", $this->misc->printVal($aggrdata->fields['aggrcomment']), "</td>\n</tr>\n";
322
            echo "</table>\n";
323
        } else {
324
            echo "<p>{$lang['strnodata']}</p>\n";
325
        }
326
327
        $navlinks = [
328
            'showall' => [
329
                'attr'    => [
330
                    'href' => [
331
                        'url'     => 'aggregates.php',
332
                        'urlvars' => [
333
                            'server'   => $_REQUEST['server'],
334
                            'database' => $_REQUEST['database'],
335
                            'schema'   => $_REQUEST['schema'],
336
                        ],
337
                    ],
338
                ],
339
                'content' => $lang['straggrshowall'],
340
            ],
341
        ];
342
343
        if ($data->hasAlterAggregate()) {
344
            $navlinks['alter'] = [
345
                'attr'    => [
346
                    'href' => [
347
                        'url'     => 'aggregates.php',
348
                        'urlvars' => [
349
                            'action'   => 'alter',
350
                            'server'   => $_REQUEST['server'],
351
                            'database' => $_REQUEST['database'],
352
                            'schema'   => $_REQUEST['schema'],
353
                            'aggrname' => $_REQUEST['aggrname'],
354
                            'aggrtype' => $_REQUEST['aggrtype'],
355
                        ],
356
                    ],
357
                ],
358
                'content' => $lang['stralter'],
359
            ];
360
        }
361
362
        $navlinks['drop'] = [
363
            'attr'    => [
364
                'href' => [
365
                    'url'     => 'aggregates.php',
366
                    'urlvars' => [
367
                        'action'   => 'confirm_drop',
368
                        'server'   => $_REQUEST['server'],
369
                        'database' => $_REQUEST['database'],
370
                        'schema'   => $_REQUEST['schema'],
371
                        'aggrname' => $_REQUEST['aggrname'],
372
                        'aggrtype' => $_REQUEST['aggrtype'],
373
                    ],
374
                ],
375
            ],
376
            'content' => $lang['strdrop'],
377
        ];
378
379
        $this->printNavLinks($navlinks, 'aggregates-properties', get_defined_vars());
380
    }
381
}
382