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

TriggersController::render()   D

Complexity

Conditions 17
Paths 17

Size

Total Lines 82
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 57
nc 17
nop 0
dl 0
loc 82
rs 4.9359
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
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 TriggersController extends BaseController
15
{
16
    public $controller_name = 'TriggersController';
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['strtables'].' - '.$_REQUEST['table'].' - '.$lang['strtriggers']);
33
        $this->printBody();
34
35
        switch ($action) {
36
            case 'alter':
37
                if (isset($_POST['alter'])) {
38
                    $this->doSaveAlter();
39
                } else {
40
                    $this->doDefault();
41
                }
42
43
                break;
44
            case 'confirm_alter':
45
                $this->doAlter();
46
47
                break;
48
            case 'confirm_enable':
49
                $this->doEnable(true);
50
51
                break;
52
            case 'confirm_disable':
53
                $this->doDisable(true);
54
55
                break;
56
            case 'save_create':
57
                if (isset($_POST['cancel'])) {
58
                    $this->doDefault();
59
                } else {
60
                    $this->doSaveCreate();
61
                }
62
63
                break;
64
            case 'create':
65
                $this->doCreate();
66
67
                break;
68
            case 'drop':
69
                if (isset($_POST['yes'])) {
70
                    $this->doDrop(false);
71
                } else {
72
                    $this->doDefault();
73
                }
74
75
                break;
76
            case 'confirm_drop':
77
                $this->doDrop(true);
78
79
                break;
80
            case 'enable':
81
                if (isset($_POST['yes'])) {
82
                    $this->doEnable(false);
83
                } else {
84
                    $this->doDefault();
85
                }
86
87
                break;
88
            case 'disable':
89
                if (isset($_POST['yes'])) {
90
                    $this->doDisable(false);
91
                } else {
92
                    $this->doDefault();
93
                }
94
95
                break;
96
            default:
97
                $this->doDefault();
98
99
                break;
100
        }
101
102
        return $this->printFooter();
103
    }
104
105
    /**
106
     * List all the triggers on the table.
107
     *
108
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
109
     */
110
    public function doDefault($msg = '')
111
    {
112
        $conf = $this->conf;
113
114
        $lang = $this->lang;
115
        $data = $this->misc->getDatabaseAccessor();
116
117
        $tgPre = function (&$rowdata, $actions) use ($data) {
118
            // toggle enable/disable trigger per trigger
119
            if (!$data->phpBool($rowdata->fields['tgenabled'])) {
120
                unset($actions['disable']);
121
            } else {
122
                unset($actions['enable']);
123
            }
124
125
            return $actions;
126
        };
127
128
        $this->printTrail('table');
129
        $this->printTabs('table', 'triggers');
130
        $this->printMsg($msg);
131
132
        $triggers = $data->getTriggers($_REQUEST['table']);
133
134
        $columns = [
135
            'trigger' => [
136
                'title' => $lang['strname'],
137
                'field' => Decorator::field('tgname'),
138
            ],
139
            'definition' => [
140
                'title' => $lang['strdefinition'],
141
                'field' => Decorator::field('tgdef'),
142
            ],
143
            'function' => [
144
                'title' => $lang['strfunction'],
145
                'field' => Decorator::field('proproto'),
146
                'url'   => "functions.php?action=properties&amp;server={$_REQUEST['server']}&amp;database={$_REQUEST['database']}&amp;",
147
                'vars'  => [
148
                    'schema'       => 'pronamespace',
149
                    'function'     => 'proproto',
150
                    'function_oid' => 'prooid',
151
                ],
152
            ],
153
            'actions' => [
154
                'title' => $lang['stractions'],
155
            ],
156
        ];
157
158
        $actions = [
159
            'alter' => [
160
                'content' => $lang['stralter'],
161
                'attr'    => [
162
                    'href' => [
163
                        'url'     => 'triggers.php',
164
                        'urlvars' => [
165
                            'action'  => 'confirm_alter',
166
                            'table'   => $_REQUEST['table'],
167
                            'trigger' => Decorator::field('tgname'),
168
                        ],
169
                    ],
170
                ],
171
            ],
172
            'drop' => [
173
                'content' => $lang['strdrop'],
174
                'attr'    => [
175
                    'href' => [
176
                        'url'     => 'triggers.php',
177
                        'urlvars' => [
178
                            'action'  => 'confirm_drop',
179
                            'table'   => $_REQUEST['table'],
180
                            'trigger' => Decorator::field('tgname'),
181
                        ],
182
                    ],
183
                ],
184
            ],
185
        ];
186
        if ($data->hasDisableTriggers()) {
187
            $actions['enable'] = [
188
                'content' => $lang['strenable'],
189
                'attr'    => [
190
                    'href' => [
191
                        'url'     => 'triggers.php',
192
                        'urlvars' => [
193
                            'action'  => 'confirm_enable',
194
                            'table'   => $_REQUEST['table'],
195
                            'trigger' => Decorator::field('tgname'),
196
                        ],
197
                    ],
198
                ],
199
            ];
200
            $actions['disable'] = [
201
                'content' => $lang['strdisable'],
202
                'attr'    => [
203
                    'href' => [
204
                        'url'     => 'triggers.php',
205
                        'urlvars' => [
206
                            'action'  => 'confirm_disable',
207
                            'table'   => $_REQUEST['table'],
208
                            'trigger' => Decorator::field('tgname'),
209
                        ],
210
                    ],
211
                ],
212
            ];
213
        }
214
215
        echo $this->printTable($triggers, $columns, $actions, 'triggers-triggers', $lang['strnotriggers'], $tgPre);
216
217
        $this->printNavLinks(['create' => [
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...
218
            'attr' => [
219
                'href' => [
220
                    'url'     => 'triggers.php',
221
                    'urlvars' => [
222
                        'action'   => 'create',
223
                        'server'   => $_REQUEST['server'],
224
                        'database' => $_REQUEST['database'],
225
                        'schema'   => $_REQUEST['schema'],
226
                        'table'    => $_REQUEST['table'],
227
                    ],
228
                ],
229
            ],
230
            'content' => $lang['strcreatetrigger'],
231
        ]], 'triggers-triggers', 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...
232
    }
233
234
    public function doTree()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
235
    {
236
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
237
238
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
239
        $data = $this->misc->getDatabaseAccessor();
240
241
        $triggers = $data->getTriggers($_REQUEST['table']);
242
243
        $reqvars = $this->misc->getRequestVars('table');
0 ignored issues
show
Unused Code introduced by
The assignment to $reqvars is dead and can be removed.
Loading history...
244
245
        $attrs = [
246
            'text' => Decorator::field('tgname'),
247
            'icon' => 'Trigger',
248
        ];
249
250
        return $this->printTree($triggers, $attrs, 'triggers');
251
    }
252
253
    /**
254
     * Function to save after altering a trigger.
255
     */
256
    public function doSaveAlter()
257
    {
258
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
259
260
        $lang = $this->lang;
261
        $data = $this->misc->getDatabaseAccessor();
262
263
        $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']);
264
        if (0 == $status) {
265
            $this->doDefault($lang['strtriggeraltered']);
266
        } else {
267
            $this->doAlter($lang['strtriggeralteredbad']);
268
        }
269
    }
270
271
    /**
272
     * Function to allow altering of a trigger.
273
     *
274
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
275
     */
276
    public function doAlter($msg = '')
277
    {
278
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
279
280
        $lang = $this->lang;
281
        $data = $this->misc->getDatabaseAccessor();
282
283
        $this->printTrail('trigger');
284
        $this->printTitle($lang['stralter'], 'pg.trigger.alter');
285
        $this->printMsg($msg);
286
287
        $triggerdata = $data->getTrigger($_REQUEST['table'], $_REQUEST['trigger']);
288
289
        if ($triggerdata->recordCount() > 0) {
290
            if (!isset($_POST['name'])) {
291
                $_POST['name'] = $triggerdata->fields['tgname'];
292
            }
293
294
            echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n";
295
            echo "<table>\n";
296
            echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
297
            echo '<td class="data1">';
298
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
299
            htmlspecialchars($_POST['name']), "\" />\n";
300
            echo "</table>\n";
301
            echo "<p><input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
302
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
303
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
304
            echo $this->misc->form;
305
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['strok']}\" />\n";
306
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
307
            echo "</form>\n";
308
        } else {
309
            echo "<p>{$lang['strnodata']}</p>\n";
310
        }
311
    }
312
313
    /**
314
     * Show confirmation of drop and perform actual drop.
315
     *
316
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
317
     */
318
    public function doDrop($confirm)
319
    {
320
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
321
322
        $lang = $this->lang;
323
        $data = $this->misc->getDatabaseAccessor();
324
325
        if ($confirm) {
326
            $this->printTrail('trigger');
327
            $this->printTitle($lang['strdrop'], 'pg.trigger.drop');
328
329
            echo '<p>', sprintf(
330
                $lang['strconfdroptrigger'],
331
                $this->misc->printVal($_REQUEST['trigger']),
332
                $this->misc->printVal($_REQUEST['table'])
333
            ), "</p>\n";
334
335
            echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n";
336
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
337
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
338
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
339
            echo $this->misc->form;
340
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
341
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
342
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
343
            echo "</form>\n";
344
        } else {
345
            $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade']));
346
            if (0 == $status) {
347
                $this->doDefault($lang['strtriggerdropped']);
348
            } else {
349
                $this->doDefault($lang['strtriggerdroppedbad']);
350
            }
351
        }
352
    }
353
354
    /**
355
     * Show confirmation of enable trigger and perform enabling the trigger.
356
     *
357
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
358
     */
359
    public function doEnable($confirm)
360
    {
361
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
362
363
        $lang = $this->lang;
364
        $data = $this->misc->getDatabaseAccessor();
365
366
        if ($confirm) {
367
            $this->printTrail('trigger');
368
            $this->printTitle($lang['strenable'], 'pg.table.alter');
369
370
            echo '<p>', sprintf(
371
                $lang['strconfenabletrigger'],
372
                $this->misc->printVal($_REQUEST['trigger']),
373
                $this->misc->printVal($_REQUEST['table'])
374
            ), "</p>\n";
375
376
            echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n";
377
            echo "<input type=\"hidden\" name=\"action\" value=\"enable\" />\n";
378
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
379
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
380
            echo $this->misc->form;
381
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
382
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
383
            echo "</form>\n";
384
        } else {
385
            $status = $data->enableTrigger($_POST['trigger'], $_POST['table']);
386
            if (0 == $status) {
387
                $this->doDefault($lang['strtriggerenabled']);
388
            } else {
389
                $this->doDefault($lang['strtriggerenabledbad']);
390
            }
391
        }
392
    }
393
394
    /**
395
     * Show confirmation of disable trigger and perform disabling the trigger.
396
     *
397
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
398
     */
399
    public function doDisable($confirm)
400
    {
401
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
402
403
        $lang = $this->lang;
404
        $data = $this->misc->getDatabaseAccessor();
405
406
        if ($confirm) {
407
            $this->printTrail('trigger');
408
            $this->printTitle($lang['strdisable'], 'pg.table.alter');
409
410
            echo '<p>', sprintf(
411
                $lang['strconfdisabletrigger'],
412
                $this->misc->printVal($_REQUEST['trigger']),
413
                $this->misc->printVal($_REQUEST['table'])
414
            ), "</p>\n";
415
416
            echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n";
417
            echo "<input type=\"hidden\" name=\"action\" value=\"disable\" />\n";
418
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
419
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
420
            echo $this->misc->form;
421
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
422
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
423
            echo "</form>\n";
424
        } else {
425
            $status = $data->disableTrigger($_POST['trigger'], $_POST['table']);
426
            if (0 == $status) {
427
                $this->doDefault($lang['strtriggerdisabled']);
428
            } else {
429
                $this->doDefault($lang['strtriggerdisabledbad']);
430
            }
431
        }
432
    }
433
434
    /**
435
     * Let them create s.th.
436
     *
437
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
438
     */
439
    public function doCreate($msg = '')
440
    {
441
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
442
443
        $lang = $this->lang;
444
        $data = $this->misc->getDatabaseAccessor();
445
446
        $this->printTrail('table');
447
        $this->printTitle($lang['strcreatetrigger'], 'pg.trigger.create');
448
        $this->printMsg($msg);
449
450
        // Get all the functions that can be used in triggers
451
        $funcs = $data->getTriggerFunctions();
452
        if (0 == $funcs->recordCount()) {
453
            $this->doDefault($lang['strnofunctions']);
454
455
            return;
456
        }
457
458
        // Populate functions
459
        $sel0 = new \PHPPgAdmin\XHtml\XHtmlSelect('formFunction');
460
        while (!$funcs->EOF) {
461
            $sel0->add(new \PHPPgAdmin\XHtml\XHtmlOption($funcs->fields['proname']));
462
            $funcs->moveNext();
463
        }
464
465
        // Populate times
466
        $sel1 = new \PHPPgAdmin\XHtml\XHtmlSelect('formExecTime');
467
        $sel1->set_data($data->triggerExecTimes);
468
469
        // Populate events
470
        $sel2 = new \PHPPgAdmin\XHtml\XHtmlSelect('formEvent');
471
        $sel2->set_data($data->triggerEvents);
472
473
        // Populate occurences
474
        $sel3 = new \PHPPgAdmin\XHtml\XHtmlSelect('formFrequency');
475
        $sel3->set_data($data->triggerFrequency);
476
477
        echo '<form action="'.\SUBFOLDER."/src/views/triggers.php\" method=\"post\">\n";
478
        echo "<table>\n";
479
        echo "<tr>\n";
480
        echo "		<th class=\"data\">{$lang['strname']}</th>\n";
481
        echo "		<th class=\"data\">{$lang['strwhen']}</th>\n";
482
        echo "</tr>\n";
483
        echo "<tr>\n";
484
        echo "		<td class=\"data1\"> <input type=\"text\" name=\"formTriggerName\" size=\"32\" /></td>\n";
485
        echo '		<td class="data1"> ', $sel1->fetch(), "</td>\n";
486
        echo "</tr>\n";
487
        echo "<tr>\n";
488
        echo "    <th class=\"data\">{$lang['strevent']}</th>\n";
489
        echo "    <th class=\"data\">{$lang['strforeach']}</th>\n";
490
        echo "</tr>\n";
491
        echo "<tr>\n";
492
        echo '     <td class="data1"> ', $sel2->fetch(), "</td>\n";
493
        echo '     <td class="data1"> ', $sel3->fetch(), "</td>\n";
494
        echo "</tr>\n";
495
        echo "<tr><th class=\"data\"> {$lang['strfunction']}</th>\n";
496
        echo "<th class=\"data\"> {$lang['strarguments']}</th></tr>\n";
497
        echo '<tr><td class="data1">', $sel0->fetch(), "</td>\n";
498
        echo "<td class=\"data1\">(<input type=\"text\" name=\"formTriggerArgs\" size=\"32\" />)</td>\n";
499
        echo "</tr></table>\n";
500
        echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
501
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
502
        echo "<input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
503
        echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
504
        echo $this->misc->form;
505
        echo "</form>\n";
506
    }
507
508
    /**
509
     * Actually creates the new trigger in the database.
510
     */
511
    public function doSaveCreate()
512
    {
513
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
514
515
        $lang = $this->lang;
516
        $data = $this->misc->getDatabaseAccessor();
517
518
        // Check that they've given a name and a definition
519
520
        if ('' == $_POST['formFunction']) {
521
            $this->doCreate($lang['strtriggerneedsfunc']);
522
        } elseif ('' == $_POST['formTriggerName']) {
523
            $this->doCreate($lang['strtriggerneedsname']);
524
        } elseif ('' == $_POST['formEvent']) {
525
            $this->doCreate();
526
        } else {
527
            $status = $data->createTrigger(
528
                $_POST['formTriggerName'],
529
                $_POST['table'],
530
                $_POST['formFunction'],
531
                $_POST['formExecTime'],
532
                $_POST['formEvent'],
533
                $_POST['formFrequency'],
534
                $_POST['formTriggerArgs']
535
            );
536
            if (0 == $status) {
537
                $this->doDefault($lang['strtriggercreated']);
538
            } else {
539
                $this->doCreate($lang['strtriggercreatedbad']);
540
            }
541
        }
542
    }
543
}
544