1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\Event;
5use dokuwiki\Extension\EventHandler;
6
7/**
8 * DokuWiki Plugin golocal (Action Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author Andreas Gohr <dokuwiki@cosmocode.de>
12 */
13class action_plugin_golocal extends ActionPlugin
14{
15    /** @inheritDoc */
16    public function register(EventHandler $controller)
17    {
18        $controller->register_hook('INIT_LANG_LOAD', 'AFTER', $this, 'handleInitLangLoad');
19
20        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handleActionActPreprocess');
21        $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handleTplActUnknown');
22    }
23
24    /**
25     * Event handler for INIT_LANG_LOAD
26     *
27     * @see https://www.dokuwiki.org/devel:events:init_lang_load
28     * @param Event $event Event object
29     * @param mixed $param optional parameter passed when event was registered
30     * @return void
31     */
32    public function handleInitLangLoad(Event $event, $param)
33    {
34        global $lang;
35        $lang['js']['nosmblinks'] = '';
36    }
37
38    /**
39     * Event handler for ACTION_ACT_PREPROCESS
40     *
41     * @see https://www.dokuwiki.org/devel:events:action_act_preprocess
42     * @param Event $event Event object
43     * @param mixed $param optional parameter passed when event was registered
44     * @return void
45     */
46    public function handleActionActPreprocess(Event $event, $param)
47    {
48        if ($event->data !== 'golocal') return;
49        $event->preventDefault();
50        $event->stopPropagation();
51    }
52
53    public function handleTplActUnknown(Event $event, $param)
54    {
55        if ($event->data !== 'golocal') return;
56        $event->preventDefault();
57        $event->stopPropagation();
58
59        $output = $this->locale_xhtml('download');
60        $output = str_replace('DOWNLOADSHERE', $this->getDownloadLinks(), $output);
61        echo $output;
62    }
63
64    /**
65     * Build the list of downloads
66     *
67     * @return string
68     * @todo this could be a syntax component
69     * @todo this could refer to the release matching the installed version
70     */
71    protected function getDownloadLinks()
72    {
73        $oslist = ['windows', 'linux', 'macos'];
74
75        $html = '<ul class="golocal-download">';
76        foreach ($oslist as $os) {
77            $file = 'golocal-' . $os;
78            $file .= $os === 'windows' ? '.exe' : '';
79            $url = 'https://github.com/cosmocode/dokuwiki-plugin-golocal/releases/latest/download/' . $file;
80
81            $classes = implode(' ', ['li', 'os-' . $os]);
82
83            $html .= '<li><div class="' . $classes . '">';
84            $html .= inlineSVG(__DIR__ . '/icons/' . $os . '.svg');
85            $html .= '<a href="' . $url . '">' . $file . '</a>';
86            $html .= '</div></li>';
87        }
88        $html .= '</ul>';
89
90        return $html;
91    }
92}
93