Test Failed
Pull Request — develop (#380)
by Felipe
03:38
created

TriggersController::render()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 78
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 54
c 1
b 0
f 0
nc 17
nop 0
dl 0
loc 78
rs 5.2166

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
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
use PHPPgAdmin\XHtml\XHtmlOption;
11
use PHPPgAdmin\XHtml\XHtmlSelect;
12
13
/**
14
 * Base controller class.
15
 */
16
class TriggersController extends BaseController
17
{
18
    public $controller_title = 'strtables';
19
20
    /**
21
     * Default method to render the controller according to the action parameter.
22
     */
23
    public function render()
24
    {
25
        if ('tree' === $this->action) {
26
            return $this->doTree();
27
        }
28
29
        $this->printHeader($this->headerTitle('', '', $_REQUEST['table'] . ' - ' . $this->lang['strtriggers']));
30
        $this->printBody();
31
32
        switch ($this->action) {
33
            case 'alter':
34
                if (null !== $this->getPostParam('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 (null !== $this->getPostParam('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
94
            default:
95
                $this->doDefault();
96
97
                break;
98
        }
99
100
        return $this->printFooter();
101
    }
102
103
    /**
104
     * List all the triggers on the table.
105
     *
106
     * @param mixed $msg
107
     */
108
    public function doDefault($msg = ''): void
109
    {
110
        $data = $this->misc->getDatabaseAccessor();
111
112
        $tgPre = static function (&$rowdata, $actions) use ($data) {
113
            // toggle enable/disable trigger per trigger
114
            if (!$data->phpBool($rowdata->fields['tgenabled'])) {
115
                unset($actions['disable']);
116
            } else {
117
                unset($actions['enable']);
118
            }
119
120
            return $actions;
121
        };
122
123
        $this->printTrail('table');
124
        $this->printTabs('table', 'triggers');
125
        $this->printMsg($msg);
126
127
        $triggers = $data->getTriggers($_REQUEST['table']);
128
129
        $columns = [
130
            'trigger' => [
131
                'title' => $this->lang['strname'],
132
                'field' => Decorator::field('tgname'),
133
            ],
134
            'definition' => [
135
                'title' => $this->lang['strdefinition'],
136
                'field' => Decorator::field('tgdef'),
137
            ],
138
            'function' => [
139
                'title' => $this->lang['strfunction'],
140
                'field' => Decorator::field('proproto'),
141
                'url' => \sprintf(
142
                    'functions?action=properties&amp;server=%s&amp;database=%s&amp;',
143
                    $_REQUEST['server'],
144
                    $_REQUEST['database']
145
                ),
146
                'vars' => [
147
                    'schema' => 'pronamespace',
148
                    'function' => 'proproto',
149
                    'function_oid' => 'prooid',
150
                ],
151
            ],
152
            'actions' => [
153
                'title' => $this->lang['stractions'],
154
            ],
155
        ];
156
157
        $actions = [
158
            'alter' => [
159
                'content' => $this->lang['stralter'],
160
                'attr' => [
161
                    'href' => [
162
                        'url' => 'triggers',
163
                        'urlvars' => [
164
                            'action' => 'confirm_alter',
165
                            'table' => $_REQUEST['table'],
166
                            'trigger' => Decorator::field('tgname'),
167
                        ],
168
                    ],
169
                ],
170
            ],
171
            'drop' => [
172
                'content' => $this->lang['strdrop'],
173
                'attr' => [
174
                    'href' => [
175
                        'url' => 'triggers',
176
                        'urlvars' => [
177
                            'action' => 'confirm_drop',
178
                            'table' => $_REQUEST['table'],
179
                            'trigger' => Decorator::field('tgname'),
180
                        ],
181
                    ],
182
                ],
183
            ],
184
        ];
185
186
        if ($data->hasDisableTriggers()) {
187
            $actions['enable'] = [
188
                'content' => $this->lang['strenable'],
189
                'attr' => [
190
                    'href' => [
191
                        'url' => 'triggers',
192
                        'urlvars' => [
193
                            'action' => 'confirm_enable',
194
                            'table' => $_REQUEST['table'],
195
                            'trigger' => Decorator::field('tgname'),
196
                        ],
197
                    ],
198
                ],
199
            ];
200
            $actions['disable'] = [
201
                'content' => $this->lang['strdisable'],
202
                'attr' => [
203
                    'href' => [
204
                        'url' => 'triggers',
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', $this->lang['strnotriggers'], $tgPre);
0 ignored issues
show
Bug introduced by
It seems like $triggers can also be of type integer; however, parameter $tabledata of PHPPgAdmin\Controller\BaseController::printTable() does only seem to accept PHPPgAdmin\ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

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

215
        echo $this->printTable(/** @scrutinizer ignore-type */ $triggers, $columns, $actions, 'triggers-triggers', $this->lang['strnotriggers'], $tgPre);
Loading history...
216
217
        $this->printNavLinks(['create' => [
218
            'attr' => [
219
                'href' => [
220
                    'url' => 'triggers',
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' => $this->lang['strcreatetrigger'],
231
        ]], 'triggers-triggers', \get_defined_vars());
232
    }
233
234
    /**
235
     * @return \Slim\Http\Response|string
236
     */
237
    public function doTree()
238
    {
239
        $data = $this->misc->getDatabaseAccessor();
240
241
        $triggers = $data->getTriggers($_REQUEST['table']);
242
243
        $attrs = [
244
            'text' => Decorator::field('tgname'),
245
            'icon' => 'Trigger',
246
        ];
247
248
        return $this->printTree($triggers, $attrs, 'triggers');
0 ignored issues
show
Bug introduced by
It seems like $triggers can also be of type integer; however, parameter $_treedata of PHPPgAdmin\Controller\BaseController::printTree() does only seem to accept PHPPgAdmin\ADORecordSet|PHPPgAdmin\ArrayRecordSet, maybe add an additional type check? ( Ignorable by Annotation )

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

248
        return $this->printTree(/** @scrutinizer ignore-type */ $triggers, $attrs, 'triggers');
Loading history...
249
    }
250
251
    /**
252
     * Function to save after altering a trigger.
253
     */
254
    public function doSaveAlter(): void
255
    {
256
        $data = $this->misc->getDatabaseAccessor();
257
258
        $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']);
259
260
        if (0 === $status) {
261
            $this->doDefault($this->lang['strtriggeraltered']);
262
        } else {
263
            $this->doAlter($this->lang['strtriggeralteredbad']);
264
        }
265
    }
266
267
    /**
268
     * Function to allow altering of a trigger.
269
     *
270
     * @param mixed $msg
271
     */
272
    public function doAlter($msg = ''): void
273
    {
274
        $data = $this->misc->getDatabaseAccessor();
275
276
        $this->printTrail('trigger');
277
        $this->printTitle($this->lang['stralter'], 'pg.trigger.alter');
278
        $this->printMsg($msg);
279
280
        $triggerdata = $data->getTrigger($_REQUEST['table'], $_REQUEST['trigger']);
281
282
        if (0 < $triggerdata->recordCount()) {
283
            $this->coalesceArr($_POST, 'name', $triggerdata->fields['tgname']);
284
285
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL;
286
            echo '<table>' . \PHP_EOL;
287
            echo \sprintf(
288
                '<tr><th class="data">%s</th>',
289
                $this->lang['strname']
290
            ) . \PHP_EOL;
291
            echo '<td class="data1">';
292
            echo \sprintf(
293
                '<input name="name" size="32" maxlength="%s" value="',
294
                $data->_maxNameLen
295
            ),
296
            \htmlspecialchars($_POST['name']), '" />' . \PHP_EOL;
297
            echo '</table>' . \PHP_EOL;
298
            echo '<p><input type="hidden" name="action" value="alter" />' . \PHP_EOL;
299
            echo \sprintf(
300
                '<input type="hidden" name="table" value="%s"  />%s',
301
                \htmlspecialchars($_REQUEST['table']),
302
                \PHP_EOL
303
            );
304
            echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL;
305
            echo $this->view->form;
306
            echo \sprintf(
307
                '<input type="submit" name="alter" value="%s" />',
308
                $this->lang['strok']
309
            ) . \PHP_EOL;
310
            echo \sprintf(
311
                '<input type="submit" name="cancel" value="%s"  /></p>%s',
312
                $this->lang['strcancel'],
313
                \PHP_EOL
314
            );
315
            echo '</form>' . \PHP_EOL;
316
        } else {
317
            echo \sprintf(
318
                '<p>%s</p>',
319
                $this->lang['strnodata']
320
            ) . \PHP_EOL;
321
        }
322
    }
323
324
    /**
325
     * Show confirmation of drop and perform actual drop.
326
     *
327
     * @param mixed $confirm
328
     */
329
    public function doDrop($confirm): void
330
    {
331
        $data = $this->misc->getDatabaseAccessor();
332
333
        if ($confirm) {
334
            $this->printTrail('trigger');
335
            $this->printTitle($this->lang['strdrop'], 'pg.trigger.drop');
336
337
            echo '<p>', \sprintf(
338
                $this->lang['strconfdroptrigger'],
339
                $this->misc->printVal($_REQUEST['trigger']),
340
                $this->misc->printVal($_REQUEST['table'])
341
            ), '</p>' . \PHP_EOL;
342
343
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL;
344
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
345
            echo \sprintf(
346
                '<input type="hidden" name="table" value="%s"  />%s',
347
                \htmlspecialchars($_REQUEST['table']),
348
                \PHP_EOL
349
            );
350
            echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL;
351
            echo $this->view->form;
352
            echo \sprintf(
353
                '<p><input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label></p>',
354
                $this->lang['strcascade']
355
            ) . \PHP_EOL;
356
            echo \sprintf(
357
                '<input type="submit" name="yes" value="%s" />',
358
                $this->lang['stryes']
359
            ) . \PHP_EOL;
360
            echo \sprintf(
361
                '<input type="submit" name="no" value="%s" />',
362
                $this->lang['strno']
363
            ) . \PHP_EOL;
364
            echo '</form>' . \PHP_EOL;
365
        } else {
366
            $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade']));
367
368
            if (0 === $status) {
369
                $this->doDefault($this->lang['strtriggerdropped']);
370
            } else {
371
                $this->doDefault($this->lang['strtriggerdroppedbad']);
372
            }
373
        }
374
    }
375
376
    /**
377
     * Show confirmation of enable trigger and perform enabling the trigger.
378
     *
379
     * @param mixed $confirm
380
     */
381
    public function doEnable($confirm): void
382
    {
383
        $data = $this->misc->getDatabaseAccessor();
384
385
        if ($confirm) {
386
            $this->printTrail('trigger');
387
            $this->printTitle($this->lang['strenable'], 'pg.table.alter');
388
389
            echo '<p>', \sprintf(
390
                $this->lang['strconfenabletrigger'],
391
                $this->misc->printVal($_REQUEST['trigger']),
392
                $this->misc->printVal($_REQUEST['table'])
393
            ), '</p>' . \PHP_EOL;
394
395
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL;
396
            echo '<input type="hidden" name="action" value="enable" />' . \PHP_EOL;
397
            echo \sprintf(
398
                '<input type="hidden" name="table" value="%s"  />%s',
399
                \htmlspecialchars($_REQUEST['table']),
400
                \PHP_EOL
401
            );
402
            echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL;
403
            echo $this->view->form;
404
            echo \sprintf(
405
                '<input type="submit" name="yes" value="%s" />',
406
                $this->lang['stryes']
407
            ) . \PHP_EOL;
408
            echo \sprintf(
409
                '<input type="submit" name="no" value="%s" />',
410
                $this->lang['strno']
411
            ) . \PHP_EOL;
412
            echo '</form>' . \PHP_EOL;
413
        } else {
414
            $status = $data->enableTrigger($_POST['trigger'], $_POST['table']);
415
416
            if (0 === $status) {
417
                $this->doDefault($this->lang['strtriggerenabled']);
418
            } else {
419
                $this->doDefault($this->lang['strtriggerenabledbad']);
420
            }
421
        }
422
    }
423
424
    /**
425
     * Show confirmation of disable trigger and perform disabling the trigger.
426
     *
427
     * @param mixed $confirm
428
     */
429
    public function doDisable($confirm): void
430
    {
431
        $data = $this->misc->getDatabaseAccessor();
432
433
        if ($confirm) {
434
            $this->printTrail('trigger');
435
            $this->printTitle($this->lang['strdisable'], 'pg.table.alter');
436
437
            echo '<p>', \sprintf(
438
                $this->lang['strconfdisabletrigger'],
439
                $this->misc->printVal($_REQUEST['trigger']),
440
                $this->misc->printVal($_REQUEST['table'])
441
            ), '</p>' . \PHP_EOL;
442
443
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL;
444
            echo '<input type="hidden" name="action" value="disable" />' . \PHP_EOL;
445
            echo \sprintf(
446
                '<input type="hidden" name="table" value="%s"  />%s',
447
                \htmlspecialchars($_REQUEST['table']),
448
                \PHP_EOL
449
            );
450
            echo '<input type="hidden" name="trigger" value="', \htmlspecialchars($_REQUEST['trigger']), '" />' . \PHP_EOL;
451
            echo $this->view->form;
452
            echo \sprintf(
453
                '<input type="submit" name="yes" value="%s" />',
454
                $this->lang['stryes']
455
            ) . \PHP_EOL;
456
            echo \sprintf(
457
                '<input type="submit" name="no" value="%s" />',
458
                $this->lang['strno']
459
            ) . \PHP_EOL;
460
            echo '</form>' . \PHP_EOL;
461
        } else {
462
            $status = $data->disableTrigger($_POST['trigger'], $_POST['table']);
463
464
            if (0 === $status) {
465
                $this->doDefault($this->lang['strtriggerdisabled']);
466
            } else {
467
                $this->doDefault($this->lang['strtriggerdisabledbad']);
468
            }
469
        }
470
    }
471
472
    /**
473
     * Let them create s.th.
474
     *
475
     * @param mixed $msg
476
     */
477
    public function doCreate($msg = ''): void
478
    {
479
        $data = $this->misc->getDatabaseAccessor();
480
481
        $this->printTrail('table');
482
        $this->printTitle($this->lang['strcreatetrigger'], 'pg.trigger.create');
483
        $this->printMsg($msg);
484
485
        // Get all the functions that can be used in triggers
486
        $funcs = $data->getTriggerFunctions();
487
488
        if (0 === $funcs->recordCount()) {
489
            $this->doDefault($this->lang['strnofunctions']);
490
491
            return;
492
        }
493
494
        // Populate functions
495
        $sel0 = new XHtmlSelect('formFunction');
496
497
        while (!$funcs->EOF) {
498
            $sel0->add(new XHtmlOption($funcs->fields['proname']));
499
            $funcs->moveNext();
500
        }
501
502
        // Populate times
503
        $sel1 = new XHtmlSelect('formExecTime');
504
        $sel1->set_data($data->triggerExecTimes);
505
506
        // Populate events
507
        $sel2 = new XHtmlSelect('formEvent');
508
        $sel2->set_data($data->triggerEvents);
509
510
        // Populate occurences
511
        $sel3 = new XHtmlSelect('formFrequency');
512
        $sel3->set_data($data->triggerFrequency);
513
514
        echo '<form action="' . \containerInstance()->subFolder . '/src/views/triggers" method="post">' . \PHP_EOL;
515
        echo '<table>' . \PHP_EOL;
516
        echo '<tr>' . \PHP_EOL;
517
        echo \sprintf(
518
            '		<th class="data">%s</th>',
519
            $this->lang['strname']
520
        ) . \PHP_EOL;
521
        echo \sprintf(
522
            '		<th class="data">%s</th>',
523
            $this->lang['strwhen']
524
        ) . \PHP_EOL;
525
        echo '</tr>' . \PHP_EOL;
526
        echo '<tr>' . \PHP_EOL;
527
        echo '		<td class="data1"> <input type="text" name="formTriggerName" size="32" /></td>' . \PHP_EOL;
528
        echo '		<td class="data1"> ', $sel1->fetch(), '</td>' . \PHP_EOL;
529
        echo '</tr>' . \PHP_EOL;
530
        echo '<tr>' . \PHP_EOL;
531
        echo \sprintf(
532
            '    <th class="data">%s</th>',
533
            $this->lang['strevent']
534
        ) . \PHP_EOL;
535
        echo \sprintf(
536
            '    <th class="data">%s</th>',
537
            $this->lang['strforeach']
538
        ) . \PHP_EOL;
539
        echo '</tr>' . \PHP_EOL;
540
        echo '<tr>' . \PHP_EOL;
541
        echo '     <td class="data1"> ', $sel2->fetch(), '</td>' . \PHP_EOL;
542
        echo '     <td class="data1"> ', $sel3->fetch(), '</td>' . \PHP_EOL;
543
        echo '</tr>' . \PHP_EOL;
544
        echo \sprintf(
545
            '<tr><th class="data"> %s</th>',
546
            $this->lang['strfunction']
547
        ) . \PHP_EOL;
548
        echo \sprintf(
549
            '<th class="data"> %s</th></tr>',
550
            $this->lang['strarguments']
551
        ) . \PHP_EOL;
552
        echo '<tr><td class="data1">', $sel0->fetch(), '</td>' . \PHP_EOL;
553
        echo '<td class="data1">(<input type="text" name="formTriggerArgs" size="32" />)</td>' . \PHP_EOL;
554
        echo '</tr></table>' . \PHP_EOL;
555
        echo \sprintf(
556
            '<p><input type="submit" value="%s" />',
557
            $this->lang['strcreate']
558
        ) . \PHP_EOL;
559
        echo \sprintf(
560
            '<input type="submit" name="cancel" value="%s"  /></p>%s',
561
            $this->lang['strcancel'],
562
            \PHP_EOL
563
        );
564
        echo '<input type="hidden" name="action" value="save_create" />' . \PHP_EOL;
565
        echo \sprintf(
566
            '<input type="hidden" name="table" value="%s"  />%s',
567
            \htmlspecialchars($_REQUEST['table']),
568
            \PHP_EOL
569
        );
570
        echo $this->view->form;
571
        echo '</form>' . \PHP_EOL;
572
    }
573
574
    /**
575
     * Actually creates the new trigger in the database.
576
     */
577
    public function doSaveCreate(): void
578
    {
579
        $data = $this->misc->getDatabaseAccessor();
580
581
        // Check that they've given a name and a definition
582
583
        if ('' === $_POST['formFunction']) {
584
            $this->doCreate($this->lang['strtriggerneedsfunc']);
585
        } elseif ('' === $_POST['formTriggerName']) {
586
            $this->doCreate($this->lang['strtriggerneedsname']);
587
        } elseif ('' === $_POST['formEvent']) {
588
            $this->doCreate();
589
        } else {
590
            $status = $data->createTrigger(
591
                $_POST['formTriggerName'],
592
                $_POST['table'],
593
                $_POST['formFunction'],
594
                $_POST['formExecTime'],
595
                $_POST['formEvent'],
596
                $_POST['formFrequency'],
597
                $_POST['formTriggerArgs']
598
            );
599
600
            if (0 === $status) {
601
                $this->doDefault($this->lang['strtriggercreated']);
602
            } else {
603
                $this->doCreate($this->lang['strtriggercreatedbad']);
604
            }
605
        }
606
    }
607
}
608