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

HistoryController::doClearHistory()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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