aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/SkipLinks.php
blob: 684ae424189dcbd8dd93e296a343275c5b49c58c (plain)
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
<?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 = [];

    /**
     * Returns whether skiplinks are enabled.
     *
     * @return boolean
     */
    public static function isEnabled()
    {
        if (isset($GLOBALS['user']->id)) {
            //Use the user configuration:
            return UserConfig::get($GLOBALS['user']->id)->SKIPLINKS_ENABLE;
        } else {
            //Use the global configuration:
            return Config::get()->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
     */
    public static function addLink(string $name, string $url, $position = null)
    {
        $position = (!$position || $position < 1) ? count(self::$links) + 100 : (int) $position;
        self::$links[$url] = [
            'name'     => $name,
            'url'      => $url,
            'position' => $position,
        ];
    }

    /**
     * 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
     */
    public static function addIndex($name, $id, $position = null)
    {
        $url = '#' . $id;
        self::addLink($name, $url, $position);
    }

    /**
     * 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 (array_values(self::$links) as $index => $link) {
            $navigation->addSubNavigation(
                "/skiplinks/link-{$index}",
                new Navigation($link['name'], $link['url'])
            );
        }
        return $GLOBALS['template_factory']->render('skiplinks', compact('navigation'));
    }
}