10a5d2da2SAndreas Gohr<?php 20a5d2da2SAndreas Gohr/** 30a5d2da2SAndreas Gohr * DokuWiki Plugin farmer (Action Component) 40a5d2da2SAndreas Gohr * 50a5d2da2SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 60a5d2da2SAndreas Gohr * @author Michael Große <grosse@cosmocode.de> 70a5d2da2SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 80a5d2da2SAndreas Gohr */ 90a5d2da2SAndreas Gohr 100a5d2da2SAndreas Gohrif(!defined('DOKU_INC')) die(); 110a5d2da2SAndreas Gohr 120a5d2da2SAndreas Gohr/** 130a5d2da2SAndreas Gohr * Manage AJAX features 140a5d2da2SAndreas Gohr */ 150a5d2da2SAndreas Gohrclass action_plugin_farmer_ajax extends DokuWiki_Action_Plugin { 160a5d2da2SAndreas Gohr 170a5d2da2SAndreas Gohr /** 180a5d2da2SAndreas Gohr * plugin should use this method to register its handlers with the DokuWiki's event controller 190a5d2da2SAndreas Gohr * 200a5d2da2SAndreas Gohr * @param Doku_Event_Handler $controller DokuWiki's event controller object. Also available as global $EVENT_HANDLER 210a5d2da2SAndreas Gohr * 220a5d2da2SAndreas Gohr */ 230a5d2da2SAndreas Gohr public function register(Doku_Event_Handler $controller) { 240a5d2da2SAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, '_ajax_call'); 250a5d2da2SAndreas Gohr } 260a5d2da2SAndreas Gohr 270a5d2da2SAndreas Gohr /** 280a5d2da2SAndreas Gohr * handle ajax requests 290a5d2da2SAndreas Gohr * 300a5d2da2SAndreas Gohr * @param Doku_Event $event 310a5d2da2SAndreas Gohr * @param $param 320a5d2da2SAndreas Gohr */ 330a5d2da2SAndreas Gohr public function _ajax_call(Doku_Event $event, $param) { 340a5d2da2SAndreas Gohr if(substr($event->data, 0, 13) !== 'plugin_farmer') { 350a5d2da2SAndreas Gohr return; 360a5d2da2SAndreas Gohr } 370a5d2da2SAndreas Gohr //no other ajax call handlers needed 380a5d2da2SAndreas Gohr $event->stopPropagation(); 390a5d2da2SAndreas Gohr $event->preventDefault(); 400a5d2da2SAndreas Gohr 41*511d09feSAndreas Gohr if(!auth_isadmin()) die('Only admins allowed'); 42*511d09feSAndreas Gohr 43*511d09feSAndreas Gohr if(substr($event->data, 14) === 'getPluginMatrix') { 44*511d09feSAndreas Gohr $this->get_plugin_matrix($event, $param); 45*511d09feSAndreas Gohr return; 46*511d09feSAndreas Gohr } 47*511d09feSAndreas Gohr if(substr($event->data, 14) === 'modPlugin') { 48*511d09feSAndreas Gohr $this->plugin_mod($event, $param); 49*511d09feSAndreas Gohr return; 50*511d09feSAndreas Gohr } 510a5d2da2SAndreas Gohr if(substr($event->data, 14, 10) === 'getPlugins') { 520a5d2da2SAndreas Gohr $this->get_animal_plugins($event, $param); 530a5d2da2SAndreas Gohr return; 540a5d2da2SAndreas Gohr } 550a5d2da2SAndreas Gohr if(substr($event->data, 14, 10) === 'checkSetup') { 560a5d2da2SAndreas Gohr $this->check_setup($event, $param); 570a5d2da2SAndreas Gohr } 580a5d2da2SAndreas Gohr } 590a5d2da2SAndreas Gohr 600a5d2da2SAndreas Gohr /** 610a5d2da2SAndreas Gohr * This function exists in order to provide a positive (i.e. 200) response to an ajax request to a non-existing animal. 620a5d2da2SAndreas Gohr * 630a5d2da2SAndreas Gohr * @param Doku_Event $event 640a5d2da2SAndreas Gohr * @param $param 650a5d2da2SAndreas Gohr */ 660a5d2da2SAndreas Gohr public function check_setup(Doku_Event $event, $param) { 670a5d2da2SAndreas Gohr $data = ''; 680a5d2da2SAndreas Gohr $json = new JSON(); 690a5d2da2SAndreas Gohr header('Content-Type: application/json'); 700a5d2da2SAndreas Gohr echo $json->encode($data); 710a5d2da2SAndreas Gohr } 720a5d2da2SAndreas Gohr 73*511d09feSAndreas Gohr public function plugin_mod(Doku_Event $event, $param) { 74*511d09feSAndreas Gohr global $INPUT; 75*511d09feSAndreas Gohr 76*511d09feSAndreas Gohr /** @var helper_plugin_farmer $helper */ 77*511d09feSAndreas Gohr $helper = plugin_load('helper', 'farmer'); 78*511d09feSAndreas Gohr 79*511d09feSAndreas Gohr $pname = $INPUT->str('plugin'); 80*511d09feSAndreas Gohr $animal = $INPUT->str('ani'); 81*511d09feSAndreas Gohr 82*511d09feSAndreas Gohr 83*511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 84*511d09feSAndreas Gohr if(!isset($plugins[$pname])) die('no such plugin'); 85*511d09feSAndreas Gohr $plugin = $plugins[$pname]; 86*511d09feSAndreas Gohr 87*511d09feSAndreas Gohr // figure out what to toggle to 88*511d09feSAndreas Gohr if($plugin['isdefault']) { 89*511d09feSAndreas Gohr $new = (int) !$plugin['actual']; 90*511d09feSAndreas Gohr } else { 91*511d09feSAndreas Gohr $new = -1; 92*511d09feSAndreas Gohr } 93*511d09feSAndreas Gohr $helper->setPluginState($pname, $animal, $new); 94*511d09feSAndreas Gohr 95*511d09feSAndreas Gohr // show new state 96*511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 97*511d09feSAndreas Gohr $plugin = $plugins[$pname]; 98*511d09feSAndreas Gohr header('Content-Type: text/html; charset=utf-8'); 99*511d09feSAndreas Gohr echo $this->plugin_matrix_cell($plugin, $animal); 100*511d09feSAndreas Gohr } 101*511d09feSAndreas Gohr 102*511d09feSAndreas Gohr /** 103*511d09feSAndreas Gohr * Create a matrix of all animals and plugin states 104*511d09feSAndreas Gohr * 105*511d09feSAndreas Gohr * @param Doku_Event $event 106*511d09feSAndreas Gohr * @param $param 107*511d09feSAndreas Gohr */ 108*511d09feSAndreas Gohr public function get_plugin_matrix(Doku_Event $event, $param) { 109*511d09feSAndreas Gohr /** @var helper_plugin_farmer $helper */ 110*511d09feSAndreas Gohr $helper = plugin_load('helper', 'farmer'); 111*511d09feSAndreas Gohr 112*511d09feSAndreas Gohr $animals = $helper->getAllAnimals(); 113*511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animals[0]); 114*511d09feSAndreas Gohr 115*511d09feSAndreas Gohr header('Content-Type: text/html; charset=utf-8'); 116*511d09feSAndreas Gohr 117*511d09feSAndreas Gohr echo '<div class="table pluginmatrix">'; 118*511d09feSAndreas Gohr echo '<table>'; 119*511d09feSAndreas Gohr echo '<thead>'; 120*511d09feSAndreas Gohr echo '<tr>'; 121*511d09feSAndreas Gohr echo '<th></th>'; 122*511d09feSAndreas Gohr foreach($plugins as $plugin) { 123*511d09feSAndreas Gohr echo '<th><div>' . hsc($plugin['name']) . '</div></th>'; 124*511d09feSAndreas Gohr } 125*511d09feSAndreas Gohr echo '</tr>'; 126*511d09feSAndreas Gohr echo '</thead>'; 127*511d09feSAndreas Gohr 128*511d09feSAndreas Gohr echo '<tbody>'; 129*511d09feSAndreas Gohr 130*511d09feSAndreas Gohr echo '<tr>'; 131*511d09feSAndreas Gohr echo '<th>Default</th>'; 132*511d09feSAndreas Gohr foreach($plugins as $plugin) { 133*511d09feSAndreas Gohr echo $this->plugin_matrix_cell($plugin, $this->getLang('plugin_default'), true); 134*511d09feSAndreas Gohr } 135*511d09feSAndreas Gohr echo '</tr>'; 136*511d09feSAndreas Gohr 137*511d09feSAndreas Gohr foreach($animals as $animal) { 138*511d09feSAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 139*511d09feSAndreas Gohr echo '<tr>'; 140*511d09feSAndreas Gohr echo '<th>' . hsc($animal) . '</th>'; 141*511d09feSAndreas Gohr foreach($plugins as $plugin) { 142*511d09feSAndreas Gohr echo $this->plugin_matrix_cell($plugin, $animal); 143*511d09feSAndreas Gohr } 144*511d09feSAndreas Gohr echo '</tr>'; 145*511d09feSAndreas Gohr } 146*511d09feSAndreas Gohr echo '</tbody>'; 147*511d09feSAndreas Gohr echo '</table>'; 148*511d09feSAndreas Gohr echo '</div>'; 149*511d09feSAndreas Gohr } 150*511d09feSAndreas Gohr 151*511d09feSAndreas Gohr /** 152*511d09feSAndreas Gohr * create a single cell in the matrix 153*511d09feSAndreas Gohr * 154*511d09feSAndreas Gohr * @param array $plugin 155*511d09feSAndreas Gohr * @param string $animal 156*511d09feSAndreas Gohr * @param bool $defaults show the defaults 157*511d09feSAndreas Gohr * @return string 158*511d09feSAndreas Gohr */ 159*511d09feSAndreas Gohr protected function plugin_matrix_cell($plugin, $animal, $defaults=false) { 160*511d09feSAndreas Gohr if($defaults) { 161*511d09feSAndreas Gohr $current = $plugin['default']; 162*511d09feSAndreas Gohr $isdefault = true; 163*511d09feSAndreas Gohr $td = 'th'; 164*511d09feSAndreas Gohr } else { 165*511d09feSAndreas Gohr $current = $plugin['actual']; 166*511d09feSAndreas Gohr $isdefault = $plugin['isdefault']; 167*511d09feSAndreas Gohr $td = 'td'; 168*511d09feSAndreas Gohr } 169*511d09feSAndreas Gohr 170*511d09feSAndreas Gohr if($current) { 171*511d09feSAndreas Gohr $class = 'on'; 172*511d09feSAndreas Gohr $lbl = '✓'; 173*511d09feSAndreas Gohr } else { 174*511d09feSAndreas Gohr $class = 'off'; 175*511d09feSAndreas Gohr $lbl = '✗'; 176*511d09feSAndreas Gohr } 177*511d09feSAndreas Gohr if($isdefault) $class .= ' default'; 178*511d09feSAndreas Gohr 179*511d09feSAndreas Gohr 180*511d09feSAndreas Gohr $attrs = array( 181*511d09feSAndreas Gohr 'class' => $class, 182*511d09feSAndreas Gohr 'title' => $animal . ': ' . $plugin['name'], 183*511d09feSAndreas Gohr 'data-animal' => $animal, 184*511d09feSAndreas Gohr 'data-plugin' => $plugin['name'] 185*511d09feSAndreas Gohr ); 186*511d09feSAndreas Gohr $attr = buildAttributes($attrs); 187*511d09feSAndreas Gohr 188*511d09feSAndreas Gohr return "<$td $attr>$lbl</$td>"; 189*511d09feSAndreas Gohr } 190*511d09feSAndreas Gohr 1910a5d2da2SAndreas Gohr /** 1920a5d2da2SAndreas Gohr * @param Doku_Event $event 1930a5d2da2SAndreas Gohr * @param $param 1940a5d2da2SAndreas Gohr */ 1950a5d2da2SAndreas Gohr public function get_animal_plugins(Doku_Event $event, $param) { 1960a5d2da2SAndreas Gohr $animal = substr($event->data, 25); 1970a5d2da2SAndreas Gohr /** @var helper_plugin_farmer $helper */ 1980a5d2da2SAndreas Gohr $helper = plugin_load('helper', 'farmer'); 1990a5d2da2SAndreas Gohr 200af1c6dd8SAndreas Gohr $plugins = $helper->getAnimalPluginRealState($animal); 2010a5d2da2SAndreas Gohr 202af1c6dd8SAndreas Gohr header('Content-Type: text/html; charset=utf-8'); 2030a5d2da2SAndreas Gohr 204af1c6dd8SAndreas Gohr echo '<table>'; 205af1c6dd8SAndreas Gohr echo '<tr>'; 206af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin') . '</th>'; 207af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin_default') . '</th>'; 208af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin_enabled') . '</th>'; 209af1c6dd8SAndreas Gohr echo '<th>' . $this->getLang('plugin_disabled') . '</th>'; 210af1c6dd8SAndreas Gohr echo '</tr>'; 211af1c6dd8SAndreas Gohr 212af1c6dd8SAndreas Gohr foreach($plugins as $plugin) { 213af1c6dd8SAndreas Gohr echo '<tr>'; 214af1c6dd8SAndreas Gohr echo '<th>' . hsc($plugin['name']) . '</th>'; 215af1c6dd8SAndreas Gohr 216af1c6dd8SAndreas Gohr echo '<td>'; 217af1c6dd8SAndreas Gohr $attr = array(); 218af1c6dd8SAndreas Gohr $attr['type'] = 'radio'; 219af1c6dd8SAndreas Gohr $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 220af1c6dd8SAndreas Gohr $attr['value'] = '-1'; 221af1c6dd8SAndreas Gohr if($plugin['isdefault']) { 222af1c6dd8SAndreas Gohr $attr['checked'] = 'checked'; 223af1c6dd8SAndreas Gohr } 224af1c6dd8SAndreas Gohr echo '<label>'; 225af1c6dd8SAndreas Gohr echo '<input ' . buildAttributes($attr) . ' />'; 226af1c6dd8SAndreas Gohr if($plugin['default']) { 227af1c6dd8SAndreas Gohr echo ' (' . $this->getLang('plugin_on') . ')'; 228af1c6dd8SAndreas Gohr } else { 229af1c6dd8SAndreas Gohr echo ' (' . $this->getLang('plugin_off') . ')'; 230af1c6dd8SAndreas Gohr } 231af1c6dd8SAndreas Gohr echo '</label>'; 232af1c6dd8SAndreas Gohr echo '</td>'; 233af1c6dd8SAndreas Gohr 234af1c6dd8SAndreas Gohr echo '<td>'; 235af1c6dd8SAndreas Gohr $attr = array(); 236af1c6dd8SAndreas Gohr $attr['type'] = 'radio'; 237af1c6dd8SAndreas Gohr $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 238af1c6dd8SAndreas Gohr $attr['value'] = '1'; 239af1c6dd8SAndreas Gohr if(!$plugin['isdefault'] && $plugin['actual']) { 240af1c6dd8SAndreas Gohr $attr['checked'] = 'checked'; 241af1c6dd8SAndreas Gohr } 242af1c6dd8SAndreas Gohr echo '<label>'; 243af1c6dd8SAndreas Gohr echo '<input ' . buildAttributes($attr) . ' />'; 244af1c6dd8SAndreas Gohr echo ' ' . $this->getLang('plugin_on'); 245af1c6dd8SAndreas Gohr echo '</label>'; 246af1c6dd8SAndreas Gohr echo '</td>'; 247af1c6dd8SAndreas Gohr 248af1c6dd8SAndreas Gohr echo '<td>'; 249af1c6dd8SAndreas Gohr $attr = array(); 250af1c6dd8SAndreas Gohr $attr['type'] = 'radio'; 251af1c6dd8SAndreas Gohr $attr['name'] = 'bulk_plugins[' . $plugin['name'] . ']'; 252af1c6dd8SAndreas Gohr $attr['value'] = '0'; 253af1c6dd8SAndreas Gohr if(!$plugin['isdefault'] && !$plugin['actual']) { 254af1c6dd8SAndreas Gohr $attr['checked'] = 'checked'; 255af1c6dd8SAndreas Gohr } 256af1c6dd8SAndreas Gohr echo '<label>'; 257af1c6dd8SAndreas Gohr echo '<input ' . buildAttributes($attr) . ' />'; 258af1c6dd8SAndreas Gohr echo ' ' . $this->getLang('plugin_off'); 259af1c6dd8SAndreas Gohr echo '</label>'; 260af1c6dd8SAndreas Gohr echo '</td>'; 261af1c6dd8SAndreas Gohr 262af1c6dd8SAndreas Gohr echo '</tr>'; 263af1c6dd8SAndreas Gohr } 2640a5d2da2SAndreas Gohr } 2650a5d2da2SAndreas Gohr 2660a5d2da2SAndreas Gohr} 2670a5d2da2SAndreas Gohr 268