1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
<?php
# Lifter010: TODO
/**
* SkipLinks.php - API for global skip links
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Peter Thienel
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
/**
* The SkipLinks class provides utility functions to handle
* the integration of skip links.
*/
class SkipLinks
{
/**
* array of Skip links
* @var array
*/
private static $links = [];
/**
* array of positions of skip links
* @var array
*/
private static $position = [];
/**
* Returns whether skiplinks are enabled.
*
* @return boolean
*/
public static function isEnabled()
{
return UserConfig::get($GLOBALS['user']->id)->SKIPLINKS_ENABLE;
}
/**
* Inserts container for skip links in page layout.
*/
public static function insertContainer()
{
if (!self::isEnabled()) {
return;
}
PageLayout::addBodyElements('<div id="skip_link_navigation" aria-busy="true"></div>');
}
/**
* Adds a link to the list of skip links.
*
* @param string $name the displayed name of the links
* @param string $url the url of the links
* @param integer $position the position of the link in the list
* @param boolean $overwriteable false if position is not overwritable by another link
*/
public static function addLink($name, $url, $position = null, $overwriteable = false)
{
$position = (!$position || $position < 1) ? count(self::$links) + 100 : (int) $position;
$new_link = [
'name' => $name,
'url' => decodeHTML($url),
'position' => $position,
'overwriteable' => $overwriteable,
];
if (self::checkOverwrite($new_link)) {
self::$links[$new_link['url']] = $new_link;
}
}
/**
* Adds a link to an anker on the same page to the list of skip links.
*
* @param string $name the displayed name of the links
* @param string $id the id of the anker
* @param integer $position the position of the link in the list
* @param boolean $overwriteable false if position is not overwritable by another link
*/
public static function addIndex($name, $id, $position = null, $overwritable = false)
{
$url = '#' . $id;
self::addLink($name, $url, $position, $overwritable);
}
/**
* Returns the formatted list of skip links
*
* @return string the formatted list of skip links
*/
public static function getHTML()
{
if (!self::isEnabled() || count(self::$links) === 0) {
return '';
}
usort(self::$links, function ($a, $b) {
return $a['position'] - $b['position'];
});
$navigation = new Navigation('');
foreach (self::$links as $index => $link) {
$navigation->addSubNavigation(
"/skiplinks/link-{$index}",
new Navigation($link['name'], $link['url'])
);
}
return $GLOBALS['template_factory']->render('skiplinks', compact('navigation'));
}
/**
* Checks if there is another link at the same position and if it is overwritable.
*
* @return boolean true if the link at the same position is overwritable
*/
private static function checkOverwrite($link)
{
if (isset(self::$position[$link['position']])) {
return false;
}
if (!isset($link['overwrite']) || !$link['overwrite']) {
self::$position[$link['position']] = true;
}
return true;
}
}
|