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

RulesController::doDrop()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 41
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 35
nc 3
nop 1
dl 0
loc 41
rs 9.36
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * PHPPgAdmin 6.1.3
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 */
14
class RulesController extends BaseController
15
{
16
    /**
17
     * Default method to render the controller according to the action parameter.
18
     */
19
    public function render()
20
    {
21
        if ('tree' === $this->action) {
22
            return $this->doTree();
23
        }
24
25
        // Different header if we're view rules or table rules
26
        $this->printHeader($_REQUEST[$_REQUEST['subject']] . ' - ' . $this->lang['strrules']);
27
        $this->printBody();
28
29
        switch ($this->action) {
30
            case 'create_rule':
31
                $this->createRule(true);
32
33
                break;
34
            case 'save_create_rule':
35
                if (null !== $this->getPostParam('cancel')) {
36
                    $this->doDefault();
37
                } else {
38
                    $this->createRule(false);
39
                }
40
41
                break;
42
            case 'drop':
43
                if (isset($_POST['yes'])) {
44
                    $this->doDrop(false);
45
                } else {
46
                    $this->doDefault();
47
                }
48
49
                break;
50
            case 'confirm_drop':
51
                $this->doDrop(true);
52
53
                break;
54
55
            default:
56
                $this->doDefault();
57
58
                break;
59
        }
60
61
        return $this->printFooter();
62
    }
63
64
    /**
65
     * List all the rules on the table.
66
     *
67
     * @param mixed $msg
68
     */
69
    public function doDefault($msg = ''): void
70
    {
71
        $data = $this->misc->getDatabaseAccessor();
72
73
        $this->printTrail($_REQUEST['subject']);
74
        $this->printTabs($_REQUEST['subject'], 'rules');
75
        $this->printMsg($msg);
76
77
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
78
79
        $columns = [
80
            'rule' => [
81
                'title' => $this->lang['strname'],
82
                'field' => Decorator::field('rulename'),
83
            ],
84
            'definition' => [
85
                'title' => $this->lang['strdefinition'],
86
                'field' => Decorator::field('definition'),
87
            ],
88
            'actions' => [
89
                'title' => $this->lang['stractions'],
90
            ],
91
        ];
92
93
        $subject = \urlencode($_REQUEST['subject']);
94
        $object = \urlencode($_REQUEST[$_REQUEST['subject']]);
95
96
        $actions = [
97
            'drop' => [
98
                'content' => $this->lang['strdrop'],
99
                'attr' => [
100
                    'href' => [
101
                        'url' => 'rules',
102
                        'urlvars' => [
103
                            'action' => 'confirm_drop',
104
                            'reltype' => $subject,
105
                            $subject => $object,
106
                            'subject' => 'rule',
107
                            'rule' => Decorator::field('rulename'),
108
                        ],
109
                    ],
110
                ],
111
            ],
112
        ];
113
114
        echo $this->printTable($rules, $columns, $actions, 'rules-rules', $this->lang['strnorules']);
0 ignored issues
show
Bug introduced by
It seems like $rules 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

114
        echo $this->printTable(/** @scrutinizer ignore-type */ $rules, $columns, $actions, 'rules-rules', $this->lang['strnorules']);
Loading history...
115
116
        $this->printNavLinks(['create' => [
117
            'attr' => [
118
                'href' => [
119
                    'url' => 'rules',
120
                    'urlvars' => [
121
                        'action' => 'create_rule',
122
                        'server' => $_REQUEST['server'],
123
                        'database' => $_REQUEST['database'],
124
                        'schema' => $_REQUEST['schema'],
125
                        $subject => $object,
126
                        'subject' => $subject,
127
                    ],
128
                ],
129
            ],
130
            'content' => $this->lang['strcreaterule'],
131
        ]], 'rules-rules', \get_defined_vars());
132
    }
133
134
    /**
135
     * @return \Slim\Http\Response|string
136
     */
137
    public function doTree()
138
    {
139
        $data = $this->misc->getDatabaseAccessor();
140
141
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
142
143
        $attrs = [
144
            'text' => Decorator::field('rulename'),
145
            'icon' => 'Rule',
146
        ];
147
148
        return $this->printTree($rules, $attrs, 'rules');
0 ignored issues
show
Bug introduced by
It seems like $rules 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

148
        return $this->printTree(/** @scrutinizer ignore-type */ $rules, $attrs, 'rules');
Loading history...
149
    }
150
151
    /**
152
     * Confirm and then actually create a rule.
153
     *
154
     * @param mixed $confirm
155
     * @param mixed $msg
156
     */
157
    public function createRule($confirm, $msg = ''): void
158
    {
159
        $data = $this->misc->getDatabaseAccessor();
160
161
        $this->coalesceArr($_POST, 'name', '');
162
163
        $this->coalesceArr($_POST, 'event', '');
164
165
        $this->coalesceArr($_POST, 'where', '');
166
167
        $this->coalesceArr($_POST, 'type', 'SOMETHING');
168
169
        $this->coalesceArr($_POST, 'raction', '');
170
171
        if ($confirm) {
172
            $this->printTrail($_REQUEST['subject']);
173
            $this->printTitle($this->lang['strcreaterule'], 'pg.rule.create');
174
            $this->printMsg($msg);
175
176
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/rules" method="post">' . \PHP_EOL;
177
            echo '<table>' . \PHP_EOL;
178
            echo \sprintf(
179
                '<tr><th class="data left required">%s</th>',
180
                $this->lang['strname']
181
            ) . \PHP_EOL;
182
            echo \sprintf(
183
                '<td class="data1"><input name="name" size="16" maxlength="%s" value="',
184
                $data->_maxNameLen
185
            ),
186
            \htmlspecialchars($_POST['name']), '" /></td></tr>' . \PHP_EOL;
187
            echo \sprintf(
188
                '<tr><th class="data left required">%s</th>',
189
                $this->lang['strevent']
190
            ) . \PHP_EOL;
191
            echo '<td class="data1"><select name="event">' . \PHP_EOL;
192
193
            foreach ($data->rule_events as $v) {
194
                echo \sprintf(
195
                    '<option value="%s"',
196
                    $v
197
                ), ($v === $_POST['event']) ? ' selected="selected"' : '',
198
                \sprintf(
199
                    '>%s</option>',
200
                    $v
201
                ) . \PHP_EOL;
202
            }
203
            echo '</select></td></tr>' . \PHP_EOL;
204
            echo \sprintf(
205
                '<tr><th class="data left">%s</th>',
206
                $this->lang['strwhere']
207
            ) . \PHP_EOL;
208
            echo '<td class="data1"><input name="where" size="32" value="',
209
            \htmlspecialchars($_POST['where']), '" /></td></tr>' . \PHP_EOL;
210
            echo \sprintf(
211
                '<tr><th class="data left"><label for="instead">%s</label></th>',
212
                $this->lang['strinstead']
213
            ) . \PHP_EOL;
214
            echo '<td class="data1">';
215
            echo '<input type="checkbox" id="instead" name="instead" ', (isset($_POST['instead'])) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
216
            echo '</td></tr>' . \PHP_EOL;
217
            echo \sprintf(
218
                '<tr><th class="data left required">%s</th>',
219
                $this->lang['straction']
220
            ) . \PHP_EOL;
221
            echo '<td class="data1">';
222
            echo '<input type="radio" id="type1" name="type" value="NOTHING"', ('NOTHING' === $_POST['type']) ? ' checked="checked"' : '', ' /> <label for="type1">NOTHING</label><br />' . \PHP_EOL;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 190 characters; contains 197 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
223
            echo '<input type="radio" name="type" value="SOMETHING"', ('SOMETHING' === $_POST['type']) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
224
            echo '(<input name="raction" size="32" value="',
225
            \htmlspecialchars($_POST['raction']), '" />)</td></tr>' . \PHP_EOL;
226
            echo '</table>' . \PHP_EOL;
227
228
            echo '<input type="hidden" name="action" value="save_create_rule" />' . \PHP_EOL;
229
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['subject']), '" />' . \PHP_EOL;
230
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['subject']),
231
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['subject']]), '" />' . \PHP_EOL;
232
            echo $this->view->form;
233
            echo \sprintf(
234
                '<p><input type="submit" name="ok" value="%s" />',
235
                $this->lang['strcreate']
236
            ) . \PHP_EOL;
237
            echo \sprintf(
238
                '<input type="submit" name="cancel" value="%s"  /></p>%s',
239
                $this->lang['strcancel'],
240
                \PHP_EOL
241
            );
242
            echo '</form>' . \PHP_EOL;
243
        } else {
244
            if ('' === \trim($_POST['name'])) {
245
                $this->createRule(true, $this->lang['strruleneedsname']);
246
            } else {
247
                $status = $data->createRule(
248
                    $_POST['name'],
249
                    $_POST['event'],
250
                    $_POST[$_POST['subject']],
251
                    $_POST['where'],
252
                    isset($_POST['instead']),
253
                    $_POST['type'],
254
                    $_POST['raction']
255
                );
256
257
                if (0 === $status) {
258
                    $this->doDefault($this->lang['strrulecreated']);
259
                } else {
260
                    $this->createRule(true, $this->lang['strrulecreatedbad']);
261
                }
262
            }
263
        }
264
    }
265
266
    /**
267
     * Show confirmation of drop and perform actual drop.
268
     *
269
     * @param mixed $confirm
270
     */
271
    public function doDrop($confirm): void
272
    {
273
        $data = $this->misc->getDatabaseAccessor();
274
275
        if ($confirm) {
276
            $this->printTrail($_REQUEST['subject']);
277
            $this->printTitle($this->lang['strdrop'], 'pg.rule.drop');
278
279
            echo '<p>', \sprintf(
280
                $this->lang['strconfdroprule'],
281
                $this->misc->printVal($_REQUEST['rule']),
282
                $this->misc->printVal($_REQUEST[$_REQUEST['reltype']])
283
            ), '</p>' . \PHP_EOL;
284
285
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/rules" method="post">' . \PHP_EOL;
286
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
287
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['reltype']), '" />' . \PHP_EOL;
288
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['reltype']),
289
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['reltype']]), '" />' . \PHP_EOL;
290
            echo '<input type="hidden" name="rule" value="', \htmlspecialchars($_REQUEST['rule']), '" />' . \PHP_EOL;
291
            echo $this->view->form;
292
            echo \sprintf(
293
                '<p><input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label></p>',
294
                $this->lang['strcascade']
295
            ) . \PHP_EOL;
296
            echo \sprintf(
297
                '<input type="submit" name="yes" value="%s" />',
298
                $this->lang['stryes']
299
            ) . \PHP_EOL;
300
            echo \sprintf(
301
                '<input type="submit" name="no" value="%s" />',
302
                $this->lang['strno']
303
            ) . \PHP_EOL;
304
            echo '</form>' . \PHP_EOL;
305
        } else {
306
            $status = $data->dropRule($_POST['rule'], $_POST[$_POST['subject']], isset($_POST['cascade']));
307
308
            if (0 === $status) {
309
                $this->doDefault($this->lang['strruledropped']);
310
            } else {
311
                $this->doDefault($this->lang['strruledroppedbad']);
312
            }
313
        }
314
    }
315
}
316