Passed
Push — master ( cf0423...965dbc )
by Felipe
09:31 queued 04:21
created

SqleditController::doDefault()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 75
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 48
nc 24
nop 0
dl 0
loc 75
rs 6.5862
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
3
/**
4
 * PHPPgAdmin v6.0.0-beta.33
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 *
12
 * @package PHPPgAdmin
13
 */
14
class SqleditController extends BaseController
15
{
16
    public $controller_name = 'SqleditController';
17
    public $query           = '';
18
    public $subject         = '';
19
    public $start_time;
20
    public $duration;
21
22
    /**
23
     * Default method to render the controller according to the action parameter.
24
     */
25
    public function render()
26
    {
27
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
28
29
        $action = $this->action;
30
31
        switch ($action) {
32
            case 'find':
33
                $title     = $this->lang['strfind'];
34
                $body_text = $this->doFind();
35
36
                break;
37
            case 'sql':
38
            default:
39
                $title     = $this->lang['strsql'];
40
                $body_text = $this->doDefault();
41
42
                break;
43
        }
44
45
        $this->setWindowName('sqledit');
46
47
        $this->scripts = '<script type="text/javascript">window.inPopUp=true;</script>';
48
49
        $this->printHeader($title, $this->scripts, true, 'header_sqledit.twig');
50
        $this->printBody(true, 'sql_edit');
51
        echo $body_text;
52
53
        $this->printFooter(true, 'footer_sqledit.twig');
54
    }
55
56
    /**
57
     * Allow execution of arbitrary SQL statements on a database.
58
     */
59
    public function doDefault()
60
    {
61
        $lang = $this->lang;
62
        $data = $this->misc->getDatabaseAccessor();
63
64
        if (!isset($_SESSION['sqlquery'])) {
65
            $_SESSION['sqlquery'] = '';
66
        }
67
68
        if (!isset($_REQUEST['search_path'])) {
69
            $_REQUEST['search_path'] = implode(',', $data->getSearchPath());
70
        }
71
        $search_path = htmlspecialchars($_REQUEST['search_path']);
72
        $sqlquery    = htmlspecialchars($_SESSION['sqlquery']);
73
74
        $default_html = $this->printTabs($this->misc->getNavTabs('popup'), 'sql', false);
75
76
        $default_html .= '<form action="' . \SUBFOLDER . '/src/views/sql" method="post" enctype="multipart/form-data" class="sqlform" id="sqlform" target="detail">';
77
        $default_html .= "\n";
78
        $default_html .= $this->_printConnection('sql');
79
80
        $default_html .= "\n";
81
82
        $default_html .= ' <div class="searchpath">';
83
        $default_html .= '<label>';
84
        $default_html .= $this->misc->printHelp($lang['strsearchpath'], 'pg.schema.search_path', false);
85
86
        $default_html .= ': <input type="text" name="search_path" size="45" value="' . $search_path . '" />';
87
        $default_html .= "</label>\n";
88
89
        $default_html .= "</div>\n";
90
91
        $default_html .= '<div id="queryedition" style="padding:1%;width:98%;float:left;">';
92
        $default_html .= "\n";
93
        $default_html .= '<textarea style="width:98%;" rows="10" cols="50" name="query" id="query" resizable="true">' . $sqlquery . '</textarea>';
94
        $default_html .= "\n";
95
        $default_html .= "</div>\n";
96
97
        $default_html .= '<div class="sqledit_bottom_inputs" >';
98
99
        if (ini_get('file_uploads')) {
100
            // Don't show upload option if max size of uploads is zero
101
            $max_size = $this->misc->inisizeToBytes(ini_get('upload_max_filesize'));
102
            if (is_double($max_size) && $max_size > 0) {
103
                $default_html .= '<p class="upload_sql_script">';
104
                $default_html .= '<input type="hidden" name="MAX_FILE_SIZE" value="' . $max_size . '" />';
105
                $default_html .= "\n";
106
                $default_html .= '<label for="script">' . $lang['struploadscript'] . '</label>';
107
                $default_html .= '&nbsp;&nbsp; <input class="btn btn-small"  id="script" name="script" type="file" /></p>';
108
                $default_html .= "</p>\n";
109
            }
110
        }
111
112
        // Check that file uploads are enabled
113
        $checked = (isset($_REQUEST['paginate']) ? ' checked="checked"' : '');
114
115
        $default_html .= '<p><input type="submit" class="btn btn-small" name="execute" accesskey="r" value="' . $lang['strexecute'] . '" />';
116
        $default_html .= "\n";
117
118
        $default_html .= '<input type="reset" class="btn btn-small"  accesskey="q" value="' . $lang['strreset'] . '" /></p>';
119
        $default_html .= "\n";
120
121
        $default_html .= '<p>';
122
        $default_html .= '<label for="paginate">';
123
        $default_html .= '<input type="checkbox" id="paginate" name="paginate"' . $checked . ' />&nbsp;' . $lang['strpaginate'] . '&nbsp;';
124
        $default_html .= "</label>\n";
125
        $default_html .= "</p>\n";
126
127
        $default_html .= "</div>\n";
128
        $default_html .= '</form>';
129
        $default_html .= "\n";
130
131
        // Default focus
132
        //$this->setFocus('forms[0].query');
133
        return $default_html;
134
    }
135
136
    /**
137
     * Private function to display server and list of databases.
138
     *
139
     * @param mixed $action
140
     */
141
    public function _printConnection($action)
1 ignored issue
show
Coding Style introduced by
Public method name "SqleditController::_printConnection" must not be prefixed with an underscore
Loading history...
142
    {
143
        $lang = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
144
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
145
146
        // The javascript action on the select box reloads the
147
        // popup whenever the server or database is changed.
148
        // This ensures that the correct page encoding is used.
149
        $onchange = "onchange=\"location.href='" . \SUBFOLDER . '/sqledit/' .
150
        urlencode($action) . "?server=' + encodeURI(server.options[server.selectedIndex].value) + '&amp;database=' + encodeURI(database.options[database.selectedIndex].value) + ";
151
152
        // The exact URL to reload to is different between SQL and Find mode, however.
153
        if ('find' == $action) {
154
            $onchange .= "'&amp;term=' + encodeURI(term.value) + '&amp;filter=' + encodeURI(filter.value) + '&amp;'\"";
155
        } else {
156
            $onchange .= "'&amp;query=' + encodeURI(query.value) + '&amp;search_path=' + encodeURI(search_path.value) + (paginate.checked ? '&amp;paginate=on' : '')  + '&amp;'\"";
157
        }
158
159
        return $this->misc->printConnection($onchange, false);
160
    }
161
162
    /**
163
     * Searches for a named database object.
164
     */
165
    public function doFind()
166
    {
167
        $lang = $this->lang;
168
        $data = $this->misc->getDatabaseAccessor();
169
170
        if (!isset($_REQUEST['term'])) {
171
            $_REQUEST['term'] = '';
172
        }
173
174
        if (!isset($_REQUEST['filter'])) {
175
            $_REQUEST['filter'] = '';
176
        }
177
178
        $default_html = $this->printTabs($this->misc->getNavTabs('popup'), 'find', false);
179
180
        $default_html .= "<form action=\"database\" method=\"post\" target=\"detail\">\n";
181
        $default_html .= $this->_printConnection('find');
182
        $default_html .= '<p><input class="focusme" name="term" value="' . htmlspecialchars($_REQUEST['term']) . "\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" />\n";
183
184
        // Output list of filters.  This is complex due to all the 'has' and 'conf' feature possibilities
185
        $default_html .= "<select name=\"filter\">\n";
186
        $default_html .= "\t<option value=\"\"" . ('' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strallobjects']}</option>\n";
187
        $default_html .= "\t<option value=\"SCHEMA\"" . ('SCHEMA' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strschemas']}</option>\n";
188
        $default_html .= "\t<option value=\"TABLE\"" . ('TABLE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strtables']}</option>\n";
189
        $default_html .= "\t<option value=\"VIEW\"" . ('VIEW' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strviews']}</option>\n";
190
        $default_html .= "\t<option value=\"SEQUENCE\"" . ('SEQUENCE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strsequences']}</option>\n";
191
        $default_html .= "\t<option value=\"COLUMN\"" . ('COLUMN' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strcolumns']}</option>\n";
192
        $default_html .= "\t<option value=\"RULE\"" . ('RULE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strrules']}</option>\n";
193
        $default_html .= "\t<option value=\"INDEX\"" . ('INDEX' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strindexes']}</option>\n";
194
        $default_html .= "\t<option value=\"TRIGGER\"" . ('TRIGGER' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strtriggers']}</option>\n";
195
        $default_html .= "\t<option value=\"CONSTRAINT\"" . ('CONSTRAINT' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strconstraints']}</option>\n";
196
        $default_html .= "\t<option value=\"FUNCTION\"" . ('FUNCTION' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strfunctions']}</option>\n";
197
        $default_html .= "\t<option value=\"DOMAIN\"" . ('DOMAIN' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strdomains']}</option>\n";
198
        if ($this->conf['show_advanced']) {
199
            $default_html .= "\t<option value=\"AGGREGATE\"" . ('AGGREGATE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['straggregates']}</option>\n";
200
            $default_html .= "\t<option value=\"TYPE\"" . ('TYPE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strtypes']}</option>\n";
201
            $default_html .= "\t<option value=\"OPERATOR\"" . ('OPERATOR' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['stroperators']}</option>\n";
202
            $default_html .= "\t<option value=\"OPCLASS\"" . ('OPCLASS' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['stropclasses']}</option>\n";
203
            $default_html .= "\t<option value=\"CONVERSION\"" . ('CONVERSION' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strconversions']}</option>\n";
204
            $default_html .= "\t<option value=\"LANGUAGE\"" . ('LANGUAGE' == $_REQUEST['filter'] ? ' selected="selected" ' : '') . ">{$lang['strlanguages']}</option>\n";
205
        }
206
        $default_html .= "</select>\n";
207
208
        $default_html .= "<input type=\"submit\" value=\"{$lang['strfind']}\" />\n";
209
        $default_html .= "<input type=\"hidden\" name=\"action\" value=\"find\" /></p>\n";
210
        $default_html .= "</form>\n";
211
212
        // Default focus
213
        $this->setFocus('forms[0].term');
214
215
        return $default_html;
216
    }
217
}
218