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 '<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>'; 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('style', 'width: 8em'); 171 $form->addTagClose('td'); 172 173 $form->addTagOpen('td'); 174 $form->addTextInput('predicate[field]')->attr('style', 'width: 8em'); 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('style', 'width: 12em'); 183 $form->addTagClose('td'); 184 185 $form->addTagOpen('td'); 186 $form->addTextarea('predicate[filters]')->attr('style', 'width: 12em; height: 5em;'); 187 $form->addTagClose('td'); 188 189 $form->addTagOpen('td'); 190 $form->addTextInput('predicate[users_and_groups]')->attr('style', 'width: 12em'); 191 $form->addTagClose('td'); 192 193 $form->addTagOpen('td'); 194 $form->addHTML('<div id="tool__bar"></div>'); 195 $form->setHiddenField('id', $ID); //for linkwiz 196 $form->addTextarea('predicate[message]') 197 ->id('wiki__text') 198 ->attr('style', 'width: 100%; height: 10em;'); 199 $form->addTagClose('td'); 200 201 $form->addTagOpen('td'); 202 $form->addButton('action', $this->getLang($action))->val($action); 203 $link = wl( 204 $ID, 205 [ 206 'do' => 'admin', 207 'page' => 'structnotification', 208 ] 209 ); 210 $cancel_link = '<a href="' . $link . '" style="margin-left:1em" id="plugin__structnotification_cancel">' . 211 $this->getLang('cancel') . '</a>'; 212 $form->addHTML($cancel_link); 213 $form->addTagClose('td'); 214 215 216 $form->addTagClose('tr'); 217 218 return $form->toHTML(); 219 } 220 221 protected function validate($predicate) 222 { 223 $errors = []; 224 if (blank($predicate['schema'])) { 225 $errors[] = 'val schema blank'; 226 } 227 228 if (blank($predicate['field'])) { 229 $errors[] = 'val field blank'; 230 } 231 232 if (blank($predicate['operator'])) { 233 $errors[] = 'val operator blank'; 234 } 235 236 if (blank($predicate['value'])) { 237 $errors[] = 'val value blank'; 238 } 239 240 if (blank($predicate['users_and_groups'])) { 241 $errors[] = 'val users_and_groups blank'; 242 } 243 244 if (blank($predicate['message'])) { 245 $errors[] = 'val message blank'; 246 } 247 248 return $errors; 249 } 250 251 protected function displayErrors($errors) 252 { 253 foreach ($errors as $error) { 254 $msg = $this->getLang($error); 255 if (!$msg) $msg = $error; 256 msg($error, -1); 257 } 258 } 259} 260