Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

TriggersController::doDrop()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 25
nc 3
nop 1
dl 0
loc 30
rs 8.8571
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
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
        $lang = $this->lang;
24
25
        $action = $this->action;
26
        if ('tree' == $action) {
27
            return $this->doTree();
28
        }
29
30
        $this->printHeader($lang['strtables'] . ' - ' . $_REQUEST['table'] . ' - ' . $lang['strtriggers']);
31
        $this->printBody();
32
33
        switch ($action) {
34
            case 'alter':
35
                if (isset($_POST['alter'])) {
36
                    $this->doSaveAlter();
37
                } else {
38
                    $this->doDefault();
39
                }
40
41
                break;
42
            case 'confirm_alter':
43
                $this->doAlter();
44
45
                break;
46
            case 'confirm_enable':
47
                $this->doEnable(true);
48
49
                break;
50
            case 'confirm_disable':
51
                $this->doDisable(true);
52
53
                break;
54
            case 'save_create':
55
                if (isset($_POST['cancel'])) {
56
                    $this->doDefault();
57
                } else {
58
                    $this->doSaveCreate();
59
                }
60
61
                break;
62
            case 'create':
63
                $this->doCreate();
64
65
                break;
66
            case 'drop':
67
                if (isset($_POST['yes'])) {
68
                    $this->doDrop(false);
69
                } else {
70
                    $this->doDefault();
71
                }
72
73
                break;
74
            case 'confirm_drop':
75
                $this->doDrop(true);
76
77
                break;
78
            case 'enable':
79
                if (isset($_POST['yes'])) {
80
                    $this->doEnable(false);
81
                } else {
82
                    $this->doDefault();
83
                }
84
85
                break;
86
            case 'disable':
87
                if (isset($_POST['yes'])) {
88
                    $this->doDisable(false);
89
                } else {
90
                    $this->doDefault();
91
                }
92
93
                break;
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
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     */
108
    public function doDefault($msg = '')
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
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
233
        $data = $this->misc->getDatabaseAccessor();
234
235
        $triggers = $data->getTriggers($_REQUEST['table']);
236
237
        $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...
238
239
        $attrs = [
240
            'text' => Decorator::field('tgname'),
241
            'icon' => 'Trigger',
242
        ];
243
244
        return $this->printTree($triggers, $attrs, 'triggers');
245
    }
246
247
    /**
248
     * Function to save after altering a trigger.
249
     */
250
    public function doSaveAlter()
251
    {
252
        $lang = $this->lang;
253
        $data = $this->misc->getDatabaseAccessor();
254
255
        $status = $data->alterTrigger($_POST['table'], $_POST['trigger'], $_POST['name']);
256
        if (0 == $status) {
257
            $this->doDefault($lang['strtriggeraltered']);
258
        } else {
259
            $this->doAlter($lang['strtriggeralteredbad']);
260
        }
261
    }
262
263
    /**
264
     * Function to allow altering of a trigger.
265
     *
266
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
267
     */
268
    public function doAlter($msg = '')
269
    {
270
        $lang = $this->lang;
271
        $data = $this->misc->getDatabaseAccessor();
272
273
        $this->printTrail('trigger');
274
        $this->printTitle($lang['stralter'], 'pg.trigger.alter');
275
        $this->printMsg($msg);
276
277
        $triggerdata = $data->getTrigger($_REQUEST['table'], $_REQUEST['trigger']);
278
279
        if ($triggerdata->recordCount() > 0) {
280
            if (!isset($_POST['name'])) {
281
                $_POST['name'] = $triggerdata->fields['tgname'];
282
            }
283
284
            echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n";
285
            echo "<table>\n";
286
            echo "<tr><th class=\"data\">{$lang['strname']}</th>\n";
287
            echo '<td class="data1">';
288
            echo "<input name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
289
            htmlspecialchars($_POST['name']), "\" />\n";
290
            echo "</table>\n";
291
            echo "<p><input type=\"hidden\" name=\"action\" value=\"alter\" />\n";
292
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
293
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
294
            echo $this->misc->form;
295
            echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['strok']}\" />\n";
296
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
297
            echo "</form>\n";
298
        } else {
299
            echo "<p>{$lang['strnodata']}</p>\n";
300
        }
301
    }
302
303
    /**
304
     * Show confirmation of drop and perform actual drop.
305
     *
306
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
307
     */
308
    public function doDrop($confirm)
309
    {
310
        $lang = $this->lang;
311
        $data = $this->misc->getDatabaseAccessor();
312
313
        if ($confirm) {
314
            $this->printTrail('trigger');
315
            $this->printTitle($lang['strdrop'], 'pg.trigger.drop');
316
317
            echo '<p>', sprintf(
318
                $lang['strconfdroptrigger'],
319
                $this->misc->printVal($_REQUEST['trigger']),
320
                $this->misc->printVal($_REQUEST['table'])
321
            ), "</p>\n";
322
323
            echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n";
324
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
325
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
326
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
327
            echo $this->misc->form;
328
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
329
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
330
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
331
            echo "</form>\n";
332
        } else {
333
            $status = $data->dropTrigger($_POST['trigger'], $_POST['table'], isset($_POST['cascade']));
334
            if (0 == $status) {
335
                $this->doDefault($lang['strtriggerdropped']);
336
            } else {
337
                $this->doDefault($lang['strtriggerdroppedbad']);
338
            }
339
        }
340
    }
341
342
    /**
343
     * Show confirmation of enable trigger and perform enabling the trigger.
344
     *
345
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
346
     */
347
    public function doEnable($confirm)
348
    {
349
        $lang = $this->lang;
350
        $data = $this->misc->getDatabaseAccessor();
351
352
        if ($confirm) {
353
            $this->printTrail('trigger');
354
            $this->printTitle($lang['strenable'], 'pg.table.alter');
355
356
            echo '<p>', sprintf(
357
                $lang['strconfenabletrigger'],
358
                $this->misc->printVal($_REQUEST['trigger']),
359
                $this->misc->printVal($_REQUEST['table'])
360
            ), "</p>\n";
361
362
            echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n";
363
            echo "<input type=\"hidden\" name=\"action\" value=\"enable\" />\n";
364
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
365
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
366
            echo $this->misc->form;
367
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
368
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
369
            echo "</form>\n";
370
        } else {
371
            $status = $data->enableTrigger($_POST['trigger'], $_POST['table']);
372
            if (0 == $status) {
373
                $this->doDefault($lang['strtriggerenabled']);
374
            } else {
375
                $this->doDefault($lang['strtriggerenabledbad']);
376
            }
377
        }
378
    }
379
380
    /**
381
     * Show confirmation of disable trigger and perform disabling the trigger.
382
     *
383
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
384
     */
385
    public function doDisable($confirm)
386
    {
387
        $lang = $this->lang;
388
        $data = $this->misc->getDatabaseAccessor();
389
390
        if ($confirm) {
391
            $this->printTrail('trigger');
392
            $this->printTitle($lang['strdisable'], 'pg.table.alter');
393
394
            echo '<p>', sprintf(
395
                $lang['strconfdisabletrigger'],
396
                $this->misc->printVal($_REQUEST['trigger']),
397
                $this->misc->printVal($_REQUEST['table'])
398
            ), "</p>\n";
399
400
            echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n";
401
            echo "<input type=\"hidden\" name=\"action\" value=\"disable\" />\n";
402
            echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
403
            echo '<input type="hidden" name="trigger" value="', htmlspecialchars($_REQUEST['trigger']), "\" />\n";
404
            echo $this->misc->form;
405
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
406
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
407
            echo "</form>\n";
408
        } else {
409
            $status = $data->disableTrigger($_POST['trigger'], $_POST['table']);
410
            if (0 == $status) {
411
                $this->doDefault($lang['strtriggerdisabled']);
412
            } else {
413
                $this->doDefault($lang['strtriggerdisabledbad']);
414
            }
415
        }
416
    }
417
418
    /**
419
     * Let them create s.th.
420
     *
421
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
422
     */
423
    public function doCreate($msg = '')
424
    {
425
        $lang = $this->lang;
426
        $data = $this->misc->getDatabaseAccessor();
427
428
        $this->printTrail('table');
429
        $this->printTitle($lang['strcreatetrigger'], 'pg.trigger.create');
430
        $this->printMsg($msg);
431
432
        // Get all the functions that can be used in triggers
433
        $funcs = $data->getTriggerFunctions();
434
        if (0 == $funcs->recordCount()) {
435
            $this->doDefault($lang['strnofunctions']);
436
437
            return;
438
        }
439
440
        // Populate functions
441
        $sel0 = new \PHPPgAdmin\XHtml\XHtmlSelect('formFunction');
442
        while (!$funcs->EOF) {
443
            $sel0->add(new \PHPPgAdmin\XHtml\XHtmlOption($funcs->fields['proname']));
444
            $funcs->moveNext();
445
        }
446
447
        // Populate times
448
        $sel1 = new \PHPPgAdmin\XHtml\XHtmlSelect('formExecTime');
449
        $sel1->set_data($data->triggerExecTimes);
450
451
        // Populate events
452
        $sel2 = new \PHPPgAdmin\XHtml\XHtmlSelect('formEvent');
453
        $sel2->set_data($data->triggerEvents);
454
455
        // Populate occurences
456
        $sel3 = new \PHPPgAdmin\XHtml\XHtmlSelect('formFrequency');
457
        $sel3->set_data($data->triggerFrequency);
458
459
        echo '<form action="' . \SUBFOLDER . "/src/views/triggers.php\" method=\"post\">\n";
460
        echo "<table>\n";
461
        echo "<tr>\n";
462
        echo "		<th class=\"data\">{$lang['strname']}</th>\n";
463
        echo "		<th class=\"data\">{$lang['strwhen']}</th>\n";
464
        echo "</tr>\n";
465
        echo "<tr>\n";
466
        echo "		<td class=\"data1\"> <input type=\"text\" name=\"formTriggerName\" size=\"32\" /></td>\n";
467
        echo '		<td class="data1"> ', $sel1->fetch(), "</td>\n";
468
        echo "</tr>\n";
469
        echo "<tr>\n";
470
        echo "    <th class=\"data\">{$lang['strevent']}</th>\n";
471
        echo "    <th class=\"data\">{$lang['strforeach']}</th>\n";
472
        echo "</tr>\n";
473
        echo "<tr>\n";
474
        echo '     <td class="data1"> ', $sel2->fetch(), "</td>\n";
475
        echo '     <td class="data1"> ', $sel3->fetch(), "</td>\n";
476
        echo "</tr>\n";
477
        echo "<tr><th class=\"data\"> {$lang['strfunction']}</th>\n";
478
        echo "<th class=\"data\"> {$lang['strarguments']}</th></tr>\n";
479
        echo '<tr><td class="data1">', $sel0->fetch(), "</td>\n";
480
        echo "<td class=\"data1\">(<input type=\"text\" name=\"formTriggerArgs\" size=\"32\" />)</td>\n";
481
        echo "</tr></table>\n";
482
        echo "<p><input type=\"submit\" value=\"{$lang['strcreate']}\" />\n";
483
        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
484
        echo "<input type=\"hidden\" name=\"action\" value=\"save_create\" />\n";
485
        echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n";
486
        echo $this->misc->form;
487
        echo "</form>\n";
488
    }
489
490
    /**
491
     * Actually creates the new trigger in the database.
492
     */
493
    public function doSaveCreate()
494
    {
495
        $lang = $this->lang;
496
        $data = $this->misc->getDatabaseAccessor();
497
498
        // Check that they've given a name and a definition
499
500
        if ('' == $_POST['formFunction']) {
501
            $this->doCreate($lang['strtriggerneedsfunc']);
502
        } elseif ('' == $_POST['formTriggerName']) {
503
            $this->doCreate($lang['strtriggerneedsname']);
504
        } elseif ('' == $_POST['formEvent']) {
505
            $this->doCreate();
506
        } else {
507
            $status = $data->createTrigger(
508
                $_POST['formTriggerName'],
509
                $_POST['table'],
510
                $_POST['formFunction'],
511
                $_POST['formExecTime'],
512
                $_POST['formEvent'],
513
                $_POST['formFrequency'],
514
                $_POST['formTriggerArgs']
515
            );
516
            if (0 == $status) {
517
                $this->doDefault($lang['strtriggercreated']);
518
            } else {
519
                $this->doCreate($lang['strtriggercreatedbad']);
520
            }
521
        }
522
    }
523
}
524