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\Extension\ActionPlugin; 11use dokuwiki\Extension\EventHandler; 12use dokuwiki\Extension\Event; 13use dokuwiki\plugin\struct\meta\Schema; 14use dokuwiki\plugin\struct\meta\StructException; 15 16class action_plugin_struct_ajax extends ActionPlugin 17{ 18 /** 19 * Registers a callback function for a given event 20 * 21 * @param Doku_Event_Handler $controller DokuWiki's event controller object 22 * @return void 23 */ 24 public function register(EventHandler $controller) 25 { 26 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjax'); 27 } 28 29 /** 30 * Pass Ajax call to a type 31 * 32 * @param Doku_Event $event event object by reference 33 */ 34 public function handleAjax(Event $event) 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 = ['error' => $e->getMessage() . ' ' . basename($e->getFile()) . ':' . $e->getLine()]; 46 if ($conf['allowdebug']) { 47 $result['stacktrace'] = $e->getTraceAsString(); 48 } 49 http_status(500); 50 } 51 52 echo json_encode($result); 53 } 54 55 /** 56 * Check the input variables and run the AJAX call 57 * 58 * @return mixed 59 * @throws StructException 60 */ 61 protected function executeTypeAjax() 62 { 63 global $INPUT; 64 65 $col = $INPUT->str('column'); 66 if (blank($col)) throw new StructException('No column provided'); 67 [$schema, $colname] = explode('.', $col, 2); 68 if (blank($schema) || blank($colname)) throw new StructException('Column format is wrong'); 69 70 $schema = new Schema($schema); 71 if (!$schema->getId()) throw new StructException('Unknown Schema'); 72 73 $column = $schema->findColumn($colname); 74 if ($column === false) throw new StructException('Column not found'); 75 76 return $column->getType()->handleAjax(); 77 } 78} 79