aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/sidebar/Sidebar.php
blob: ba1e609d2c1673f7ffc10158ae187f11f5e91ab9 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
 * The sidebar supersedes the pretty static infobox of Stud.IP.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL 2 or later
 * @since   3.1
 */
class Sidebar extends WidgetContainer
{
    protected $title = false;
    protected $context_avatar = null;

    /**
     * Constructor, tries to automagically set the sidebar's title.
     */
    protected function __construct()
    {
        $this->setTitle();
    }

    /**
     * Set a title of the sidebar.
     *
     * @param String $title The title of the sidebar.
     */
    public function setTitle($title = true)
    {
        $this->title = $title;
    }

    /**
     * Returns the title of the sidebar.
     *
     * @return mixed Either the previously set title or false if no title has
     *               been set
     */
    public function getTitle()
    {
        if ($this->title === true) {
            $breadcrumbs = $this->getBreadCrumbs();
            if (count($breadcrumbs) >= 2) {
                $nav = array_slice($breadcrumbs, 1, 1);
                $nav = reset($nav);
                $this->title = $nav->getTitle();
            } else {
                $this->title = false;
            }
        }
        return $this->title;
    }

    /**
     * Removes a previously set title.
     */
    public function removeTitle()
    {
        $this->title = false;
    }

    /**
     * Sets an avatar as a context-indicator. For example in a course a course-
     * avatar will indicate which course teh user is navigating in.
     * @param Avatar $avatar : the avatar object of the context
     */
    public function setContextAvatar(Avatar $avatar)
    {
        $this->context_avatar = $avatar;
    }

    /**
     * Removes a previously set context-indicator.
     */
    public function removeContextAvatar()
    {
        $this->context_avatar = null;
    }


    /**
     * Renders the sidebar.
     * The sidebar will only be rendered if it actually contains any widgets.
     * It will use the template "sidebar.php" located at "templates/sidebar".
     * A notification is dispatched before and after the actual rendering
     * process.
     *
     * @return String The HTML code of the rendered sidebar.
     */
    public function render()
    {
        $content = '';

        if ($this->context_avatar === null) {
            $breadcrumbs = $this->getBreadCrumbs();
            $keys = array_keys($breadcrumbs);
            $main_navigation = reset($keys);
            if ($main_navigation === 'course') {
                $course = Course::findCurrent();
                if ($course) {
                    if ($course->getSemClass()->offsetGet('studygroup_mode')) {
                        $avatar = StudygroupAvatar::getAvatar($course->id);
                    } else {
                        $avatar = CourseAvatar::getAvatar($course->id);
                    }
                } else {
                    $institute = Institute::findCurrent();
                    $avatar = InstituteAvatar::getAvatar($institute->id);
                }
                $this->setContextAvatar($avatar);
            }
            if ($main_navigation === 'profile') {
                if ($keys[1] !== "index") {
                    $user = Request::get("username")
                        ? User::findByUsername(Request::get("username"))
                        : User::findCurrent();
                    $this->setContextAvatar(Avatar::getAvatar($user->id));
                }
            }
        }

        NotificationCenter::postNotification('SidebarWillRender', $this);

        $template = $GLOBALS['template_factory']->open('sidebar/sidebar');

        if ($this->hasWidgets()) {
            $template->widgets = $this->widgets;
            foreach ($template->widgets as $widget) {
                $this->setupSkipLinks($widget);
            }
        } else {
            $template->widgets = [];
        }

        $title = PageLayout::getTitle();
        if (Context::getHeaderLine()) {
            $needles = [
                Context::getHeaderLine() . ' - ',
                '- ' . Context::getHeaderLine(),
                Context::getHeaderLine() . ': ',
                Context::getHeaderLine(),
            ];
            $title = str_replace($needles, '', $title);
        }
        $title = ucfirst(trim($title));

        $template->title  = $title;
        $template->avatar = $this->context_avatar;
        $content = $template->render();

        NotificationCenter::postNotification('SidebarDidRender', $this);

        return $content;
    }

    /**
     * Returns a breadcrumb path of the currently active navigation.
     *
     * @return Array List of currently active navigation items.
     */
    private function getBreadCrumbs($navigation = null)
    {
        if ($navigation === null) {
            $navigation = Navigation::getItem('/');
        }

        $breadcrumbs = [];
        foreach ($navigation as $idx => $nav) {
            if ($nav->isActive()) {
                $breadcrumbs[$idx] = $nav;
                if ($nav->activeSubnavigation()) {
                    $breadcrumbs = array_merge($breadcrumbs, $this->getBreadCrumbs($nav));
                }
            }
        }
        return $breadcrumbs;
    }

    /**
     * Inspects a widget and will set appropriate skiplinks.
     *
     * @param Widget $widget
     */
    protected function setupSkipLinks(Widget $widget): void
    {
        static $navigation_widget_added = false;
        static $actions_widget_added = false;


        if ($widget instanceof NavigationWidget && !$navigation_widget_added && $widget->hasElements()) {
            SkipLinks::addIndex(
                _('Dritte Navigationsebene'),
                $widget->getId(),
                20,
                false
            );

            $navigation_widget_added = true;
        }

        if ($widget instanceof ActionsWidget && !$actions_widget_added && $widget->hasElements()) {
            if (!$widget->getId()) {
                $widget->setId('sidebar-actions');
            }
            SkipLinks::addIndex(
                _('Aktionen'),
                $widget->getId(),
                21,
                false
            );

            $actions_widget_added = true;
        }
    }
}