Passed
Push — master ( bc7ecd...4efa95 )
by Stefan
07:48
created

Menu::printMinimalMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * *****************************************************************************
4
 * Contributions to this work were made on behalf of the GÉANT project, a 
5
 * project that has received funding from the European Union’s Framework 
6
 * Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus),
7
 * Horizon 2020 research and innovation programme under Grant Agreements No. 
8
 * 691567 (GN4-1) and No. 731122 (GN4-2).
9
 * On behalf of the aforementioned projects, GEANT Association is the sole owner
10
 * of the copyright in all material which was developed by a member of the GÉANT
11
 * project. GÉANT Vereniging (Association) is registered with the Chamber of 
12
 * Commerce in Amsterdam with registration number 40535155 and operates in the 
13
 * UK as a branch of GÉANT Vereniging.
14
 * 
15
 * Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. 
16
 * UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK
17
 *
18
 * License: see the web/copyright.inc.php file in the file structure or
19
 *          <base_url>/copyright.php after deploying the software
20
 */
21
namespace web\skins\modern;
22
require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/config/_config.php";
23
24
/**
25
 * Menu class helps to define the menu on the main page
26
 */
27
class Menu {
28
29
    /**
30
     * the constructor takes an array argument defining menu items.
31
     * the array must be indexed by strings which will be passed to user/cat_info.php a the page argument
32
     * the values of the array can be either a simple string which is passed to user/cat_info.php
33
     * as the title argument or an two element array - the first element of this array will be
34
     * the title and the second is a style specification applied to the given menu item
35
     * @param string $visibility
36
     * @param string $selectedLang
37
     */
38
    public function __construct($visibility = 'all', $selectedLang = '') {
39
        $langsArray = [];
40
        foreach (CONFIG['LANGUAGES'] as $lang => $value) {
41
            if ($lang == $selectedLang) {
42
                $langsArray[] = ['text'=>$value['display'], 'link'=>'javascript:changeLang("' . $lang . '")', 'class'=>'selected-lang'];
43
            } else {
44
                $langsArray[] = ['text'=>$value['display'], 'link'=>'javascript:changeLang("' . $lang . '")'];
45
            }
46
        }
47
        $this->menu = [['id' => 'start',
48
        'text' => _("Start page"), 'link' => '/',
49
        'visibility' => 'index'],
50
            ['id' => 'about',
51
                'text' => _("About"), 'link' => '', 'submenu' => [
52
                    ['text' => sprintf(_("About %s"), CONFIG['APPEARANCE']['productname']),
53
                        'catInfo' => ['about_cat', sprintf(_("About %s"), CONFIG['APPEARANCE']['productname'])]],
54
                    ['text' => sprintf(_("About %s"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']),
55
                        'link' => CONFIG_CONFASSISTANT['CONSORTIUM']['homepage']],
56
                ]],
57
            ['id' => 'lang',
58
                'text' => _("Language"), 'submenu' => $langsArray, ],
59
            ['id' => 'help',
60
                'text' => _("Help"), 'submenu' => [
61
                    ['text' => _("My institution is not listed"), 'catInfo' => ['idp_not_listed', _("FAQ")], 'visibility' => 'index'],
62
                    ['text' => _("My device is not listed"), 'catInfo' => ['device_not_listed', _("FAQ")], 'visibility' => 'index'],
63
                    ['text' => \core\ProfileSilverbullet::PRODUCTNAME._("Help"), 'visibility' => 'sb', 'link'=> CONFIG_CONFASSISTANT['SILVERBULLET']['documentation']],
64
                    ['text' => _("What is eduroam"), 'catInfo' => ['what_is_eduroam', _("FAQ")]],
65
                    ['text' => _("FAQ"), 'catInfo' => ['faq', _("FAQ")]],
66
                    ['text' => _("Contact"), 'catInfo' => ['contact', _("FAQ")]],
67
                    ['text' => _("Diagnostics"), 'link' => '/diag/diag.php'], 
68
//                    ['text' => _("Documentation"), 'link' => '/apidoc' ],
69
                ]],
70
            ['id' => 'manage',
71
                'text' => _("Manage"), 'submenu' => [
72
                    ['text' => sprintf(_("%s admin access"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name']),
73
                        'catInfo' => ['admin', sprintf(_("%s admin:<br>manage your IdP"), CONFIG_CONFASSISTANT['CONSORTIUM']['display_name'])]],
74
                    ['text' => _("Become a CAT developer"),
75
                        'catInfo' => ['develop', _("Become a CAT developer")]],
76
                ],
77
                'visibility' => 'index'],
78
            ['id' => 'tou',
79
                'text' => _("Terms of use"), 'catInfo' => ['tou', 'TOU']],
80
        ];
81
        $this->visibility = $visibility;
82
    }
83
    public function printMenu($menu = NULL, $id = NULL) {
84
        $menu = $menu ?? $this->menu;
85
        if (count($menu) == 0) {
86
            return;
87
        }
88
        $out = "\n<ul>\n";
89
        foreach ($menu as $menuItem) {
90
            $itemVisibility = $menuItem['visibility'] ?? 'all';
91
            if ($this->visibility === 'all' || $itemVisibility === 'all' || $itemVisibility === $this->visibility) {
92
                $iD = $menuItem['id'] ?? $id;
93
                $catInfo = NULL;
94
                if (!empty($menuItem['catInfo'])) {
95
                    $catInfo = 'javascript:infoCAT("' . $iD . '", "' . $menuItem['catInfo'][0] . '","' . $menuItem['catInfo'][1] . '")';
96
                }
97
                if (!empty($menuItem['link']) && substr($menuItem['link'], 0, 1) === '/') {
98
                    $menuItem['link'] = \core\CAT::getRootUrlPath() . $menuItem['link'];
99
                }
100
                $link = $catInfo ?? $menuItem['link'] ?? '';
101
                $class = empty($menuItem['class']) ? '' : ' class="' . $menuItem['class'] . '"';
102
                $submenu = $menuItem['submenu'] ?? [];
103
                $out .= $this->printMenuItem($menuItem['text'], $link, $class);
104
                $out .= $this->printMenu($submenu, $iD);
105
                $out .= "</li>\n";
106
            }
107
        }
108
        $out .= '</ul>';
109
        return($out);
110
    }
111
112
    private function printMenuItem($itemText, $itemLink = '', $itemClass = '') {
113
        
114
        if ($itemLink === '') {
115
            return("<li><span>$itemText</span>");
116
        }
117
        return "<li><a href='" . $itemLink . "'" . $itemClass . '>' . $itemText . "</a>";
118
    }
119
    
120
    public function printMinimalMenu() {
121
        $out = "\n<ul>\n";
122
        $out .= $this->printMenuItem(_("Start page"), \core\CAT::getRootUrlPath());
123
        $out .= '</ul>';
124
        return($out);
125
    }
126
127
    private $menu;
128
    private $visibility;
129
}
130