xref: /plugin/structnotification/admin.php (revision e96b00ce614311bc1690b610d7467dc7a3323015)
1<?php
2/**
3 * DokuWiki Plugin structnotification (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Szymon Olewniczak <it@rid.pl>
7 */
8
9class admin_plugin_structnotification extends DokuWiki_Admin_Plugin
10{
11    protected $headers = ['schema', 'field', 'operator', 'value', 'filters', 'users_and_groups', 'message'];
12    protected $operators = ['before', 'after', 'at'];
13
14    /**
15     * @return int sort number in admin menu
16     */
17    public function getMenuSort()
18    {
19        return 499;
20    }
21
22    /**
23     * @return bool true if only access for superuser, false is for superusers and moderators
24     */
25    public function forAdminOnly()
26    {
27        return false;
28    }
29
30    /**
31     * Should carry out any processing required by the plugin.
32     */
33    public function handle()
34    {
35        global $INPUT;
36        global $ID;
37
38        try {
39            /** @var \helper_plugin_structnotification_db$db_helper */
40            $db_helper = plugin_load('helper', 'structnotification_db');
41            $sqlite = $db_helper->getDB();
42        } catch (Exception $e) {
43            msg($e->getMessage(), -1);
44            return;
45        }
46
47        if ($INPUT->str('action') && $INPUT->arr('predicate') && checkSecurityToken()) {
48            $predicate = $INPUT->arr('predicate');
49            if ($INPUT->str('action') === 'add') {
50                $errors = $this->validate($predicate);
51                if ($errors) {
52                    $this->display_errors($errors);
53                    return;
54                }
55                $ok = $sqlite->storeEntry('predicate', $predicate);
56                if(!$ok) msg('failed to add predicate', -1);
57            } elseif($INPUT->str('action') === 'delete') {
58                $ok = $sqlite->query('DELETE FROM predicate WHERE id=?', $predicate['id']);
59                if(!$ok) msg('failed to delete predicate', -1);
60            } elseif($INPUT->str('action') === 'update') {
61                $errors = $this->validate($predicate);
62                if ($errors) {
63                    $this->display_errors($errors);
64                    return;
65                }
66
67                $set = implode(',', array_map(function ($header) {
68                    return "$header=?";
69                }, $this->headers));
70                $predicate['id'] = $INPUT->str('edit');
71                $ok = $sqlite->query("UPDATE predicate SET $set WHERE id=?", $predicate);
72                if(!$ok) msg('failed to update predicate', -1);
73            }
74
75            if ($ok) send_redirect(wl($ID, array('do' => 'admin', 'page' => 'structnotification'), true, '&'));
76        }
77    }
78
79    /**
80     * Render HTML output, e.g. helpful text and a form
81     */
82    public function html()
83    {
84        global $INPUT;
85        global $ID;
86
87        try {
88            /** @var \helper_plugin_structnotification_db$db_helper */
89            $db_helper = plugin_load('helper', 'structnotification_db');
90            $sqlite = $db_helper->getDB();
91        } catch (Exception $e) {
92            msg($e->getMessage(), -1);
93            return;
94        }
95
96        ptln('<h1>' . $this->getLang('menu') . '</h1>');
97        ptln('<table>');
98
99        ptln('<tr>');
100        foreach ($this->headers as $header) {
101            ptln('<th>' . $this->getLang('admin header '. $header) . '</th>');
102        }
103        //extra field for buttons
104        ptln('<th></th>');
105        ptln('</tr>');
106
107        $q = 'SELECT * FROM predicate';
108        $res = $sqlite->query($q);
109
110        $predicates = $sqlite->res2arr($res);
111
112        foreach ($predicates as $predicate) {
113            if ($INPUT->str('edit') == $predicate['id']) {
114                if (!$INPUT->has('predicate')) {
115                    $INPUT->set('predicate', $predicate);
116                }
117
118                ptln($this->form('update'));
119                continue;
120            }
121
122            ptln('<tr>');
123            foreach ($this->headers as $header) {
124                $value = $predicate[$header];
125                if ($header == 'message') {
126                    $html = p_render('xhtml',p_get_instructions($value), $info);
127                    ptln('<td>' . $html . '</td>');
128                } else {
129                    ptln('<td>' . $value . '</td>');
130                }
131            }
132
133            ptln('<td>');
134            $link = wl(
135                $ID, array(
136                    'do' => 'admin',
137                    'page' => 'structnotification',
138                    'edit' => $predicate['id']
139                )
140            );
141            ptln('<a href="' . $link . '">'.$this->getLang('edit').'</a> | ');
142
143            $link = wl(
144                $ID, array(
145                    'do' => 'admin',
146                    'page' => 'structnotification',
147                    'action' => 'delete',
148                    'sectok' => getSecurityToken(),
149                    'predicate[id]' => $predicate['id']
150                )
151            );
152            ptln('<a class="plugin__structnotification_delete" href="' . $link . '">'.$this->getLang('delete').'</a>');
153
154            ptln('</td>');
155            ptln('</tr>');
156        }
157        if (!$INPUT->has('edit')) {
158            ptln($this->form());
159        }
160        ptln('</table>');
161    }
162
163    protected function form($action='add')
164    {
165        global $ID;
166
167        $form = new dokuwiki\Form\Form();
168        $form->addTagOpen('tr');
169
170        $form->addTagOpen('td');
171        $form->addTextInput('predicate[schema]')->attr('style', 'width: 8em');
172        $form->addTagClose('td');
173
174        $form->addTagOpen('td');
175        $form->addTextInput('predicate[field]')->attr('style', 'width: 8em');
176        $form->addTagClose('td');
177
178        $form->addTagOpen('td');
179        $form->addDropdown('predicate[operator]', $this->operators);
180        $form->addTagClose('td');
181
182        $form->addTagOpen('td');
183        $form->addTextInput('predicate[value]')->attr('style', 'width: 12em');
184        $form->addTagClose('td');
185
186        $form->addTagOpen('td');
187        $form->addTextarea('predicate[filters]')->attr('style', 'width: 12em; height: 5em;');
188        $form->addTagClose('td');
189
190        $form->addTagOpen('td');
191        $form->addTextInput('predicate[users_and_groups]')->attr('style', 'width: 12em');
192        $form->addTagClose('td');
193
194        $form->addTagOpen('td');
195        $form->addHTML('<div id="tool__bar"></div>');
196        $form->setHiddenField('id', $ID); //for linkwiz
197        $form->addTextarea('predicate[message]')
198            ->id('wiki__text')
199            ->attr('style', 'width: 100%; height: 10em;');
200        $form->addTagClose('td');
201
202        $form->addTagOpen('td');
203        $form->addButton('action', $this->getLang($action))->val($action);
204        $link = wl(
205            $ID, [
206                'do' => 'admin',
207                'page' => 'structnotification',
208            ]
209        );
210        $cancel_link = '<a href="' . $link . '" style="margin-left:1em" id="plugin__structnotification_cancel">' . $this->getLang('cancel') . '</a>';
211        $form->addHTML($cancel_link);
212        $form->addTagClose('td');
213
214
215        $form->addTagClose('tr');
216
217        return $form->toHTML();
218    }
219
220    protected function validate($predicate)
221    {
222        $errors = [];
223        if (blank($predicate['schema'])) {
224            $errors[] = 'val schema blank';
225        }
226
227        if (blank($predicate['field'])) {
228            $errors[] = 'val field blank';
229        }
230
231        if (blank($predicate['operator'])) {
232            $errors[] = 'val operator blank';
233        }
234
235        if (blank($predicate['value'])) {
236            $errors[] = 'val value blank';
237        }
238
239        if (blank($predicate['users_and_groups'])) {
240            $errors[] = 'val users_and_groups blank';
241        }
242
243        if (blank($predicate['message'])) {
244            $errors[] = 'val message blank';
245        }
246
247        return $errors;
248    }
249
250    protected function display_errors($errors) {
251        foreach ($errors as $error) {
252            $msg = $this->getLang($error);
253            if (!$msg) $msg = $error;
254            msg($error, -1);
255        }
256    }
257}
258
259