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

RulesController::render()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 27
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 43
rs 8.4444
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
    public function doTree()
135
    {
136
        $data = $this->misc->getDatabaseAccessor();
137
138
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
139
140
        $attrs = [
141
            'text' => Decorator::field('rulename'),
142
            'icon' => 'Rule',
143
        ];
144
145
        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

145
        return $this->printTree(/** @scrutinizer ignore-type */ $rules, $attrs, 'rules');
Loading history...
146
    }
147
148
    /**
149
     * Confirm and then actually create a rule.
150
     *
151
     * @param mixed $confirm
152
     * @param mixed $msg
153
     */
154
    public function createRule($confirm, $msg = ''): void
155
    {
156
        $data = $this->misc->getDatabaseAccessor();
157
158
        $this->coalesceArr($_POST, 'name', '');
159
160
        $this->coalesceArr($_POST, 'event', '');
161
162
        $this->coalesceArr($_POST, 'where', '');
163
164
        $this->coalesceArr($_POST, 'type', 'SOMETHING');
165
166
        $this->coalesceArr($_POST, 'raction', '');
167
168
        if ($confirm) {
169
            $this->printTrail($_REQUEST['subject']);
170
            $this->printTitle($this->lang['strcreaterule'], 'pg.rule.create');
171
            $this->printMsg($msg);
172
173
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/rules" method="post">' . \PHP_EOL;
174
            echo '<table>' . \PHP_EOL;
175
            echo \sprintf(
176
                '<tr><th class="data left required">%s</th>',
177
                $this->lang['strname']
178
            ) . \PHP_EOL;
179
            echo \sprintf(
180
                '<td class="data1"><input name="name" size="16" maxlength="%s" value="',
181
                $data->_maxNameLen
182
            ),
183
            \htmlspecialchars($_POST['name']), '" /></td></tr>' . \PHP_EOL;
184
            echo \sprintf(
185
                '<tr><th class="data left required">%s</th>',
186
                $this->lang['strevent']
187
            ) . \PHP_EOL;
188
            echo '<td class="data1"><select name="event">' . \PHP_EOL;
189
190
            foreach ($data->rule_events as $v) {
191
                echo \sprintf(
192
                    '<option value="%s"',
193
                    $v
194
                ), ($v === $_POST['event']) ? ' selected="selected"' : '',
195
                \sprintf(
196
                    '>%s</option>',
197
                    $v
198
                ) . \PHP_EOL;
199
            }
200
            echo '</select></td></tr>' . \PHP_EOL;
201
            echo \sprintf(
202
                '<tr><th class="data left">%s</th>',
203
                $this->lang['strwhere']
204
            ) . \PHP_EOL;
205
            echo '<td class="data1"><input name="where" size="32" value="',
206
            \htmlspecialchars($_POST['where']), '" /></td></tr>' . \PHP_EOL;
207
            echo \sprintf(
208
                '<tr><th class="data left"><label for="instead">%s</label></th>',
209
                $this->lang['strinstead']
210
            ) . \PHP_EOL;
211
            echo '<td class="data1">';
212
            echo '<input type="checkbox" id="instead" name="instead" ', (isset($_POST['instead'])) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
213
            echo '</td></tr>' . \PHP_EOL;
214
            echo \sprintf(
215
                '<tr><th class="data left required">%s</th>',
216
                $this->lang['straction']
217
            ) . \PHP_EOL;
218
            echo '<td class="data1">';
219
            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...
220
            echo '<input type="radio" name="type" value="SOMETHING"', ('SOMETHING' === $_POST['type']) ? ' checked="checked"' : '', ' />' . \PHP_EOL;
221
            echo '(<input name="raction" size="32" value="',
222
            \htmlspecialchars($_POST['raction']), '" />)</td></tr>' . \PHP_EOL;
223
            echo '</table>' . \PHP_EOL;
224
225
            echo '<input type="hidden" name="action" value="save_create_rule" />' . \PHP_EOL;
226
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['subject']), '" />' . \PHP_EOL;
227
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['subject']),
228
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['subject']]), '" />' . \PHP_EOL;
229
            echo $this->view->form;
230
            echo \sprintf(
231
                '<p><input type="submit" name="ok" value="%s" />',
232
                $this->lang['strcreate']
233
            ) . \PHP_EOL;
234
            echo \sprintf(
235
                '<input type="submit" name="cancel" value="%s"  /></p>%s',
236
                $this->lang['strcancel'],
237
                \PHP_EOL
238
            );
239
            echo '</form>' . \PHP_EOL;
240
        } else {
241
            if ('' === \trim($_POST['name'])) {
242
                $this->createRule(true, $this->lang['strruleneedsname']);
243
            } else {
244
                $status = $data->createRule(
245
                    $_POST['name'],
246
                    $_POST['event'],
247
                    $_POST[$_POST['subject']],
248
                    $_POST['where'],
249
                    isset($_POST['instead']),
250
                    $_POST['type'],
251
                    $_POST['raction']
252
                );
253
254
                if (0 === $status) {
255
                    $this->doDefault($this->lang['strrulecreated']);
256
                } else {
257
                    $this->createRule(true, $this->lang['strrulecreatedbad']);
258
                }
259
            }
260
        }
261
    }
262
263
    /**
264
     * Show confirmation of drop and perform actual drop.
265
     *
266
     * @param mixed $confirm
267
     */
268
    public function doDrop($confirm): void
269
    {
270
        $data = $this->misc->getDatabaseAccessor();
271
272
        if ($confirm) {
273
            $this->printTrail($_REQUEST['subject']);
274
            $this->printTitle($this->lang['strdrop'], 'pg.rule.drop');
275
276
            echo '<p>', \sprintf(
277
                $this->lang['strconfdroprule'],
278
                $this->misc->printVal($_REQUEST['rule']),
279
                $this->misc->printVal($_REQUEST[$_REQUEST['reltype']])
280
            ), '</p>' . \PHP_EOL;
281
282
            echo '<form action="' . \containerInstance()->subFolder . '/src/views/rules" method="post">' . \PHP_EOL;
283
            echo '<input type="hidden" name="action" value="drop" />' . \PHP_EOL;
284
            echo '<input type="hidden" name="subject" value="', \htmlspecialchars($_REQUEST['reltype']), '" />' . \PHP_EOL;
285
            echo '<input type="hidden" name="', \htmlspecialchars($_REQUEST['reltype']),
286
            '" value="', \htmlspecialchars($_REQUEST[$_REQUEST['reltype']]), '" />' . \PHP_EOL;
287
            echo '<input type="hidden" name="rule" value="', \htmlspecialchars($_REQUEST['rule']), '" />' . \PHP_EOL;
288
            echo $this->view->form;
289
            echo \sprintf(
290
                '<p><input type="checkbox" id="cascade" name="cascade" /> <label for="cascade">%s</label></p>',
291
                $this->lang['strcascade']
292
            ) . \PHP_EOL;
293
            echo \sprintf(
294
                '<input type="submit" name="yes" value="%s" />',
295
                $this->lang['stryes']
296
            ) . \PHP_EOL;
297
            echo \sprintf(
298
                '<input type="submit" name="no" value="%s" />',
299
                $this->lang['strno']
300
            ) . \PHP_EOL;
301
            echo '</form>' . \PHP_EOL;
302
        } else {
303
            $status = $data->dropRule($_POST['rule'], $_POST[$_POST['subject']], isset($_POST['cascade']));
304
305
            if (0 === $status) {
306
                $this->doDefault($this->lang['strruledropped']);
307
            } else {
308
                $this->doDefault($this->lang['strruledroppedbad']);
309
            }
310
        }
311
    }
312
}
313