Passed
Pull Request — develop (#92)
by Felipe
04:47
created

OperatorsController::doDrop()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 3
nop 1
dl 0
loc 28
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 OperatorsController extends BaseController
15
{
16
    public $controller_name = 'OperatorsController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
24
25
        $lang = $this->lang;
26
27
        $action = $this->action;
28
        if ('tree' == $action) {
29
            return $this->doTree();
30
        }
31
32
        $this->printHeader($lang['stroperators']);
33
        $this->printBody();
34
35
        switch ($action) {
36
            case 'save_create':
37
                if (isset($_POST['cancel'])) {
38
                    $this->doDefault();
39
                } else {
40
                    $this->doSaveCreate();
0 ignored issues
show
Bug introduced by
The method doSaveCreate() does not exist on PHPPgAdmin\Controller\OperatorsController. ( Ignorable by Annotation )

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

40
                    $this->/** @scrutinizer ignore-call */ 
41
                           doSaveCreate();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
                }
42
43
                break;
44
            case 'create':
45
                doCreate();
0 ignored issues
show
Bug introduced by
The function doCreate was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

45
                /** @scrutinizer ignore-call */ 
46
                doCreate();
Loading history...
46
47
                break;
48
            case 'drop':
49
                if (isset($_POST['cancel'])) {
50
                    $this->doDefault();
51
                } else {
52
                    $this->doDrop(false);
53
                }
54
55
                break;
56
            case 'confirm_drop':
57
                $this->doDrop(true);
58
59
                break;
60
            case 'properties':
61
                $this->doProperties();
62
63
                break;
64
            default:
65
                $this->doDefault();
66
67
                break;
68
        }
69
70
        $this->printFooter();
71
    }
72
73
    /**
74
     * Generate XML for the browser tree.
75
     */
76
    public function doTree()
77
    {
78
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
79
80
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
81
        $data = $this->misc->getDatabaseAccessor();
82
83
        $operators = $data->getOperators();
84
85
        // Operator prototype: "type operator type"
86
        $proto = Decorator::concat(Decorator::field('oprleftname'), ' ', Decorator::field('oprname'), ' ', Decorator::field('oprrightname'));
87
88
        $reqvars = $this->misc->getRequestVars('operator');
89
90
        $attrs = [
91
            'text'    => $proto,
92
            'icon'    => 'Operator',
93
            'toolTip' => Decorator::field('oprcomment'),
94
            'action'  => Decorator::actionurl(
95
                'operators.php',
96
                $reqvars,
97
                [
98
                    'action'       => 'properties',
99
                    'operator'     => $proto,
100
                    'operator_oid' => Decorator::field('oid'),
101
                ]
102
            ),
103
        ];
104
105
        return $this->printTree($operators, $attrs, 'operators');
106
    }
107
108
    /**
109
     * Show default list of operators in the database.
110
     *
111
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
112
     */
113
    public function doDefault($msg = '')
114
    {
115
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
116
117
        $lang = $this->lang;
118
        $data = $this->misc->getDatabaseAccessor();
119
120
        $this->printTrail('schema');
121
        $this->printTabs('schema', 'operators');
122
        $this->printMsg($msg);
123
124
        $operators = $data->getOperators();
125
126
        $columns = [
127
            'operator' => [
128
                'title' => $lang['stroperator'],
129
                'field' => Decorator::field('oprname'),
130
                'url'   => "operators.php?action=properties&amp;{$this->misc->href}&amp;",
131
                'vars'  => ['operator' => 'oprname', 'operator_oid' => 'oid'],
132
            ],
133
            'leftarg' => [
134
                'title' => $lang['strleftarg'],
135
                'field' => Decorator::field('oprleftname'),
136
            ],
137
            'rightarg' => [
138
                'title' => $lang['strrightarg'],
139
                'field' => Decorator::field('oprrightname'),
140
            ],
141
            'returns' => [
142
                'title' => $lang['strreturns'],
143
                'field' => Decorator::field('resultname'),
144
            ],
145
            'actions' => [
146
                'title' => $lang['stractions'],
147
            ],
148
            'comment' => [
149
                'title' => $lang['strcomment'],
150
                'field' => Decorator::field('oprcomment'),
151
            ],
152
        ];
153
154
        $actions = [
155
            'drop' => [
156
                // 'title' => $lang['strdrop'],
157
                // 'url'   => "operators.php?action=confirm_drop&amp;{$this->misc->href}&amp;",
158
                // 'vars'  => array('operator' => 'oprname', 'operator_oid' => 'oid'),
159
                'content' => $lang['strdrop'],
160
                'attr'    => [
161
                    'href' => [
162
                        'url'     => 'operators.php',
163
                        'urlvars' => [
164
                            'action'       => 'confirm_drop',
165
                            'operator'     => Decorator::field('oprname'),
166
                            'operator_oid' => Decorator::field('oid'),
167
                        ],
168
                    ],
169
                ],
170
            ],
171
        ];
172
173
        echo $this->printTable($operators, $columns, $actions, 'operators-operators', $lang['strnooperators']);
174
175
        //        TODO operators.php action=create $lang['strcreateoperator']
176
    }
177
178
    /**
179
     * Show read only properties for an operator.
180
     *
181
     * @param mixed $msg
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
182
     */
183
    public function doProperties($msg = '')
184
    {
185
        $conf = $this->conf;
186
187
        $lang = $this->lang;
188
        $data = $this->misc->getDatabaseAccessor();
189
190
        $this->printTrail('operator');
191
        $this->printTitle($lang['strproperties'], 'pg.operator');
192
        $this->printMsg($msg);
193
194
        $oprdata                       = $data->getOperator($_REQUEST['operator_oid']);
195
        $oprdata->fields['oprcanhash'] = $data->phpBool($oprdata->fields['oprcanhash']);
196
197
        if ($oprdata->recordCount() > 0) {
198
            echo "<table>\n";
199
            echo "<tr><th class=\"data left\">{$lang['strname']}</th>\n";
200
            echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprname']), "</td></tr>\n";
201
            echo "<tr><th class=\"data left\">{$lang['strleftarg']}</th>\n";
202
            echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprleftname']), "</td></tr>\n";
203
            echo "<tr><th class=\"data left\">{$lang['strrightarg']}</th>\n";
204
            echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprrightname']), "</td></tr>\n";
205
            echo "<tr><th class=\"data left\">{$lang['strcommutator']}</th>\n";
206
            echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprcom']), "</td></tr>\n";
207
            echo "<tr><th class=\"data left\">{$lang['strnegator']}</th>\n";
208
            echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprnegate']), "</td></tr>\n";
209
            echo "<tr><th class=\"data left\">{$lang['strjoin']}</th>\n";
210
            echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprjoin']), "</td></tr>\n";
211
            echo "<tr><th class=\"data left\">{$lang['strhashes']}</th>\n";
212
            echo '<td class="data1">', ($oprdata->fields['oprcanhash']) ? $lang['stryes'] : $lang['strno'], "</td></tr>\n";
213
214
            // these field only exists in 8.2 and before in pg_catalog
215
            if (isset($oprdata->fields['oprlsortop'])) {
216
                echo "<tr><th class=\"data left\">{$lang['strmerges']}</th>\n";
217
                echo '<td class="data1">', ('0' !== $oprdata->fields['oprlsortop'] && '0' !== $oprdata->fields['oprrsortop']) ? $lang['stryes'] : $lang['strno'], "</td></tr>\n";
218
                echo "<tr><th class=\"data left\">{$lang['strrestrict']}</th>\n";
219
                echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprrest']), "</td></tr>\n";
220
                echo "<tr><th class=\"data left\">{$lang['strleftsort']}</th>\n";
221
                echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprlsortop']), "</td></tr>\n";
222
                echo "<tr><th class=\"data left\">{$lang['strrightsort']}</th>\n";
223
                echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprrsortop']), "</td></tr>\n";
224
                echo "<tr><th class=\"data left\">{$lang['strlessthan']}</th>\n";
225
                echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprltcmpop']), "</td></tr>\n";
226
                echo "<tr><th class=\"data left\">{$lang['strgreaterthan']}</th>\n";
227
                echo '<td class="data1">', $this->misc->printVal($oprdata->fields['oprgtcmpop']), "</td></tr>\n";
228
            } else {
229
                echo "<tr><th class=\"data left\">{$lang['strmerges']}</th>\n";
230
                echo '<td class="data1">', $data->phpBool($oprdata->fields['oprcanmerge']) ? $lang['stryes'] : $lang['strno'], "</td></tr>\n";
231
            }
232
            echo "</table>\n";
233
234
            $this->printNavLinks(
235
                [
236
                    'showall' => [
237
                        'attr' => [
238
                            'href' => [
239
                                'url'     => 'operators.php',
240
                                'urlvars' => [
241
                                    'server'   => $_REQUEST['server'],
242
                                    'database' => $_REQUEST['database'],
243
                                    'schema'   => $_REQUEST['schema'],
244
                                ],
245
                            ],
246
                        ],
247
                        'content' => $lang['strshowalloperators'],
248
                    ], ],
249
                'operators-properties',
250
                get_defined_vars()
251
            );
252
        } else {
253
            $this->doDefault($lang['strinvalidparam']);
254
        }
255
    }
256
257
    /**
258
     * Show confirmation of drop and perform actual drop.
259
     *
260
     * @param mixed $confirm
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
261
     */
262
    public function doDrop($confirm)
263
    {
264
        $conf = $this->conf;
0 ignored issues
show
Unused Code introduced by
The assignment to $conf is dead and can be removed.
Loading history...
265
266
        $lang = $this->lang;
267
        $data = $this->misc->getDatabaseAccessor();
268
269
        if ($confirm) {
270
            $this->printTrail('operator');
271
            $this->printTitle($lang['strdrop'], 'pg.operator.drop');
272
273
            echo '<p>', sprintf($lang['strconfdropoperator'], $this->misc->printVal($_REQUEST['operator'])), "</p>\n";
274
275
            echo '<form action="'.\SUBFOLDER."/src/views/operators.php\" method=\"post\">\n";
276
            echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n";
277
            echo "<p><input type=\"hidden\" name=\"action\" value=\"drop\" />\n";
278
            echo '<input type="hidden" name="operator" value="', htmlspecialchars($_REQUEST['operator']), "\" />\n";
279
            echo '<input type="hidden" name="operator_oid" value="', htmlspecialchars($_REQUEST['operator_oid']), "\" />\n";
280
            echo $this->misc->form;
281
            echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n";
282
            echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
283
            echo "</form>\n";
284
        } else {
285
            $status = $data->dropOperator($_POST['operator_oid'], isset($_POST['cascade']));
286
            if (0 == $status) {
287
                $this->doDefault($lang['stroperatordropped']);
288
            } else {
289
                $this->doDefault($lang['stroperatordroppedbad']);
290
            }
291
        }
292
    }
293
}
294