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