10a5d2da2SAndreas Gohr<?php 21da41c8bSAndreas Gohr 31da41c8bSAndreas Gohruse dokuwiki\Extension\ActionPlugin; 41da41c8bSAndreas Gohruse dokuwiki\Extension\EventHandler; 51da41c8bSAndreas Gohruse dokuwiki\Extension\Event; 61da41c8bSAndreas Gohr 70a5d2da2SAndreas Gohr/** 80a5d2da2SAndreas Gohr * DokuWiki Plugin farmer (Action Component) 90a5d2da2SAndreas Gohr * 101da41c8bSAndreas Gohr * Manage AJAX features 111da41c8bSAndreas Gohr * 120a5d2da2SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 130a5d2da2SAndreas Gohr * @author Michael Große <grosse@cosmocode.de> 140a5d2da2SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 150a5d2da2SAndreas Gohr */ 161da41c8bSAndreas Gohrclass action_plugin_farmer_ajax extends ActionPlugin 171da41c8bSAndreas Gohr{ 180a5d2da2SAndreas Gohr /** 190a5d2da2SAndreas Gohr * plugin should use this method to register its handlers with the DokuWiki's event controller 200a5d2da2SAndreas Gohr * 211da41c8bSAndreas Gohr * @param EventHandler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER 220a5d2da2SAndreas Gohr */ 231da41c8bSAndreas Gohr public function register(EventHandler $controller) 241da41c8bSAndreas Gohr { 251da41c8bSAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxCall'); 260a5d2da2SAndreas Gohr } 270a5d2da2SAndreas Gohr 280a5d2da2SAndreas Gohr /** 290a5d2da2SAndreas Gohr * handle ajax requests 300a5d2da2SAndreas Gohr * 311da41c8bSAndreas Gohr * @param Event $event 320a5d2da2SAndreas Gohr * @param $param 330a5d2da2SAndreas Gohr */ 341da41c8bSAndreas Gohr public function handleAjaxCall(Event $event, $param) 351da41c8bSAndreas Gohr { 36*f64a85f7SAndreas Gohr if (!str_starts_with($event->data, 'plugin_farmer')) { 370a5d2da2SAndreas Gohr return; 380a5d2da2SAndreas Gohr } 390a5d2da2SAndreas Gohr //no other ajax call handlers needed 400a5d2da2SAndreas Gohr $event->stopPropagation(); 410a5d2da2SAndreas Gohr $event->preventDefault(); 420a5d2da2SAndreas Gohr 43511d09feSAndreas Gohr if (!auth_isadmin()) die('Only admins allowed'); 44511d09feSAndreas Gohr 45511d09feSAndreas Gohr if (substr($event->data, 14) === 'getPluginMatrix') { 461da41c8bSAndreas Gohr $this->printPluginMatrix($event, $param); 47511d09feSAndreas Gohr return; 48511d09feSAndreas Gohr } 49511d09feSAndreas Gohr if (substr($event->data, 14) === 'modPlugin') { 501da41c8bSAndreas Gohr $this->togglePluginState($event, $param); 51511d09feSAndreas Gohr return; 52511d09feSAndreas Gohr } 530a5d2da2SAndreas Gohr if (substr($event->data, 14, 10) === 'getPlugins') { 541da41c8bSAndreas Gohr $this->printAnimalPlugins($event, $param); 550a5d2da2SAndreas Gohr return; 560a5d2da2SAndreas Gohr } 570a5d2da2SAndreas Gohr if (substr($event->data, 14, 10) === 'checkSetup') { 581da41c8bSAndreas Gohr $this->checkSetup($event, $param); 590a5d2da2SAndreas Gohr } 600a5d2da2SAndreas Gohr } 610a5d2da2SAndreas Gohr 620a5d2da2SAndreas Gohr /** 631da41c8bSAndreas Gohr * Always return an empty response 640a5d2da2SAndreas Gohr * 651da41c8bSAndreas Gohr * This function exists in order to provide a positive (i.e. 200) response 661da41c8bSAndreas Gohr * to an ajax request to a non-existing animal. 671da41c8bSAndreas Gohr * 681da41c8bSAndreas Gohr * @param Event $event 690a5d2da2SAndreas Gohr * @param $param 700a5d2da2SAndreas Gohr */ 711da41c8bSAndreas Gohr public function checkSetup(Event $event, $param) 721da41c8bSAndreas Gohr { 730a5d2da2SAndreas Gohr $data = ''; 740a5d2da2SAndreas Gohr header('Content-Type: application/json'); 751da41c8bSAndreas Gohr json_encode($data); 760a5d2da2SAndreas Gohr } 770a5d2da2SAndreas Gohr 781da41c8bSAndreas Gohr /** 791da41c8bSAndreas Gohr * Turn a plugin on or off 801da41c8bSAndreas Gohr * 811da41c8bSAndreas Gohr * @param Event $event 821da41c8bSAndreas Gohr * @param $param 831da41c8bSAndreas Gohr */ 841da41c8bSAndreas Gohr public function togglePluginState(Event $event, $param) 851da41c8bSAndreas Gohr { 86511d09feSAndreas Gohr global $INPUT; 87511d09feSAndreas Gohr 88511d09feSAndreas Gohr /** @var helper_plugin_farmer $helper */ 89511d09feSAndreas Gohr $helper = plugin_load('helper', 'farmer'); 90511d09feSAndreas Gohr 91511d09feSAndreas Gohr $pname = $INPUT->str('plugin'); 92511d09feSAndreas Gohr $animal = $INPUT->str('ani'); 93511d09feSAndreas Gohr 94511d09feSAndreas Gohr 95511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 96511d09feSAndreas Gohr if (!isset($plugins[$pname])) die('no such plugin'); 97511d09feSAndreas Gohr $plugin = $plugins[$pname]; 98511d09feSAndreas Gohr 99511d09feSAndreas Gohr // figure out what to toggle to 100511d09feSAndreas Gohr if ($plugin['isdefault']) { 101511d09feSAndreas Gohr $new = (int) !$plugin['actual']; 102511d09feSAndreas Gohr } else { 103511d09feSAndreas Gohr $new = -1; 104511d09feSAndreas Gohr } 105511d09feSAndreas Gohr $helper->setPluginState($pname, $animal, $new); 106511d09feSAndreas Gohr 107511d09feSAndreas Gohr // show new state 108511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 109511d09feSAndreas Gohr $plugin = $plugins[$pname]; 110511d09feSAndreas Gohr header('Content-Type: text/html; charset=utf-8'); 1111da41c8bSAndreas Gohr echo $this->createPluginMatrixCell($plugin, $animal); 112511d09feSAndreas Gohr } 113511d09feSAndreas Gohr 114511d09feSAndreas Gohr /** 115511d09feSAndreas Gohr * Create a matrix of all animals and plugin states 116511d09feSAndreas Gohr * 1171da41c8bSAndreas Gohr * @param Event $event 118511d09feSAndreas Gohr * @param $param 119511d09feSAndreas Gohr */ 1201da41c8bSAndreas Gohr public function printPluginMatrix(Event $event, $param) 1211da41c8bSAndreas Gohr { 122511d09feSAndreas Gohr /** @var helper_plugin_farmer $helper */ 123511d09feSAndreas Gohr $helper = plugin_load('helper', 'farmer'); 124511d09feSAndreas Gohr 125511d09feSAndreas Gohr $animals = $helper->getAllAnimals(); 126511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animals[0]); 127511d09feSAndreas Gohr 128511d09feSAndreas Gohr header('Content-Type: text/html; charset=utf-8'); 129511d09feSAndreas Gohr 130511d09feSAndreas Gohr echo '<div class="table pluginmatrix">'; 131511d09feSAndreas Gohr echo '<table>'; 132511d09feSAndreas Gohr echo '<thead>'; 133511d09feSAndreas Gohr echo '<tr>'; 134511d09feSAndreas Gohr echo '<th></th>'; 135511d09feSAndreas Gohr foreach ($plugins as $plugin) { 136511d09feSAndreas Gohr echo '<th><div>' . hsc($plugin['name']) . '</div></th>'; 137511d09feSAndreas Gohr } 138511d09feSAndreas Gohr echo '</tr>'; 139511d09feSAndreas Gohr echo '</thead>'; 140511d09feSAndreas Gohr 141511d09feSAndreas Gohr echo '<tbody>'; 142511d09feSAndreas Gohr 143511d09feSAndreas Gohr echo '<tr>'; 144511d09feSAndreas Gohr echo '<th>Default</th>'; 145511d09feSAndreas Gohr foreach ($plugins as $plugin) { 1461da41c8bSAndreas Gohr echo $this->createPluginMatrixCell($plugin, $this->getLang('plugin_default'), true); 147511d09feSAndreas Gohr } 148511d09feSAndreas Gohr echo '</tr>'; 149511d09feSAndreas Gohr 150511d09feSAndreas Gohr foreach ($animals as $animal) { 151511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 152511d09feSAndreas Gohr echo '<tr>'; 153511d09feSAndreas Gohr echo '<th>' . hsc($animal) . '</th>'; 154511d09feSAndreas Gohr foreach ($plugins as $plugin) { 1551da41c8bSAndreas Gohr echo $this->createPluginMatrixCell($plugin, $animal); 156511d09feSAndreas Gohr } 157511d09feSAndreas Gohr echo '</tr>'; 158511d09feSAndreas Gohr } 159511d09feSAndreas Gohr echo '</tbody>'; 160511d09feSAndreas Gohr echo '</table>'; 161511d09feSAndreas Gohr echo '</div>'; 162511d09feSAndreas Gohr } 163511d09feSAndreas Gohr 164511d09feSAndreas Gohr /** 165511d09feSAndreas Gohr * create a single cell in the matrix 166511d09feSAndreas Gohr * 167511d09feSAndreas Gohr * @param array $plugin 168511d09feSAndreas Gohr * @param string $animal 169511d09feSAndreas Gohr * @param bool $defaults show the defaults 170511d09feSAndreas Gohr * @return string 171511d09feSAndreas Gohr */ 1721da41c8bSAndreas Gohr protected function createPluginMatrixCell($plugin, $animal, $defaults = false) 1731da41c8bSAndreas Gohr { 174511d09feSAndreas Gohr if ($defaults) { 175511d09feSAndreas Gohr $current = $plugin['default']; 176511d09feSAndreas Gohr $isdefault = true; 177511d09feSAndreas Gohr $td = 'th'; 178511d09feSAndreas Gohr } else { 179511d09feSAndreas Gohr $current = $plugin['actual']; 180511d09feSAndreas Gohr $isdefault = $plugin['isdefault']; 181511d09feSAndreas Gohr $td = 'td'; 182511d09feSAndreas Gohr } 183511d09feSAndreas Gohr 184511d09feSAndreas Gohr if ($current) { 185511d09feSAndreas Gohr $class = 'on'; 186511d09feSAndreas Gohr $lbl = '✓'; 187511d09feSAndreas Gohr } else { 188511d09feSAndreas Gohr $class = 'off'; 189511d09feSAndreas Gohr $lbl = '✗'; 190511d09feSAndreas Gohr } 191511d09feSAndreas Gohr if ($isdefault) $class .= ' default'; 192511d09feSAndreas Gohr 193511d09feSAndreas Gohr 1941da41c8bSAndreas Gohr $attrs = [ 195511d09feSAndreas Gohr 'class' => $class, 196511d09feSAndreas Gohr 'title' => $animal . ': ' . $plugin['name'], 197511d09feSAndreas Gohr 'data-animal' => $animal, 198511d09feSAndreas Gohr 'data-plugin' => $plugin['name'] 1991da41c8bSAndreas Gohr ]; 200511d09feSAndreas Gohr $attr = buildAttributes($attrs); 201511d09feSAndreas Gohr 202511d09feSAndreas Gohr return "<$td $attr>$lbl</$td>"; 203511d09feSAndreas Gohr } 204511d09feSAndreas Gohr 2050a5d2da2SAndreas Gohr /** 2061da41c8bSAndreas Gohr * Create an overview on all plugins for a given animal 2071da41c8bSAndreas Gohr * 2081da41c8bSAndreas Gohr * @param Event $event 2090a5d2da2SAndreas Gohr * @param $param 2100a5d2da2SAndreas Gohr */ 2111da41c8bSAndreas Gohr public function printAnimalPlugins(Event $event, $param) 2121da41c8bSAndreas Gohr { 2130a5d2da2SAndreas Gohr $animal = substr($event->data, 25); 2140a5d2da2SAndreas Gohr /** @var helper_plugin_farmer $helper */ 2150a5d2da2SAndreas Gohr $helper = plugin_load('helper', 'farmer'); 2160a5d2da2SAndreas Gohr 217af1c6dd8SAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 2180a5d2da2SAndreas Gohr 219af1c6dd8SAndreas Gohr header('Content-Type: text/html; charset=utf-8'); 2200a5d2da2SAndreas Gohr 221af1c6dd8SAndreas Gohr echo '<table>'; 222af1c6dd8SAndreas Gohr echo '<tr>'; 223af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin') . '</th>'; 224af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin_default') . '</th>'; 225af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin_enabled') . '</th>'; 226af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin_disabled') . '</th>'; 227af1c6dd8SAndreas Gohr echo '</tr>'; 228af1c6dd8SAndreas Gohr 229af1c6dd8SAndreas Gohr foreach ($plugins as $plugin) { 230af1c6dd8SAndreas Gohr echo '<tr>'; 231af1c6dd8SAndreas Gohr echo '<th>' . hsc($plugin['name']) . '</th>'; 232af1c6dd8SAndreas Gohr 233af1c6dd8SAndreas Gohr echo '<td>'; 2341da41c8bSAndreas Gohr $attr = []; 235af1c6dd8SAndreas Gohr $attr['type'] = 'radio'; 236af1c6dd8SAndreas Gohr $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 237af1c6dd8SAndreas Gohr $attr['value'] = '-1'; 238af1c6dd8SAndreas Gohr if ($plugin['isdefault']) { 239af1c6dd8SAndreas Gohr $attr['checked'] = 'checked'; 240af1c6dd8SAndreas Gohr } 241af1c6dd8SAndreas Gohr echo '<label>'; 242af1c6dd8SAndreas Gohr echo '<input ' . buildAttributes($attr) . ' />'; 243af1c6dd8SAndreas Gohr if ($plugin['default']) { 244af1c6dd8SAndreas Gohr echo ' (' . $this->getLang('plugin_on') . ')'; 245af1c6dd8SAndreas Gohr } else { 246af1c6dd8SAndreas Gohr echo ' (' . $this->getLang('plugin_off') . ')'; 247af1c6dd8SAndreas Gohr } 248af1c6dd8SAndreas Gohr echo '</label>'; 249af1c6dd8SAndreas Gohr echo '</td>'; 250af1c6dd8SAndreas Gohr 251af1c6dd8SAndreas Gohr echo '<td>'; 2521da41c8bSAndreas Gohr $attr = []; 253af1c6dd8SAndreas Gohr $attr['type'] = 'radio'; 254af1c6dd8SAndreas Gohr $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 255af1c6dd8SAndreas Gohr $attr['value'] = '1'; 256af1c6dd8SAndreas Gohr if (!$plugin['isdefault'] && $plugin['actual']) { 257af1c6dd8SAndreas Gohr $attr['checked'] = 'checked'; 258af1c6dd8SAndreas Gohr } 259af1c6dd8SAndreas Gohr echo '<label>'; 260af1c6dd8SAndreas Gohr echo '<input ' . buildAttributes($attr) . ' />'; 261af1c6dd8SAndreas Gohr echo ' ' . $this->getLang('plugin_on'); 262af1c6dd8SAndreas Gohr echo '</label>'; 263af1c6dd8SAndreas Gohr echo '</td>'; 264af1c6dd8SAndreas Gohr 265af1c6dd8SAndreas Gohr echo '<td>'; 2661da41c8bSAndreas Gohr $attr = []; 267af1c6dd8SAndreas Gohr $attr['type'] = 'radio'; 268af1c6dd8SAndreas Gohr $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 269af1c6dd8SAndreas Gohr $attr['value'] = '0'; 270af1c6dd8SAndreas Gohr if (!$plugin['isdefault'] && !$plugin['actual']) { 271af1c6dd8SAndreas Gohr $attr['checked'] = 'checked'; 272af1c6dd8SAndreas Gohr } 273af1c6dd8SAndreas Gohr echo '<label>'; 274af1c6dd8SAndreas Gohr echo '<input ' . buildAttributes($attr) . ' />'; 275af1c6dd8SAndreas Gohr echo ' ' . $this->getLang('plugin_off'); 276af1c6dd8SAndreas Gohr echo '</label>'; 277af1c6dd8SAndreas Gohr echo '</td>'; 278af1c6dd8SAndreas Gohr 279af1c6dd8SAndreas Gohr echo '</tr>'; 280af1c6dd8SAndreas Gohr } 2810a5d2da2SAndreas Gohr } 2820a5d2da2SAndreas Gohr} 283