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

    /**
     * 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, bool $inFullscreen = true)
    {
        $position = (!$position || $position < 1) ? count(self::$links) + 100 : (int) $position;
        self::$links[$url] = [
            'name'       => $name,
            'url'        => $url,
            'position'   => $position,
            'fullscreen' => $inFullscreen
        ];
    }

    /**
     * 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 bool    $inFullscreen is this link relevant in fullscreen mode?
     */
    public static function addIndex($name, $id, $position = null, bool $inFullscreen = true)
    {
        $url = '#' . $id;
        self::addLink($name, $url, $position, $inFullscreen);
    }

    /**
     * Returns the formatted list of skip links
     *
     * @return string the formatted list of skip links
     */
    public static function getHTML()
    {
        if (count(self::$links) === 0) {
            return '';
        }

        usort(self::$links, function ($a, $b) {
            return $a['position'] - $b['position'];
        });

        $navigation = new Navigation('');
        $fullscreen = [];
        foreach (array_values(self::$links) as $index => $link) {
            $navigation->addSubNavigation(
                "/skiplinks/link-{$index}",
                new Navigation($link['name'], $link['url'])
            );
            $fullscreen['/skiplinks/link-' . $index] = $link['fullscreen'];
        }
        return $GLOBALS['template_factory']->render('skiplinks', compact('navigation', 'fullscreen'));
    }
}