Test Setup Failed
Push — release_2_0 ( c5cba9...f39600 )
by Stefan
11:39
created

Menu::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 44
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
c 0
b 0
f 0
dl 0
loc 44
rs 9.344
cc 3
nc 3
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 22.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
36
     * @param string $selectedLang
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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) {
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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 = '') {
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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() {
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
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;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
128
    private $visibility;
0 ignored issues
show
Coding Style Documentation introduced by
Missing member variable doc comment
Loading history...
129
}
130