1<?php
2
3namespace dokuwiki\Extension;
4
5/**
6 * Admin Plugin Prototype
7 *
8 * Implements an admin interface in a plugin
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Christopher Smith <chris@jalakai.co.uk>
12 */
13abstract class AdminPlugin extends Plugin
14{
15    /**
16     * Return the text that is displayed at the main admin menu
17     * (Default localized language string 'menu' is returned, override this function for setting another name)
18     *
19     * @param string $language language code
20     * @return string menu string
21     */
22    public function getMenuText($language)
23    {
24        $menutext = $this->getLang('menu');
25        if (!$menutext) {
26            $info = $this->getInfo();
27            $menutext = $info['name'] . ' ...';
28        }
29        return $menutext;
30    }
31
32    /**
33     * Return the path to the icon being displayed in the main admin menu.
34     * By default it tries to find an 'admin.svg' file in the plugin directory.
35     * (Override this function for setting another image)
36     *
37     * Important: you have to return a single path, monochrome SVG icon! It has to be
38     * under 2 Kilobytes!
39     *
40     * We recommend icons from https://materialdesignicons.com/ or to use a matching
41     * style.
42     *
43     * @return string full path to the icon file
44     */
45    public function getMenuIcon()
46    {
47        $plugin = $this->getPluginName();
48        return DOKU_PLUGIN . $plugin . '/admin.svg';
49    }
50
51    /**
52     * Determine position in list in admin window
53     * Lower values are sorted up
54     *
55     * @return int
56     */
57    public function getMenuSort()
58    {
59        return 1000;
60    }
61
62    /**
63     * Return true if the plugin should be shown in the admin menu
64     *
65     * @return bool
66     */
67    public function showInMenu()
68    {
69        return true;
70    }
71
72    /**
73     * Carry out required processing
74     */
75    public function handle()
76    {
77        // some plugins might not need this
78    }
79
80    /**
81     * Output html of the admin page
82     */
83    abstract public function html();
84
85    /**
86     * Checks if access should be granted to this admin plugin
87     *
88     * @return bool true if the current user may access this admin plugin
89     */
90    public function isAccessibleByCurrentUser()
91    {
92        $data = [];
93        $data['instance'] = $this;
94        $data['hasAccess'] = false;
95
96        $event = new Event('ADMINPLUGIN_ACCESS_CHECK', $data);
97        if ($event->advise_before()) {
98            if ($this->forAdminOnly()) {
99                $data['hasAccess'] = auth_isadmin();
100            } else {
101                $data['hasAccess'] = auth_ismanager();
102            }
103        }
104        $event->advise_after();
105
106        return $data['hasAccess'];
107    }
108
109    /**
110     * Return true for access only by admins (config:superuser) or false if managers are allowed as well
111     *
112     * @return bool
113     */
114    public function forAdminOnly()
115    {
116        return true;
117    }
118
119    /**
120     * Return array with ToC items. Items can be created with the html_mktocitem()
121     *
122     * @see html_mktocitem()
123     * @see tpl_toc()
124     *
125     * @return array
126     */
127    public function getTOC()
128    {
129        return [];
130    }
131}
132