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