Passed
Push — develop ( c6c0b1...90e9a5 )
by Felipe
10:30 queued 02:43
created

RulesController   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
dl 0
loc 253
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C render() 0 42 8
A doDefault() 0 63 1
A doTree() 0 14 1
B doDrop() 0 31 3
C createRule() 0 71 9
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.45
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Base controller class.
13
 *
14
 * @package PHPPgAdmin
15
 */
16
class RulesController extends BaseController
17
{
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        if ('tree' == $this->action) {
24
            return $this->doTree();
25
        }
26
27
        // Different header if we're view rules or table rules
28
        $this->printHeader($_REQUEST[$_REQUEST['subject']] . ' - ' . $this->lang['strrules']);
29
        $this->printBody();
30
31
        switch ($this->action) {
32
            case 'create_rule':
33
                $this->createRule(true);
34
35
                break;
36
            case 'save_create_rule':
37
                if (isset($_POST['cancel'])) {
38
                    $this->doDefault();
39
                } else {
40
                    $this->createRule(false);
41
                }
42
43
                break;
44
            case 'drop':
45
                if (isset($_POST['yes'])) {
46
                    $this->doDrop(false);
47
                } else {
48
                    $this->doDefault();
49
                }
50
51
                break;
52
            case 'confirm_drop':
53
                $this->doDrop(true);
54
55
                break;
56
            default:
57
                $this->doDefault();
58
59
                break;
60
        }
61
62
        return $this->printFooter();
63
    }
64
65
    /**
66
     * List all the rules on the table.
67
     *
68
     * @param mixed $msg
69
     */
70
    public function doDefault($msg = '')
71
    {
72
        $data = $this->misc->getDatabaseAccessor();
73
74
        $this->printTrail($_REQUEST['subject']);
75
        $this->printTabs($_REQUEST['subject'], 'rules');
76
        $this->printMsg($msg);
77
78
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
79
80
        $columns = [
81
            'rule'       => [
82
                'title' => $this->lang['strname'],
83
                'field' => Decorator::field('rulename'),
84
            ],
85
            'definition' => [
86
                'title' => $this->lang['strdefinition'],
87
                'field' => Decorator::field('definition'),
88
            ],
89
            'actions'    => [
90
                'title' => $this->lang['stractions'],
91
            ],
92
        ];
93
94
        $subject = urlencode($_REQUEST['subject']);
95
        $object  = urlencode($_REQUEST[$_REQUEST['subject']]);
96
97
        $actions = [
98
            'drop' => [
99
                'content' => $this->lang['strdrop'],
100
                'attr'    => [
101
                    'href' => [
102
                        'url'     => 'rules',
103
                        'urlvars' => [
104
                            'action'  => 'confirm_drop',
105
                            'reltype' => $subject,
106
                            $subject  => $object,
107
                            'subject' => 'rule',
108
                            'rule'    => Decorator::field('rulename'),
109
                        ],
110
                    ],
111
                ],
112
            ],
113
        ];
114
115
        echo $this->printTable($rules, $columns, $actions, 'rules-rules', $this->lang['strnorules']);
116
117
        $this->printNavLinks(['create' => [
118
            'attr'    => [
119
                'href' => [
120
                    'url'     => 'rules',
121
                    'urlvars' => [
122
                        'action'   => 'create_rule',
123
                        'server'   => $_REQUEST['server'],
124
                        'database' => $_REQUEST['database'],
125
                        'schema'   => $_REQUEST['schema'],
126
                        $subject   => $object,
127
                        'subject'  => $subject,
128
                    ],
129
                ],
130
            ],
131
            'content' => $this->lang['strcreaterule'],
132
        ]], 'rules-rules', get_defined_vars());
133
    }
134
135
    public function doTree()
136
    {
137
        $data = $this->misc->getDatabaseAccessor();
138
139
        $rules = $data->getRules($_REQUEST[$_REQUEST['subject']]);
140
141
        $reqvars = $this->misc->getRequestVars($_REQUEST['subject']);
0 ignored issues
show
Unused Code introduced by
The assignment to $reqvars is dead and can be removed.
Loading history...
142
143
        $attrs = [
144
            'text' => Decorator::field('rulename'),
145
            'icon' => 'Rule',
146
        ];
147
148
        return $this->printTree($rules, $attrs, 'rules');
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 = '')
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="' . \SUBFOLDER . "/src/views/rules\" method=\"post\">\n";
177
            echo "<table>\n";
178
            echo "<tr><th class=\"data left required\">{$this->lang['strname']}</th>\n";
179
            echo "<td class=\"data1\"><input name=\"name\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"",
180
            htmlspecialchars($_POST['name']), "\" /></td></tr>\n";
181
            echo "<tr><th class=\"data left required\">{$this->lang['strevent']}</th>\n";
182
            echo "<td class=\"data1\"><select name=\"event\">\n";
183
            foreach ($data->rule_events as $v) {
184
                echo "<option value=\"{$v}\"", ($v == $_POST['event']) ? ' selected="selected"' : '',
185
                    ">{$v}</option>\n";
186
            }
187
            echo "</select></td></tr>\n";
188
            echo "<tr><th class=\"data left\">{$this->lang['strwhere']}</th>\n";
189
            echo '<td class="data1"><input name="where" size="32" value="',
190
            htmlspecialchars($_POST['where']), "\" /></td></tr>\n";
191
            echo "<tr><th class=\"data left\"><label for=\"instead\">{$this->lang['strinstead']}</label></th>\n";
192
            echo '<td class="data1">';
193
            echo '<input type="checkbox" id="instead" name="instead" ', (isset($_POST['instead'])) ? ' checked="checked"' : '', " />\n";
194
            echo "</td></tr>\n";
195
            echo "<tr><th class=\"data left required\">{$this->lang['straction']}</th>\n";
196
            echo '<td class="data1">';
197
            echo '<input type="radio" id="type1" name="type" value="NOTHING"', ('NOTHING' == $_POST['type']) ? ' checked="checked"' : '', " /> <label for=\"type1\">NOTHING</label><br />\n";
198
            echo '<input type="radio" name="type" value="SOMETHING"', ('SOMETHING' == $_POST['type']) ? ' checked="checked"' : '', " />\n";
199
            echo '(<input name="raction" size="32" value="',
200
            htmlspecialchars($_POST['raction']), "\" />)</td></tr>\n";
201
            echo "</table>\n";
202
203
            echo "<input type=\"hidden\" name=\"action\" value=\"save_create_rule\" />\n";
204
            echo '<input type="hidden" name="subject" value="', htmlspecialchars($_REQUEST['subject']), "\" />\n";
205
            echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['subject']),
206
            '" value="', htmlspecialchars($_REQUEST[$_REQUEST['subject']]), "\" />\n";
207
            echo $this->misc->form;
208
            echo "<p><input type=\"submit\" name=\"ok\" value=\"{$this->lang['strcreate']}\" />\n";
209
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$this->lang['strcancel']}\" /></p>\n";
210
            echo "</form>\n";
211
        } else {
212
            if ('' == trim($_POST['name'])) {
213
                $this->createRule(true, $this->lang['strruleneedsname']);
214
            } else {
215
                $status = $data->createRule(
216
                    $_POST['name'],
217
                    $_POST['event'],
218
                    $_POST[$_POST['subject']],
219
                    $_POST['where'],
220
                    isset($_POST['instead']),
221
                    $_POST['type'],
222
                    $_POST['raction']
223
                );
224
                if (0 == $status) {
225
                    $this->doDefault($this->lang['strrulecreated']);
226
                } else {
227
                    $this->createRule(true, $this->lang['strrulecreatedbad']);
228
                }
229
            }
230
        }
231
    }
232
233
    /**
234
     * Show confirmation of drop and perform actual drop.
235
     *
236
     * @param mixed $confirm
237
     */
238
    public function doDrop($confirm)
239
    {
240
        $data = $this->misc->getDatabaseAccessor();
241
242
        if ($confirm) {
243
            $this->printTrail($_REQUEST['subject']);
244
            $this->printTitle($this->lang['strdrop'], 'pg.rule.drop');
245
246
            echo '<p>', sprintf(
247
                $this->lang['strconfdroprule'],
248
                $this->misc->printVal($_REQUEST['rule']),
249
                $this->misc->printVal($_REQUEST[$_REQUEST['reltype']])
250
            ), "</p>\n";
251
252
            echo '<form action="' . \SUBFOLDER . "/src/views/rules\" method=\"post\">\n";
253
            echo "<input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
254
            echo '<input type="hidden" name="subject" value="', htmlspecialchars($_REQUEST['reltype']), "\" />\n";
255
            echo '<input type="hidden" name="', htmlspecialchars($_REQUEST['reltype']),
256
            '" value="', htmlspecialchars($_REQUEST[$_REQUEST['reltype']]), "\" />\n";
257
            echo '<input type="hidden" name="rule" value="', htmlspecialchars($_REQUEST['rule']), "\" />\n";
258
            echo $this->misc->form;
259
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$this->lang['strcascade']}</label></p>\n";
260
            echo "<input type=\"submit\" name=\"yes\" value=\"{$this->lang['stryes']}\" />\n";
261
            echo "<input type=\"submit\" name=\"no\" value=\"{$this->lang['strno']}\" />\n";
262
            echo "</form>\n";
263
        } else {
264
            $status = $data->dropRule($_POST['rule'], $_POST[$_POST['subject']], isset($_POST['cascade']));
265
            if (0 == $status) {
266
                $this->doDefault($this->lang['strruledropped']);
267
            } else {
268
                $this->doDefault($this->lang['strruledroppedbad']);
269
            }
270
        }
271
    }
272
}
273