aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ResponsiveHelper.php
blob: eb7f41c636c9e50d88f08655c9b5c9b4cbf0affa (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
111
112
113
114
115
116
<?php
/**
 * ResponsiveHelper.php
 *
 * This class collects helper methods for Stud.IP's responsive design.
 *
 * @author    Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license   GPL2 or any later version
 * @copyright Stud.IP core group
 * @since     Stud.IP 3.2
 */
class ResponsiveHelper
{
    /**
     * Returns the current navigation as an array.
     *
     * @return Array containing the navigation
     */
    public static function getNavigationArray()
    {
        $navigation = [];
        $activated  = [];

        $link_params = array_fill_keys(array_keys(URLHelper::getLinkParams()), null);

        foreach (Navigation::getItem('/')->getSubNavigation() as $path => $nav) {
            if (!$nav->isVisible(true)) {
                continue;
            }

            $image = $nav->getImage();
            $image_src = $image ? $image->copyWithRole('info_alt')->asImagePath() : false;
            $item = [
                'icon'   => $image_src ? self::getAssetsURL($image_src) : false,
                'title'  => (string) $nav->getTitle(),
                'url'    => self::getURL($nav->getURL(), $link_params),
            ];

            if ($nav->isActive()) {
                $activated[] = $path;
            }

            if ($nav->getSubnavigation()  && $path != 'start') {
                $item['children'] = self::getChildren($nav, $path, $activated);
            }

            $navigation[$path] = $item;
        }

        return [$navigation, $activated];
    }

    /**
     * Recursively build a navigation array from the subnavigation/children
     * of a navigation object.
     *
     * @param Navigation $navigation The navigation object
     * @param String     $path       Current path segment
     * @param array      $activated  Activated items
     * @return Array containing the children (+ grandchildren...)
     */
    protected static function getChildren(Navigation $navigation, $path, &$activated = [])
    {
        $children = [];

        foreach ($navigation->getSubNavigation() as $subpath => $subnav) {
            if (!$subnav->isVisible()) {
                continue;
            }

            $subpath = "{$path}/{$subpath}";

            $item = [
                'title' => (string) $subnav->getTitle(),
                'url'   => self::getURL($subnav->getURL()),
            ];

            if ($subnav->isActive()) {
                $activated[] = $subpath;
            }

            if ($subnav->getSubNavigation()) {
                $item['children'] = self::getChildren($subnav, $subpath);
            }

            $children[$subpath] = $item;
        }

        return $children;
    }

    /**
     * Try to get a compressed version of the passed navigation url.
     * The URL is processed is processed by URLHelper and the absolute uri
     * of the Stud.IP installation is stripped from it afterwards.
     *
     * @param  String $url The url to compress
     * @return String containing the compressed url
     */
    protected static function getURL($url, $params = [])
    {
        return str_replace($GLOBALS['ABSOLUTE_URI_STUDIP'], '', URLHelper::getURL($url, $params));
    }

    /**
     * Try to get a compressed version of the passed assets url.
     * The absolute uri of the Stud.IP installation is stripped from the url.
     *
     * @param  String $url The assets url to compress
     * @return String containing the compressed assets url
     */
    protected static function getAssetsURL($url)
    {
        return str_replace($GLOBALS['ASSETS_URL'], '', $url);
    }
}