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 /** 17 * Registers a callback function for a given event 18 * 19 * @param Doku_Event_Handler $controller DokuWiki's event controller object 20 * @return void 21 */ 22 public function register(Doku_Event_Handler $controller) 23 { 24 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax'); 25 } 26 27 /** 28 * Pass Ajax call to a type 29 * 30 * @param Doku_Event $event event object by reference 31 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 32 * handler was registered] 33 */ 34 public function handleAjax(Doku_Event $event, $param) 35 { 36 if ($event->data != 'plugin_struct') return; 37 $event->preventDefault(); 38 $event->stopPropagation(); 39 global $conf; 40 41 header('Content-Type: application/json'); 42 try { 43 $result = $this->executeTypeAjax(); 44 } catch (StructException $e) { 45 $result = array( 46 'error' => $e->getMessage() . ' ' . basename($e->getFile()) . ':' . $e->getLine() 47 ); 48 if ($conf['allowdebug']) { 49 $result['stacktrace'] = $e->getTraceAsString(); 50 } 51 http_status(500); 52 } 53 54 $json = new JSON(); 55 echo $json->encode($result); 56 } 57 58 /** 59 * Check the input variables and run the AJAX call 60 * 61 * @throws StructException 62 * @return mixed 63 */ 64 protected function executeTypeAjax() 65 { 66 global $INPUT; 67 68 $col = $INPUT->str('column'); 69 if (blank($col)) throw new StructException('No column provided'); 70 list($schema, $colname) = explode('.', $col, 2); 71 if (blank($schema) || blank($colname)) throw new StructException('Column format is wrong'); 72 73 $schema = new Schema($schema); 74 if (!$schema->getId()) throw new StructException('Unknown Schema'); 75 76 $column = $schema->findColumn($colname); 77 if ($column === false) throw new StructException('Column not found'); 78 79 return $column->getType()->handleAjax(); 80 } 81} 82