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 */ 31 public function handleAjax(Doku_Event $event) 32 { 33 if ($event->data != 'plugin_struct') return; 34 $event->preventDefault(); 35 $event->stopPropagation(); 36 global $conf; 37 38 header('Content-Type: application/json'); 39 try { 40 $result = $this->executeTypeAjax(); 41 } catch (StructException $e) { 42 $result = array( 43 'error' => $e->getMessage() . ' ' . basename($e->getFile()) . ':' . $e->getLine() 44 ); 45 if ($conf['allowdebug']) { 46 $result['stacktrace'] = $e->getTraceAsString(); 47 } 48 http_status(500); 49 } 50 51 echo json_encode($result); 52 } 53 54 /** 55 * Check the input variables and run the AJAX call 56 * 57 * @return mixed 58 * @throws StructException 59 */ 60 protected function executeTypeAjax() 61 { 62 global $INPUT; 63 64 $col = $INPUT->str('column'); 65 if (blank($col)) throw new StructException('No column provided'); 66 list($schema, $colname) = explode('.', $col, 2); 67 if (blank($schema) || blank($colname)) throw new StructException('Column format is wrong'); 68 69 $schema = new Schema($schema); 70 if (!$schema->getId()) throw new StructException('Unknown Schema'); 71 72 $column = $schema->findColumn($colname); 73 if ($column === false) throw new StructException('Column not found'); 74 75 return $column->getType()->handleAjax(); 76 } 77} 78