Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

HistoryController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 242
rs 10
c 0
b 0
f 0
wmc 20

5 Methods

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