Test Failed
Pull Request — develop (#340)
by Felipe
05:31
created

HTMLNavbarController::getLastTabURL()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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