aboutsummaryrefslogtreecommitdiff
path: root/lib/navigation/AutoNavigation.php
blob: d91edfcf1162a8efef15b6d8c9ce2aa72de8e08e (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
<?php
# Lifter010: TODO
/*
 * AutoNavigation.php - Stud.IP auto navigation class
 *
 * 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      Elmar Ludwig
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */

class AutoNavigation extends Navigation
{
    /**
     * Determine whether this navigation item is active or not.
     * This implementation tries to guess it based on the request
     * URL and query parameters.
     *
     * @return boolean  true if item is active, false otherwise
     */
    public function isActive()
    {
        if (isset($this->active)) {
            return $this->active;
        }

        $url = $this->getURL();

        // if URL is set, try to guess whether active or not
        if (isset($url)) {
            $request_path = explode('?', Request::path())[0];
            $request_url  = explode('?', Request::url())[0];

            if (!preg_match('%^[a-z]+:%', $url) && $url[0] !== '/') {
                $url = $GLOBALS['CANONICAL_RELATIVE_PATH_STUDIP'].$url;
            }

            if ($url === $request_path || $url === $request_url) {
                $this->active = true;

                if (isset($this->params)) {
                    foreach ($this->params as $key => $val) {
                        if (Request::get($key) != $val) {
                            $this->active = false;
                        }
                    }
                }

                if ($this->active) {
                    return true;
                }
            }
        }

        return $this->active = (boolean) $this->activeSubNavigation();
    }
}