Passed
Push — develop ( 51bd2c...501708 )
by Felipe
05:51
created

HistoryController::doDelHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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