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