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

HTMLNavbarController::_getCrumbs()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 17
nop 1
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.46
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
42
        return $crumbs;
43
    }
44
45
    private function _getSearchPathsCrumbs($crumbs, $viewVars)
46
    {
47
        $data = $this->misc->getDatabaseAccessor();
48
        $lang = $this->lang;
49
        if (isset($crumbs['database'])) {
50
            $search_path_crumbs = [];
51
            $dburl              = $crumbs['database']['url'];
52
            $search_paths       = $data->getSearchPath();
53
            foreach ($search_paths as $schema) {
54
                $search_path_crumbs[$schema] = [
55
                    'title'   => $lang['strschema'],
56
                    'text'    => $schema,
57
                    'icon'    => $this->misc->icon('Schema'),
58
                    'iconalt' => $lang['strschema'],
59
                    'url'     => str_replace(['&amp;', 'redirect/database'], ['&', 'redirect/schema'], $dburl.'&schema='.$schema),
60
                ];
61
            }
62
            $viewVars['search_paths'] = $search_path_crumbs;
63
        }
64
65
        return $viewVars;
66
    }
67
68
    /**
69
     * Display a bread crumb trail.
70
     *
71
     * @param array|string $trail an array of breadcrumb items, or a string to identify one of them
72
     * @param  $do_print true to echo, false to return html
73
     * @param null|string $from
74
     */
75
    public function printTrail($trail = [], $do_print = true, $from = null)
76
    {
77
        $plugin_manager = $this->plugin_manager;
78
        $from           = $from ? $from : __METHOD__;
79
80
        $trail_html = $this->printTopbar(false, $from);
81
82
        if (is_string($trail)) {
83
            $subject = $trail;
84
            $trail   = $this->_getTrail($subject);
85
            // Trail hook's place
86
            $plugin_functions_parameters = [
87
                'trail'   => &$trail,
88
                'section' => $subject,
89
            ];
90
91
            $plugin_manager->doHook('trail', $plugin_functions_parameters);
92
        }
93
94
        $crumbs = $this->_getCrumbs($trail);
95
96
        $viewVars = [
97
            'crumbs'          => $crumbs,
98
            'controller_name' => $this->controller_name,
99
        ];
100
        $viewVars = $this->_getSearchPathsCrumbs($crumbs, $viewVars);
101
102
        //\Kint::dump($viewVars);
103
104
        $trail_html .= $this->getContainer()->view->fetch('components/trail.twig', $viewVars);
105
106
        if ($do_print) {
107
            echo $trail_html;
108
        } else {
109
            return $trail_html;
110
        }
111
    }
112
113
    /**
114
     * Display navigation tabs.
115
     *
116
     * @param string      $alltabs   The name of current section (Ex: intro, server, ...),
117
     *                               or an array with tabs (Ex: sqledit::doFind function)
118
     * @param string      $activetab the name of the tab to be highlighted
119
     * @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...
120
     * @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...
121
     * @param null|string $from      whichi method is calling this one
122
     */
123
    public function printTabs($alltabs, $activetab, $do_print = true, $from = null)
124
    {
125
        $from = $from ? $from : __METHOD__;
126
127
        $lang       = $this->lang;
0 ignored issues
show
Unused Code introduced by
The assignment to $lang is dead and can be removed.
Loading history...
128
        $this->misc = $this->misc;
129
130
        if (is_string($alltabs)) {
1 ignored issue
show
introduced by
The condition is_string($alltabs) is always true.
Loading history...
131
            $_SESSION['webdbLastTab'][$alltabs] = $activetab;
132
            $alltabs                            = $this->misc->getNavTabs($alltabs);
133
        }
134
        //$this->prtrace($tabs);
135
        $tabs_html = '';
136
137
        //Getting only visible tabs
138
        $tabs = [];
139
        if (count($alltabs) > 0) {
140
            foreach ($alltabs as $tab_id => $tab) {
141
                if (!isset($tab['hide']) || true !== $tab['hide']) {
142
                    $tabs[$tab_id]            = $tab;
143
                    $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...
144
                    $tabs[$tab_id]['tablink'] = str_replace(['&amp;', '.php'], ['&', ''], htmlentities($this->getActionUrl($tab, $_REQUEST, $from)));
145
                    //$this->prtrace('link for ' . $tab_id, $tabs[$tab_id]['tablink']);
146
                    if (isset($tab['icon']) && $icon = $this->misc->icon($tab['icon'])) {
147
                        $tabs[$tab_id]['iconurl'] = $icon;
148
                    }
149
                    if (isset($tab['help'])) {
150
                        $tabs[$tab_id]['helpurl'] = str_replace('&amp;', '&', $this->misc->getHelpLink($tab['help']));
151
                    }
152
                }
153
            }
154
        }
155
156
        //$this->prtrace($tabs);
157
158
        if (count($tabs) > 0) {
159
            $width = (int) (100 / count($tabs)).'%';
160
161
            $viewVars = [
162
                'width'           => $width,
163
                'tabs'            => $tabs,
164
                'controller_name' => $this->controller_name,
165
            ];
166
167
            $tabs_html = $this->getContainer()->view->fetch('components/tabs.twig', $viewVars);
168
        }
169
170
        if ($do_print) {
171
            echo $tabs_html;
172
        } else {
173
            return $tabs_html;
174
        }
175
    }
176
177
    /**
178
     * [printTopbar description].
179
     *
180
     * @param bool       $do_print true to print, false to return html
181
     * @param null|mixed $from     which method is calling this one
182
     *
183
     * @return string
184
     */
185
    private function printTopbar($do_print = true, $from = null)
186
    {
187
        $from = $from ? $from : __METHOD__;
188
189
        $lang           = $this->lang;
190
        $plugin_manager = $this->plugin_manager;
191
        $this->misc     = $this->misc;
192
        $appName        = $this->misc->appName;
193
        $appVersion     = $this->misc->appVersion;
194
195
        $server_info = $this->misc->getServerInfo();
196
        $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...
197
        $reqvars     = $this->misc->getRequestVars('table');
198
199
        $topbar_html = '<div class="topbar" data-controller="'.$this->controller_name.'"><table style="width: 100%"><tr><td>';
200
201
        if ($server_info && isset($server_info['platform'], $server_info['username'])) {
202
            // top left informations when connected
203
            $topbar_html .= sprintf(
204
                $lang['strtopbar'],
205
                '<span class="platform">'.htmlspecialchars($server_info['platform']).'</span>',
206
                '<span class="host">'.htmlspecialchars((empty($server_info['host'])) ? 'localhost' : $server_info['host']).'</span>',
207
                '<span class="port">'.htmlspecialchars($server_info['port']).'</span>',
208
                '<span class="username">'.htmlspecialchars($server_info['username']).'</span>'
209
            );
210
211
            $topbar_html .= '</td>';
212
213
            // top right informations when connected
214
215
            $toplinks = [
216
                'sql'     => [
217
                    'attr'    => [
218
                        'href'   => [
219
                            'url'     => SUBFOLDER.'/src/views/sqledit',
220
                            'urlvars' => array_merge($reqvars, [
221
                                'action' => 'sql',
222
                            ]),
223
                        ],
224
                        'target' => 'sqledit',
225
                        'id'     => 'toplink_sql',
226
                    ],
227
                    'content' => $lang['strsql'],
228
                ],
229
                'history' => [
230
                    'attr'    => [
231
                        'href' => [
232
                            'url'     => SUBFOLDER.'/src/views/history',
233
                            'urlvars' => array_merge($reqvars, [
234
                                'action' => 'pophistory',
235
                            ]),
236
                        ],
237
                        'id'   => 'toplink_history',
238
                    ],
239
                    'content' => $lang['strhistory'],
240
                ],
241
                'find'    => [
242
                    'attr'    => [
243
                        'href'   => [
244
                            'url'     => SUBFOLDER.'/src/views/sqledit',
245
                            'urlvars' => array_merge($reqvars, [
246
                                'action' => 'find',
247
                            ]),
248
                        ],
249
                        'target' => 'sqledit',
250
                        'id'     => 'toplink_find',
251
                    ],
252
                    'content' => $lang['strfind'],
253
                ],
254
                'logout'  => [
255
                    'attr'    => [
256
                        'href' => [
257
                            'url'     => SUBFOLDER.'/src/views/servers',
258
                            'urlvars' => [
259
                                'action'       => 'logout',
260
                                'logoutServer' => sha1("{$server_info['host']}:{$server_info['port']}:{$server_info['sslmode']}"),
261
                            ],
262
                        ],
263
                        'id'   => 'toplink_logout',
264
                    ],
265
                    'content' => $lang['strlogout'],
266
                ],
267
            ];
268
269
            // Toplink hook's place
270
            $plugin_functions_parameters = [
271
                'toplinks' => &$toplinks,
272
            ];
273
274
            $plugin_manager->doHook('toplinks', $plugin_functions_parameters);
275
276
            $topbar_html .= '<td style="text-align: right">';
277
278
            $topbar_html .= $this->printLinksList($toplinks, 'toplink', false, $from);
279
280
            $topbar_html .= '</td>';
281
        } else {
282
            $topbar_html .= "<span class=\"appname\">{$appName}</span> <span class=\"version\">{$appVersion}</span>";
283
        }
284
285
        $topbar_html .= "</tr></table></div>\n";
286
287
        if ($do_print) {
288
            echo $topbar_html;
289
        } else {
290
            return $topbar_html;
291
        }
292
    }
293
294
    private function getHREFSubject($subject)
295
    {
296
        $vars = $this->misc->getSubjectParams($subject);
297
        ksort($vars['params']);
298
299
        return "{$vars['url']}?".http_build_query($vars['params'], '', '&amp;');
300
    }
301
302
    /**
303
     * Create a bread crumb trail of the object hierarchy.
304
     *
305
     * @param null|string $subject sunkect of the trail
306
     *
307
     * @return array the trail array
308
     */
309
    private function _getTrail($subject = null)
310
    {
311
        $lang = $this->lang;
312
313
        $appName = $this->misc->appName;
314
315
        $trail = [];
316
317
        $trail['root'] = [
318
            'text' => $appName,
319
            'url'  => SUBFOLDER.'/src/views/servers',
320
            'icon' => 'Introduction',
321
        ];
322
323
        if ('root' == $subject) {
324
            return $trail;
325
        }
326
327
        $server_info     = $this->misc->getServerInfo();
328
        $trail['server'] = [
329
            'title' => $lang['strserver'],
330
            'text'  => $server_info['desc'],
331
            'url'   => $this->getHREFSubject('server'),
332
            'help'  => 'pg.server',
333
            'icon'  => 'Server',
334
        ];
335
336
        if ('server' == $subject) {
337
            return $trail;
338
        }
339
340
        $database_rolename = [
341
            'database' => [
342
                'title'   => $lang['strdatabase'],
343
                'subject' => 'database',
344
                'help'    => 'pg.database',
345
                'icon'    => 'Database',
346
            ],
347
            'rolename' => [
348
                'title'   => $lang['strrole'],
349
                'subject' => 'role',
350
                'help'    => 'pg.role',
351
                'icon'    => 'Roles',
352
            ],
353
        ];
354
355
        $trail = $this->_getTrailsFromArray($trail, $database_rolename);
356
357
        if (in_array($subject, ['database', 'role'], true)) {
358
            return $trail;
359
        }
360
361
        $schema = [
362
            'schema' => [
363
                'title'   => $lang['strschema'],
364
                'subject' => 'schema',
365
                'help'    => 'pg.schema',
366
                'icon'    => 'Schema',
367
            ],
368
        ];
369
370
        $trail = $this->_getTrailsFromArray($trail, $schema);
371
        if ('schema' == $subject) {
372
            return $trail;
373
        }
374
375
        $table_view_matview_fts = [
376
            'table'   => [
377
                'title'   => $lang['strtable'],
378
                'subject' => 'table',
379
                'help'    => 'pg.table',
380
                'icon'    => 'Table',
381
            ],
382
            'view'    => [
383
                'title'   => $lang['strview'],
384
                'subject' => 'view',
385
                'help'    => 'pg.view',
386
                'icon'    => 'View',
387
            ],
388
            'matview' => [
389
                'title'   => 'M'.$lang['strview'],
390
                'subject' => 'matview',
391
                'help'    => 'pg.matview',
392
                'icon'    => 'MViews',
393
            ],
394
            'ftscfg'  => [
395
                'title'   => $lang['strftsconfig'],
396
                'subject' => 'ftscfg',
397
                'help'    => 'pg.ftscfg.example',
398
                'icon'    => 'Fts',
399
            ],
400
        ];
401
402
        $trail = $this->_getTrailsFromArray($trail, $table_view_matview_fts);
403
404
        if (in_array($subject, ['table', 'view', 'matview', 'ftscfg'], true)) {
405
            return $trail;
406
        }
407
408
        if (!is_null($subject)) {
409
            $trail = $this->_getLastTrailPart($subject, $trail);
410
        }
411
412
        //$this->prtrace($trail);
413
414
        return $trail;
415
    }
416
417
    private function _getTrailsFromArray($trail, $the_array)
418
    {
419
        foreach ($the_array as $key => $value) {
420
            if (isset($_REQUEST[$key])) {
421
                $trail[$key] = [
422
                    'title' => $value['title'],
423
                    'text'  => $_REQUEST[$key],
424
                    'url'   => $this->getHREFSubject($value['subject']),
425
                    'help'  => $value['help'],
426
                    'icon'  => $value['icon'],
427
                ];
428
429
                break;
430
            }
431
        }
432
433
        return $trail;
434
    }
435
436
    private function _getLastTrailPart($subject, $trail)
437
    {
438
        $lang = $this->lang;
439
440
        switch ($subject) {
441
            case 'function':
442
                $trail[$subject] = [
443
                    'title' => $lang['str'.$subject],
444
                    'text'  => $_REQUEST[$subject],
445
                    'url'   => $this->getHREFSubject('function'),
446
                    'help'  => 'pg.function',
447
                    'icon'  => 'Function',
448
                ];
449
450
                break;
451
            case 'aggregate':
452
                $trail[$subject] = [
453
                    'title' => $lang['straggregate'],
454
                    'text'  => $_REQUEST['aggrname'],
455
                    'url'   => $this->getHREFSubject('aggregate'),
456
                    'help'  => 'pg.aggregate',
457
                    'icon'  => 'Aggregate',
458
                ];
459
460
                break;
461
            case 'column':
462
                $trail['column'] = [
463
                    'title' => $lang['strcolumn'],
464
                    'text'  => $_REQUEST['column'],
465
                    'icon'  => 'Column',
466
                    'url'   => $this->getHREFSubject('column'),
467
                ];
468
469
                break;
470
            default:
471
                if (isset($_REQUEST[$subject])) {
472
                    switch ($subject) {
473
                        case 'domain':
474
                            $icon = 'Domain';
475
476
                            break;
477
                        case 'sequence':
478
                            $icon = 'Sequence';
479
480
                            break;
481
                        case 'type':
482
                            $icon = 'Type';
483
484
                            break;
485
                        case 'operator':
486
                            $icon = 'Operator';
487
488
                            break;
489
                        case 'index':
490
                            $icon = 'Index';
491
492
                            break;
493
                        default:
494
                            $icon = null;
495
496
                            break;
497
                    }
498
                    $trail[$subject] = [
499
                        'title' => array_key_exists('str'.$subject, $lang) ? $lang['str'.$subject] : $subject,
500
                        'text'  => $_REQUEST[$subject],
501
                        'help'  => 'pg.'.$subject,
502
                        'icon'  => $icon,
503
                    ];
504
                }
505
        }
506
507
        return $trail;
508
    }
509
}
510