1<?php 2/** 3 * DokuWiki Plugin farmer (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Michael Große <grosse@cosmocode.de> 7 * @author Andreas Gohr <gohr@cosmocode.de> 8 */ 9 10if(!defined('DOKU_INC')) die(); 11 12/** 13 * Manage AJAX features 14 */ 15class action_plugin_farmer_ajax extends DokuWiki_Action_Plugin { 16 17 /** 18 * plugin should use this method to register its handlers with the DokuWiki's event controller 19 * 20 * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER 21 * 22 */ 23 public function register(Doku_Event_Handler $controller) { 24 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call'); 25 } 26 27 /** 28 * handle ajax requests 29 * 30 * @param Doku_Event $event 31 * @param $param 32 */ 33 public function _ajax_call(Doku_Event $event, $param) { 34 if(substr($event->data, 0, 13) !== 'plugin_farmer') { 35 return; 36 } 37 //no other ajax call handlers needed 38 $event->stopPropagation(); 39 $event->preventDefault(); 40 41 if(!auth_isadmin()) die('Only admins allowed'); 42 43 if(substr($event->data, 14) === 'getPluginMatrix') { 44 $this->get_plugin_matrix($event, $param); 45 return; 46 } 47 if(substr($event->data, 14) === 'modPlugin') { 48 $this->plugin_mod($event, $param); 49 return; 50 } 51 if(substr($event->data, 14, 10) === 'getPlugins') { 52 $this->get_animal_plugins($event, $param); 53 return; 54 } 55 if(substr($event->data, 14, 10) === 'checkSetup') { 56 $this->check_setup($event, $param); 57 } 58 } 59 60 /** 61 * This function exists in order to provide a positive (i.e. 200) response to an ajax request to a non-existing animal. 62 * 63 * @param Doku_Event $event 64 * @param $param 65 */ 66 public function check_setup(Doku_Event $event, $param) { 67 $data = ''; 68 $json = new JSON(); 69 header('Content-Type: application/json'); 70 echo $json->encode($data); 71 } 72 73 public function plugin_mod(Doku_Event $event, $param) { 74 global $INPUT; 75 76 /** @var helper_plugin_farmer $helper */ 77 $helper = plugin_load('helper', 'farmer'); 78 79 $pname = $INPUT->str('plugin'); 80 $animal = $INPUT->str('ani'); 81 82 83 $plugins = $helper->getAnimalPluginRealState($animal); 84 if(!isset($plugins[$pname])) die('no such plugin'); 85 $plugin = $plugins[$pname]; 86 87 // figure out what to toggle to 88 if($plugin['isdefault']) { 89 $new = (int) !$plugin['actual']; 90 } else { 91 $new = -1; 92 } 93 $helper->setPluginState($pname, $animal, $new); 94 95 // show new state 96 $plugins = $helper->getAnimalPluginRealState($animal); 97 $plugin = $plugins[$pname]; 98 header('Content-Type: text/html; charset=utf-8'); 99 echo $this->plugin_matrix_cell($plugin, $animal); 100 } 101 102 /** 103 * Create a matrix of all animals and plugin states 104 * 105 * @param Doku_Event $event 106 * @param $param 107 */ 108 public function get_plugin_matrix(Doku_Event $event, $param) { 109 /** @var helper_plugin_farmer $helper */ 110 $helper = plugin_load('helper', 'farmer'); 111 112 $animals = $helper->getAllAnimals(); 113 $plugins = $helper->getAnimalPluginRealState($animals[0]); 114 115 header('Content-Type: text/html; charset=utf-8'); 116 117 echo '<div class="table pluginmatrix">'; 118 echo '<table>'; 119 echo '<thead>'; 120 echo '<tr>'; 121 echo '<th></th>'; 122 foreach($plugins as $plugin) { 123 echo '<th><div>' . hsc($plugin['name']) . '</div></th>'; 124 } 125 echo '</tr>'; 126 echo '</thead>'; 127 128 echo '<tbody>'; 129 130 echo '<tr>'; 131 echo '<th>Default</th>'; 132 foreach($plugins as $plugin) { 133 echo $this->plugin_matrix_cell($plugin, $this->getLang('plugin_default'), true); 134 } 135 echo '</tr>'; 136 137 foreach($animals as $animal) { 138 $plugins = $helper->getAnimalPluginRealState($animal); 139 echo '<tr>'; 140 echo '<th>' . hsc($animal) . '</th>'; 141 foreach($plugins as $plugin) { 142 echo $this->plugin_matrix_cell($plugin, $animal); 143 } 144 echo '</tr>'; 145 } 146 echo '</tbody>'; 147 echo '</table>'; 148 echo '</div>'; 149 } 150 151 /** 152 * create a single cell in the matrix 153 * 154 * @param array $plugin 155 * @param string $animal 156 * @param bool $defaults show the defaults 157 * @return string 158 */ 159 protected function plugin_matrix_cell($plugin, $animal, $defaults=false) { 160 if($defaults) { 161 $current = $plugin['default']; 162 $isdefault = true; 163 $td = 'th'; 164 } else { 165 $current = $plugin['actual']; 166 $isdefault = $plugin['isdefault']; 167 $td = 'td'; 168 } 169 170 if($current) { 171 $class = 'on'; 172 $lbl = '✓'; 173 } else { 174 $class = 'off'; 175 $lbl = '✗'; 176 } 177 if($isdefault) $class .= ' default'; 178 179 180 $attrs = array( 181 'class' => $class, 182 'title' => $animal . ': ' . $plugin['name'], 183 'data-animal' => $animal, 184 'data-plugin' => $plugin['name'] 185 ); 186 $attr = buildAttributes($attrs); 187 188 return "<$td $attr>$lbl</$td>"; 189 } 190 191 /** 192 * @param Doku_Event $event 193 * @param $param 194 */ 195 public function get_animal_plugins(Doku_Event $event, $param) { 196 $animal = substr($event->data, 25); 197 /** @var helper_plugin_farmer $helper */ 198 $helper = plugin_load('helper', 'farmer'); 199 200 $plugins = $helper->getAnimalPluginRealState($animal); 201 202 header('Content-Type: text/html; charset=utf-8'); 203 204 echo '<table>'; 205 echo '<tr>'; 206 echo '<th>' . $this->getLang('plugin') . '</th>'; 207 echo '<th>' . $this->getLang('plugin_default') . '</th>'; 208 echo '<th>' . $this->getLang('plugin_enabled') . '</th>'; 209 echo '<th>' . $this->getLang('plugin_disabled') . '</th>'; 210 echo '</tr>'; 211 212 foreach($plugins as $plugin) { 213 echo '<tr>'; 214 echo '<th>' . hsc($plugin['name']) . '</th>'; 215 216 echo '<td>'; 217 $attr = array(); 218 $attr['type'] = 'radio'; 219 $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 220 $attr['value'] = '-1'; 221 if($plugin['isdefault']) { 222 $attr['checked'] = 'checked'; 223 } 224 echo '<label>'; 225 echo '<input ' . buildAttributes($attr) . ' />'; 226 if($plugin['default']) { 227 echo ' (' . $this->getLang('plugin_on') . ')'; 228 } else { 229 echo ' (' . $this->getLang('plugin_off') . ')'; 230 } 231 echo '</label>'; 232 echo '</td>'; 233 234 echo '<td>'; 235 $attr = array(); 236 $attr['type'] = 'radio'; 237 $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 238 $attr['value'] = '1'; 239 if(!$plugin['isdefault'] && $plugin['actual']) { 240 $attr['checked'] = 'checked'; 241 } 242 echo '<label>'; 243 echo '<input ' . buildAttributes($attr) . ' />'; 244 echo ' ' . $this->getLang('plugin_on'); 245 echo '</label>'; 246 echo '</td>'; 247 248 echo '<td>'; 249 $attr = array(); 250 $attr['type'] = 'radio'; 251 $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 252 $attr['value'] = '0'; 253 if(!$plugin['isdefault'] && !$plugin['actual']) { 254 $attr['checked'] = 'checked'; 255 } 256 echo '<label>'; 257 echo '<input ' . buildAttributes($attr) . ' />'; 258 echo ' ' . $this->getLang('plugin_off'); 259 echo '</label>'; 260 echo '</td>'; 261 262 echo '</tr>'; 263 } 264 } 265 266} 267 268