1<?php 2 3/** 4 * DokuWiki Plugin struct (Action Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 8 */ 9 10use dokuwiki\plugin\struct\meta\Schema; 11use dokuwiki\plugin\struct\meta\StructException; 12 13class action_plugin_struct_ajax extends DokuWiki_Action_Plugin 14{ 15 /** 16 * Registers a callback function for a given event 17 * 18 * @param Doku_Event_Handler $controller DokuWiki's event controller object 19 * @return void 20 */ 21 public function register(Doku_Event_Handler $controller) 22 { 23 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax'); 24 } 25 26 /** 27 * Pass Ajax call to a type 28 * 29 * @param Doku_Event $event event object by reference 30 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 31 * handler was registered] 32 */ 33 public function handleAjax(Doku_Event $event, $param) 34 { 35 if ($event->data != 'plugin_struct') return; 36 $event->preventDefault(); 37 $event->stopPropagation(); 38 global $conf; 39 40 header('Content-Type: application/json'); 41 try { 42 $result = $this->executeTypeAjax(); 43 } catch (StructException $e) { 44 $result = array( 45 'error' => $e->getMessage() . ' ' . basename($e->getFile()) . ':' . $e->getLine() 46 ); 47 if ($conf['allowdebug']) { 48 $result['stacktrace'] = $e->getTraceAsString(); 49 } 50 http_status(500); 51 } 52 53 $json = new JSON(); 54 echo $json->encode($result); 55 } 56 57 /** 58 * Check the input variables and run the AJAX call 59 * 60 * @return mixed 61 * @throws StructException 62 */ 63 protected function executeTypeAjax() 64 { 65 global $INPUT; 66 67 $col = $INPUT->str('column'); 68 if (blank($col)) throw new StructException('No column provided'); 69 list($schema, $colname) = explode('.', $col, 2); 70 if (blank($schema) || blank($colname)) throw new StructException('Column format is wrong'); 71 72 $schema = new Schema($schema); 73 if (!$schema->getId()) throw new StructException('Unknown Schema'); 74 75 $column = $schema->findColumn($colname); 76 if ($column === false) throw new StructException('Column not found'); 77 78 return $column->getType()->handleAjax(); 79 } 80} 81