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_settingstree 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_settingstree') {
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 'loadlevel':
37					if (!($helper = plugin_load('helper', 'settingstree'))){
38						$data = array('error'=>true,'msg'=>"Can't load tree helper.");
39						break;
40					}
41					switch ($INPUT->str('showtype','normal')){
42						case 'export':
43							$data = array(
44								'html' => $helper->showExportHtml($INPUT->str('pluginname'),':'.ltrim($INPUT->str('path'),':'),$INPUT->arr('options',array())),
45								'path'=> ':'.ltrim($INPUT->str('path'),':'),
46							);
47							break;
48						case 'normal':
49						default:
50							$data = array(
51								'html' => $helper->showHtml($INPUT->str('pluginname'),':'.ltrim($INPUT->str('path'),':')),
52								'path'=> ':'.ltrim($INPUT->str('path'),':')
53							);
54					}
55					if (!$data['html']) {$data['error'] = true; $data['msg'] = "Can't load level html.";}
56					break;
57				case 'show_hierarchy':
58					if (!($helper = plugin_load('helper', 'settingstree'))){
59						$data = array('error'=>true,'msg'=>"Can't load tree helper.");
60						break;
61					}
62					$data = array('html' => $helper->showHierarchy($INPUT->str('pluginname'),$INPUT->str('key')));
63					if (!$data['html']) {$data['error'] = true; $data['msg'] = "Can't load level html.";}
64					break;
65				case 'savelevel':
66					if (!($helper = plugin_load('helper', 'settingstree'))){
67						$data = array('error'=>true,'msg'=>"Can't load tree helper.");
68						break;
69					}
70					$html = $helper->saveLevel($INPUT->str('pluginname'),':'.ltrim($INPUT->str('path'),':'),$INPUT->arr('data'),$data);
71					$data['html'] = $html;
72
73					if (!$data['html']) {$data['error'] = true; $data['msg'] = "Can't load level html.";}
74					break;
75				case 'exportlevel':
76					if (!($helper = plugin_load('helper', 'settingstree'))){
77						$data = array('error'=>true,'msg'=>"Can't load tree helper.");
78						break;
79					}
80					$html = $helper->exportLevel($INPUT->str('pluginname'),':'.ltrim($INPUT->str('path'),':'),$INPUT->arr('data'),$data,$INPUT->arr('options',array()));
81					$data['html'] = $html;
82					// we expect null for success (export will start with the options) and only need to display the configurations again when there is an error.
83					if (!$data['html'] && !$data['success']) {$data['error'] = true; $data['msg'] = "Can't load level html.";}
84					break;
85				default:
86					$data = array('error'=>true,'msg'=>'Unknown operation: '.$INPUT->str('operation'));
87					break;
88			}
89		//data
90		//json library of DokuWiki
91		}
92		if (is_array($data)) $data['token'] = getSecurityToken();
93		require_once DOKU_INC . 'inc/JSON.php';
94		$json = new JSON();
95	 	//set content type
96		header('Content-Type: application/json');
97		echo $json->encode($data);
98//		$this->get_helper()->check_meta_changes();
99
100	}
101}