1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6
7/**
8 * Action part of the tag plugin, handles tag display and index updates
9 */
10class action_plugin_explorertree extends DokuWiki_Action_Plugin {
11
12    public function register(Doku_Event_Handler $controller) {
13		$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,	'_ajax_call');
14	}
15
16
17    /**
18     * Register the events
19     *
20     * @param $event DOKU event on ajax call
21     * @param $param parameters, ignored
22     */
23	function _ajax_call(&$event, $param) {
24		if ($event->data !== 'plugin_explorertree') {
25			return;
26		}
27		//no other ajax call handlers needed
28		$event->stopPropagation();
29		$event->preventDefault();
30		//e.g. access additional request variables
31		global $INPUT; //available since release 2012-10-13 "Adora Belle"
32		if (!checkSecurityToken()){
33			$data = array('error'=>true,'msg'=>'invalid security token!');
34		}else{
35		switch($INPUT->str('operation')){
36			case 'explorertree_branch':
37				if (!($helper = plugin_load('helper', 'explorertree'))){
38					$data = array('error'=>true,'msg'=>"Can't load tree helper.");
39					break;
40				}
41				if (!($route = $helper->loadRoute($INPUT->str('route'),$INPUT->arr('loader')))){
42					$data = array('error'=>true,'msg'=>"Can't load route '".$INPUT->str('route')."'!");
43				}
44				$data = array('html' => $helper->htmlExplorer($INPUT->str('route'),ltrim(':'.$INPUT->str('itemid')),':') );
45				if (!$data['html']) {$data['error'] = true; $data['msg'] = "Can't load tree html.";}
46				break;
47			case 'callback':
48				if (!($helper = plugin_load('helper', 'explorertree'))){
49					$data = array('error'=>true,'msg'=>"Can't load tree helper.");
50					break;
51				}
52				$route = $helper->loadRoute($INPUT->str('route'),$INPUT->arr('loader'));
53				if (!$route || !is_callable(@$route['callbacks'][$INPUT->str(event)])) {
54					$data = array('error'=>true,'msg'=>"Can't load callback '".$INPUT->str('event')."'for '".$INPUT->str('route')."'!");
55				}
56				$data = @call_user_func_array($route['callbacks'][$INPUT->str(event)],array($INPUT->str('itemid')));
57				if (!is_array($data)) $data = array('error'=>true,'msg'=>"Callback for '".$INPUT->str('event')."' does not exists!");
58				break;
59			default:
60				$data = array('error'=>true,'msg'=>'Unknown operation: '.$INPUT->str('operation'));
61				break;
62		}
63		//data
64		//json library of DokuWiki
65		}
66		if (is_array($data)) $data['token'] = getSecurityToken();
67		require_once DOKU_INC . 'inc/JSON.php';
68		$json = new JSON();
69	 	//set content type
70		header('Content-Type: application/json');
71		echo $json->encode($data);
72//		$this->get_helper()->check_meta_changes();
73
74	}
75}