Passed
Push — develop ( c6c0b1...90e9a5 )
by Felipe
10:30 queued 02:43
created

AggregatesController::doCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 9.6818
c 0
b 0
f 0
cc 1
eloc 44
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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