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

HistoryController::doDefault()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 123
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 77
nc 5
nop 0
dl 0
loc 123
rs 8.1463
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 HistoryController extends BaseController
15
{
16
    public $controller_name = 'HistoryController';
17
18
    /**
19
     * Default method to render the controller according to the action parameter.
20
     */
21
    public function render()
22
    {
23
        $lang   = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
24
        $action = $this->action;
25
26
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
27
28
        $this->scripts = '<script type="text/javascript">window.inPopUp=true;</script>';
29
30
        switch ($action) {
31
            case 'confdelhistory':
32
                $this->doDelHistory($_REQUEST['queryid'], true);
33
34
                break;
35
            case 'delhistory':
36
                if (isset($_POST['yes'])) {
37
                    $this->doDelHistory($_REQUEST['queryid'], false);
38
                }
39
40
                $this->doDefault();
41
42
                break;
43
            case 'confclearhistory':
44
                $this->doClearHistory(true);
45
46
                break;
47
            case 'clearhistory':
48
                if (isset($_POST['yes'])) {
49
                    $this->doClearHistory(false);
50
                }
51
52
                $this->doDefault();
53
54
                break;
55
            case 'download':
56
                return $this->doDownloadHistory();
57
            default:
58
                $this->doDefault();
59
        }
60
61
        // Set the name of the window
62
        $this->setWindowName('history');
63
64
        return $this->printFooter();
65
    }
66
67
    public function doDefault()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
68
    {
69
        $lang = $this->lang;
70
        $data = $this->misc->getDatabaseAccessor();
71
72
        $onchange = "onchange=\"location.href='" . \SUBFOLDER . "/src/views/history.php?server=' + encodeURI(server.options[server.selectedIndex].value) + '&amp;database=' + encodeURI(database.options[database.selectedIndex].value) + '&amp;'\"";
73
74
        $this->printHeader($lang['strhistory'], $this->scripts, true, 'header.twig');
75
76
        // Bring to the front always
77
        echo "<body onload=\"window.focus();\">\n";
78
79
        echo '<form action="' . \SUBFOLDER . "/src/views/history.php\" method=\"post\">\n";
80
        $this->misc->printConnection($onchange);
81
        echo '</form><br />';
82
83
        if (!isset($_REQUEST['database'])) {
84
            echo "<p>{$lang['strnodatabaseselected']}</p>\n";
85
86
            return;
87
        }
88
89
        if (isset($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']])) {
90
            $history = new \PHPPgAdmin\ArrayRecordSet($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']]);
91
92
            //Kint::dump($history);
93
            $columns = [
94
                'query'    => [
95
                    'title' => $lang['strsql'],
96
                    'field' => Decorator::field('query'),
97
                ],
98
                'paginate' => [
99
                    'title' => $lang['strpaginate'],
100
                    'field' => Decorator::field('paginate'),
101
                    'type'  => 'yesno',
102
                ],
103
                'actions'  => [
104
                    'title' => $lang['stractions'],
105
                ],
106
            ];
107
108
            $actions = [
109
                'run'    => [
110
                    'content' => $lang['strexecute'],
111
                    'attr'    => [
112
                        'href'   => [
113
                            'url'     => 'sql.php',
114
                            'urlvars' => [
115
                                'subject'   => 'history',
116
                                'nohistory' => 't',
117
                                'queryid'   => Decorator::field('queryid'),
118
                                'paginate'  => Decorator::field('paginate'),
119
                            ],
120
                        ],
121
                        'target' => 'detail',
122
                    ],
123
                ],
124
                'remove' => [
125
                    'content' => $lang['strdelete'],
126
                    'attr'    => [
127
                        'href' => [
128
                            'url'     => 'history.php',
129
                            'urlvars' => [
130
                                'action'  => 'confdelhistory',
131
                                'queryid' => Decorator::field('queryid'),
132
                            ],
133
                        ],
134
                    ],
135
                ],
136
            ];
137
138
            echo $this->printTable($history, $columns, $actions, 'history-history', $lang['strnohistory']);
139
        } else {
140
            echo "<p>{$lang['strnohistory']}</p>\n";
141
        }
142
143
        $navlinks = [
144
            'refresh' => [
145
                'attr'    => [
146
                    'href' => [
147
                        'url'     => 'history.php',
148
                        'urlvars' => [
149
                            'action'   => 'history',
150
                            'server'   => $_REQUEST['server'],
151
                            'database' => $_REQUEST['database'],
152
                        ],
153
                    ],
154
                ],
155
                'content' => $lang['strrefresh'],
156
            ],
157
        ];
158
159
        if (isset($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']])
160
            && count($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']])) {
1 ignored issue
show
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
161
            $navlinks['download'] = [
162
                'attr'    => [
163
                    'href' => [
164
                        'url'     => 'history.php',
165
                        'urlvars' => [
166
                            'action'   => 'download',
167
                            'server'   => $_REQUEST['server'],
168
                            'database' => $_REQUEST['database'],
169
                        ],
170
                    ],
171
                ],
172
                'content' => $lang['strdownload'],
173
            ];
174
            $navlinks['clear'] = [
175
                'attr'    => [
176
                    'href' => [
177
                        'url'     => 'history.php',
178
                        'urlvars' => [
179
                            'action'   => 'confclearhistory',
180
                            'server'   => $_REQUEST['server'],
181
                            'database' => $_REQUEST['database'],
182
                        ],
183
                    ],
184
                ],
185
                'content' => $lang['strclearhistory'],
186
            ];
187
        }
188
189
        $this->printNavLinks($navlinks, 'history-history', get_defined_vars());
190
    }
191
192
    public function doDelHistory($qid, $confirm)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
193
    {
194
        $lang = $this->lang;
195
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
196
197
        if ($confirm) {
198
            $this->printHeader($lang['strhistory'], $this->scripts);
199
200
            // Bring to the front always
201
            echo "<body onload=\"window.focus();\">\n";
202
203
            echo "<h3>{$lang['strdelhistory']}</h3>\n";
204
            echo "<p>{$lang['strconfdelhistory']}</p>\n";
205
206
            echo '<pre>', htmlentities($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']][$qid]['query'], ENT_QUOTES, 'UTF-8'), '</pre>';
207
            echo '<form action="' . \SUBFOLDER . "/src/views/history.php\" method=\"post\">\n";
208
            echo "<input type=\"hidden\" name=\"action\" value=\"delhistory\" />\n";
209
            echo "<input type=\"hidden\" name=\"queryid\" value=\"${qid}\" />\n";
210
            echo $this->misc->form;
211
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
212
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
213
            echo "</form>\n";
214
        } else {
215
            unset($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']][$qid]);
216
        }
217
    }
218
219
    public function doClearHistory($confirm)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
220
    {
221
        $lang = $this->lang;
222
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
223
224
        if ($confirm) {
225
            $this->printHeader($lang['strhistory'], $this->scripts);
226
227
            // Bring to the front always
228
            echo "<body onload=\"window.focus();\">\n";
229
230
            echo "<h3>{$lang['strclearhistory']}</h3>\n";
231
            echo "<p>{$lang['strconfclearhistory']}</p>\n";
232
233
            echo '<form action="' . \SUBFOLDER . "/src/views/history.php\" method=\"post\">\n";
234
            echo "<input type=\"hidden\" name=\"action\" value=\"clearhistory\" />\n";
235
            echo $this->misc->form;
236
            echo "<input type=\"submit\" name=\"yes\" value=\"{$lang['stryes']}\" />\n";
237
            echo "<input type=\"submit\" name=\"no\" value=\"{$lang['strno']}\" />\n";
238
            echo "</form>\n";
239
        } else {
240
            unset($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']]);
241
        }
242
    }
243
244
    public function doDownloadHistory()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
245
    {
246
        header('Content-Type: application/download');
247
        $datetime = date('YmdHis');
248
        header("Content-Disposition: attachment; filename=history{$datetime}.sql");
249
250
        foreach ($_SESSION['history'][$_REQUEST['server']][$_REQUEST['database']] as $queries) {
251
            $query = rtrim($queries['query']);
252
            echo $query;
253
            if (';' != substr($query, -1)) {
254
                echo ';';
255
            }
256
257
            echo "\n";
258
        }
259
    }
260
}
261