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