Passed
Push — master ( 87f465...0970e4 )
by Tomasz
05:21
created

Menu::printMenuItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/config/_config.php");
3
4
/**
5
 * Menu class helps to define the menu on the main page
6
 */
7
8
class Menu {
9
10
    /**
11
     * the constructor takes an array argument defining menu items.
12
     * the array must be indexed by strings which will be passed to user/cat_info.php a the page argument
13
     * the values of the array can be either a simple string which is passed to user/cat_info.php
14
     * as the title argument or an two element array - the first element of this array will be
15
     * the title and the second is a style specification applied to the given menu item
16
     * @param string $visibility
17
     * @param string $selectedLang
18
     */
19
    public function __construct($visibility = 'all', $selectedLang = '') {
20
        $langsArray = [];
21
        foreach (CONFIG['LANGUAGES'] as $lang => $value) {
22
            if ($lang == $selectedLang) {
23
                $langsArray[] = ['text'=>$value['display'], 'link'=>'javascript:changeLang("' . $lang . '")', 'class'=>'selected-lang'];
24
            } else {
25
                $langsArray[] = ['text'=>$value['display'], 'link'=>'javascript:changeLang("' . $lang . '")'];
26
            }
27
        }
28
        $this->menu = [['id' => 'start',
29
        'text' => _("Start page"),
30
        'visibility' => 'index'],
31
            ['id' => 'about',
32
                'text' => _("About"), 'link' => '', 'submenu' => [
33
                    ['text' => sprintf(_("About %s"), CONFIG['APPEARANCE']['productname']),
34
                        'catInfo' => ['about_cat', sprintf(_("About %s"), CONFIG['APPEARANCE']['productname'])]],
35
                    ['text' => sprintf(_("About %s"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']),
36
                        'link' => CONFIG_CONFASSISTANT['CONSORTIUM']['homepage']],
37
                ]],
38
            ['id' => 'lang',
39
                'text' => _("Language"), 'submenu' => $langsArray, ],
40
            ['id' => 'help',
41
                'text' => _("Help"), 'submenu' => [
42
                    ['text' => _("My institution is not listed"), 'catInfo' => ['idp_not_listed', _("FAQ")], 'visibility' => 'index'],
43
                    ['text' => _("My device is not listed"), 'catInfo' => ['device_not_listed', _("FAQ")], 'visibility' => 'index'],
44
                    ['text' => _("SB help item"), 'visibility' => 'sb', 'link'=>'xxx.php'],
45
                    ['text' => _("What is eduroam"), 'catInfo' => ['what_is_eduroam', _("FAQ")]],
46
                    ['text' => _("FAQ"), 'catInfo' => ['faq', _("FAQ")]],
47
                    ['text' => _("Contact"), 'catInfo' => ['contact', _("FAQ")]],
48
                    ['text' => _("Diagnostics"), 'link' => '/diag/diag.php'], 
49
                ]],
50
            ['id' => 'manage',
51
                'text' => _("Manage"), 'submenu' => [
52
                    ['text' => sprintf(_("%s admin access"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']),
53
                        'catInfo' => ['admin', sprintf(_("%s admin:<br>manage your IdP"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'])]],
54
                    ['text' => _("Become a CAT developer"),
55
                        'catInfo' => ['develop', _("Become a CAT developer")]],
56
                    ['text' => _("Documentation")],
57
                ],
58
                'visibility' => 'index'],
59
            ['id' => 'tou',
60
                'text' => _("Terms of use"), 'catInfo' => ['tou', 'TOU']],
61
        ];
62
        $this->visibility = $visibility;
63
    }
64
    public function printMenu($menu = NULL, $id = NULL) {
65
        $menu = $menu ?? $this->menu;
66
        if (count($menu) == 0) {
67
            return;
68
        }
69
        $out = "\n<ul>\n";
70
        foreach ($menu as $menuItem) {
71
            $itemVisibility = $menuItem['visibility'] ?? 'all';
72
            if ($this->visibility === 'all' || $itemVisibility === 'all' || $itemVisibility === $this->visibility) {
73
                $iD = $menuItem['id'] ?? $id;
74
                $catInfo = NULL;
75
                if (!empty($menuItem['catInfo'])) {
76
                    $catInfo = 'javascript:infoCAT("' . $iD . '", "' . $menuItem['catInfo'][0] . '","' . $menuItem['catInfo'][1] . '")';
77
                }
78
                if (!empty($menuItem['link']) && substr($menuItem['link'], 0, 1) === '/') {
79
                    $menuItem['link'] = \core\CAT::getRootUrlPath() . $menuItem['link'];
80
                }
81
                $link = $catInfo ?? $menuItem['link'] ?? CONFIG['PATHS']['cat_base_url'];
82
                $class = empty($menuItem['class']) ? '' : ' class="' . $menuItem['class'] . '"';
83
                $submenu = $menuItem['submenu'] ?? [];
84
                $out .= $this->printMenuItem($menuItem['text'], $link, $class);
85
                $out .= $this->printMenu($submenu, $iD);
86
                $out .= "</li>\n";
87
            }
88
        }
89
        $out .= '</ul>';
90
        return($out);
91
    }
92
93
    private function printMenuItem($itemText, $itemLink = '', $itemClass = '') {
94
        
95
        return "<li><a href='" . $itemLink . "'" . $itemClass . '>' . $itemText . "</a>";
96
    }
97
    
98
99
    private $menu;
100
    private $visibility;
101
}
102