1<?php
2/**
3 * Userpage Plugin: places userpage link in usertools
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Blake Martin
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 * @author     Anika Henke <anika@selfthinker.org>
9 */
10
11class action_plugin_userpage extends DokuWiki_Action_Plugin
12{
13    /** @inheritdoc */
14    function register(Doku_Event_Handler $controller)
15    {
16        $controller->register_hook('TEMPLATE_USERTOOLS_DISPLAY', 'BEFORE', $this, 'addLegacyMenuLink');
17        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuLink');
18    }
19
20    /**
21     * Get all relevant data to build link to the user page
22     *
23     * @return array text, url and attributes for the link
24     */
25    function getLinkData() {
26        global $INFO;
27        global $conf;
28        global $ACT;
29
30        $userPage = $this->getConf('userPage');
31        $userPage = str_replace('@USER@', $_SERVER['REMOTE_USER'], $userPage);
32        if (substr($userPage, -strlen(':')) === ':') {
33            $userPage = $userPage.$conf['start'];
34        }
35
36        $title = $this->getLang('userpage');
37        // highlight when on user page (only works with old menu)
38        $activeClass = ($userPage == $INFO['id'] && act_clean($ACT) == 'show') ? ' active' : '';
39
40        $attr = array();
41        $attr['href'] = wl($userPage);
42        $attr['class'] = 'userpage'.$activeClass;
43
44        return array(
45            'goto' => $userPage,
46            'text' => $title,
47            'attr' => $attr,
48        );
49    }
50
51    /**
52     * Add user page to the old menu (before Greebo)
53     *
54     * @param Doku_Event $event
55     * @return bool
56     */
57    public function addLegacyMenuLink(Doku_Event $event)
58    {
59        if (empty($_SERVER['REMOTE_USER'])) return false;
60        if (!$event->data['view'] == 'main') return false;
61
62        $data = $this->getLinkData();
63        $data['attr']['class'] .= ' action';
64
65        $link = '<li><a '.buildAttributes($data['attr']).'><span>'.$data['text'].'</span></a></li>';
66
67        // insert at second position
68        $event->data['items'] = array_slice($event->data['items'], 0, 1, true) +
69            array('userpage' => $link) +
70            array_slice($event->data['items'], 1, null, true);
71
72        return true;
73    }
74
75    /**
76     * Add user page to the menu system
77     *
78     * @param Doku_Event $event
79     * @return bool
80     */
81    public function addMenuLink(Doku_Event $event)
82    {
83        if (empty($_SERVER['REMOTE_USER'])) return false;
84        if ($event->data['view'] !== 'user') return false;
85
86        array_splice($event->data['items'], 1, 0, [new \dokuwiki\plugin\userpage\MenuItem()]);
87
88        return true;
89    }
90}
91