1<?php
2
3/**
4 * DokuWiki Plugin submgr (Admin Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr <dokuwiki@cosmocode.de>
8 */
9class admin_plugin_submgr extends DokuWiki_Admin_Plugin
10{
11
12    /** @var helper_plugin_submgr */
13    protected $hlp;
14
15    /**
16     * admin_plugin_submgr constructor.
17     */
18    public function __construct()
19    {
20        $this->hlp = plugin_load('helper', 'submgr');
21    }
22
23    /**
24     * @return bool true if only access for superuser, false is for superusers and moderators
25     */
26    public function forAdminOnly()
27    {
28        return false;
29    }
30
31    /**
32     * Should carry out any processing required by the plugin.
33     */
34    public function handle()
35    {
36        global $INPUT;
37        global $ID;
38
39        $url = wl($ID, array('do' => 'admin', 'page' => 'submgr'), true, '&');
40
41        if ($INPUT->has('d')) {
42            try {
43                $new = $INPUT->arr('d');
44                $this->hlp->addRule($new['item'], $new['type'], $new['members']);
45                send_redirect($url);
46            } catch (Exception $e) {
47                msg($e->getMessage(), -1);
48            }
49        }
50
51        if ($INPUT->has('rm')) {
52            try {
53                $this->hlp->removeRule($INPUT->str('rm'));
54                send_redirect($url);
55            } catch (Exception $e) {
56                msg($e->getMessage(), -1);
57            }
58        }
59
60    }
61
62    /**
63     * Render HTML output, e.g. helpful text and a form
64     */
65    public function html()
66    {
67        global $ID;
68
69        $url = wl($ID, array('do' => 'admin', 'page' => 'submgr'));
70
71        echo $this->locale_xhtml('intro');
72
73        if (!$this->checkSettings()) return;
74
75        echo '<h2>' . $this->getLang('rules') . '</h2>';
76
77        echo '<table>';
78        echo '<tr>';
79        echo '<th>' . $this->getLang('item') . '</th>';
80        echo '<th>' . $this->getLang('members') . '</th>';
81        echo '<th>' . $this->getLang('type') . '</th>';
82        echo '<th></th>';
83        echo '</tr>';
84
85        foreach ($this->hlp->getRules() as $item => $data) {
86            echo '<tr>';
87            echo '<td>' . hsc($item) . '</td>';
88            echo '<td>' . hsc($data[1]) . '</td>';
89            echo '<td>' . $this->getLang($data[0]) . '</td>';
90            echo '<td>';
91
92            echo '<form method="post" action="' . $url . '">';
93            echo '<input type="hidden" name="rm" value="' . hsc($item) . '" />';
94            echo '<button type="submit">' . $this->getLang('remove') . '</button>';
95            echo '</form>';
96
97            echo '</td>';
98            echo '</tr>';
99        }
100
101        echo '</table>';
102
103        echo '<h2>' . $this->getLang('legend') . '</h2>';
104
105        echo '<form method="post" action="' . $url . '" class="plugin_submgr">';
106        echo '<fieldset>';
107        echo '<label class="block"><span>' . $this->getLang('item') . '</span> <input type="text" name="d[item]" /></label>';
108        echo '<label class="block"><span>' . $this->getLang('members') . '</span> <input type="text" name="d[members]" /></label>';
109        echo '<label class="block"><span>' . $this->getLang('type') . '</span>
110                <select name="d[type]">
111                    <option value="every">' . $this->getLang('every') . '</option>
112                    <option value="digest">' . $this->getLang('digest') . '</option>
113                    <option value="list">' . $this->getLang('list') . '</option>
114                </select>
115              </label>';
116        echo '<button type="submit">' . $this->getLang('add') . '</button>';
117        echo '</fieldset>';
118        echo '</form>';
119
120        echo $this->locale_xhtml('help');
121
122    }
123
124    /**
125     * Check capabilities and print errors
126     *
127     * @return bool
128     */
129    protected function checkSettings()
130    {
131        /** @var DokuWiki_Auth_Plugin $auth */
132        global $auth;
133        $ok = true;
134
135        if (!actionOK('subscribe')) {
136            echo $this->locale_xhtml('nosubs');
137            $ok = false;
138        }
139
140        if (!$auth->canDo('getUsers')) {
141            echo $this->locale_xhtml('nousers');
142            $ok = false;
143        }
144
145        return $ok;
146    }
147}
148