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