Passed
Push — master ( 5397c5...a7725b )
by Felipe
05:40
created

ViewsAndMaterializedViewsTrait::doDefault()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 149
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 149
rs 8.2857
c 0
b 0
f 0
cc 3
eloc 99
nc 4
nop 1

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.43
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
use PHPPgAdmin\Decorators\Decorator;
10
11
/**
12
 * Common trait for dealing with views or materialized views
13
 */
14
trait ViewsAndMaterializedViewsTrait
15
{
16
    public $href = '';
17
    public $misc;
18
    public $view_name;
19
20
    /**
21
     * Show view definition and virtual columns.
22
     *
23
     * @param mixed $msg
24
     */
25
    public function doDefault($msg = '')
26
    {
27
        $data = $this->misc->getDatabaseAccessor();
28
29
        $attPre = function (&$rowdata) use ($data) {
30
            $rowdata->fields['+type'] = $data->formatType($rowdata->fields['type'], $rowdata->fields['atttypmod']);
31
        };
32
33
        $this->printTrail($this->subject);
34
        $this->printTabs($this->subject, 'columns');
35
        $this->printMsg($msg);
36
37
        // Get view
38
        $vdata = $data->getView($_REQUEST[$this->subject]);
39
        // Get columns (using same method for getting a view)
40
        $attrs = $data->getTableAttributes($_REQUEST[$this->subject]);
41
42
        // Show comment if any
43
        if (null !== $vdata->fields['relcomment']) {
44
            echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n";
45
        }
46
47
        $columns = [
48
            'column'  => [
49
                'title' => $this->lang['strcolumn'],
50
                'field' => Decorator::field('attname'),
51
                'url'   => "colproperties?subject=column&amp;{$this->misc->href}&amp;view=" . urlencode($_REQUEST[$this->subject]) . '&amp;',
52
                'vars'  => ['column' => 'attname'],
53
            ],
54
            'type'    => [
55
                'title' => $this->lang['strtype'],
56
                'field' => Decorator::field('+type'),
57
            ],
58
            'default' => [
59
                'title' => $this->lang['strdefault'],
60
                'field' => Decorator::field('adsrc'),
61
            ],
62
            'actions' => [
63
                'title' => $this->lang['stractions'],
64
            ],
65
            'comment' => [
66
                'title' => $this->lang['strcomment'],
67
                'field' => Decorator::field('comment'),
68
            ],
69
        ];
70
71
        $actions = [
72
            'alter' => [
73
                'content' => $this->lang['stralter'],
74
                'attr'    => [
75
                    'href' => [
76
                        'url'     => $this->view_name,
77
                        'urlvars' => [
78
                            'action'       => 'properties',
79
                            $this->subject => $_REQUEST[$this->subject],
80
                            'column'       => Decorator::field('attname'),
81
                        ],
82
                    ],
83
                ],
84
            ],
85
        ];
86
87
        echo $this->printTable($attrs, $columns, $actions, "{$this->view_name}-{$this->view_name}", null, $attPre);
88
89
        echo "<br />\n";
90
91
        $navlinks = [
92
            'browse' => [
93
                'attr'    => [
94
                    'href' => [
95
                        'url'     => 'display',
96
                        'urlvars' => [
97
                            'server'       => $_REQUEST['server'],
98
                            'database'     => $_REQUEST['database'],
99
                            'schema'       => $_REQUEST['schema'],
100
                            $this->subject => $_REQUEST[$this->subject],
101
                            'subject'      => $this->subject,
102
                            'return'       => $this->subject,
103
                        ],
104
                    ],
105
                ],
106
                'content' => $this->lang['strbrowse'],
107
            ],
108
            'select' => [
109
                'attr'    => [
110
                    'href' => [
111
                        'url'     => str_replace('properties', 's', $this->view_name),
112
                        'urlvars' => [
113
                            'action'       => 'confselectrows',
114
                            'server'       => $_REQUEST['server'],
115
                            'database'     => $_REQUEST['database'],
116
                            'schema'       => $_REQUEST['schema'],
117
                            $this->subject => $_REQUEST[$this->subject],
118
                        ],
119
                    ],
120
                ],
121
                'content' => $this->lang['strselect'],
122
            ],
123
            'drop'   => [
124
                'attr'    => [
125
                    'href' => [
126
                        'url'     => str_replace('properties', 's', $this->view_name),
127
                        'urlvars' => [
128
                            'action'       => 'confirm_drop',
129
                            'server'       => $_REQUEST['server'],
130
                            'database'     => $_REQUEST['database'],
131
                            'schema'       => $_REQUEST['schema'],
132
                            $this->subject => $_REQUEST[$this->subject],
133
                        ],
134
                    ],
135
                ],
136
                'content' => $this->lang['strdrop'],
137
            ],
138
            'alter'  => [
139
                'attr'    => [
140
                    'href' => [
141
                        'url'     => $this->view_name,
142
                        'urlvars' => [
143
                            'action'       => 'confirm_alter',
144
                            'server'       => $_REQUEST['server'],
145
                            'database'     => $_REQUEST['database'],
146
                            'schema'       => $_REQUEST['schema'],
147
                            $this->subject => $_REQUEST[$this->subject],
148
                        ],
149
                    ],
150
                ],
151
                'content' => $this->lang['stralter'],
152
            ],
153
        ];
154
        $this->prtrace($this->view_name);
155
        if ($this->view_name === 'materializedviewproperties') {
156
            $navlinks['refresh'] = [
157
                'attr'    => [
158
                    'href' => [
159
                        'url'     => $this->view_name,
160
                        'urlvars' => [
161
                            'action'       => 'refresh',
162
                            'server'       => $_REQUEST['server'],
163
                            'database'     => $_REQUEST['database'],
164
                            'schema'       => $_REQUEST['schema'],
165
                            $this->subject => $_REQUEST[$this->subject],
166
                        ],
167
                    ],
168
                ],
169
                'content' => $this->lang['strrefresh'],
170
            ];
171
        }
172
173
        $this->printNavLinks($navlinks, "{$this->view_name}-{$this->view_name}", get_defined_vars());
174
    }
175
176
    public function doTree()
177
    {
178
        $data = $this->misc->getDatabaseAccessor();
179
180
        $reqvars = $this->misc->getRequestVars('column');
181
        $columns = $data->getTableAttributes($_REQUEST[$this->subject]);
182
183
        $attrs = [
184
            'text'       => Decorator::field('attname'),
185
            'action'     => Decorator::actionurl(
186
                'colproperties',
187
                $reqvars,
188
                [
189
                    $this->subject => $_REQUEST[$this->subject],
190
                    'column'       => Decorator::field('attname'),
191
                ]
192
            ),
193
            'icon'       => 'Column',
194
            'iconAction' => Decorator::url(
195
                'display',
196
                $reqvars,
197
                [
198
                    $this->subject => $_REQUEST[$this->subject],
199
                    'column'       => Decorator::field('attname'),
200
                    'query'        => Decorator::replace(
201
                        'SELECT "%column%", count(*) AS "count" FROM %view% GROUP BY "%column%" ORDER BY "%column%"',
202
                        [
203
                            '%column%' => Decorator::field('attname'),
204
                            '%view%'   => $_REQUEST[$this->subject],
205
                        ]
206
                    ),
207
                ]
208
            ),
209
            'toolTip'    => Decorator::field('comment'),
210
        ];
211
212
        return $this->printTree($columns, $attrs, 'viewcolumns');
213
    }
214
215
    /**
216
     * Allow the dumping of the data "in" a view
217
     * NOTE:: PostgreSQL doesn't currently support dumping the data in a view
218
     *        so I have disabled the data related parts for now. In the future
219
     *        we should allow it conditionally if it becomes supported.  This is
220
     *        a SMOP since it is based on pg_dump version not backend version.
221
     *
222
     * @param mixed $msg
223
     */
224
    public function doExport($msg = '')
225
    {
226
        $this->printTrail($this->subject);
227
        $this->printTabs($this->subject, 'export');
228
        $this->printMsg($msg);
229
230
        $subject = $this->subject;
231
        $object  = $_REQUEST[$this->subject];
232
233
        echo $this->formHeader();
234
        // Data only
235
        // echo $this->dataOnly(false);
236
237
        // Structure only
238
        echo $this->structureOnly(true);
239
240
        // Structure and data
241
        // echo $this->structureAndData();
242
243
        echo "</table>\n";
244
245
        echo $this->displayOrDownload();
246
247
        echo $this->formFooter($subject, $object);
248
    }
249
250
    /**
251
     * Show definition for a view or matview
252
     *
253
     * @param mixed $msg
254
     */
255
    public function doDefinition($msg = '')
256
    {
257
        $data = $this->misc->getDatabaseAccessor();
258
259
        // Get view
260
        $vdata = $data->getView($_REQUEST[$this->subject]);
261
262
        $this->printTrail($this->subject);
263
        $this->printTabs($this->subject, 'definition');
264
        $this->printMsg($msg);
265
266
        if ($vdata->RecordCount() > 0) {
267
            // Show comment if any
268
            if (null !== $vdata->fields['relcomment']) {
269
                echo '<p class="comment">', $this->misc->printVal($vdata->fields['relcomment']), "</p>\n";
270
            }
271
272
            echo "<table style=\"width: 100%\">\n";
273
            echo "<tr><th class=\"data\">{$this->lang['strdefinition']}</th></tr>\n";
274
            echo '<tr><td class="data1">', $this->misc->printVal($vdata->fields['vwdefinition']), "</td></tr>\n";
275
            echo "</table>\n";
276
        } else {
277
            echo "<p>{$this->lang['strnodata']}</p>\n";
278
        }
279
280
        $this->printNavLinks(['alter' => [
281
            'attr'    => [
282
                'href' => [
283
                    'url'     => $this->view_name,
284
                    'urlvars' => [
285
                        'action'       => 'edit',
286
                        'server'       => $_REQUEST['server'],
287
                        'database'     => $_REQUEST['database'],
288
                        'schema'       => $_REQUEST['schema'],
289
                        $this->subject => $_REQUEST[$this->subject],
290
                    ],
291
                ],
292
            ],
293
            'content' => $this->lang['stralter'],
294
        ]], "{$this->view_name}-definition", get_defined_vars());
295
    }
296
}
297