1<?php
2/**
3 * Talkpage Plugin: places talkpage icon in pagetools
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Blake Martin
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 */
9
10/**
11 * Add the template as a page dependency for the caching system
12 */
13class action_plugin_talkpage extends DokuWiki_Action_Plugin
14{
15    /** @inheritdoc */
16    function register(Doku_Event_Handler $controller)
17    {
18        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addLegacyButton');
19        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuButton');
20    }
21
22    /**
23     * Add talk page to the old menu
24     *
25     * @param Doku_Event $event
26     * @return bool
27     */
28    public function addLegacyButton(Doku_Event $event)
29    {
30        if (!$this->getConf('showbutton')) return false;
31        if (!$event->data['view'] == 'main') return false;
32
33        /** @var syntax_plugin_talkpage $helper */
34        $helper = plugin_load('syntax', 'talkpage');
35        $data = $helper->getLink();
36        $data['attr']['class'] .= ' action';
37
38        $link = '<li><a ' . buildAttributes($data['attr']) . '><span>' . $data['text'] . '</span></a></li>';
39
40        // insert at second position
41        $event->data['items'] = array_slice($event->data['items'], 0, 1, true) +
42            array('talkpage' => $link) +
43            array_slice($event->data['items'], 1, null, true);
44
45        return true;
46    }
47
48    /**
49     * Add Talkpage to the new menu system
50     *
51     * @param Doku_Event $event
52     * @return bool
53     */
54    public function addMenuButton(Doku_Event $event)
55    {
56        if (!$this->getConf('showbutton')) return false;
57        if ($event->data['view'] !== 'page') return false;
58        array_splice($event->data['items'], 1, 0, [new \dokuwiki\plugin\talkpage\MenuItem()]);
59        return true;
60    }
61}
62