1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4
5/**
6 * DokuWiki Plugin farmer (Admin Component)
7 *
8 * This is the main admin page. It displays the tabs and then loads the sub components
9 * according to the selected tab
10 *
11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12 * @author  Michael Große <grosse@cosmocode.de>
13 * @author  Andreas Gohr <gohr@cosmocode.de>
14 */
15class admin_plugin_farmer extends AdminPlugin
16{
17    /** @var helper_plugin_farmer */
18    protected $helper;
19    /** @var array The available pages for the current user in the current wiki */
20    protected $pages;
21    /** @var string The currently selected page */
22    protected $page;
23    /** @var AdminPlugin the plugin to use for the current page */
24    protected $adminplugin;
25
26    /**
27     * @return bool we're available for managers and admins
28     */
29    public function forAdminOnly()
30    {
31        return false;
32    }
33
34    /**
35     * Initialize current page
36     */
37    public function __construct()
38    {
39        global $INPUT;
40        $this->helper = plugin_load('helper', 'farmer');
41
42        // set available pages depending on user and animal
43        $isanimal = (bool) $this->helper->getAnimal();
44        if ($isanimal || !auth_isadmin()) {
45            $this->pages = ['info'];
46        } elseif (!$this->helper->checkFarmSetup()) {
47            $this->pages = ['setup'];
48        } else {
49            $this->pages = ['info', 'config', 'new', 'plugins', 'delete'];
50        }
51
52        // make sure current page requested is available
53        $this->page = $INPUT->str('sub');
54        if (!in_array($this->page, $this->pages)) {
55            $this->page = $this->pages[0];
56        }
57
58        // load the sub component
59        $this->adminplugin = plugin_load('admin', 'farmer_' . $this->page);
60        if (!$this->adminplugin) nice_die('Something went wrong loading the plugin component for ' . hsc($this->page));
61    }
62
63    /**
64     * handle user request
65     */
66    public function handle()
67    {
68        $this->adminplugin->handle();
69    }
70
71    /**
72     * output appropriate tab
73     */
74    public function html()
75    {
76        global $ID;
77
78        echo '<div id="plugin__farmer_admin">';
79        echo '<h1>' . $this->getLang('menu') . '</h1>';
80
81        echo '<ul class="tabs" id="plugin__farmer_tabs">';
82        foreach ($this->pages as $page) {
83            $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => $page]);
84            $class = ($page == $this->page) ? 'active' : '';
85
86            echo '<li class="' . $class . '"><a href="' . $link . '">' . $this->getLang('tab_' . $page) . '</a></li>';
87        }
88        echo '</ul>';
89        echo '<div class="panelHeader">';
90        echo $this->locale_xhtml('tab_' . $this->page);
91        echo '</div>';
92        echo '<div class="panelMain">';
93        $this->adminplugin->html();
94        echo '</div>';
95        echo '<div class="panelFooter">';
96        echo $this->locale_xhtml('tab_' . $this->page . '_help');
97        echo '</div>';
98        echo '</div>';
99    }
100
101    /**
102     * @return int
103     */
104    public function getMenuSort()
105    {
106        return 42;
107    }
108}
109