Passed
Pull Request — develop (#186)
by Felipe
07:21 queued 01:59
created

HTMLNavbarController::printTrail()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 8
nop 3
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.45
5
 */
6
7
namespace PHPPgAdmin\XHtml;
8
9
/**
10
 * Class to render tables. Formerly part of Misc.php.
11
 */
12
class HTMLNavbarController extends HTMLController
13
{
14
    public $controller_name = 'HTMLNavbarController';
15
16
    private function _getCrumbs($trail)
17
    {
18
        $crumbs = [];
19
        foreach ($trail as $crumb_id => $crumb) {
20
            if (isset($crumb['url'])) {
21
                $crumbs[$crumb_id]['url'] = str_replace('&amp;', '&', $crumb['url']);
22
            }
23
24
            if (isset($crumb['title'])) {
25
                $crumbs[$crumb_id]['title']   = $crumb['title'];
26
                $crumbs[$crumb_id]['iconalt'] = $crumb['title'];
27
            } else {
28
                $crumbs[$crumb_id]['iconalt'] = 'Database Root';
29
            }
30
31
            if (isset($crumb['icon']) && $icon = $this->misc->icon($crumb['icon'])) {
32
                $crumbs[$crumb_id]['icon'] = $icon;
33
            }
34
35
            $crumbs[$crumb_id]['text'] = $crumb['text'];
36
37
            if (isset($crumb['help'])) {
38
                $crumbs[$crumb_id]['helpurl'] = str_replace('&amp;', '&', $this->misc->getHelpLink($crumb['help']));
39
            }
40
        }
41
        return $crumbs;
42
    }
43
44
    private function _getSearchPathsCrumbs($crumbs, $viewVars)
45
    {
46
        $data = $this->misc->getDatabaseAccessor();
47
        $lang = $this->lang;
48
        if (isset($crumbs['database'])) {
49
            $search_path_crumbs = [];
50
            $dburl              = $crumbs['database']['url'];
51
            $search_paths       = $data->getSearchPath();
52
            foreach ($search_paths as $schema) {
53
                $search_path_crumbs[$schema] = [
54
                    'title'   => $lang['strschema'],
55
                    'text'    => $schema,
56
                    'icon'    => $this->misc->icon('Schema'),
57
                    'iconalt' => $lang['strschema'],
58
                    'url'     => str_replace(['&amp;', 'redirect/database'], ['&', 'redirect/schema'], $dburl . '&schema=' . $schema),
59
                ];
60
            }
61
            $viewVars['search_paths'] = $search_path_crumbs;
62
        }
63
        return $viewVars;
64
    }
65
66
    /**
67
     * Display a bread crumb trail.
68
     *
69
     * @param array|string $trail an array of breadcrumb items, or a string to identify one of them
70
     * @param  $do_print true to echo, false to return html
71
     * @param null|string $from
72
     */
73
    public function printTrail($trail = [], $do_print = true, $from = null)
74
    {
75
        $plugin_manager = $this->plugin_manager;
76
        $from           = $from ? $from : __METHOD__;
77
78
        $trail_html = $this->printTopbar(false, $from);
79
80
        if (is_string($trail)) {
81
            $subject = $trail;
82
            $trail   = $this->_getTrail($subject);
83
            // Trail hook's place
84
            $plugin_functions_parameters = [
85
                'trail'   => &$trail,
86
                'section' => $subject,
87
            ];
88
89
            $plugin_manager->doHook('trail', $plugin_functions_parameters);
90
        }
91
92
        $crumbs = $this->_getCrumbs($trail);
93
94
        $viewVars = [
95
            'crumbs'          => $crumbs,
96
            'controller_name' => $this->controller_name,
97
        ];
98
        $viewVars = $this->_getSearchPathsCrumbs($crumbs, $viewVars);
99
100
        //\Kint::dump($viewVars);
101
102
        $trail_html .= $this->getContainer()->view->fetch('components/trail.twig', $viewVars);
103
104
        if ($do_print) {
105
            echo $trail_html;
106
        } else {
107
            return $trail_html;
108
        }
109
    }
110
111
    /**
112
     * Display navigation tabs.
113
     *
114
     * @param string      $alltabs   The name of current section (Ex: intro, server, ...),
115
     *                               or an array with tabs (Ex: sqledit::doFind function)
116
     * @param string      $activetab the name of the tab to be highlighted
117
     * @param bool        $print     if false, return html
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $print does not match actual variable name $do_print
Loading history...
118
     * @param bool        $do_print  true to print html, false to return html
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $do_print does not match actual variable name $from
Loading history...
119
     * @param null|string $from      whichi method is calling this one
120
     */
121
    public function printTabs($alltabs, $activetab, $do_print = true, $from = null)
122
    {
123
        $from = $from ? $from : __METHOD__;
124
125
        $lang       = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
126
        $this->misc = $this->misc;
127
128
        if (is_string($alltabs)) {
1 ignored issue
show
introduced by
The condition is_string($alltabs) is always true.
Loading history...
129
            $_SESSION['webdbLastTab'][$alltabs] = $activetab;
130
            $alltabs                            = $this->misc->getNavTabs($alltabs);
131
        }
132
        //$this->prtrace($tabs);
133
        $tabs_html = '';
134
135
        //Getting only visible tabs
136
        $tabs = [];
137
        if (count($alltabs) > 0) {
138
            foreach ($alltabs as $tab_id => $tab) {
139
                if (!isset($tab['hide']) || true !== $tab['hide']) {
140
                    $tabs[$tab_id]            = $tab;
141
                    $tabs[$tab_id]['active']  = $active  = ($tab_id == $activetab) ? ' active' : '';
0 ignored issues
show
Unused Code introduced by
The assignment to $active is dead and can be removed.
Loading history...
142
                    $tabs[$tab_id]['tablink'] = str_replace(['&amp;', '.php'], ['&', ''], htmlentities($this->getActionUrl($tab, $_REQUEST, $from)));
143
                    //$this->prtrace('link for ' . $tab_id, $tabs[$tab_id]['tablink']);
144
                    if (isset($tab['icon']) && $icon = $this->misc->icon($tab['icon'])) {
145
                        $tabs[$tab_id]['iconurl'] = $icon;
146
                    }
147
                    if (isset($tab['help'])) {
148
                        $tabs[$tab_id]['helpurl'] = str_replace('&amp;', '&', $this->misc->getHelpLink($tab['help']));
149
                    }
150
                }
151
            }
152
        }
153
154
        //$this->prtrace($tabs);
155
156
        if (count($tabs) > 0) {
157
            $width = (int) (100 / count($tabs)) . '%';
158
159
            $viewVars = [
160
                'width'           => $width,
161
                'tabs'            => $tabs,
162
                'controller_name' => $this->controller_name,
163
            ];
164
165
            $tabs_html = $this->getContainer()->view->fetch('components/tabs.twig', $viewVars);
166
        }
167
168
        if ($do_print) {
169
            echo $tabs_html;
170
        } else {
171
            return $tabs_html;
172
        }
173
    }
174
175
    /**
176
     * [printTopbar description].
177
     *
178
     * @param bool       $do_print true to print, false to return html
179
     * @param null|mixed $from     which method is calling this one
180
     *
181
     * @return string
182
     */
183
    private function printTopbar($do_print = true, $from = null)
1 ignored issue
show
Coding Style introduced by
Private method name "HTMLNavbarController::printTopbar" must be prefixed with an underscore
Loading history...
184
    {
185
        $from = $from ? $from : __METHOD__;
186
187
        $lang           = $this->lang;
188
        $plugin_manager = $this->plugin_manager;
189
        $this->misc     = $this->misc;
190
        $appName        = $this->misc->appName;
191
        $appVersion     = $this->misc->appVersion;
192
193
        $server_info = $this->misc->getServerInfo();
194
        $server_id   = $this->misc->getServerId();
1 ignored issue
show
Unused Code introduced by
The assignment to $server_id is dead and can be removed.
Loading history...
195
        $reqvars     = $this->misc->getRequestVars('table');
196
197
        $topbar_html = '<div class="topbar" data-controller="' . $this->controller_name . '"><table style="width: 100%"><tr><td>';
198
199
        if ($server_info && isset($server_info['platform'], $server_info['username'])) {
200
            // top left informations when connected
201
            $topbar_html .= sprintf(
202
                $lang['strtopbar'],
203
                '<span class="platform">' . htmlspecialchars($server_info['platform']) . '</span>',
204
                '<span class="host">' . htmlspecialchars((empty($server_info['host'])) ? 'localhost' : $server_info['host']) . '</span>',
205
                '<span class="port">' . htmlspecialchars($server_info['port']) . '</span>',
206
                '<span class="username">' . htmlspecialchars($server_info['username']) . '</span>'
207
            );
208
209
            $topbar_html .= '</td>';
210
211
            // top right informations when connected
212
213
            $toplinks = [
214
                'sql'     => [
215
                    'attr'    => [
216
                        'href'   => [
217
                            'url'     => SUBFOLDER . '/src/views/sqledit',
218
                            'urlvars' => array_merge($reqvars, [
219
                                'action' => 'sql',
220
                            ]),
221
                        ],
222
                        'target' => 'sqledit',
223
                        'id'     => 'toplink_sql',
224
                    ],
225
                    'content' => $lang['strsql'],
226
                ],
227
                'history' => [
228
                    'attr'    => [
229
                        'href' => [
230
                            'url'     => SUBFOLDER . '/src/views/history',
231
                            'urlvars' => array_merge($reqvars, [
232
                                'action' => 'pophistory',
233
                            ]),
234
                        ],
235
                        'id'   => 'toplink_history',
236
                    ],
237
                    'content' => $lang['strhistory'],
238
                ],
239
                'find'    => [
240
                    'attr'    => [
241
                        'href'   => [
242
                            'url'     => SUBFOLDER . '/src/views/sqledit',
243
                            'urlvars' => array_merge($reqvars, [
244
                                'action' => 'find',
245
                            ]),
246
                        ],
247
                        'target' => 'sqledit',
248
                        'id'     => 'toplink_find',
249
                    ],
250
                    'content' => $lang['strfind'],
251
                ],
252
                'logout'  => [
253
                    'attr'    => [
254
                        'href' => [
255
                            'url'     => SUBFOLDER . '/src/views/servers',
256
                            'urlvars' => [
257
                                'action'       => 'logout',
258
                                'logoutServer' => sha1("{$server_info['host']}:{$server_info['port']}:{$server_info['sslmode']}"),
259
                            ],
260
                        ],
261
                        'id'   => 'toplink_logout',
262
                    ],
263
                    'content' => $lang['strlogout'],
264
                ],
265
            ];
266
267
            // Toplink hook's place
268
            $plugin_functions_parameters = [
269
                'toplinks' => &$toplinks,
270
            ];
271
272
            $plugin_manager->doHook('toplinks', $plugin_functions_parameters);
273
274
            $topbar_html .= '<td style="text-align: right">';
275
276
            $topbar_html .= $this->printLinksList($toplinks, 'toplink', [], false, $from);
0 ignored issues
show
Unused Code introduced by
The call to PHPPgAdmin\XHtml\HTMLController::printLinksList() has too many arguments starting with $from. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

276
            $topbar_html .= $this->/** @scrutinizer ignore-call */ printLinksList($toplinks, 'toplink', [], false, $from);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
array() of type array is incompatible with the type boolean expected by parameter $do_print of PHPPgAdmin\XHtml\HTMLController::printLinksList(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

276
            $topbar_html .= $this->printLinksList($toplinks, 'toplink', /** @scrutinizer ignore-type */ [], false, $from);
Loading history...
Bug introduced by
false of type false is incompatible with the type null|string expected by parameter $from of PHPPgAdmin\XHtml\HTMLController::printLinksList(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

276
            $topbar_html .= $this->printLinksList($toplinks, 'toplink', [], /** @scrutinizer ignore-type */ false, $from);
Loading history...
277
278
            $topbar_html .= '</td>';
279
        } else {
280
            $topbar_html .= "<span class=\"appname\">{$appName}</span> <span class=\"version\">{$appVersion}</span>";
281
        }
282
283
        $topbar_html .= "</tr></table></div>\n";
284
285
        if ($do_print) {
286
            echo $topbar_html;
287
        } else {
288
            return $topbar_html;
289
        }
290
    }
291
292
    private function getHREFSubject($subject)
1 ignored issue
show
Coding Style introduced by
Private method name "HTMLNavbarController::getHREFSubject" must be prefixed with an underscore
Loading history...
293
    {
294
        $vars = $this->misc->getSubjectParams($subject);
295
        ksort($vars['params']);
296
297
        return "{$vars['url']}?" . http_build_query($vars['params'], '', '&amp;');
298
    }
299
300
    /**
301
     * Create a bread crumb trail of the object hierarchy.
302
     *
303
     * @param null|string $subject sunkect of the trail
304
     */
305
    private function _getTrail($subject = null)
306
    {
307
        $lang = $this->lang;
308
309
        $this->misc = $this->misc;
310
        $appName    = $this->misc->appName;
311
312
        $data = $this->misc->getDatabaseAccessor();
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
313
314
        $trail = [];
315
        $vars  = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $vars is dead and can be removed.
Loading history...
316
        $done  = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $done is dead and can be removed.
Loading history...
317
318
        $trail['root'] = [
319
            'text' => $appName,
320
            'url'  => SUBFOLDER . '/src/views/servers',
321
            'icon' => 'Introduction',
322
        ];
323
324
        if ('root' == $subject) {
325
            return $trail;
326
        }
327
328
        $server_info     = $this->misc->getServerInfo();
329
        $trail['server'] = [
330
            'title' => $lang['strserver'],
331
            'text'  => $server_info['desc'],
332
            'url'   => $this->getHREFSubject('server'),
333
            'help'  => 'pg.server',
334
            'icon'  => 'Server',
335
        ];
336
337
        if ('server' == $subject) {
338
            return $trail;
339
        }
340
341
        $database_rolename = [
342
            'database' => [
343
                'title'   => $lang['strdatabase'],
344
                'subject' => 'database',
345
                'help'    => 'pg.database',
346
                'icon'    => 'Database',
347
            ],
348
            'rolename' => [
349
                'title'   => $lang['strrole'],
350
                'subject' => 'role',
351
                'help'    => 'pg.role',
352
                'icon'    => 'Roles',
353
            ],
354
        ];
355
356
        $trail = $this->_getTrailsFromArray($trail, $database_rolename);
357
358
        if (in_array($subject, ['database', 'role'])) {
359
            return $trail;
360
        }
361
362
        $schema = [
363
            'schema' => [
364
                'title'   => $lang['strschema'],
365
                'subject' => 'schema',
366
                'help'    => 'pg.schema',
367
                'icon'    => 'Schema',
368
            ],
369
        ];
370
371
        $trail = $this->_getTrailsFromArray($trail, $schema);
372
        if ('schema' == $subject) {
373
            return $trail;
374
        }
375
376
        $table_view_matview_fts = [
377
            'table'   => [
378
                'title'   => $lang['strtable'],
379
                'subject' => 'table',
380
                'help'    => 'pg.table',
381
                'icon'    => 'Table',
382
            ],
383
            'view'    => [
384
                'title'   => $lang['strview'],
385
                'subject' => 'view',
386
                'help'    => 'pg.view',
387
                'icon'    => 'View',
388
            ],
389
            'matview' => [
390
                'title'   => 'M' . $lang['strview'],
391
                'subject' => 'matview',
392
                'help'    => 'pg.matview',
393
                'icon'    => 'MViews',
394
            ],
395
            'ftscfg'  => [
396
                'title'   => $lang['strftsconfig'],
397
                'subject' => 'ftscfg',
398
                'help'    => 'pg.ftscfg.example',
399
                'icon'    => 'Fts',
400
            ],
401
        ];
402
403
        $trail = $this->_getTrailsFromArray($trail, $table_view_matview_fts);
404
405
        if (in_array($subject, ['table', 'view', 'matview', 'ftscfg'])) {
406
            return $trail;
407
        }
408
409
        if (!is_null($subject)) {
410
            $trail = $this->_getLastTrailPart($subject, $trail);
411
        }
412
413
        //$this->prtrace($trail);
414
415
        return $trail;
416
    }
417
418
    private function _getTrailsFromArray($trail, $the_array)
419
    {
420
        foreach ($the_array as $key => $value) {
421
            if (isset($_REQUEST[$key])) {
422
                $trail[$key] = [
423
                    'title' => $value['title'],
424
                    'text'  => $_REQUEST[$key],
425
                    'url'   => $this->getHREFSubject($value['subject']),
426
                    'help'  => $value['help'],
427
                    'icon'  => $value['icon'],
428
                ];
429
                break;
430
            }
431
        }
432
        return $trail;
433
    }
434
435
    private function _getLastTrailPart($subject, $trail)
436
    {
437
        $lang = $this->lang;
438
439
        switch ($subject) {
440
            case 'function':
441
                $trail[$subject] = [
442
                    'title' => $lang['str' . $subject],
443
                    'text'  => $_REQUEST[$subject],
444
                    'url'   => $this->getHREFSubject('function'),
445
                    'help'  => 'pg.function',
446
                    'icon'  => 'Function',
447
                ];
448
449
                break;
450
            case 'aggregate':
451
                $trail[$subject] = [
452
                    'title' => $lang['straggregate'],
453
                    'text'  => $_REQUEST['aggrname'],
454
                    'url'   => $this->getHREFSubject('aggregate'),
455
                    'help'  => 'pg.aggregate',
456
                    'icon'  => 'Aggregate',
457
                ];
458
459
                break;
460
            case 'column':
461
                $trail['column'] = [
462
                    'title' => $lang['strcolumn'],
463
                    'text'  => $_REQUEST['column'],
464
                    'icon'  => 'Column',
465
                    'url'   => $this->getHREFSubject('column'),
466
                ];
467
468
                break;
469
            default:
470
                if (isset($_REQUEST[$subject])) {
471
                    switch ($subject) {
472
                        case 'domain':
473
                            $icon = 'Domain';
474
475
                            break;
476
                        case 'sequence':
477
                            $icon = 'Sequence';
478
479
                            break;
480
                        case 'type':
481
                            $icon = 'Type';
482
483
                            break;
484
                        case 'operator':
485
                            $icon = 'Operator';
486
487
                            break;
488
                        case 'index':
489
                            $icon = 'Index';
490
491
                            break;
492
                        default:
493
                            $icon = null;
494
495
                            break;
496
                    }
497
                    $trail[$subject] = [
498
                        'title' => array_key_exists('str' . $subject, $lang) ? $lang['str' . $subject] : $subject,
499
                        'text'  => $_REQUEST[$subject],
500
                        'help'  => 'pg.' . $subject,
501
                        'icon'  => $icon,
502
                    ];
503
                }
504
        }
505
        return trail;
0 ignored issues
show
Bug introduced by
The constant PHPPgAdmin\XHtml\trail was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
506
    }
507
}
508